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>
41 #include <QMimeDatabase>
43 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
45 const KFileItem
& fileInfo
,
46 const QUrl
& baseUrl
) :
52 m_baseFileItem(nullptr),
54 m_selectedItemsProperties(nullptr),
59 m_removeAction(nullptr)
61 // The context menu either accesses the URLs of the selected items
62 // or the items itself. To increase the performance both lists are cached.
63 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
64 m_selectedItems
= view
->selectedItems();
67 DolphinContextMenu::~DolphinContextMenu()
69 delete m_baseFileItem
;
70 m_baseFileItem
= nullptr;
71 delete m_selectedItemsProperties
;
72 m_selectedItemsProperties
= nullptr;
75 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
77 m_customActions
= actions
;
80 DolphinContextMenu::Command
DolphinContextMenu::open()
82 // get the context information
83 const auto scheme
= m_baseUrl
.scheme();
84 if (scheme
== QLatin1String("trash")) {
85 m_context
|= TrashContext
;
86 } else if (scheme
.contains(QLatin1String("search"))) {
87 m_context
|= SearchContext
;
88 } else if (scheme
.contains(QLatin1String("timeline"))) {
89 m_context
|= TimelineContext
;
92 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
93 m_context
|= ItemContext
;
94 // TODO: handle other use cases like devices + desktop files
97 // open the corresponding popup for the context
98 if (m_context
& TrashContext
) {
99 if (m_context
& ItemContext
) {
100 openTrashItemContextMenu();
102 openTrashContextMenu();
104 } else if (m_context
& ItemContext
) {
105 openItemContextMenu();
107 Q_ASSERT(m_context
== NoContext
);
108 openViewportContextMenu();
114 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
116 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
117 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
119 QMenu::keyPressEvent(ev
);
122 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
124 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
125 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
127 QMenu::keyReleaseEvent(ev
);
130 void DolphinContextMenu::openTrashContextMenu()
132 Q_ASSERT(m_context
& TrashContext
);
134 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
135 emptyTrashAction
->setEnabled(!Trash::isEmpty());
136 addAction(emptyTrashAction
);
140 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
141 addAction(propertiesAction
);
143 addShowMenuBarAction();
145 if (exec(m_pos
) == emptyTrashAction
) {
146 Trash::empty(m_mainWindow
);
150 void DolphinContextMenu::openTrashItemContextMenu()
152 Q_ASSERT(m_context
& TrashContext
);
153 Q_ASSERT(m_context
& ItemContext
);
155 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
156 addAction(restoreAction
);
158 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
159 addAction(deleteAction
);
161 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
162 addAction(propertiesAction
);
164 if (exec(m_pos
) == restoreAction
) {
165 QList
<QUrl
> selectedUrls
;
166 selectedUrls
.reserve(m_selectedItems
.count());
167 foreach (const KFileItem
&item
, m_selectedItems
) {
168 selectedUrls
.append(item
.url());
171 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
172 KJobWidgets::setWindow(job
, m_mainWindow
);
173 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
177 void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions
&fileItemActions
)
179 // insert 'Open in new window' and 'Open in new tab' entries
181 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
183 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
184 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
186 // Insert 'Open With' entries
187 addOpenWithActions(fileItemActions
);
189 // set up 'Create New' menu
190 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
191 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
192 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
193 newFileMenu
->checkUpToDate();
194 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_fileInfo
.url());
195 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
196 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
197 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
199 QMenu
* menu
= newFileMenu
->menu();
200 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
201 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
207 void DolphinContextMenu::openItemContextMenu()
209 Q_ASSERT(!m_fileInfo
.isNull());
211 QAction
* openParentAction
= nullptr;
212 QAction
* openParentInNewWindowAction
= nullptr;
213 QAction
* openParentInNewTabAction
= nullptr;
214 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
216 KFileItemActions fileItemActions
;
217 fileItemActions
.setParentWidget(m_mainWindow
);
218 fileItemActions
.setItemListProperties(selectedItemsProps
);
220 if (m_selectedItems
.count() == 1) {
222 if (m_fileInfo
.isDir()) {
223 addDirectoryItemContextMenu(fileItemActions
);
224 } else if (m_context
& TimelineContext
|| m_context
& SearchContext
) {
225 addOpenWithActions(fileItemActions
);
227 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
228 i18nc("@action:inmenu",
231 addAction(openParentAction
);
233 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
234 i18nc("@action:inmenu",
235 "Open Path in New Window"),
237 addAction(openParentInNewWindowAction
);
239 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
240 i18nc("@action:inmenu",
241 "Open Path in New Tab"),
243 addAction(openParentInNewTabAction
);
247 // Insert 'Open With" entries
248 addOpenWithActions(fileItemActions
);
250 if (m_fileInfo
.isLink()) {
251 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
256 bool selectionHasOnlyDirs
= true;
257 for (const auto &item
: qAsConst(m_selectedItems
)) {
258 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
260 selectionHasOnlyDirs
= false;
265 if (selectionHasOnlyDirs
) {
266 // insert 'Open in new tab' entry
267 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
269 // Insert 'Open With" entries
270 addOpenWithActions(fileItemActions
);
273 insertDefaultItemActions(selectedItemsProps
);
275 // insert 'Add to Places' entry if appropriate
276 if (m_selectedItems
.count() == 1) {
277 if (m_fileInfo
.isDir()) {
278 if (!placeExists(m_fileInfo
.url())) {
279 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
286 fileItemActions
.addServiceActionsTo(this);
287 fileItemActions
.addPluginActionsTo(this);
289 addVersionControlPluginActions();
291 // insert 'Copy To' and 'Move To' sub menus
292 if (GeneralSettings::showCopyMoveMenu()) {
293 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
294 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
295 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
296 m_copyToMenu
.addActionsTo(this);
299 // insert 'Properties...' entry
301 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
302 addAction(propertiesAction
);
304 QAction
* activatedAction
= exec(m_pos
);
305 if (activatedAction
) {
306 if (activatedAction
== openParentAction
) {
307 m_command
= OpenParentFolder
;
308 } else if (activatedAction
== openParentInNewWindowAction
) {
309 m_command
= OpenParentFolderInNewWindow
;
310 } else if (activatedAction
== openParentInNewTabAction
) {
311 m_command
= OpenParentFolderInNewTab
;
316 void DolphinContextMenu::openViewportContextMenu()
318 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
320 // Insert 'Open With' entries
321 KFileItem baseItem
= view
->rootItem();
322 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
323 baseItem
= baseFileItem();
326 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
327 KFileItemActions fileItemActions
;
328 fileItemActions
.setParentWidget(m_mainWindow
);
329 fileItemActions
.setItemListProperties(baseUrlProperties
);
331 // Set up and insert 'Create New' menu
332 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
333 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
334 newFileMenu
->checkUpToDate();
335 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_baseUrl
);
336 addMenu(newFileMenu
->menu());
338 // Don't show "Open With" menu items if the current dir is empty, because there's
339 // generally no app that can do anything interesting with an empty directory
340 if (view
->itemsCount() != 0) {
341 addOpenWithActions(fileItemActions
);
344 QAction
* pasteAction
= createPasteAction();
345 addAction(pasteAction
);
347 // Insert 'Add to Places' entry if it's not already in the places panel
348 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
349 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
353 // Insert 'Sort By' and 'View Mode'
354 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
355 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
359 // Insert service actions
360 fileItemActions
.addServiceActionsTo(this);
361 fileItemActions
.addPluginActionsTo(this);
363 addVersionControlPluginActions();
369 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
370 addAction(propertiesAction
);
372 addShowMenuBarAction();
377 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
379 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
381 // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
382 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
383 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
384 QAction
* copyPathAction
= collection
->action(QString("copy_location"));
385 copyPathAction
->setEnabled(m_selectedItems
.size() == 1);
386 addAction(copyPathAction
);
387 addAction(createPasteAction());
388 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("duplicate")));
393 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
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;
439 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
440 if (isDir
&& (m_selectedItems
.count() == 1)) {
441 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
443 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
444 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
445 action
->setEnabled(canPaste
);
446 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
448 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
454 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
456 if (!m_selectedItemsProperties
) {
457 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
459 return *m_selectedItemsProperties
;
462 KFileItem
DolphinContextMenu::baseFileItem()
464 if (!m_baseFileItem
) {
465 m_baseFileItem
= new KFileItem(m_baseUrl
);
467 return *m_baseFileItem
;
470 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
472 // insert 'Open With...' action or sub menu
473 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
476 void DolphinContextMenu::addVersionControlPluginActions()
478 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
479 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
480 if (!versionControlActions
.isEmpty()) {
481 addActions(versionControlActions
);
486 void DolphinContextMenu::addCustomActions()
488 addActions(m_customActions
);