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