]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
When you view the context menu of a file/folder and shift is pressed (and the delete...
[dolphin.git] / src / dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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 "dolphincontextmenu.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinnewfilemenu.h"
25 #include "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28
29 #include <kactioncollection.h>
30 #include <kdesktopfile.h>
31 #include <kfileitemlistproperties.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobal.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
36 #include <kmenu.h>
37 #include <kmenubar.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
40 #include <knewfilemenu.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kfileitemactions.h>
44 #include <klocale.h>
45 #include <kpropertiesdialog.h>
46 #include <kstandardaction.h>
47 #include <kstandarddirs.h>
48
49 #include <QtGui/QApplication>
50 #include <QtGui/QClipboard>
51 #include <QtCore/QDir>
52
53 #include "views/dolphinview.h"
54 #include "views/viewmodecontroller.h"
55
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
57 const KFileItem& fileInfo,
58 const KUrl& baseUrl) :
59 m_mainWindow(parent),
60 m_capabilities(0),
61 m_fileInfo(fileInfo),
62 m_baseUrl(baseUrl),
63 m_context(NoContext),
64 m_copyToMenu(parent),
65 m_customActions(),
66 m_popup(new KMenu(m_mainWindow)),
67 m_showDeleteCommand(false),
68 m_shiftIsPressed(false)
69 {
70 // The context menu either accesses the URLs of the selected items
71 // or the items itself. To increase the performance both lists are cached.
72 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
73 m_selectedUrls = view->selectedUrls();
74 m_selectedItems = view->selectedItems();
75
76 m_showDeleteCommand = KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false);
77 }
78
79 DolphinContextMenu::~DolphinContextMenu()
80 {
81 delete m_capabilities;
82 m_capabilities = 0;
83 }
84
85 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
86 {
87 m_customActions = actions;
88 }
89
90 void DolphinContextMenu::open()
91 {
92 // get the context information
93 if (m_baseUrl.protocol() == "trash") {
94 m_context |= TrashContext;
95 }
96
97 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
98 m_context |= ItemContext;
99 // TODO: handle other use cases like devices + desktop files
100 }
101
102 // open the corresponding popup for the context
103 if (m_context & TrashContext) {
104 if (m_context & ItemContext) {
105 openTrashItemContextMenu();
106 } else {
107 openTrashContextMenu();
108 }
109 } else if (m_context & ItemContext) {
110 openItemContextMenu();
111 } else {
112 Q_ASSERT(m_context == NoContext);
113 openViewportContextMenu();
114 }
115 }
116
117 void DolphinContextMenu::openTrashContextMenu()
118 {
119 Q_ASSERT(m_context & TrashContext);
120
121 addShowMenubarAction();
122
123 QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup.data());
124 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
125 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
126 m_popup->addAction(emptyTrashAction);
127
128 QAction* addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
129 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
130
131 // Don't show if url is already in places
132 if (placeExists(m_mainWindow->activeViewContainer()->url())) {
133 addToPlacesAction->setVisible(false);
134 }
135
136 addCustomActions();
137
138 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
139 m_popup->addAction(propertiesAction);
140
141 QAction *action = m_popup->exec(QCursor::pos());
142 if (action == emptyTrashAction) {
143 const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
144 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
145 text,
146 QString(),
147 KGuiItem(i18nc("@action:button", "Empty Trash"),
148 KIcon("user-trash"))
149 ) == KMessageBox::Continue;
150 if (del) {
151 KonqOperations::emptyTrash(m_mainWindow);
152 }
153 } else if (action == addToPlacesAction) {
154 const KUrl& url = m_mainWindow->activeViewContainer()->url();
155 if (url.isValid()) {
156 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url);
157 }
158 }
159 }
160
161 void DolphinContextMenu::openTrashItemContextMenu()
162 {
163 Q_ASSERT(m_context & TrashContext);
164 Q_ASSERT(m_context & ItemContext);
165
166 addShowMenubarAction();
167
168 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
169 m_popup->addAction(restoreAction);
170
171 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
172 m_popup->addAction(deleteAction);
173
174 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
175 m_popup->addAction(propertiesAction);
176
177 if (m_popup->exec(QCursor::pos()) == restoreAction) {
178 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
179 }
180 }
181
182 void DolphinContextMenu::openItemContextMenu()
183 {
184 Q_ASSERT(!m_fileInfo.isNull());
185
186 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
187 // setup 'Create New' menu
188 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
189 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
190 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
191 newFileMenu->checkUpToDate();
192 newFileMenu->setPopupFiles(m_fileInfo.url());
193 newFileMenu->setEnabled(capabilities().supportsWriting());
194
195 KMenu* menu = newFileMenu->menu();
196 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
197 menu->setIcon(KIcon("document-new"));
198 m_popup->addMenu(menu);
199 m_popup->addSeparator();
200
201 // insert 'Open in new window' and 'Open in new tab' entries
202 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
203 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
204 m_popup->addSeparator();
205 }
206 addShowMenubarAction();
207 insertDefaultItemActions();
208
209 m_popup->addSeparator();
210
211 KFileItemActions fileItemActions;
212 fileItemActions.setItemListProperties(capabilities());
213 addServiceActions(fileItemActions);
214
215 addVersionControlActions();
216
217 // insert 'Copy To' and 'Move To' sub menus
218 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
219 m_copyToMenu.setItems(m_selectedItems);
220 m_copyToMenu.setReadOnly(!capabilities().supportsWriting());
221 m_copyToMenu.addActionsTo(m_popup.data());
222 m_popup->addSeparator();
223 }
224
225 // insert 'Add to Places' entry if exactly one item is selected
226 QAction* addToPlacesAction = 0;
227 if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1) && !placeExists(m_fileInfo.url())) {
228 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
229 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
230 }
231
232 // insert 'Properties...' entry
233 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
234 m_popup->addAction(propertiesAction);
235
236 QAction* activatedAction = m_popup->exec(QCursor::pos());
237
238 if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
239 const KUrl selectedUrl(m_fileInfo.url());
240 if (selectedUrl.isValid()) {
241 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
242 selectedUrl);
243 }
244 }
245 }
246
247 void DolphinContextMenu::openViewportContextMenu()
248 {
249 addShowMenubarAction();
250
251 // setup 'Create New' menu
252 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
253 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
254 newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
255 newFileMenu->checkUpToDate();
256 newFileMenu->setPopupFiles(m_baseUrl);
257 m_popup->addMenu(newFileMenu->menu());
258 m_popup->addSeparator();
259
260 // insert 'Open in new window' and 'Open in new tab' entries
261 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
262 m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
263 m_popup->addSeparator();
264
265 QAction* pasteAction = createPasteAction();
266 m_popup->addAction(pasteAction);
267 m_popup->addSeparator();
268
269 // insert service actions
270 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
271 const KFileItemListProperties baseUrlProperties(KFileItemList() << item);
272 KFileItemActions fileItemActions;
273 fileItemActions.setItemListProperties(baseUrlProperties);
274 addServiceActions(fileItemActions);
275
276 addVersionControlActions();
277
278 // insert 'Add to Places' entry if exactly one item is selected
279 QAction* addToPlacesAction = 0;
280 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
281 addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
282 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
283 }
284
285 addCustomActions();
286
287 QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
288 propertiesAction->setIcon(KIcon("document-properties"));
289 QAction* action = m_popup->exec(QCursor::pos());
290 if (action == propertiesAction) {
291 const KUrl& url = m_mainWindow->activeViewContainer()->url();
292
293 KPropertiesDialog* dialog = new KPropertiesDialog(url, m_mainWindow);
294 dialog->setAttribute(Qt::WA_DeleteOnClose);
295 dialog->show();
296 } else if ((addToPlacesAction != 0) && (action == addToPlacesAction)) {
297 const KUrl& url = m_mainWindow->activeViewContainer()->url();
298 if (url.isValid()) {
299 DolphinSettings::instance().placesModel()->addPlace(placesName(url), url);
300 }
301 }
302 }
303
304 void DolphinContextMenu::insertDefaultItemActions()
305 {
306 const KActionCollection* collection = m_mainWindow->actionCollection();
307
308 // Cut action
309 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
310
311 // Copy action
312 m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
313
314 // Paste action
315 m_popup->addAction(createPasteAction());
316
317 m_popup->addSeparator();
318
319 // Insert 'Rename'
320 QAction* renameAction = collection->action("rename");
321 m_popup->addAction(renameAction);
322
323 // Insert move to trash and delete. We need to insert both because both can be visible.
324 m_popup->addAction(collection->action("move_to_trash"));
325 m_popup->addAction(collection->action("delete"));
326
327 const KUrl& url = m_mainWindow->activeViewContainer()->url();
328 if (url.isLocalFile()) {
329 collection->action("move_to_trash")->setVisible(true);
330 collection->action("delete")->setVisible(false);
331 } else {
332 collection->action("delete")->setVisible(true);
333 }
334
335 if(m_showDeleteCommand) {
336 collection->action("delete")->setVisible(true);
337 }
338
339 if(m_shiftIsPressed) {
340 deleteOrTrashMenuEntry(Qt::Key_Shift, m_shiftIsPressed);
341 }
342
343 }
344
345 void DolphinContextMenu::addShowMenubarAction()
346 {
347 KAction* showMenuBar = m_mainWindow->showMenuBarAction();
348 if (!m_mainWindow->menuBar()->isVisible()) {
349 m_popup->addAction(showMenuBar);
350 m_popup->addSeparator();
351 }
352 }
353
354 QString DolphinContextMenu::placesName(const KUrl& url) const
355 {
356 QString name = url.fileName();
357 if (name.isEmpty()) {
358 name = url.host();
359 }
360 return name;
361 }
362
363 bool DolphinContextMenu::placeExists(const KUrl& url) const
364 {
365 const KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
366 const int count = placesModel->rowCount();
367
368 for (int i = 0; i < count; ++i) {
369 const QModelIndex index = placesModel->index(i, 0);
370
371 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
372 return true;
373 }
374 }
375 return false;
376 }
377
378 QAction* DolphinContextMenu::createPasteAction()
379 {
380 QAction* action = 0;
381 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
382 if (isDir && (m_selectedItems.count() == 1)) {
383 action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
384 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
385 const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
386 action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting());
387 connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
388 } else {
389 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
390 }
391
392 return action;
393 }
394
395 KFileItemListProperties& DolphinContextMenu::capabilities()
396 {
397 if (m_capabilities == 0) {
398 m_capabilities = new KFileItemListProperties(m_selectedItems);
399 }
400 return *m_capabilities;
401 }
402
403 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
404 {
405 fileItemActions.setParentWidget(m_mainWindow);
406
407 // insert 'Open With...' action or sub menu
408 fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'");
409
410 // insert 'Actions' sub menu
411 if (fileItemActions.addServiceActionsTo(m_popup.data())) {
412 m_popup->addSeparator();
413 }
414 }
415
416 void DolphinContextMenu::addVersionControlActions()
417 {
418 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
419 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
420 if (!versionControlActions.isEmpty()) {
421 foreach (QAction* action, versionControlActions) {
422 m_popup->addAction(action);
423 }
424 m_popup->addSeparator();
425 }
426 }
427
428 void DolphinContextMenu::addCustomActions()
429 {
430 foreach (QAction* action, m_customActions) {
431 m_popup->addAction(action);
432 }
433 }
434
435 void DolphinContextMenu::setShiftIsPressed(bool pressed)
436 {
437 m_shiftIsPressed = pressed;
438 }
439
440 void DolphinContextMenu::deleteOrTrashMenuEntry(Qt::Key key, bool pressed)
441 {
442 if(m_mainWindow->activeViewContainer()->url().isLocalFile() && !m_showDeleteCommand && key == Qt::Key_Shift) {
443
444 // Set the current size as fixed size so that the menu isn't flickering when pressing shift.
445 m_popup->setFixedSize(m_popup->size());
446 if(pressed) {
447 m_mainWindow->actionCollection()->action("delete")->setVisible(true);
448 m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(false);
449 }
450 else {
451 m_mainWindow->actionCollection()->action("delete")->setVisible(false);
452 m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(true);
453 }
454
455 // This sets the menu back to a dynamic size followed by a forced resize incase the newly made visible action has bigger text.
456 m_popup->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
457 m_popup->resize(m_popup->sizeHint());
458 }
459 }
460
461 #include "dolphincontextmenu.moc"