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