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_target")));
200 if (m_fileInfo
.isDir()) {
201 // setup 'Create New' menu
202 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
203 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
204 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
205 newFileMenu
->checkUpToDate();
206 newFileMenu
->setPopupFiles(m_fileInfo
.url());
207 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
208 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
209 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
211 QMenu
* menu
= newFileMenu
->menu();
212 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
213 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
217 // insert 'Open in new window' and 'Open in new tab' entries
218 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
219 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
221 // insert 'Add to Places' entry
222 if (!placeExists(m_fileInfo
.url())) {
223 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
224 i18nc("@action:inmenu Add selected folder to places",
229 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
230 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
231 i18nc("@action:inmenu",
234 addAction(openParentAction
);
236 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
237 i18nc("@action:inmenu",
238 "Open Path in New Window"),
240 addAction(openParentInNewWindowAction
);
242 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
243 i18nc("@action:inmenu",
244 "Open Path in New Tab"),
246 addAction(openParentInNewTabAction
);
249 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
250 // insert 'Open in new window' and 'Open in new tab' entries
251 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
252 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
257 bool selectionHasOnlyDirs
= true;
258 foreach (const KFileItem
& item
, m_selectedItems
) {
259 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
261 selectionHasOnlyDirs
= false;
266 if (selectionHasOnlyDirs
) {
267 // insert 'Open in new tab' entry
268 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
273 insertDefaultItemActions(selectedItemsProps
);
277 KFileItemActions fileItemActions
;
278 fileItemActions
.setItemListProperties(selectedItemsProps
);
279 addServiceActions(fileItemActions
);
281 fileItemActions
.addPluginActionsTo(this);
283 addVersionControlPluginActions();
285 // insert 'Copy To' and 'Move To' sub menus
286 if (GeneralSettings::showCopyMoveMenu()) {
287 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
288 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
289 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
290 m_copyToMenu
.addActionsTo(this);
293 // insert 'Properties...' entry
294 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
295 addAction(propertiesAction
);
297 QAction
* activatedAction
= exec(m_pos
);
298 if (activatedAction
) {
299 if (activatedAction
== addToPlacesAction
) {
300 const QUrl
selectedUrl(m_fileInfo
.url());
301 if (selectedUrl
.isValid()) {
302 PlacesItemModel model
;
303 const QString text
= selectedUrl
.fileName();
304 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
306 } else 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 // setup 'Create New' menu
319 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
320 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
321 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
322 newFileMenu
->checkUpToDate();
323 newFileMenu
->setPopupFiles(m_baseUrl
);
324 addMenu(newFileMenu
->menu());
327 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
328 // "open_in_new_tab" here, as the current selection should get ignored.
329 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_window")));
330 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
332 // Insert 'Add to Places' entry if exactly one item is selected
333 QAction
* addToPlacesAction
= nullptr;
334 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
335 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
336 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
341 QAction
* pasteAction
= createPasteAction();
342 addAction(pasteAction
);
345 // Insert service actions
346 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
347 KFileItemActions fileItemActions
;
348 fileItemActions
.setItemListProperties(baseUrlProperties
);
349 addServiceActions(fileItemActions
);
351 fileItemActions
.addPluginActionsTo(this);
353 addVersionControlPluginActions();
357 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
358 addAction(propertiesAction
);
360 addShowMenuBarAction();
362 QAction
* action
= exec(m_pos
);
363 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
364 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
365 const QUrl url
= container
->url();
367 PlacesItemModel model
;
369 if (container
->isSearchModeEnabled()) {
370 icon
= QStringLiteral("folder-saved-search-symbolic");
372 icon
= KIO::iconNameForUrl(url
);
374 model
.createPlacesItem(container
->placesText(), url
, icon
);
379 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
381 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
383 // Insert 'Cut', 'Copy' and 'Paste'
384 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
385 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
386 addAction(createPasteAction());
391 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
393 // Insert 'Move to Trash' and/or 'Delete'
394 if (properties
.supportsDeleting()) {
395 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
396 !properties
.isLocal());
397 const bool showMoveToTrashAction
= (properties
.isLocal() &&
398 properties
.supportsMoving());
400 if (showDeleteAction
&& showMoveToTrashAction
) {
401 delete m_removeAction
;
402 m_removeAction
= nullptr;
403 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
404 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
405 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
406 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
408 if (!m_removeAction
) {
409 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
411 addAction(m_removeAction
);
412 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
430 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
431 // can be expensive because the model asks Solid for the devices which are
432 // available, which can take some time.
433 // TODO: Consider restoring this check if the handling of Places and devices
434 // will be decoupled in the future.
438 QAction
* DolphinContextMenu::createPasteAction()
440 QAction
* action
= nullptr;
441 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
442 if (isDir
&& (m_selectedItems
.count() == 1)) {
443 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
445 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
446 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
447 action
->setEnabled(canPaste
);
448 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
450 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
456 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
458 if (!m_selectedItemsProperties
) {
459 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
461 return *m_selectedItemsProperties
;
464 KFileItem
DolphinContextMenu::baseFileItem()
466 if (!m_baseFileItem
) {
467 m_baseFileItem
= new KFileItem(m_baseUrl
);
469 return *m_baseFileItem
;
472 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
474 fileItemActions
.setParentWidget(m_mainWindow
);
476 // insert 'Open With...' action or sub menu
477 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
479 // insert 'Actions' sub menu
480 fileItemActions
.addServiceActionsTo(this);
483 void DolphinContextMenu::addVersionControlPluginActions()
485 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
486 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
487 if (!versionControlActions
.isEmpty()) {
488 addActions(versionControlActions
);
493 void DolphinContextMenu::addCustomActions()
495 addActions(m_customActions
);