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
);
276 fileItemActions
.addServiceActionsTo(this);
277 fileItemActions
.addPluginActionsTo(this);
279 addVersionControlPluginActions();
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);
289 // insert 'Properties...' entry
291 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
292 addAction(propertiesAction
);
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
;
306 void DolphinContextMenu::openViewportContextMenu()
308 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
310 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
311 KFileItemActions fileItemActions
;
312 fileItemActions
.setParentWidget(m_mainWindow
);
313 fileItemActions
.setItemListProperties(baseUrlProperties
);
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());
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
);
326 QAction
* pasteAction
= createPasteAction();
328 addAction(pasteAction
);
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")));
337 // Insert 'Sort By' and 'View Mode'
338 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
339 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
343 // Insert service actions
344 QList
<QAction
*> additionalActions
;
345 if (baseUrlProperties
.isDirectory() && baseUrlProperties
.isLocal()) {
346 additionalActions
<< m_mainWindow
->actionCollection()->action(QStringLiteral("open_terminal"));
348 fileItemActions
.addServiceActionsTo(this, additionalActions
);
349 fileItemActions
.addPluginActionsTo(this);
351 addVersionControlPluginActions();
357 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
358 addAction(propertiesAction
);
360 addShowMenuBarAction();
365 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
367 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
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();
377 addAction(pasteAction
);
379 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("duplicate")));
382 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
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")));
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());
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
)));
409 if (!m_removeAction
) {
410 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
412 addAction(m_removeAction
);
413 m_removeAction
->update();
417 void DolphinContextMenu::addShowMenuBarAction()
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()) {
423 addAction(showMenuBar
);
427 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
429 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
431 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
433 return !matchedPlaces
.isEmpty();
436 QAction
* DolphinContextMenu::createPasteAction()
438 QAction
* action
= nullptr;
440 if (!m_fileInfo
.isNull()) {
441 destItem
= m_fileInfo
;
443 destItem
= baseFileItem();
446 if (!destItem
.isNull() && destItem
.isDir()) {
447 if (m_selectedItems
.count() <= 1) {
448 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
450 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, destItem
);
451 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
453 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
455 // don't add the unavailable action
464 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
466 if (!m_selectedItemsProperties
) {
467 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
469 return *m_selectedItemsProperties
;
472 KFileItem
DolphinContextMenu::baseFileItem()
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
);
480 m_baseFileItem
= new KFileItem(baseItem
);
483 return *m_baseFileItem
;
486 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
488 // insert 'Open With...' action or sub menu
489 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
492 void DolphinContextMenu::addVersionControlPluginActions()
494 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
495 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
496 if (!versionControlActions
.isEmpty()) {
497 addActions(versionControlActions
);
502 void DolphinContextMenu::addCustomActions()
504 addActions(m_customActions
);