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