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 openViewportContextMenu();
112 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
114 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
115 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
117 QMenu::keyPressEvent(ev
);
120 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
122 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
123 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
125 QMenu::keyReleaseEvent(ev
);
128 void DolphinContextMenu::openTrashContextMenu()
130 Q_ASSERT(m_context
& TrashContext
);
132 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
133 emptyTrashAction
->setEnabled(!Trash::isEmpty());
134 addAction(emptyTrashAction
);
138 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
139 addAction(propertiesAction
);
141 addShowMenuBarAction();
143 if (exec(m_pos
) == emptyTrashAction
) {
144 Trash::empty(m_mainWindow
);
148 void DolphinContextMenu::openTrashItemContextMenu()
150 Q_ASSERT(m_context
& TrashContext
);
151 Q_ASSERT(m_context
& ItemContext
);
153 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
154 addAction(restoreAction
);
156 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
157 addAction(deleteAction
);
159 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
160 addAction(propertiesAction
);
162 if (exec(m_pos
) == restoreAction
) {
163 QList
<QUrl
> selectedUrls
;
164 selectedUrls
.reserve(m_selectedItems
.count());
165 for (const KFileItem
&item
: qAsConst(m_selectedItems
)) {
166 selectedUrls
.append(item
.url());
169 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
170 KJobWidgets::setWindow(job
, m_mainWindow
);
171 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
175 void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions
&fileItemActions
)
177 // insert 'Open in new window' and 'Open in new tab' entries
179 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
181 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
182 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
184 // Insert 'Open With' entries
185 addOpenWithActions(fileItemActions
);
187 // set up 'Create New' menu
188 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
189 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
190 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
191 newFileMenu
->checkUpToDate();
192 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_fileInfo
.url());
193 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
194 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
195 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
197 QMenu
* menu
= newFileMenu
->menu();
198 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
199 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
205 void DolphinContextMenu::openItemContextMenu()
207 Q_ASSERT(!m_fileInfo
.isNull());
209 QAction
* openParentAction
= nullptr;
210 QAction
* openParentInNewWindowAction
= nullptr;
211 QAction
* openParentInNewTabAction
= nullptr;
212 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
214 KFileItemActions fileItemActions
;
215 fileItemActions
.setParentWidget(m_mainWindow
);
216 fileItemActions
.setItemListProperties(selectedItemsProps
);
218 if (m_selectedItems
.count() == 1) {
220 if (m_fileInfo
.isDir()) {
221 addDirectoryItemContextMenu(fileItemActions
);
222 } else if (m_context
& TimelineContext
|| m_context
& SearchContext
) {
223 addOpenWithActions(fileItemActions
);
225 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
226 i18nc("@action:inmenu",
229 addAction(openParentAction
);
231 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
232 i18nc("@action:inmenu",
233 "Open Path in New Window"),
235 addAction(openParentInNewWindowAction
);
237 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
238 i18nc("@action:inmenu",
239 "Open Path in New Tab"),
241 addAction(openParentInNewTabAction
);
245 // Insert 'Open With" entries
246 addOpenWithActions(fileItemActions
);
248 if (m_fileInfo
.isLink()) {
249 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
254 bool selectionHasOnlyDirs
= true;
255 for (const auto &item
: qAsConst(m_selectedItems
)) {
256 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
258 selectionHasOnlyDirs
= false;
263 if (selectionHasOnlyDirs
) {
264 // insert 'Open in new tab' entry
265 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
267 // Insert 'Open With" entries
268 addOpenWithActions(fileItemActions
);
271 insertDefaultItemActions(selectedItemsProps
);
273 // insert 'Add to Places' entry if appropriate
274 if (m_selectedItems
.count() == 1) {
275 if (m_fileInfo
.isDir()) {
276 if (!placeExists(m_fileInfo
.url())) {
277 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
284 fileItemActions
.addServiceActionsTo(this);
285 fileItemActions
.addPluginActionsTo(this);
287 addVersionControlPluginActions();
289 // insert 'Copy To' and 'Move To' sub menus
290 if (GeneralSettings::showCopyMoveMenu()) {
291 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
292 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
293 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
294 m_copyToMenu
.addActionsTo(this);
297 // insert 'Properties...' entry
299 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
300 addAction(propertiesAction
);
302 QAction
* activatedAction
= exec(m_pos
);
303 if (activatedAction
) {
304 if (activatedAction
== openParentAction
) {
305 m_command
= OpenParentFolder
;
306 } else if (activatedAction
== openParentInNewWindowAction
) {
307 m_command
= OpenParentFolderInNewWindow
;
308 } else if (activatedAction
== openParentInNewTabAction
) {
309 m_command
= OpenParentFolderInNewTab
;
314 void DolphinContextMenu::openViewportContextMenu()
316 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
318 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
319 KFileItemActions fileItemActions
;
320 fileItemActions
.setParentWidget(m_mainWindow
);
321 fileItemActions
.setItemListProperties(baseUrlProperties
);
323 // Set up and insert 'Create New' menu
324 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
325 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
326 newFileMenu
->checkUpToDate();
327 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_baseUrl
);
328 addMenu(newFileMenu
->menu());
330 // Show "open with" menu items even if the dir is empty, because there are legitimate
331 // use cases for this, such as opening an empty dir in Kate or VSCode or something
332 addOpenWithActions(fileItemActions
);
334 QAction
* pasteAction
= createPasteAction();
336 addAction(pasteAction
);
339 // Insert 'Add to Places' entry if it's not already in the places panel
340 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
341 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
345 // Insert 'Sort By' and 'View Mode'
346 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
347 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
351 // Insert service actions
352 fileItemActions
.addServiceActionsTo(this);
353 fileItemActions
.addPluginActionsTo(this);
355 addVersionControlPluginActions();
361 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
362 addAction(propertiesAction
);
364 addShowMenuBarAction();
369 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
371 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
373 // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
374 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
375 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
376 QAction
* copyPathAction
= collection
->action(QString("copy_location"));
377 copyPathAction
->setEnabled(m_selectedItems
.size() == 1);
378 addAction(copyPathAction
);
379 QAction
* pasteAction
= createPasteAction();
381 addAction(pasteAction
);
383 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("duplicate")));
388 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
390 // Insert 'Move to Trash' and/or 'Delete'
391 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
392 !properties
.isLocal());
393 const bool showMoveToTrashAction
= (properties
.isLocal() &&
394 properties
.supportsMoving());
396 if (showDeleteAction
&& showMoveToTrashAction
) {
397 delete m_removeAction
;
398 m_removeAction
= nullptr;
399 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
400 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
401 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
402 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
404 if (!m_removeAction
) {
405 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
407 addAction(m_removeAction
);
408 m_removeAction
->update();
412 void DolphinContextMenu::addShowMenuBarAction()
414 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
415 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
416 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
418 addAction(showMenuBar
);
422 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
424 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
426 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
428 return !matchedPlaces
.isEmpty();
431 QAction
* DolphinContextMenu::createPasteAction()
433 QAction
* action
= nullptr;
435 if (!m_fileInfo
.isNull() && m_selectedItems
.count() <= 1) {
436 destItem
= m_fileInfo
;
438 destItem
= baseFileItem();
441 if (!destItem
.isNull() && destItem
.isDir()) {
442 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
444 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, destItem
);
446 if (destItem
== m_fileInfo
) {
447 // if paste destination is a selected folder
448 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
449 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
451 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
459 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
461 if (!m_selectedItemsProperties
) {
462 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
464 return *m_selectedItemsProperties
;
467 KFileItem
DolphinContextMenu::baseFileItem()
469 if (!m_baseFileItem
) {
470 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
471 KFileItem baseItem
= view
->rootItem();
472 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
473 m_baseFileItem
= new KFileItem(m_baseUrl
);
475 m_baseFileItem
= new KFileItem(baseItem
);
478 return *m_baseFileItem
;
481 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
483 // insert 'Open With...' action or sub menu
484 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
487 void DolphinContextMenu::addVersionControlPluginActions()
489 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
490 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
491 if (!versionControlActions
.isEmpty()) {
492 addActions(versionControlActions
);
497 void DolphinContextMenu::addCustomActions()
499 addActions(m_customActions
);