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 KFileItemActions fileItemActions
;
196 fileItemActions
.setParentWidget(m_mainWindow
);
197 fileItemActions
.setItemListProperties(selectedItemsProps
);
199 if (m_selectedItems
.count() == 1) {
200 if (m_fileInfo
.isDir()) {
201 // insert 'Open in new window' and 'Open in new tab' entries
202 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
203 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
205 // Insert 'Open With' entries
206 addOpenWithActions(fileItemActions
);
208 // insert 'Add to Places' entry
209 if (!placeExists(m_fileInfo
.url())) {
210 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
211 i18nc("@action:inmenu Add selected folder to places",
217 // set up 'Create New' menu
218 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
219 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
220 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
221 newFileMenu
->checkUpToDate();
222 newFileMenu
->setPopupFiles(m_fileInfo
.url());
223 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
224 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
225 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
227 QMenu
* menu
= newFileMenu
->menu();
228 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
229 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
233 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
234 addOpenWithActions(fileItemActions
);
236 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
237 i18nc("@action:inmenu",
240 addAction(openParentAction
);
242 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
243 i18nc("@action:inmenu",
244 "Open Path in New Window"),
246 addAction(openParentInNewWindowAction
);
248 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
249 i18nc("@action:inmenu",
250 "Open Path in New Tab"),
252 addAction(openParentInNewTabAction
);
255 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
256 // Insert 'Open With" entries
257 addOpenWithActions(fileItemActions
);
259 // insert 'Open in new window' and 'Open in new tab' entries
260 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
261 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
265 // Insert 'Open With" entries
266 addOpenWithActions(fileItemActions
);
268 if (m_fileInfo
.isLink()) {
269 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
273 bool selectionHasOnlyDirs
= true;
274 foreach (const KFileItem
& item
, m_selectedItems
) {
275 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
277 selectionHasOnlyDirs
= false;
282 if (selectionHasOnlyDirs
) {
283 // insert 'Open in new tab' entry
284 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
286 // Insert 'Open With" entries
287 addOpenWithActions(fileItemActions
);
290 insertDefaultItemActions(selectedItemsProps
);
294 fileItemActions
.addServiceActionsTo(this);
295 fileItemActions
.addPluginActionsTo(this);
297 addVersionControlPluginActions();
299 // insert 'Copy To' and 'Move To' sub menus
300 if (GeneralSettings::showCopyMoveMenu()) {
301 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
302 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
303 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
304 m_copyToMenu
.addActionsTo(this);
307 // insert 'Properties...' entry
308 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
309 addAction(propertiesAction
);
311 QAction
* activatedAction
= exec(m_pos
);
312 if (activatedAction
) {
313 if (activatedAction
== addToPlacesAction
) {
314 const QUrl
selectedUrl(m_fileInfo
.url());
315 if (selectedUrl
.isValid()) {
316 PlacesItemModel model
;
317 const QString text
= selectedUrl
.fileName();
318 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
320 } else 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 // setup 'Create New' menu
333 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
334 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
335 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
336 newFileMenu
->checkUpToDate();
337 newFileMenu
->setPopupFiles(m_baseUrl
);
338 addMenu(newFileMenu
->menu());
341 // Insert 'Open With' entries
342 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
343 KFileItemActions fileItemActions
;
344 fileItemActions
.setParentWidget(m_mainWindow
);
345 fileItemActions
.setItemListProperties(baseUrlProperties
);
346 addOpenWithActions(fileItemActions
);
348 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
349 // "open_in_new_tab" here, as the current selection should get ignored.
350 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("file_new")));
351 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
353 // Insert 'Add to Places' entry if exactly one item is selected
354 QAction
* addToPlacesAction
= nullptr;
355 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
356 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
357 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
362 QAction
* pasteAction
= createPasteAction();
363 addAction(pasteAction
);
366 // Insert service actions
367 fileItemActions
.addServiceActionsTo(this);
368 fileItemActions
.addPluginActionsTo(this);
370 addVersionControlPluginActions();
374 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
375 addAction(propertiesAction
);
377 addShowMenuBarAction();
379 QAction
* action
= exec(m_pos
);
380 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
381 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
382 const QUrl url
= container
->url();
384 PlacesItemModel model
;
386 if (container
->isSearchModeEnabled()) {
387 icon
= QStringLiteral("folder-saved-search-symbolic");
389 icon
= KIO::iconNameForUrl(url
);
391 model
.createPlacesItem(container
->placesText(), url
, icon
);
396 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
398 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
400 // Insert 'Cut', 'Copy' and 'Paste'
401 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
402 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
403 addAction(createPasteAction());
408 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
410 // Insert 'Move to Trash' and/or 'Delete'
411 if (properties
.supportsDeleting()) {
412 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
413 !properties
.isLocal());
414 const bool showMoveToTrashAction
= (properties
.isLocal() &&
415 properties
.supportsMoving());
417 if (showDeleteAction
&& showMoveToTrashAction
) {
418 delete m_removeAction
;
419 m_removeAction
= nullptr;
420 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
421 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
422 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
423 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
425 if (!m_removeAction
) {
426 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
428 addAction(m_removeAction
);
429 m_removeAction
->update();
434 void DolphinContextMenu::addShowMenuBarAction()
436 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
437 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
438 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
440 addAction(showMenuBar
);
444 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
447 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
448 // can be expensive because the model asks Solid for the devices which are
449 // available, which can take some time.
450 // TODO: Consider restoring this check if the handling of Places and devices
451 // will be decoupled in the future.
455 QAction
* DolphinContextMenu::createPasteAction()
457 QAction
* action
= nullptr;
458 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
459 if (isDir
&& (m_selectedItems
.count() == 1)) {
460 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
462 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
463 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
464 action
->setEnabled(canPaste
);
465 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
467 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
473 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
475 if (!m_selectedItemsProperties
) {
476 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
478 return *m_selectedItemsProperties
;
481 KFileItem
DolphinContextMenu::baseFileItem()
483 if (!m_baseFileItem
) {
484 m_baseFileItem
= new KFileItem(m_baseUrl
);
486 return *m_baseFileItem
;
489 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
491 // insert 'Open With...' action or sub menu
492 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
495 void DolphinContextMenu::addVersionControlPluginActions()
497 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
498 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
499 if (!versionControlActions
.isEmpty()) {
500 addActions(versionControlActions
);
505 void DolphinContextMenu::addCustomActions()
507 addActions(m_customActions
);