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 // Insert 'Open With' entries
320 KFileItem baseItem
= view
->rootItem();
321 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
322 baseItem
= baseFileItem();
325 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
326 KFileItemActions fileItemActions
;
327 fileItemActions
.setParentWidget(m_mainWindow
);
328 fileItemActions
.setItemListProperties(baseUrlProperties
);
330 // Set up and insert 'Create New' menu
331 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
332 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
333 newFileMenu
->checkUpToDate();
334 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_baseUrl
);
335 addMenu(newFileMenu
->menu());
337 // Show "open with" menu items even if the dir is empty, because there are legitimate
338 // use cases for this, such as opening an empty dir in Kate or VSCode or something
339 addOpenWithActions(fileItemActions
);
341 QAction
* pasteAction
= createPasteAction();
342 addAction(pasteAction
);
344 // Insert 'Add to Places' entry if it's not already in the places panel
345 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
346 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
350 // Insert 'Sort By' and 'View Mode'
351 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
352 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
356 // Insert service actions
357 fileItemActions
.addServiceActionsTo(this);
358 fileItemActions
.addPluginActionsTo(this);
360 addVersionControlPluginActions();
366 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
367 addAction(propertiesAction
);
369 addShowMenuBarAction();
374 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
376 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
378 // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
379 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
380 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
381 QAction
* copyPathAction
= collection
->action(QString("copy_location"));
382 copyPathAction
->setEnabled(m_selectedItems
.size() == 1);
383 addAction(copyPathAction
);
384 addAction(createPasteAction());
385 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("duplicate")));
390 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
392 // Insert 'Move to Trash' and/or 'Delete'
393 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
394 !properties
.isLocal());
395 const bool showMoveToTrashAction
= (properties
.isLocal() &&
396 properties
.supportsMoving());
398 if (showDeleteAction
&& showMoveToTrashAction
) {
399 delete m_removeAction
;
400 m_removeAction
= nullptr;
401 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
402 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
403 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
404 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
406 if (!m_removeAction
) {
407 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
409 addAction(m_removeAction
);
410 m_removeAction
->update();
414 void DolphinContextMenu::addShowMenuBarAction()
416 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
417 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
418 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
420 addAction(showMenuBar
);
424 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
426 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
428 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
430 return !matchedPlaces
.isEmpty();
433 QAction
* DolphinContextMenu::createPasteAction()
435 QAction
* action
= nullptr;
436 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
437 if (isDir
&& (m_selectedItems
.count() == 1)) {
438 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
440 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
441 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
442 action
->setEnabled(canPaste
);
443 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
445 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
451 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
453 if (!m_selectedItemsProperties
) {
454 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
456 return *m_selectedItemsProperties
;
459 KFileItem
DolphinContextMenu::baseFileItem()
461 if (!m_baseFileItem
) {
462 m_baseFileItem
= new KFileItem(m_baseUrl
);
464 return *m_baseFileItem
;
467 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
469 // insert 'Open With...' action or sub menu
470 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
473 void DolphinContextMenu::addVersionControlPluginActions()
475 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
476 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
477 if (!versionControlActions
.isEmpty()) {
478 addActions(versionControlActions
);
483 void DolphinContextMenu::addCustomActions()
485 addActions(m_customActions
);