]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Merge branch 'Applications/18.12'
[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 "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinnewfilemenu.h"
26 #include "dolphinremoveaction.h"
27 #include "dolphinviewcontainer.h"
28 #include "panels/places/placesitem.h"
29 #include "panels/places/placesitemmodel.h"
30 #include "trash/dolphintrash.h"
31 #include "views/dolphinview.h"
32 #include "views/viewmodecontroller.h"
33
34 #include <KAbstractFileItemActionPlugin>
35 #include <KActionCollection>
36 #include <KFileItemActions>
37 #include <KFileItemListProperties>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
40 #include <KIO/Paste>
41 #include <KIO/RestoreJob>
42 #include <KJobWidgets>
43 #include <KLocalizedString>
44 #include <KMimeTypeTrader>
45 #include <KNewFileMenu>
46 #include <KPluginMetaData>
47 #include <KService>
48 #include <KStandardAction>
49 #include <KToolBar>
50
51 #include <QApplication>
52 #include <QClipboard>
53 #include <QKeyEvent>
54 #include <QMenu>
55 #include <QMenuBar>
56 #include <QMimeDatabase>
57
58 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
59 const QPoint& pos,
60 const KFileItem& fileInfo,
61 const QUrl& baseUrl) :
62 QMenu(parent),
63 m_pos(pos),
64 m_mainWindow(parent),
65 m_fileInfo(fileInfo),
66 m_baseUrl(baseUrl),
67 m_baseFileItem(nullptr),
68 m_selectedItems(),
69 m_selectedItemsProperties(nullptr),
70 m_context(NoContext),
71 m_copyToMenu(parent),
72 m_customActions(),
73 m_command(None),
74 m_removeAction(nullptr)
75 {
76 // The context menu either accesses the URLs of the selected items
77 // or the items itself. To increase the performance both lists are cached.
78 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
79 m_selectedItems = view->selectedItems();
80 }
81
82 DolphinContextMenu::~DolphinContextMenu()
83 {
84 delete m_baseFileItem;
85 m_baseFileItem = nullptr;
86 delete m_selectedItemsProperties;
87 m_selectedItemsProperties = nullptr;
88 }
89
90 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
91 {
92 m_customActions = actions;
93 }
94
95 DolphinContextMenu::Command DolphinContextMenu::open()
96 {
97 // get the context information
98 if (m_baseUrl.scheme() == QLatin1String("trash")) {
99 m_context |= TrashContext;
100 }
101
102 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
103 m_context |= ItemContext;
104 // TODO: handle other use cases like devices + desktop files
105 }
106
107 // open the corresponding popup for the context
108 if (m_context & TrashContext) {
109 if (m_context & ItemContext) {
110 openTrashItemContextMenu();
111 } else {
112 openTrashContextMenu();
113 }
114 } else if (m_context & ItemContext) {
115 openItemContextMenu();
116 } else {
117 Q_ASSERT(m_context == NoContext);
118 openViewportContextMenu();
119 }
120
121 return m_command;
122 }
123
124 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
125 {
126 if (m_removeAction && ev->key() == Qt::Key_Shift) {
127 m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
128 }
129 QMenu::keyPressEvent(ev);
130 }
131
132 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
133 {
134 if (m_removeAction && ev->key() == Qt::Key_Shift) {
135 m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
136 }
137 QMenu::keyReleaseEvent(ev);
138 }
139
140 void DolphinContextMenu::openTrashContextMenu()
141 {
142 Q_ASSERT(m_context & TrashContext);
143
144 QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
145 emptyTrashAction->setEnabled(!Trash::isEmpty());
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 Trash::empty(m_mainWindow);
157 }
158 }
159
160 void DolphinContextMenu::openTrashItemContextMenu()
161 {
162 Q_ASSERT(m_context & TrashContext);
163 Q_ASSERT(m_context & ItemContext);
164
165 QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow);
166 addAction(restoreAction);
167
168 QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
169 addAction(deleteAction);
170
171 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
172 addAction(propertiesAction);
173
174 if (exec(m_pos) == restoreAction) {
175 QList<QUrl> selectedUrls;
176 selectedUrls.reserve(m_selectedItems.count());
177 foreach (const KFileItem &item, m_selectedItems) {
178 selectedUrls.append(item.url());
179 }
180
181 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
182 KJobWidgets::setWindow(job, m_mainWindow);
183 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
184 }
185 }
186
187 void DolphinContextMenu::openItemContextMenu()
188 {
189 Q_ASSERT(!m_fileInfo.isNull());
190
191 QAction* openParentAction = nullptr;
192 QAction* openParentInNewWindowAction = nullptr;
193 QAction* openParentInNewTabAction = nullptr;
194 QAction* addToPlacesAction = nullptr;
195 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
196
197 KFileItemActions fileItemActions;
198 fileItemActions.setParentWidget(m_mainWindow);
199 fileItemActions.setItemListProperties(selectedItemsProps);
200
201 if (m_selectedItems.count() == 1) {
202 if (m_fileInfo.isDir()) {
203 // insert 'Open in new window' and 'Open in new tab' entries
204 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
205 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
206
207 // Insert 'Open With' entries
208 addOpenWithActions(fileItemActions);
209
210 // insert 'Add to Places' entry
211 if (!placeExists(m_fileInfo.url())) {
212 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
213 i18nc("@action:inmenu Add selected folder to places",
214 "Add to Places"));
215 }
216
217 addSeparator();
218
219 // set up 'Create New' menu
220 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
221 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
222 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
223 newFileMenu->checkUpToDate();
224 newFileMenu->setPopupFiles(m_fileInfo.url());
225 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
226 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
227 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
228
229 QMenu* menu = newFileMenu->menu();
230 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
231 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
232 addMenu(menu);
233
234 addSeparator();
235 } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
236 addOpenWithActions(fileItemActions);
237
238 openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
239 i18nc("@action:inmenu",
240 "Open Path"),
241 this);
242 addAction(openParentAction);
243
244 openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
245 i18nc("@action:inmenu",
246 "Open Path in New Window"),
247 this);
248 addAction(openParentInNewWindowAction);
249
250 openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
251 i18nc("@action:inmenu",
252 "Open Path in New Tab"),
253 this);
254 addAction(openParentInNewTabAction);
255
256 addSeparator();
257 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
258 // Insert 'Open With" entries
259 addOpenWithActions(fileItemActions);
260
261 // insert 'Open in new window' and 'Open in new tab' entries
262 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
263 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
264
265 addSeparator();
266 } else {
267 // Insert 'Open With" entries
268 addOpenWithActions(fileItemActions);
269 }
270 if (m_fileInfo.isLink()) {
271 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
272 addSeparator();
273 }
274 } else {
275 bool selectionHasOnlyDirs = true;
276 foreach (const KFileItem& item, m_selectedItems) {
277 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
278 if (url.isEmpty()) {
279 selectionHasOnlyDirs = false;
280 break;
281 }
282 }
283
284 if (selectionHasOnlyDirs) {
285 // insert 'Open in new tab' entry
286 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
287 }
288 // Insert 'Open With" entries
289 addOpenWithActions(fileItemActions);
290 }
291
292 insertDefaultItemActions(selectedItemsProps);
293
294 addSeparator();
295
296 fileItemActions.addServiceActionsTo(this);
297 fileItemActions.addPluginActionsTo(this);
298
299 addVersionControlPluginActions();
300
301 // insert 'Copy To' and 'Move To' sub menus
302 if (GeneralSettings::showCopyMoveMenu()) {
303 m_copyToMenu.setUrls(m_selectedItems.urlList());
304 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
305 m_copyToMenu.setAutoErrorHandlingEnabled(true);
306 m_copyToMenu.addActionsTo(this);
307 }
308
309 // insert 'Properties...' entry
310 addSeparator();
311 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
312 addAction(propertiesAction);
313
314 QAction* activatedAction = exec(m_pos);
315 if (activatedAction) {
316 if (activatedAction == addToPlacesAction) {
317 const QUrl selectedUrl(m_fileInfo.url());
318 if (selectedUrl.isValid()) {
319 PlacesItemModel model;
320 const QString text = selectedUrl.fileName();
321 model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
322 }
323 } else if (activatedAction == openParentAction) {
324 m_command = OpenParentFolder;
325 } else if (activatedAction == openParentInNewWindowAction) {
326 m_command = OpenParentFolderInNewWindow;
327 } else if (activatedAction == openParentInNewTabAction) {
328 m_command = OpenParentFolderInNewTab;
329 }
330 }
331 }
332
333 void DolphinContextMenu::openViewportContextMenu()
334 {
335 // setup 'Create New' menu
336 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
337 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
338 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
339 newFileMenu->checkUpToDate();
340 newFileMenu->setPopupFiles(m_baseUrl);
341 addMenu(newFileMenu->menu());
342 addSeparator();
343
344 // Insert 'Open With' entries
345 KFileItem baseItem = view->rootItem();
346 if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
347 baseItem = baseFileItem();
348 }
349
350 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseItem);
351 KFileItemActions fileItemActions;
352 fileItemActions.setParentWidget(m_mainWindow);
353 fileItemActions.setItemListProperties(baseUrlProperties);
354 addOpenWithActions(fileItemActions);
355
356 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
357 // "open_in_new_tab" here, as the current selection should get ignored.
358 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("file_new")));
359 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
360
361 // Insert 'Add to Places' entry if exactly one item is selected
362 QAction* addToPlacesAction = nullptr;
363 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
364 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
365 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
366 }
367
368 addSeparator();
369
370 QAction* pasteAction = createPasteAction();
371 addAction(pasteAction);
372 addSeparator();
373
374 // Insert 'Sort By' and 'View Mode'
375 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
376 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
377
378 addSeparator();
379
380 // Insert service actions
381 fileItemActions.addServiceActionsTo(this);
382 fileItemActions.addPluginActionsTo(this);
383
384 addVersionControlPluginActions();
385
386 addCustomActions();
387
388 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
389 addAction(propertiesAction);
390
391 addShowMenuBarAction();
392
393 QAction* action = exec(m_pos);
394 if (addToPlacesAction && (action == addToPlacesAction)) {
395 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
396 const QUrl url = container->url();
397 if (url.isValid()) {
398 PlacesItemModel model;
399 QString icon;
400 if (container->isSearchModeEnabled()) {
401 icon = QStringLiteral("folder-saved-search-symbolic");
402 } else {
403 icon = KIO::iconNameForUrl(url);
404 }
405 model.createPlacesItem(container->placesText(), url, icon);
406 }
407 }
408 }
409
410 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
411 {
412 const KActionCollection* collection = m_mainWindow->actionCollection();
413
414 // Insert 'Cut', 'Copy' and 'Paste'
415 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
416 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
417 addAction(createPasteAction());
418
419 addSeparator();
420
421 // Insert 'Rename'
422 addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
423
424 // Insert 'Move to Trash' and/or 'Delete'
425 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
426 !properties.isLocal());
427 const bool showMoveToTrashAction = (properties.isLocal() &&
428 properties.supportsMoving());
429
430 if (showDeleteAction && showMoveToTrashAction) {
431 delete m_removeAction;
432 m_removeAction = nullptr;
433 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
434 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
435 } else if (showDeleteAction && !showMoveToTrashAction) {
436 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
437 } else {
438 if (!m_removeAction) {
439 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
440 }
441 addAction(m_removeAction);
442 m_removeAction->update();
443 }
444 }
445
446 void DolphinContextMenu::addShowMenuBarAction()
447 {
448 const KActionCollection* ac = m_mainWindow->actionCollection();
449 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
450 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
451 addSeparator();
452 addAction(showMenuBar);
453 }
454 }
455
456 bool DolphinContextMenu::placeExists(const QUrl& url) const
457 {
458 Q_UNUSED(url)
459 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
460 // can be expensive because the model asks Solid for the devices which are
461 // available, which can take some time.
462 // TODO: Consider restoring this check if the handling of Places and devices
463 // will be decoupled in the future.
464 return false;
465 }
466
467 QAction* DolphinContextMenu::createPasteAction()
468 {
469 QAction* action = nullptr;
470 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
471 if (isDir && (m_selectedItems.count() == 1)) {
472 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
473 bool canPaste;
474 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
475 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
476 action->setEnabled(canPaste);
477 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
478 } else {
479 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
480 }
481
482 return action;
483 }
484
485 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
486 {
487 if (!m_selectedItemsProperties) {
488 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
489 }
490 return *m_selectedItemsProperties;
491 }
492
493 KFileItem DolphinContextMenu::baseFileItem()
494 {
495 if (!m_baseFileItem) {
496 m_baseFileItem = new KFileItem(m_baseUrl);
497 }
498 return *m_baseFileItem;
499 }
500
501 void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
502 {
503 // insert 'Open With...' action or sub menu
504 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
505 }
506
507 void DolphinContextMenu::addVersionControlPluginActions()
508 {
509 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
510 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
511 if (!versionControlActions.isEmpty()) {
512 addActions(versionControlActions);
513 addSeparator();
514 }
515 }
516
517 void DolphinContextMenu::addCustomActions()
518 {
519 addActions(m_customActions);
520 }
521