1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphincontextmenu.h"
23 #include "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinnewfilemenu.h"
26 #include "dolphinplacesmodelsingleton.h"
27 #include "dolphinremoveaction.h"
28 #include "dolphinviewcontainer.h"
29 #include "panels/places/placesitem.h"
30 #include "panels/places/placesitemmodel.h"
31 #include "trash/dolphintrash.h"
32 #include "views/dolphinview.h"
33 #include "views/viewmodecontroller.h"
35 #include <KActionCollection>
36 #include <KFileItemActions>
37 #include <KFileItemListProperties>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
41 #include <KIO/RestoreJob>
42 #include <KJobWidgets>
43 #include <KLocalizedString>
44 #include <KNewFileMenu>
45 #include <KPluginMetaData>
47 #include <KStandardAction>
50 #include <QApplication>
55 #include <QMimeDatabase>
57 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
59 const KFileItem
& fileInfo
,
60 const QUrl
& baseUrl
) :
66 m_baseFileItem(nullptr),
68 m_selectedItemsProperties(nullptr),
73 m_removeAction(nullptr)
75 // The context menu either accesses the URLs of the selected items
76 // or the items itself. To increase the performance both lists are cached.
77 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
78 m_selectedItems
= view
->selectedItems();
81 DolphinContextMenu::~DolphinContextMenu()
83 delete m_baseFileItem
;
84 m_baseFileItem
= nullptr;
85 delete m_selectedItemsProperties
;
86 m_selectedItemsProperties
= nullptr;
89 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
91 m_customActions
= actions
;
94 DolphinContextMenu::Command
DolphinContextMenu::open()
96 // get the context information
97 const auto scheme
= m_baseUrl
.scheme();
98 if (scheme
== QLatin1String("trash")) {
99 m_context
|= TrashContext
;
100 } else if (scheme
.contains(QLatin1String("search"))) {
101 m_context
|= SearchContext
;
102 } else if (scheme
.contains(QLatin1String("timeline"))) {
103 m_context
|= TimelineContext
;
106 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
107 m_context
|= ItemContext
;
108 // TODO: handle other use cases like devices + desktop files
111 // open the corresponding popup for the context
112 if (m_context
& TrashContext
) {
113 if (m_context
& ItemContext
) {
114 openTrashItemContextMenu();
116 openTrashContextMenu();
118 } else if (m_context
& ItemContext
) {
119 openItemContextMenu();
121 Q_ASSERT(m_context
== NoContext
);
122 openViewportContextMenu();
128 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
130 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
131 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
133 QMenu::keyPressEvent(ev
);
136 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
138 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
139 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
141 QMenu::keyReleaseEvent(ev
);
144 void DolphinContextMenu::openTrashContextMenu()
146 Q_ASSERT(m_context
& TrashContext
);
148 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
149 emptyTrashAction
->setEnabled(!Trash::isEmpty());
150 addAction(emptyTrashAction
);
154 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
155 addAction(propertiesAction
);
157 addShowMenuBarAction();
159 if (exec(m_pos
) == emptyTrashAction
) {
160 Trash::empty(m_mainWindow
);
164 void DolphinContextMenu::openTrashItemContextMenu()
166 Q_ASSERT(m_context
& TrashContext
);
167 Q_ASSERT(m_context
& ItemContext
);
169 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
170 addAction(restoreAction
);
172 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
173 addAction(deleteAction
);
175 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
176 addAction(propertiesAction
);
178 if (exec(m_pos
) == restoreAction
) {
179 QList
<QUrl
> selectedUrls
;
180 selectedUrls
.reserve(m_selectedItems
.count());
181 foreach (const KFileItem
&item
, m_selectedItems
) {
182 selectedUrls
.append(item
.url());
185 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
186 KJobWidgets::setWindow(job
, m_mainWindow
);
187 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
191 void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions
&fileItemActions
)
193 // insert 'Open in new window' and 'Open in new tab' entries
195 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
197 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
198 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
200 // Insert 'Open With' entries
201 addOpenWithActions(fileItemActions
);
203 // set up 'Create New' menu
204 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
205 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
206 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
207 newFileMenu
->checkUpToDate();
208 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_fileInfo
.url());
209 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
210 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
211 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
213 QMenu
* menu
= newFileMenu
->menu();
214 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
215 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
221 void DolphinContextMenu::openItemContextMenu()
223 Q_ASSERT(!m_fileInfo
.isNull());
225 QAction
* openParentAction
= nullptr;
226 QAction
* openParentInNewWindowAction
= nullptr;
227 QAction
* openParentInNewTabAction
= nullptr;
228 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
230 KFileItemActions fileItemActions
;
231 fileItemActions
.setParentWidget(m_mainWindow
);
232 fileItemActions
.setItemListProperties(selectedItemsProps
);
234 if (m_selectedItems
.count() == 1) {
236 if (m_fileInfo
.isDir()) {
237 addDirectoryItemContextMenu(fileItemActions
);
238 } else if (m_context
& TimelineContext
|| m_context
& SearchContext
) {
239 addOpenWithActions(fileItemActions
);
241 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
242 i18nc("@action:inmenu",
245 addAction(openParentAction
);
247 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
248 i18nc("@action:inmenu",
249 "Open Path in New Window"),
251 addAction(openParentInNewWindowAction
);
253 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
254 i18nc("@action:inmenu",
255 "Open Path in New Tab"),
257 addAction(openParentInNewTabAction
);
261 // Insert 'Open With" entries
262 addOpenWithActions(fileItemActions
);
264 if (m_fileInfo
.isLink()) {
265 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
270 bool selectionHasOnlyDirs
= true;
271 for (const auto &item
: qAsConst(m_selectedItems
)) {
272 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
274 selectionHasOnlyDirs
= false;
279 if (selectionHasOnlyDirs
) {
280 // insert 'Open in new tab' entry
281 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
283 // Insert 'Open With" entries
284 addOpenWithActions(fileItemActions
);
287 insertDefaultItemActions(selectedItemsProps
);
289 // insert 'Add to Places' entry if appropriate
290 if (m_selectedItems
.count() == 1) {
291 if (m_fileInfo
.isDir()) {
292 if (!placeExists(m_fileInfo
.url())) {
293 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
300 fileItemActions
.addServiceActionsTo(this);
301 fileItemActions
.addPluginActionsTo(this);
303 addVersionControlPluginActions();
305 // insert 'Copy To' and 'Move To' sub menus
306 if (GeneralSettings::showCopyMoveMenu()) {
307 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
308 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
309 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
310 m_copyToMenu
.addActionsTo(this);
313 // insert 'Properties...' entry
315 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
316 addAction(propertiesAction
);
318 QAction
* activatedAction
= exec(m_pos
);
319 if (activatedAction
) {
320 if (activatedAction
== openParentAction
) {
321 m_command
= OpenParentFolder
;
322 } else if (activatedAction
== openParentInNewWindowAction
) {
323 m_command
= OpenParentFolderInNewWindow
;
324 } else if (activatedAction
== openParentInNewTabAction
) {
325 m_command
= OpenParentFolderInNewTab
;
330 void DolphinContextMenu::openViewportContextMenu()
332 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
334 // Insert 'Open With' entries
335 KFileItem baseItem
= view
->rootItem();
336 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
337 baseItem
= baseFileItem();
340 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
341 KFileItemActions fileItemActions
;
342 fileItemActions
.setParentWidget(m_mainWindow
);
343 fileItemActions
.setItemListProperties(baseUrlProperties
);
345 // Set up and insert 'Create New' menu
346 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
347 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
348 newFileMenu
->checkUpToDate();
349 newFileMenu
->setPopupFiles(QList
<QUrl
>() << m_baseUrl
);
350 addMenu(newFileMenu
->menu());
352 // Don't show "Open With" menu items if the current dir is empty, because there's
353 // generally no app that can do anything interesting with an empty directory
354 if (view
->itemsCount() != 0) {
355 addOpenWithActions(fileItemActions
);
358 QAction
* pasteAction
= createPasteAction();
359 addAction(pasteAction
);
361 // Insert 'Add to Places' entry if it's not already in the places panel
362 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
363 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
367 // Insert 'Sort By' and 'View Mode'
368 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
369 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
373 // Insert service actions
374 fileItemActions
.addServiceActionsTo(this);
375 fileItemActions
.addPluginActionsTo(this);
377 addVersionControlPluginActions();
383 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
384 addAction(propertiesAction
);
386 addShowMenuBarAction();
391 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
393 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
395 // Insert 'Cut', 'Copy' and 'Paste'
396 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
397 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
398 addAction(createPasteAction());
403 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
405 // Insert 'Move to Trash' and/or 'Delete'
406 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
407 !properties
.isLocal());
408 const bool showMoveToTrashAction
= (properties
.isLocal() &&
409 properties
.supportsMoving());
411 if (showDeleteAction
&& showMoveToTrashAction
) {
412 delete m_removeAction
;
413 m_removeAction
= nullptr;
414 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
415 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
416 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
417 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
419 if (!m_removeAction
) {
420 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
422 addAction(m_removeAction
);
423 m_removeAction
->update();
427 void DolphinContextMenu::addShowMenuBarAction()
429 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
430 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
431 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
433 addAction(showMenuBar
);
437 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
439 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
441 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
443 return !matchedPlaces
.isEmpty();
446 QAction
* DolphinContextMenu::createPasteAction()
448 QAction
* action
= nullptr;
449 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
450 if (isDir
&& (m_selectedItems
.count() == 1)) {
451 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
453 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
454 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
455 action
->setEnabled(canPaste
);
456 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
458 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
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 m_baseFileItem
= new KFileItem(m_baseUrl
);
477 return *m_baseFileItem
;
480 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
482 // insert 'Open With...' action or sub menu
483 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
486 void DolphinContextMenu::addVersionControlPluginActions()
488 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
489 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
490 if (!versionControlActions
.isEmpty()) {
491 addActions(versionControlActions
);
496 void DolphinContextMenu::addCustomActions()
498 addActions(m_customActions
);