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 "dolphinremoveaction.h"
27 #include "dolphinviewcontainer.h"
28 #include "panels/places/placesitem.h"
29 #include "panels/places/placesitemmodel.h"
30 #include "trash/dolphintrash.h"
31 #include "views/dolphinview.h"
32 #include "views/viewmodecontroller.h"
34 #include <KAbstractFileItemActionPlugin>
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 <KMimeTypeTrader>
45 #include <KNewFileMenu>
46 #include <KPluginMetaData>
48 #include <KStandardAction>
51 #include <QApplication>
56 #include <QMimeDatabase>
58 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
60 const KFileItem
& fileInfo
,
61 const QUrl
& baseUrl
) :
67 m_baseFileItem(nullptr),
69 m_selectedItemsProperties(nullptr),
74 m_removeAction(nullptr)
76 // The context menu either accesses the URLs of the selected items
77 // or the items itself. To increase the performance both lists are cached.
78 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
79 m_selectedItems
= view
->selectedItems();
82 DolphinContextMenu::~DolphinContextMenu()
84 delete m_selectedItemsProperties
;
85 m_selectedItemsProperties
= nullptr;
88 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
90 m_customActions
= actions
;
93 DolphinContextMenu::Command
DolphinContextMenu::open()
95 // get the context information
96 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
97 m_context
|= TrashContext
;
100 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
101 m_context
|= ItemContext
;
102 // TODO: handle other use cases like devices + desktop files
105 // open the corresponding popup for the context
106 if (m_context
& TrashContext
) {
107 if (m_context
& ItemContext
) {
108 openTrashItemContextMenu();
110 openTrashContextMenu();
112 } else if (m_context
& ItemContext
) {
113 openItemContextMenu();
115 Q_ASSERT(m_context
== NoContext
);
116 openViewportContextMenu();
122 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
124 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
125 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
127 QMenu::keyPressEvent(ev
);
130 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
132 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
133 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
135 QMenu::keyReleaseEvent(ev
);
138 void DolphinContextMenu::openTrashContextMenu()
140 Q_ASSERT(m_context
& TrashContext
);
142 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
143 emptyTrashAction
->setEnabled(!Trash::isEmpty());
144 addAction(emptyTrashAction
);
148 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
149 addAction(propertiesAction
);
151 addShowMenuBarAction();
153 if (exec(m_pos
) == emptyTrashAction
) {
154 Trash::empty(m_mainWindow
);
158 void DolphinContextMenu::openTrashItemContextMenu()
160 Q_ASSERT(m_context
& TrashContext
);
161 Q_ASSERT(m_context
& ItemContext
);
163 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
164 addAction(restoreAction
);
166 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
167 addAction(deleteAction
);
169 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
170 addAction(propertiesAction
);
172 if (exec(m_pos
) == restoreAction
) {
173 QList
<QUrl
> selectedUrls
;
174 selectedUrls
.reserve(m_selectedItems
.count());
175 foreach (const KFileItem
&item
, m_selectedItems
) {
176 selectedUrls
.append(item
.url());
179 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
180 KJobWidgets::setWindow(job
, m_mainWindow
);
181 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
185 void DolphinContextMenu::openItemContextMenu()
187 Q_ASSERT(!m_fileInfo
.isNull());
189 QAction
* openParentAction
= nullptr;
190 QAction
* openParentInNewWindowAction
= nullptr;
191 QAction
* openParentInNewTabAction
= nullptr;
192 QAction
* addToPlacesAction
= nullptr;
193 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
195 if (m_selectedItems
.count() == 1) {
196 if (m_fileInfo
.isLink()) {
197 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_original")));
199 } else if (m_fileInfo
.isDir()) {
200 // setup 'Create New' menu
201 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
202 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
203 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
204 newFileMenu
->checkUpToDate();
205 newFileMenu
->setPopupFiles(m_fileInfo
.url());
206 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
207 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
208 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
210 QMenu
* menu
= newFileMenu
->menu();
211 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
212 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
216 // insert 'Open in new window' and 'Open in new tab' entries
217 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
218 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
220 // insert 'Add to Places' entry
221 if (!placeExists(m_fileInfo
.url())) {
222 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
223 i18nc("@action:inmenu Add selected folder to places",
228 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
229 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
230 i18nc("@action:inmenu",
233 addAction(openParentAction
);
235 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
236 i18nc("@action:inmenu",
237 "Open Path in New Window"),
239 addAction(openParentInNewWindowAction
);
241 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
242 i18nc("@action:inmenu",
243 "Open Path in New Tab"),
245 addAction(openParentInNewTabAction
);
248 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
249 // insert 'Open in new window' and 'Open in new tab' entries
250 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
251 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
256 bool selectionHasOnlyDirs
= true;
257 foreach (const KFileItem
& item
, 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")));
272 insertDefaultItemActions(selectedItemsProps
);
276 KFileItemActions fileItemActions
;
277 fileItemActions
.setItemListProperties(selectedItemsProps
);
278 addServiceActions(fileItemActions
);
280 fileItemActions
.addPluginActionsTo(this);
282 addVersionControlPluginActions();
284 // insert 'Copy To' and 'Move To' sub menus
285 if (GeneralSettings::showCopyMoveMenu()) {
286 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
287 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
288 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
289 m_copyToMenu
.addActionsTo(this);
292 // insert 'Properties...' entry
293 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
294 addAction(propertiesAction
);
296 QAction
* activatedAction
= exec(m_pos
);
297 if (activatedAction
) {
298 if (activatedAction
== addToPlacesAction
) {
299 const QUrl
selectedUrl(m_fileInfo
.url());
300 if (selectedUrl
.isValid()) {
301 PlacesItemModel model
;
302 const QString text
= selectedUrl
.fileName();
303 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
305 } else 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 // setup 'Create New' menu
318 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
319 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
320 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
321 newFileMenu
->checkUpToDate();
322 newFileMenu
->setPopupFiles(m_baseUrl
);
323 addMenu(newFileMenu
->menu());
326 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
327 // "open_in_new_tab" here, as the current selection should get ignored.
328 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_window")));
329 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
331 // Insert 'Add to Places' entry if exactly one item is selected
332 QAction
* addToPlacesAction
= nullptr;
333 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
334 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
335 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
340 QAction
* pasteAction
= createPasteAction();
341 addAction(pasteAction
);
344 // Insert service actions
345 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
346 KFileItemActions fileItemActions
;
347 fileItemActions
.setItemListProperties(baseUrlProperties
);
348 addServiceActions(fileItemActions
);
350 fileItemActions
.addPluginActionsTo(this);
352 addVersionControlPluginActions();
356 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
357 addAction(propertiesAction
);
359 addShowMenuBarAction();
361 QAction
* action
= exec(m_pos
);
362 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
363 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
364 const QUrl url
= container
->url();
366 PlacesItemModel model
;
368 if (container
->isSearchModeEnabled()) {
369 icon
= QStringLiteral("folder-saved-search-symbolic");
371 icon
= KIO::iconNameForUrl(url
);
373 model
.createPlacesItem(container
->placesText(), url
, icon
);
378 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
380 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
382 // Insert 'Cut', 'Copy' and 'Paste'
383 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
384 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
385 addAction(createPasteAction());
390 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
392 // Insert 'Move to Trash' and/or 'Delete'
393 if (properties
.supportsDeleting()) {
394 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
395 !properties
.isLocal());
396 const bool showMoveToTrashAction
= (properties
.isLocal() &&
397 properties
.supportsMoving());
399 if (showDeleteAction
&& showMoveToTrashAction
) {
400 delete m_removeAction
;
401 m_removeAction
= nullptr;
402 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
403 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
404 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
405 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
407 if (!m_removeAction
) {
408 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
410 addAction(m_removeAction
);
411 m_removeAction
->update();
416 void DolphinContextMenu::addShowMenuBarAction()
418 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
419 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
420 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
422 addAction(showMenuBar
);
426 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
429 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
430 // can be expensive because the model asks Solid for the devices which are
431 // available, which can take some time.
432 // TODO: Consider restoring this check if the handling of Places and devices
433 // will be decoupled in the future.
437 QAction
* DolphinContextMenu::createPasteAction()
439 QAction
* action
= nullptr;
440 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
441 if (isDir
&& (m_selectedItems
.count() == 1)) {
442 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
444 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
445 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
446 action
->setEnabled(canPaste
);
447 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
449 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
455 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
457 if (!m_selectedItemsProperties
) {
458 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
460 return *m_selectedItemsProperties
;
463 KFileItem
DolphinContextMenu::baseFileItem()
465 if (!m_baseFileItem
) {
466 m_baseFileItem
= new KFileItem(m_baseUrl
);
468 return *m_baseFileItem
;
471 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
473 fileItemActions
.setParentWidget(m_mainWindow
);
475 // insert 'Open With...' action or sub menu
476 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
478 // insert 'Actions' sub menu
479 fileItemActions
.addServiceActionsTo(this);
482 void DolphinContextMenu::addVersionControlPluginActions()
484 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
485 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
486 if (!versionControlActions
.isEmpty()) {
487 addActions(versionControlActions
);
492 void DolphinContextMenu::addCustomActions()
494 addActions(m_customActions
);