]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
port away from KMimeType
[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 "dolphinviewcontainer.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphinremoveaction.h"
28
29 #include <KActionCollection>
30 #include <kfileitemactionplugin.h>
31 #include <kabstractfileitemactionplugin.h>
32 #include <KFileItemActions>
33 #include <KFileItemListProperties>
34 #include <KIO/RestoreJob>
35 #include <KIO/EmptyTrashJob>
36 #include <KIO/JobUiDelegate>
37 #include <KJobWidgets>
38 #include <QMenu>
39 #include <KMimeTypeTrader>
40 #include <KNewFileMenu>
41 #include <konq_operations.h>
42 #include <KService>
43 #include <KLocalizedString>
44 #include <KStandardAction>
45 #include <KToolBar>
46
47 #include <QMenuBar>
48 #include <panels/places/placesitem.h>
49 #include <panels/places/placesitemmodel.h>
50
51
52 #include "views/dolphinview.h"
53 #include "views/viewmodecontroller.h"
54
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
56 const QPoint& pos,
57 const KFileItem& fileInfo,
58 const QUrl& baseUrl) :
59 QMenu(parent),
60 m_pos(pos),
61 m_mainWindow(parent),
62 m_fileInfo(fileInfo),
63 m_baseUrl(baseUrl),
64 m_baseFileItem(0),
65 m_selectedItems(),
66 m_selectedItemsProperties(0),
67 m_context(NoContext),
68 m_copyToMenu(parent),
69 m_customActions(),
70 m_command(None),
71 m_removeAction(0)
72 {
73 // The context menu either accesses the URLs of the selected items
74 // or the items itself. To increase the performance both lists are cached.
75 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
76 m_selectedItems = view->selectedItems();
77 }
78
79 DolphinContextMenu::~DolphinContextMenu()
80 {
81 delete m_selectedItemsProperties;
82 m_selectedItemsProperties = 0;
83 }
84
85 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
86 {
87 m_customActions = actions;
88 }
89
90 DolphinContextMenu::Command DolphinContextMenu::open()
91 {
92 // get the context information
93 if (m_baseUrl.scheme() == QLatin1String("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 return m_command;
117 }
118
119 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
120 {
121 if (m_removeAction && ev->key() == Qt::Key_Shift) {
122 m_removeAction->update();
123 }
124 QMenu::keyPressEvent(ev);
125 }
126
127 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
128 {
129 if (m_removeAction && ev->key() == Qt::Key_Shift) {
130 m_removeAction->update();
131 }
132 QMenu::keyReleaseEvent(ev);
133 }
134
135 void DolphinContextMenu::openTrashContextMenu()
136 {
137 Q_ASSERT(m_context & TrashContext);
138
139 QAction* emptyTrashAction = new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
140 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
141 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
142 addAction(emptyTrashAction);
143
144 addCustomActions();
145
146 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
147 addAction(propertiesAction);
148
149 addShowMenuBarAction();
150
151 if (exec(m_pos) == emptyTrashAction) {
152 KIO::JobUiDelegate uiDelegate;
153 uiDelegate.setWindow(m_mainWindow);
154 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
155 KIO::Job* job = KIO::emptyTrash();
156 KJobWidgets::setWindow(job, m_mainWindow);
157 job->ui()->setAutoErrorHandlingEnabled(true);
158 }
159 }
160 }
161
162 void DolphinContextMenu::openTrashItemContextMenu()
163 {
164 Q_ASSERT(m_context & TrashContext);
165 Q_ASSERT(m_context & ItemContext);
166
167 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
168 addAction(restoreAction);
169
170 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
171 addAction(deleteAction);
172
173 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
174 addAction(propertiesAction);
175
176 if (exec(m_pos) == restoreAction) {
177 QList<QUrl> selectedUrls;
178 foreach (const KFileItem &item, m_selectedItems) {
179 selectedUrls.append(item.url());
180 }
181
182 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
183 KJobWidgets::setWindow(job, m_mainWindow);
184 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
185 }
186 }
187
188 void DolphinContextMenu::openItemContextMenu()
189 {
190 Q_ASSERT(!m_fileInfo.isNull());
191
192 QAction* openParentAction = 0;
193 QAction* openParentInNewWindowAction = 0;
194 QAction* openParentInNewTabAction = 0;
195 QAction* addToPlacesAction = 0;
196 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
197
198 if (m_selectedItems.count() == 1) {
199 if (m_fileInfo.isDir()) {
200 // setup 'Create New' menu
201 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
202 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
203 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
204 newFileMenu->checkUpToDate();
205 newFileMenu->setPopupFiles(m_fileInfo.url());
206 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
207 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
208 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
209
210 QMenu* menu = newFileMenu->menu();
211 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
212 menu->setIcon(QIcon::fromTheme("document-new"));
213 addMenu(menu);
214 addSeparator();
215
216 // insert 'Open in new window' and 'Open in new tab' entries
217 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
218 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
219
220 // insert 'Add to Places' entry
221 if (!placeExists(m_fileInfo.url())) {
222 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
223 i18nc("@action:inmenu Add selected folder to places",
224 "Add to Places"));
225 }
226
227 addSeparator();
228 } else if (m_baseUrl.scheme().contains("search") || m_baseUrl.scheme().contains("timeline")) {
229 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
230 i18nc("@action:inmenu",
231 "Open Path"),
232 this);
233 addAction(openParentAction);
234
235 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
236 i18nc("@action:inmenu",
237 "Open Path in New Window"),
238 this);
239 addAction(openParentInNewWindowAction);
240
241 openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"),
242 i18nc("@action:inmenu",
243 "Open Path in New Tab"),
244 this);
245 addAction(openParentInNewTabAction);
246
247 addSeparator();
248 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
249 // insert 'Open in new window' and 'Open in new tab' entries
250 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
251 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
252
253 addSeparator();
254 }
255 } else {
256 bool selectionHasOnlyDirs = true;
257 foreach (const KFileItem& item, m_selectedItems) {
258 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
259 if (url.isEmpty()) {
260 selectionHasOnlyDirs = false;
261 break;
262 }
263 }
264
265 if (selectionHasOnlyDirs) {
266 // insert 'Open in new tab' entry
267 addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs"));
268 addSeparator();
269 }
270 }
271
272 insertDefaultItemActions(selectedItemsProps);
273
274 addSeparator();
275
276 KFileItemActions fileItemActions;
277 fileItemActions.setItemListProperties(selectedItemsProps);
278 addServiceActions(fileItemActions);
279
280 addFileItemPluginActions();
281
282 addVersionControlPluginActions();
283
284 // insert 'Copy To' and 'Move To' sub menus
285 if (GeneralSettings::showCopyMoveMenu()) {
286 m_copyToMenu.setItems(m_selectedItems);
287 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
288 m_copyToMenu.addActionsTo(this);
289 }
290
291 // insert 'Properties...' entry
292 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
293 addAction(propertiesAction);
294
295 QAction* activatedAction = exec(m_pos);
296 if (activatedAction) {
297 if (activatedAction == addToPlacesAction) {
298 const QUrl selectedUrl(m_fileInfo.url());
299 if (selectedUrl.isValid()) {
300 PlacesItemModel model;
301 const QString text = selectedUrl.fileName();
302 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
303 model.appendItemToGroup(item);
304 }
305 } else if (activatedAction == openParentAction) {
306 m_command = OpenParentFolder;
307 } else if (activatedAction == openParentInNewWindowAction) {
308 m_command = OpenParentFolderInNewWindow;
309 } else if (activatedAction == openParentInNewTabAction) {
310 m_command = OpenParentFolderInNewTab;
311 }
312 }
313 }
314
315 void DolphinContextMenu::openViewportContextMenu()
316 {
317 // setup 'Create New' menu
318 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
319 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
320 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
321 newFileMenu->checkUpToDate();
322 newFileMenu->setPopupFiles(m_baseUrl);
323 addMenu(newFileMenu->menu());
324 addSeparator();
325
326 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
327 // "open_in_new_tab" here, as the current selection should get ignored.
328 addAction(m_mainWindow->actionCollection()->action("new_window"));
329 addAction(m_mainWindow->actionCollection()->action("new_tab"));
330
331 // Insert 'Add to Places' entry if exactly one item is selected
332 QAction* addToPlacesAction = 0;
333 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
334 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
335 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
336 }
337
338 addSeparator();
339
340 QAction* pasteAction = createPasteAction();
341 addAction(pasteAction);
342 addSeparator();
343
344 // Insert service actions
345 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
346 KFileItemActions fileItemActions;
347 fileItemActions.setItemListProperties(baseUrlProperties);
348 addServiceActions(fileItemActions);
349
350 addFileItemPluginActions();
351
352 addVersionControlPluginActions();
353
354 addCustomActions();
355
356 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
357 addAction(propertiesAction);
358
359 addShowMenuBarAction();
360
361 QAction* action = exec(m_pos);
362 if (addToPlacesAction && (action == addToPlacesAction)) {
363 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
364 if (container->url().isValid()) {
365 PlacesItemModel model;
366 PlacesItem* item = model.createPlacesItem(container->placesText(),
367 container->url());
368 model.appendItemToGroup(item);
369 }
370 }
371 }
372
373 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
374 {
375 const KActionCollection* collection = m_mainWindow->actionCollection();
376
377 // Insert 'Cut', 'Copy' and 'Paste'
378 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
379 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
380 addAction(createPasteAction());
381
382 addSeparator();
383
384 // Insert 'Rename'
385 QAction* renameAction = collection->action("rename");
386 addAction(renameAction);
387
388 // Insert 'Move to Trash' and/or 'Delete'
389 if (properties.supportsDeleting()) {
390 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
391 !properties.isLocal());
392 const bool showMoveToTrashAction = (properties.isLocal() &&
393 properties.supportsMoving());
394
395 if (showDeleteAction && showMoveToTrashAction) {
396 delete m_removeAction;
397 m_removeAction = 0;
398 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
399 addAction(m_mainWindow->actionCollection()->action("delete"));
400 } else if (showDeleteAction && !showMoveToTrashAction) {
401 addAction(m_mainWindow->actionCollection()->action("delete"));
402 } else {
403 if (!m_removeAction) {
404 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
405 }
406 addAction(m_removeAction);
407 m_removeAction->update();
408 }
409 }
410 }
411
412 void DolphinContextMenu::addShowMenuBarAction()
413 {
414 const KActionCollection* ac = m_mainWindow->actionCollection();
415 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
416 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
417 addSeparator();
418 addAction(showMenuBar);
419 }
420 }
421
422 bool DolphinContextMenu::placeExists(const QUrl& url) const
423 {
424 PlacesItemModel model;
425
426 const int count = model.count();
427 for (int i = 0; i < count; ++i) {
428 const QUrl placeUrl = model.placesItem(i)->url();
429 if (placeUrl.matches(url, QUrl::StripTrailingSlash)) {
430 return true;
431 }
432 }
433
434 return false;
435 }
436
437 QAction* DolphinContextMenu::createPasteAction()
438 {
439 QAction* action = 0;
440 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
441 if (isDir && (m_selectedItems.count() == 1)) {
442 const QPair<bool, QString> pasteInfo = KonqOperations::pasteInfo(m_fileInfo.url());
443 action = new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
444 action->setEnabled(pasteInfo.first);
445 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
446 } else {
447 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
448 }
449
450 return action;
451 }
452
453 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
454 {
455 if (!m_selectedItemsProperties) {
456 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
457 }
458 return *m_selectedItemsProperties;
459 }
460
461 KFileItem DolphinContextMenu::baseFileItem()
462 {
463 if (!m_baseFileItem) {
464 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
465 }
466 return *m_baseFileItem;
467 }
468
469 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
470 {
471 fileItemActions.setParentWidget(m_mainWindow);
472
473 // insert 'Open With...' action or sub menu
474 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
475
476 // insert 'Actions' sub menu
477 fileItemActions.addServiceActionsTo(this);
478 }
479
480 void DolphinContextMenu::addFileItemPluginActions()
481 {
482 KFileItemListProperties props;
483 if (m_selectedItems.isEmpty()) {
484 props.setItems(KFileItemList() << baseFileItem());
485 } else {
486 props = selectedItemsProperties();
487 }
488
489 QString commonMimeType = props.mimeType();
490 if (commonMimeType.isEmpty()) {
491 commonMimeType = QLatin1String("application/octet-stream");
492 }
493
494 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
495 if (pluginServices.isEmpty()) {
496 return;
497 }
498
499 const KConfig config("kservicemenurc", KConfig::NoGlobals);
500 const KConfigGroup showGroup = config.group("Show");
501
502 foreach (const KService::Ptr& service, pluginServices) {
503 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
504 // The plugin has been disabled
505 continue;
506 }
507
508 // Old API (kdelibs-4.6.0 only)
509 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
510 if (plugin) {
511 plugin->setParent(this);
512 addActions(plugin->actions(props, m_mainWindow));
513 }
514 // New API (kdelibs >= 4.6.1)
515 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
516 if (abstractPlugin) {
517 abstractPlugin->setParent(this);
518 addActions(abstractPlugin->actions(props, m_mainWindow));
519 }
520 }
521 }
522
523 void DolphinContextMenu::addVersionControlPluginActions()
524 {
525 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
526 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
527 if (!versionControlActions.isEmpty()) {
528 foreach (QAction* action, versionControlActions) {
529 addAction(action);
530 }
531 addSeparator();
532 }
533 }
534
535 void DolphinContextMenu::addCustomActions()
536 {
537 foreach (QAction* action, m_customActions) {
538 addAction(action);
539 }
540 }
541