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