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 "dolphinmainwindow.h"
24 #include "dolphinnewfilemenu.h"
25 #include "dolphinviewcontainer.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphinremoveaction.h"
29 #include <KActionCollection>
30 #include <KAbstractFileItemActionPlugin>
31 #include <KFileItemActions>
32 #include <KFileItemListProperties>
33 #include <KIO/RestoreJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
37 #include <KJobWidgets>
38 #include <KMimeTypeTrader>
39 #include <KNewFileMenu>
40 #include <KPluginMetaData>
42 #include <KLocalizedString>
43 #include <KStandardAction>
46 #include <QApplication>
52 #include <panels/places/placesitem.h>
53 #include <panels/places/placesitemmodel.h>
56 #include "views/dolphinview.h"
57 #include "views/viewmodecontroller.h"
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
61 const KFileItem
& fileInfo
,
62 const QUrl
& baseUrl
) :
70 m_selectedItemsProperties(0),
77 // The context menu either accesses the URLs of the selected items
78 // or the items itself. To increase the performance both lists are cached.
79 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
80 m_selectedItems
= view
->selectedItems();
83 DolphinContextMenu::~DolphinContextMenu()
85 delete m_selectedItemsProperties
;
86 m_selectedItemsProperties
= 0;
89 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
91 m_customActions
= actions
;
94 DolphinContextMenu::Command
DolphinContextMenu::open()
96 // get the context information
97 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
98 m_context
|= TrashContext
;
101 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
102 m_context
|= ItemContext
;
103 // TODO: handle other use cases like devices + desktop files
106 // open the corresponding popup for the context
107 if (m_context
& TrashContext
) {
108 if (m_context
& ItemContext
) {
109 openTrashItemContextMenu();
111 openTrashContextMenu();
113 } else if (m_context
& ItemContext
) {
114 openItemContextMenu();
116 Q_ASSERT(m_context
== NoContext
);
117 openViewportContextMenu();
123 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
125 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
126 m_removeAction
->update();
128 QMenu::keyPressEvent(ev
);
131 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
133 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
134 m_removeAction
->update();
136 QMenu::keyReleaseEvent(ev
);
139 void DolphinContextMenu::openTrashContextMenu()
141 Q_ASSERT(m_context
& TrashContext
);
143 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
144 KConfig
trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig
);
145 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
146 addAction(emptyTrashAction
);
150 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
151 addAction(propertiesAction
);
153 addShowMenuBarAction();
155 if (exec(m_pos
) == emptyTrashAction
) {
156 KIO::JobUiDelegate uiDelegate
;
157 uiDelegate
.setWindow(m_mainWindow
);
158 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
159 KIO::Job
* job
= KIO::emptyTrash();
160 KJobWidgets::setWindow(job
, m_mainWindow
);
161 job
->ui()->setAutoErrorHandlingEnabled(true);
166 void DolphinContextMenu::openTrashItemContextMenu()
168 Q_ASSERT(m_context
& TrashContext
);
169 Q_ASSERT(m_context
& ItemContext
);
171 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
172 addAction(restoreAction
);
174 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("delete"));
175 addAction(deleteAction
);
177 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
178 addAction(propertiesAction
);
180 if (exec(m_pos
) == restoreAction
) {
181 QList
<QUrl
> selectedUrls
;
182 selectedUrls
.reserve(m_selectedItems
.count());
183 foreach (const KFileItem
&item
, m_selectedItems
) {
184 selectedUrls
.append(item
.url());
187 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
188 KJobWidgets::setWindow(job
, m_mainWindow
);
189 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
193 void DolphinContextMenu::openItemContextMenu()
195 Q_ASSERT(!m_fileInfo
.isNull());
197 QAction
* openParentAction
= 0;
198 QAction
* openParentInNewWindowAction
= 0;
199 QAction
* openParentInNewTabAction
= 0;
200 QAction
* addToPlacesAction
= 0;
201 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
203 if (m_selectedItems
.count() == 1) {
204 if (m_fileInfo
.isDir()) {
205 // setup 'Create New' menu
206 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
207 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
208 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
209 newFileMenu
->checkUpToDate();
210 newFileMenu
->setPopupFiles(m_fileInfo
.url());
211 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
212 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
213 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
215 QMenu
* menu
= newFileMenu
->menu();
216 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
217 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
221 // insert 'Open in new window' and 'Open in new tab' entries
222 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
223 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
225 // insert 'Add to Places' entry
226 if (!placeExists(m_fileInfo
.url())) {
227 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
228 i18nc("@action:inmenu Add selected folder to places",
233 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
234 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
235 i18nc("@action:inmenu",
238 addAction(openParentAction
);
240 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
241 i18nc("@action:inmenu",
242 "Open Path in New Window"),
244 addAction(openParentInNewWindowAction
);
246 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
247 i18nc("@action:inmenu",
248 "Open Path in New Tab"),
250 addAction(openParentInNewTabAction
);
253 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
254 // insert 'Open in new window' and 'Open in new tab' entries
255 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
256 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
261 bool selectionHasOnlyDirs
= true;
262 foreach (const KFileItem
& item
, m_selectedItems
) {
263 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
265 selectionHasOnlyDirs
= false;
270 if (selectionHasOnlyDirs
) {
271 // insert 'Open in new tab' entry
272 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
277 insertDefaultItemActions(selectedItemsProps
);
281 KFileItemActions fileItemActions
;
282 fileItemActions
.setItemListProperties(selectedItemsProps
);
283 addServiceActions(fileItemActions
);
285 addFileItemPluginActions();
287 addVersionControlPluginActions();
289 // insert 'Copy To' and 'Move To' sub menus
290 if (GeneralSettings::showCopyMoveMenu()) {
291 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
292 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
293 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
294 m_copyToMenu
.addActionsTo(this);
297 // insert 'Properties...' entry
298 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
299 addAction(propertiesAction
);
301 QAction
* activatedAction
= exec(m_pos
);
302 if (activatedAction
) {
303 if (activatedAction
== addToPlacesAction
) {
304 const QUrl
selectedUrl(m_fileInfo
.url());
305 if (selectedUrl
.isValid()) {
306 PlacesItemModel model
;
307 const QString text
= selectedUrl
.fileName();
308 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
309 model
.appendItemToGroup(item
);
310 model
.saveBookmarks();
312 } else if (activatedAction
== openParentAction
) {
313 m_command
= OpenParentFolder
;
314 } else if (activatedAction
== openParentInNewWindowAction
) {
315 m_command
= OpenParentFolderInNewWindow
;
316 } else if (activatedAction
== openParentInNewTabAction
) {
317 m_command
= OpenParentFolderInNewTab
;
322 void DolphinContextMenu::openViewportContextMenu()
324 // setup 'Create New' menu
325 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
326 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
327 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
328 newFileMenu
->checkUpToDate();
329 newFileMenu
->setPopupFiles(m_baseUrl
);
330 addMenu(newFileMenu
->menu());
333 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
334 // "open_in_new_tab" here, as the current selection should get ignored.
335 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_window")));
336 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
338 // Insert 'Add to Places' entry if exactly one item is selected
339 QAction
* addToPlacesAction
= 0;
340 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
341 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
342 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
347 QAction
* pasteAction
= createPasteAction();
348 addAction(pasteAction
);
351 // Insert service actions
352 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
353 KFileItemActions fileItemActions
;
354 fileItemActions
.setItemListProperties(baseUrlProperties
);
355 addServiceActions(fileItemActions
);
357 addFileItemPluginActions();
359 addVersionControlPluginActions();
363 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
364 addAction(propertiesAction
);
366 addShowMenuBarAction();
368 QAction
* action
= exec(m_pos
);
369 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
370 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
371 if (container
->url().isValid()) {
372 PlacesItemModel model
;
373 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
375 model
.appendItemToGroup(item
);
376 model
.saveBookmarks();
381 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
383 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
385 // Insert 'Cut', 'Copy' and 'Paste'
386 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
387 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
388 addAction(createPasteAction());
393 QAction
* renameAction
= collection
->action(QStringLiteral("rename"));
394 addAction(renameAction
);
396 // Insert 'Move to Trash' and/or 'Delete'
397 if (properties
.supportsDeleting()) {
398 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
399 !properties
.isLocal());
400 const bool showMoveToTrashAction
= (properties
.isLocal() &&
401 properties
.supportsMoving());
403 if (showDeleteAction
&& showMoveToTrashAction
) {
404 delete m_removeAction
;
406 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("move_to_trash")));
407 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("delete")));
408 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
409 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("delete")));
411 if (!m_removeAction
) {
412 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
414 addAction(m_removeAction
);
415 m_removeAction
->update();
420 void DolphinContextMenu::addShowMenuBarAction()
422 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
423 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
424 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
426 addAction(showMenuBar
);
430 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
432 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
433 // can be expensive because the model asks Solid for the devices which are
434 // available, which can take some time.
435 // TODO: Consider restoring this check if the handling of Places and devices
436 // will be decoupled in the future.
440 QAction
* DolphinContextMenu::createPasteAction()
443 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
444 if (isDir
&& (m_selectedItems
.count() == 1)) {
445 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
447 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
448 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
449 action
->setEnabled(canPaste
);
450 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
452 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
458 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
460 if (!m_selectedItemsProperties
) {
461 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
463 return *m_selectedItemsProperties
;
466 KFileItem
DolphinContextMenu::baseFileItem()
468 if (!m_baseFileItem
) {
469 m_baseFileItem
= new KFileItem(m_baseUrl
);
471 return *m_baseFileItem
;
474 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
476 fileItemActions
.setParentWidget(m_mainWindow
);
478 // insert 'Open With...' action or sub menu
479 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
481 // insert 'Actions' sub menu
482 fileItemActions
.addServiceActionsTo(this);
485 void DolphinContextMenu::addFileItemPluginActions()
487 KFileItemListProperties props
;
488 if (m_selectedItems
.isEmpty()) {
489 props
.setItems(KFileItemList() << baseFileItem());
491 props
= selectedItemsProperties();
494 QString commonMimeType
= props
.mimeType();
495 if (commonMimeType
.isEmpty()) {
496 commonMimeType
= QStringLiteral("application/octet-stream");
499 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, QStringLiteral("KFileItemAction/Plugin"), QStringLiteral("exist Library"));
500 if (pluginServices
.isEmpty()) {
504 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
505 const KConfigGroup showGroup
= config
.group("Show");
507 QSet
<QString
> addedPlugins
;
508 foreach (const KService::Ptr
& service
, pluginServices
) {
509 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
510 // The plugin has been disabled
514 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
515 if (abstractPlugin
) {
516 abstractPlugin
->setParent(this);
517 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
518 addedPlugins
<< service
->desktopEntryName();
522 const auto jsonPlugins
= KPluginLoader::findPlugins(QString(), [](const KPluginMetaData
& metaData
) {
523 return metaData
.serviceTypes().contains(QStringLiteral("KFileItemAction/Plugin"));
526 foreach (const auto& jsonMetadata
, jsonPlugins
) {
527 // The plugin has been disabled
528 if (!showGroup
.readEntry(jsonMetadata
.pluginId(), true)) {
532 // The plugin also has a .desktop file and has already been added.
533 if (addedPlugins
.contains(jsonMetadata
.pluginId())) {
537 KPluginFactory
*factory
= KPluginLoader(jsonMetadata
.fileName()).factory();
538 KAbstractFileItemActionPlugin
* abstractPlugin
= factory
->create
<KAbstractFileItemActionPlugin
>();
539 if (abstractPlugin
) {
540 abstractPlugin
->setParent(this);
541 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
546 void DolphinContextMenu::addVersionControlPluginActions()
548 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
549 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
550 if (!versionControlActions
.isEmpty()) {
551 addActions(versionControlActions
);
556 void DolphinContextMenu::addCustomActions()
558 addActions(m_customActions
);