2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Cvetoslav Ludmiloff
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphincontextmenu.h"
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"
21 #include <KActionCollection>
22 #include <KFileItemActions>
23 #include <KFileItemListProperties>
24 #include <KIO/EmptyTrashJob>
25 #include <KIO/JobUiDelegate>
27 #include <KIO/RestoreJob>
28 #include <KJobWidgets>
29 #include <KLocalizedString>
30 #include <KNewFileMenu>
31 #include <KPluginMetaData>
33 #include <KStandardAction>
36 #include <QApplication>
40 #include <QMimeDatabase>
42 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
44 const KFileItem
& fileInfo
,
45 const QUrl
& baseUrl
) :
51 m_baseFileItem(nullptr),
53 m_selectedItemsProperties(nullptr),
58 m_removeAction(nullptr)
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();
66 DolphinContextMenu::~DolphinContextMenu()
68 delete m_baseFileItem
;
69 m_baseFileItem
= nullptr;
70 delete m_selectedItemsProperties
;
71 m_selectedItemsProperties
= nullptr;
74 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
76 m_customActions
= actions
;
79 DolphinContextMenu::Command
DolphinContextMenu::open()
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
;
91 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
92 m_context
|= ItemContext
;
93 // TODO: handle other use cases like devices + desktop files
96 // open the corresponding popup for the context
97 if (m_context
& TrashContext
) {
98 if (m_context
& ItemContext
) {
99 openTrashItemContextMenu();
101 openTrashContextMenu();
103 } else if (m_context
& ItemContext
) {
104 openItemContextMenu();
106 Q_ASSERT(m_context
== NoContext
);
107 openViewportContextMenu();
113 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
115 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
116 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
118 QMenu::keyPressEvent(ev
);
121 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
123 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
124 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
126 QMenu::keyReleaseEvent(ev
);
129 void DolphinContextMenu::openTrashContextMenu()
131 Q_ASSERT(m_context
& TrashContext
);
133 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
134 emptyTrashAction
->setEnabled(!Trash::isEmpty());
135 addAction(emptyTrashAction
);
139 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
140 addAction(propertiesAction
);
142 addShowMenuBarAction();
144 if (exec(m_pos
) == emptyTrashAction
) {
145 Trash::empty(m_mainWindow
);
149 void DolphinContextMenu::openTrashItemContextMenu()
151 Q_ASSERT(m_context
& TrashContext
);
152 Q_ASSERT(m_context
& ItemContext
);
154 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
155 addAction(restoreAction
);
157 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
158 addAction(deleteAction
);
160 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
161 addAction(propertiesAction
);
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());
170 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
171 KJobWidgets::setWindow(job
, m_mainWindow
);
172 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
176 void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions
&fileItemActions
)
178 // insert 'Open in new window' and 'Open in new tab' entries
180 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
182 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
183 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
185 // Insert 'Open With' entries
186 addOpenWithActions(fileItemActions
);
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
);
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")));
206 void DolphinContextMenu::openItemContextMenu()
208 Q_ASSERT(!m_fileInfo
.isNull());
210 QAction
* openParentAction
= nullptr;
211 QAction
* openParentInNewWindowAction
= nullptr;
212 QAction
* openParentInNewTabAction
= nullptr;
213 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
215 KFileItemActions fileItemActions
;
216 fileItemActions
.setParentWidget(m_mainWindow
);
217 fileItemActions
.setItemListProperties(selectedItemsProps
);
219 if (m_selectedItems
.count() == 1) {
221 if (m_fileInfo
.isDir()) {
222 addDirectoryItemContextMenu(fileItemActions
);
223 } else if (m_context
& TimelineContext
|| m_context
& SearchContext
) {
224 addOpenWithActions(fileItemActions
);
226 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
227 i18nc("@action:inmenu",
230 addAction(openParentAction
);
232 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
233 i18nc("@action:inmenu",
234 "Open Path in New Window"),
236 addAction(openParentInNewWindowAction
);
238 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
239 i18nc("@action:inmenu",
240 "Open Path in New Tab"),
242 addAction(openParentInNewTabAction
);
246 // Insert 'Open With" entries
247 addOpenWithActions(fileItemActions
);
249 if (m_fileInfo
.isLink()) {
250 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
255 bool selectionHasOnlyDirs
= true;
256 for (const auto &item
: qAsConst(m_selectedItems
)) {
257 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
259 selectionHasOnlyDirs
= false;
264 if (selectionHasOnlyDirs
) {
265 // insert 'Open in new tab' entry
266 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
268 // Insert 'Open With" entries
269 addOpenWithActions(fileItemActions
);
272 insertDefaultItemActions(selectedItemsProps
);
274 // insert 'Add to Places' entry if appropriate
275 if (m_selectedItems
.count() == 1) {
276 if (m_fileInfo
.isDir()) {
277 if (!placeExists(m_fileInfo
.url())) {
278 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
285 fileItemActions
.addServiceActionsTo(this);
286 fileItemActions
.addPluginActionsTo(this);
288 addVersionControlPluginActions();
290 // insert 'Copy To' and 'Move To' sub menus
291 if (GeneralSettings::showCopyMoveMenu()) {
292 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
293 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
294 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
295 m_copyToMenu
.addActionsTo(this);
298 // insert 'Properties...' entry
300 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
301 addAction(propertiesAction
);
303 QAction
* activatedAction
= exec(m_pos
);
304 if (activatedAction
) {
305 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
;
315 void DolphinContextMenu::openViewportContextMenu()
317 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
319 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
320 KFileItemActions fileItemActions
;
321 fileItemActions
.setParentWidget(m_mainWindow
);
322 fileItemActions
.setItemListProperties(baseUrlProperties
);
324 // Set up and insert 'Create New' menu
325 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
326 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
327 newFileMenu
->checkUpToDate();
328 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_baseUrl
);
329 addMenu(newFileMenu
->menu());
331 // Show "open with" menu items even if the dir is empty, because there are legitimate
332 // use cases for this, such as opening an empty dir in Kate or VSCode or something
333 addOpenWithActions(fileItemActions
);
335 QAction
* pasteAction
= createPasteAction();
337 addAction(pasteAction
);
340 // Insert 'Add to Places' entry if it's not already in the places panel
341 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
342 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
346 // Insert 'Sort By' and 'View Mode'
347 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
348 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
352 // Insert service actions
353 fileItemActions
.addServiceActionsTo(this);
354 fileItemActions
.addPluginActionsTo(this);
356 addVersionControlPluginActions();
362 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
363 addAction(propertiesAction
);
365 addShowMenuBarAction();
370 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
372 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
374 // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
375 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
376 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
377 QAction
* copyPathAction
= collection
->action(QString("copy_location"));
378 copyPathAction
->setEnabled(m_selectedItems
.size() == 1);
379 addAction(copyPathAction
);
380 QAction
* pasteAction
= createPasteAction();
382 addAction(pasteAction
);
384 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("duplicate")));
389 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
391 // Insert 'Move to Trash' and/or 'Delete'
392 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
393 !properties
.isLocal());
394 const bool showMoveToTrashAction
= (properties
.isLocal() &&
395 properties
.supportsMoving());
397 if (showDeleteAction
&& showMoveToTrashAction
) {
398 delete m_removeAction
;
399 m_removeAction
= nullptr;
400 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
401 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
402 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
403 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
405 if (!m_removeAction
) {
406 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
408 addAction(m_removeAction
);
409 m_removeAction
->update();
413 void DolphinContextMenu::addShowMenuBarAction()
415 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
416 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
417 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
419 addAction(showMenuBar
);
423 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
425 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
427 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
429 return !matchedPlaces
.isEmpty();
432 QAction
* DolphinContextMenu::createPasteAction()
434 QAction
* action
= nullptr;
436 if (!m_fileInfo
.isNull() && m_selectedItems
.count() <= 1) {
437 destItem
= m_fileInfo
;
439 destItem
= baseFileItem();
442 if (!destItem
.isNull() && destItem
.isDir()) {
443 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
445 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, destItem
);
447 if (destItem
== m_fileInfo
) {
448 // if paste destination is a selected folder
449 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
450 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
452 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
460 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
462 if (!m_selectedItemsProperties
) {
463 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
465 return *m_selectedItemsProperties
;
468 KFileItem
DolphinContextMenu::baseFileItem()
470 if (!m_baseFileItem
) {
471 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
472 KFileItem baseItem
= view
->rootItem();
473 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
474 m_baseFileItem
= new KFileItem(m_baseUrl
);
476 m_baseFileItem
= new KFileItem(baseItem
);
479 return *m_baseFileItem
;
482 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
484 // insert 'Open With...' action or sub menu
485 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
488 void DolphinContextMenu::addVersionControlPluginActions()
490 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
491 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
492 if (!versionControlActions
.isEmpty()) {
493 addActions(versionControlActions
);
498 void DolphinContextMenu::addCustomActions()
500 addActions(m_customActions
);