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 <KAbstractFileItemActionPlugin>
36 #include <KActionCollection>
37 #include <KFileItemActions>
38 #include <KFileItemListProperties>
39 #include <KIO/EmptyTrashJob>
40 #include <KIO/JobUiDelegate>
42 #include <KIO/RestoreJob>
43 #include <KJobWidgets>
44 #include <KLocalizedString>
45 #include <KMimeTypeTrader>
46 #include <KNewFileMenu>
47 #include <KPluginMetaData>
49 #include <KStandardAction>
52 #include <QApplication>
57 #include <QMimeDatabase>
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
61 const KFileItem
& fileInfo
,
62 const QUrl
& baseUrl
) :
68 m_baseFileItem(nullptr),
70 m_selectedItemsProperties(nullptr),
75 m_removeAction(nullptr)
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_baseFileItem
;
86 m_baseFileItem
= nullptr;
87 delete m_selectedItemsProperties
;
88 m_selectedItemsProperties
= nullptr;
91 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
93 m_customActions
= actions
;
96 DolphinContextMenu::Command
DolphinContextMenu::open()
98 // get the context information
99 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
100 m_context
|= TrashContext
;
103 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
104 m_context
|= ItemContext
;
105 // TODO: handle other use cases like devices + desktop files
108 // open the corresponding popup for the context
109 if (m_context
& TrashContext
) {
110 if (m_context
& ItemContext
) {
111 openTrashItemContextMenu();
113 openTrashContextMenu();
115 } else if (m_context
& ItemContext
) {
116 openItemContextMenu();
118 Q_ASSERT(m_context
== NoContext
);
119 openViewportContextMenu();
125 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
127 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
128 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
130 QMenu::keyPressEvent(ev
);
133 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
135 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
136 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
138 QMenu::keyReleaseEvent(ev
);
141 void DolphinContextMenu::openTrashContextMenu()
143 Q_ASSERT(m_context
& TrashContext
);
145 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
146 emptyTrashAction
->setEnabled(!Trash::isEmpty());
147 addAction(emptyTrashAction
);
151 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
152 addAction(propertiesAction
);
154 addShowMenuBarAction();
156 if (exec(m_pos
) == emptyTrashAction
) {
157 Trash::empty(m_mainWindow
);
161 void DolphinContextMenu::openTrashItemContextMenu()
163 Q_ASSERT(m_context
& TrashContext
);
164 Q_ASSERT(m_context
& ItemContext
);
166 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
167 addAction(restoreAction
);
169 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
170 addAction(deleteAction
);
172 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
173 addAction(propertiesAction
);
175 if (exec(m_pos
) == restoreAction
) {
176 QList
<QUrl
> selectedUrls
;
177 selectedUrls
.reserve(m_selectedItems
.count());
178 foreach (const KFileItem
&item
, m_selectedItems
) {
179 selectedUrls
.append(item
.url());
182 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
183 KJobWidgets::setWindow(job
, m_mainWindow
);
184 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
188 void DolphinContextMenu::openItemContextMenu()
190 Q_ASSERT(!m_fileInfo
.isNull());
192 QAction
* openParentAction
= nullptr;
193 QAction
* openParentInNewWindowAction
= nullptr;
194 QAction
* openParentInNewTabAction
= nullptr;
195 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
197 KFileItemActions fileItemActions
;
198 fileItemActions
.setParentWidget(m_mainWindow
);
199 fileItemActions
.setItemListProperties(selectedItemsProps
);
201 if (m_selectedItems
.count() == 1) {
202 if (m_fileInfo
.isDir()) {
203 // insert 'Open in new window' and 'Open in new tab' entries
204 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
205 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
207 // Insert 'Open With' entries
208 addOpenWithActions(fileItemActions
);
210 // insert 'Add to Places' entry
211 if (!placeExists(m_fileInfo
.url())) {
212 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_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
309 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
310 addAction(propertiesAction
);
312 QAction
* activatedAction
= exec(m_pos
);
313 if (activatedAction
) {
314 if (activatedAction
== openParentAction
) {
315 m_command
= OpenParentFolder
;
316 } else if (activatedAction
== openParentInNewWindowAction
) {
317 m_command
= OpenParentFolderInNewWindow
;
318 } else if (activatedAction
== openParentInNewTabAction
) {
319 m_command
= OpenParentFolderInNewTab
;
324 void DolphinContextMenu::openViewportContextMenu()
326 // setup 'Create New' menu
327 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
328 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
329 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
330 newFileMenu
->checkUpToDate();
331 newFileMenu
->setPopupFiles(m_baseUrl
);
332 addMenu(newFileMenu
->menu());
335 // Insert 'Open With' entries
336 KFileItem baseItem
= view
->rootItem();
337 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
338 baseItem
= baseFileItem();
341 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
342 KFileItemActions fileItemActions
;
343 fileItemActions
.setParentWidget(m_mainWindow
);
344 fileItemActions
.setItemListProperties(baseUrlProperties
);
346 // Don't show "Open With" menu items if the current dir is empty, because there's
347 // generally no app that can do anything interesting with an empty directory
348 if (view
->itemsCount() != 0) {
349 addOpenWithActions(fileItemActions
);
352 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
353 // "open_in_new_tab" here, as the current selection should get ignored.
354 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("file_new")));
355 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
357 // Insert 'Add to Places' entry if exactly one item is selected
358 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
359 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
364 QAction
* pasteAction
= createPasteAction();
365 addAction(pasteAction
);
368 // Insert 'Sort By' and 'View Mode'
369 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
370 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
374 // Insert service actions
375 fileItemActions
.addServiceActionsTo(this);
376 fileItemActions
.addPluginActionsTo(this);
378 addVersionControlPluginActions();
382 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
383 addAction(propertiesAction
);
385 addShowMenuBarAction();
390 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
392 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
394 // Insert 'Cut', 'Copy' and 'Paste'
395 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
396 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
397 addAction(createPasteAction());
402 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
404 // Insert 'Move to Trash' and/or 'Delete'
405 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
406 !properties
.isLocal());
407 const bool showMoveToTrashAction
= (properties
.isLocal() &&
408 properties
.supportsMoving());
410 if (showDeleteAction
&& showMoveToTrashAction
) {
411 delete m_removeAction
;
412 m_removeAction
= nullptr;
413 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
414 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
415 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
416 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
418 if (!m_removeAction
) {
419 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
421 addAction(m_removeAction
);
422 m_removeAction
->update();
426 void DolphinContextMenu::addShowMenuBarAction()
428 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
429 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
430 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
432 addAction(showMenuBar
);
436 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
438 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
440 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
442 return !matchedPlaces
.isEmpty();
445 QAction
* DolphinContextMenu::createPasteAction()
447 QAction
* action
= nullptr;
448 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
449 if (isDir
&& (m_selectedItems
.count() == 1)) {
450 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
452 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
453 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
454 action
->setEnabled(canPaste
);
455 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
457 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
463 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
465 if (!m_selectedItemsProperties
) {
466 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
468 return *m_selectedItemsProperties
;
471 KFileItem
DolphinContextMenu::baseFileItem()
473 if (!m_baseFileItem
) {
474 m_baseFileItem
= new KFileItem(m_baseUrl
);
476 return *m_baseFileItem
;
479 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
481 // insert 'Open With...' action or sub menu
482 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
485 void DolphinContextMenu::addVersionControlPluginActions()
487 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
488 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
489 if (!versionControlActions
.isEmpty()) {
490 addActions(versionControlActions
);
495 void DolphinContextMenu::addCustomActions()
497 addActions(m_customActions
);