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