]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/treeviewcontextmenu.cpp
Make the settings dialog work in the frameworks branch
[dolphin.git] / src / panels / folders / treeviewcontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "treeviewcontextmenu.h"
22
23 #include <KFileItem>
24 #include <KIconLoader>
25 #include <KIO/DeleteJob>
26 #include <KMenu>
27 #include <KIcon>
28 #include <KSharedConfig>
29 #include <KConfigGroup>
30 #include <konqmimedata.h>
31 #include <KFileItemListProperties>
32 #include <konq_operations.h>
33 #include <KLocale>
34 #include <KPropertiesDialog>
35
36 #include "folderspanel.h"
37
38 #include <QApplication>
39 #include <QClipboard>
40 #include <QMimeData>
41
42 TreeViewContextMenu::TreeViewContextMenu(FoldersPanel* parent,
43 const KFileItem& fileInfo) :
44 QObject(parent),
45 m_parent(parent),
46 m_fileItem(fileInfo)
47 {
48 }
49
50 TreeViewContextMenu::~TreeViewContextMenu()
51 {
52 }
53
54 void TreeViewContextMenu::open()
55 {
56 KMenu* popup = new KMenu(m_parent);
57
58 if (!m_fileItem.isNull()) {
59 KFileItemListProperties capabilities(KFileItemList() << m_fileItem);
60
61 // insert 'Cut', 'Copy' and 'Paste'
62 QAction* cutAction = new QAction(KIcon("edit-cut"), i18nc("@action:inmenu", "Cut"), this);
63 cutAction->setEnabled(capabilities.supportsMoving());
64 connect(cutAction, &QAction::triggered, this, &TreeViewContextMenu::cut);
65
66 QAction* copyAction = new QAction(KIcon("edit-copy"), i18nc("@action:inmenu", "Copy"), this);
67 connect(copyAction, &QAction::triggered, this, &TreeViewContextMenu::copy);
68
69 QAction* pasteAction = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste"), this);
70 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
71 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
72 connect(pasteAction, &QAction::triggered, this, &TreeViewContextMenu::paste);
73 pasteAction->setEnabled(!pasteData.isEmpty() && capabilities.supportsWriting());
74
75 popup->addAction(cutAction);
76 popup->addAction(copyAction);
77 popup->addAction(pasteAction);
78 popup->addSeparator();
79
80 // insert 'Rename'
81 QAction* renameAction = new QAction(i18nc("@action:inmenu", "Rename..."), this);
82 renameAction->setEnabled(capabilities.supportsMoving());
83 renameAction->setIcon(KIcon("edit-rename"));
84 connect(renameAction, &QAction::triggered, this, &TreeViewContextMenu::rename);
85 popup->addAction(renameAction);
86
87 // insert 'Move to Trash' and (optionally) 'Delete'
88 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
89 KConfigGroup configGroup(globalConfig, "KDE");
90 bool showDeleteCommand = configGroup.readEntry("ShowDeleteCommand", false);
91
92 const KUrl url = m_fileItem.url();
93 if (url.isLocalFile()) {
94 QAction* moveToTrashAction = new QAction(KIcon("user-trash"),
95 i18nc("@action:inmenu", "Move to Trash"), this);
96 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
97 moveToTrashAction->setEnabled(enableMoveToTrash);
98 connect(moveToTrashAction, &QAction::triggered, this, &TreeViewContextMenu::moveToTrash);
99 popup->addAction(moveToTrashAction);
100 } else {
101 showDeleteCommand = true;
102 }
103
104 if (showDeleteCommand) {
105 QAction* deleteAction = new QAction(KIcon("edit-delete"), i18nc("@action:inmenu", "Delete"), this);
106 deleteAction->setEnabled(capabilities.supportsDeleting());
107 connect(deleteAction, &QAction::triggered, this, &TreeViewContextMenu::deleteItem);
108 popup->addAction(deleteAction);
109 }
110
111 popup->addSeparator();
112 }
113
114 // insert 'Show Hidden Files'
115 QAction* showHiddenFilesAction = new QAction(i18nc("@action:inmenu", "Show Hidden Files"), this);
116 showHiddenFilesAction->setCheckable(true);
117 showHiddenFilesAction->setChecked(m_parent->showHiddenFiles());
118 popup->addAction(showHiddenFilesAction);
119 connect(showHiddenFilesAction, &QAction::toggled, this, &TreeViewContextMenu::setShowHiddenFiles);
120
121 // insert 'Automatic Scrolling'
122 QAction* autoScrollingAction = new QAction(i18nc("@action:inmenu", "Automatic Scrolling"), this);
123 autoScrollingAction->setCheckable(true);
124 autoScrollingAction->setChecked(m_parent->autoScrolling());
125 // TODO: Temporary disabled. Horizontal autoscrolling will be implemented later either
126 // in KItemViews or manually as part of the FoldersPanel
127 //popup->addAction(autoScrollingAction);
128 connect(autoScrollingAction, &QAction::toggled, this, &TreeViewContextMenu::setAutoScrolling);
129
130 if (!m_fileItem.isNull()) {
131 // insert 'Properties' entry
132 QAction* propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
133 propertiesAction->setIcon(KIcon("document-properties"));
134 connect(propertiesAction, &QAction::triggered, this, &TreeViewContextMenu::showProperties);
135 popup->addAction(propertiesAction);
136 }
137
138 QList<QAction*> customActions = m_parent->customContextMenuActions();
139 if (!customActions.isEmpty()) {
140 popup->addSeparator();
141 foreach (QAction* action, customActions) {
142 popup->addAction(action);
143 }
144 }
145
146 QWeakPointer<KMenu> popupPtr = popup;
147 popup->exec(QCursor::pos());
148 if (popupPtr.data()) {
149 popupPtr.data()->deleteLater();
150 }
151 }
152
153 void TreeViewContextMenu::populateMimeData(QMimeData* mimeData, bool cut)
154 {
155 KUrl::List kdeUrls;
156 kdeUrls.append(m_fileItem.url());
157 KUrl::List mostLocalUrls;
158 bool dummy;
159 mostLocalUrls.append(m_fileItem.mostLocalUrl(dummy));
160 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, cut);
161 }
162
163 void TreeViewContextMenu::cut()
164 {
165 QMimeData* mimeData = new QMimeData();
166 populateMimeData(mimeData, true);
167 QApplication::clipboard()->setMimeData(mimeData);
168 }
169
170 void TreeViewContextMenu::copy()
171 {
172 QMimeData* mimeData = new QMimeData();
173 populateMimeData(mimeData, false);
174 QApplication::clipboard()->setMimeData(mimeData);
175 }
176
177 void TreeViewContextMenu::paste()
178 {
179 QClipboard* clipboard = QApplication::clipboard();
180 const QMimeData* mimeData = clipboard->mimeData();
181
182 const KUrl::List source = KUrl::List::fromMimeData(mimeData);
183 const KUrl& dest = m_fileItem.url();
184 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
185 KonqOperations::copy(m_parent, KonqOperations::MOVE, source, dest);
186 clipboard->clear();
187 } else {
188 KonqOperations::copy(m_parent, KonqOperations::COPY, source, dest);
189 }
190 }
191
192 void TreeViewContextMenu::rename()
193 {
194 m_parent->rename(m_fileItem);
195 }
196
197 void TreeViewContextMenu::moveToTrash()
198 {
199 KonqOperations::del(m_parent, KonqOperations::TRASH, KUrl::List() << m_fileItem.url());
200 }
201
202 void TreeViewContextMenu::deleteItem()
203 {
204 KonqOperations::del(m_parent, KonqOperations::DEL, KUrl::List() << m_fileItem.url());
205 }
206
207 void TreeViewContextMenu::showProperties()
208 {
209 KPropertiesDialog* dialog = new KPropertiesDialog(m_fileItem.url(), m_parent);
210 dialog->setAttribute(Qt::WA_DeleteOnClose);
211 dialog->show();
212 }
213
214 void TreeViewContextMenu::setShowHiddenFiles(bool show)
215 {
216 m_parent->setShowHiddenFiles(show);
217 }
218
219 void TreeViewContextMenu::setAutoScrolling(bool enable)
220 {
221 m_parent->setAutoScrolling(enable);
222 }
223
224 #include "treeviewcontextmenu.moc"