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