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 <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 <KNewFileMenu>
45 #include <KPluginMetaData>
47 #include <KStandardAction>
50 #include <QApplication>
55 #include <QMimeDatabase>
57 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
59 const KFileItem
& fileInfo
,
60 const QUrl
& baseUrl
) :
66 m_baseFileItem(nullptr),
68 m_selectedItemsProperties(nullptr),
73 m_removeAction(nullptr)
75 // The context menu either accesses the URLs of the selected items
76 // or the items itself. To increase the performance both lists are cached.
77 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
78 m_selectedItems
= view
->selectedItems();
81 DolphinContextMenu::~DolphinContextMenu()
83 delete m_baseFileItem
;
84 m_baseFileItem
= nullptr;
85 delete m_selectedItemsProperties
;
86 m_selectedItemsProperties
= nullptr;
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(DolphinRemoveAction::ShiftState::Pressed
);
128 QMenu::keyPressEvent(ev
);
131 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
133 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
134 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
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 emptyTrashAction
->setEnabled(!Trash::isEmpty());
145 addAction(emptyTrashAction
);
149 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
150 addAction(propertiesAction
);
152 addShowMenuBarAction();
154 if (exec(m_pos
) == emptyTrashAction
) {
155 Trash::empty(m_mainWindow
);
159 void DolphinContextMenu::openTrashItemContextMenu()
161 Q_ASSERT(m_context
& TrashContext
);
162 Q_ASSERT(m_context
& ItemContext
);
164 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
165 addAction(restoreAction
);
167 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
168 addAction(deleteAction
);
170 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
171 addAction(propertiesAction
);
173 if (exec(m_pos
) == restoreAction
) {
174 QList
<QUrl
> selectedUrls
;
175 selectedUrls
.reserve(m_selectedItems
.count());
176 foreach (const KFileItem
&item
, m_selectedItems
) {
177 selectedUrls
.append(item
.url());
180 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
181 KJobWidgets::setWindow(job
, m_mainWindow
);
182 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
186 void DolphinContextMenu::openItemContextMenu()
188 Q_ASSERT(!m_fileInfo
.isNull());
190 QAction
* openParentAction
= nullptr;
191 QAction
* openParentInNewWindowAction
= nullptr;
192 QAction
* openParentInNewTabAction
= 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 // set up 'Create New' menu
209 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
210 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
211 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
212 newFileMenu
->checkUpToDate();
213 newFileMenu
->setPopupFiles(m_fileInfo
.url());
214 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
215 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
216 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
218 QMenu
* menu
= newFileMenu
->menu();
219 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
220 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
224 } else if (m_baseUrl
.scheme().contains(QLatin1String("search")) || m_baseUrl
.scheme().contains(QLatin1String("timeline"))) {
225 addOpenWithActions(fileItemActions
);
227 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
228 i18nc("@action:inmenu",
231 addAction(openParentAction
);
233 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
234 i18nc("@action:inmenu",
235 "Open Path in New Window"),
237 addAction(openParentInNewWindowAction
);
239 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
240 i18nc("@action:inmenu",
241 "Open Path in New Tab"),
243 addAction(openParentInNewTabAction
);
247 // Insert 'Open With" entries
248 addOpenWithActions(fileItemActions
);
250 if (m_fileInfo
.isLink()) {
251 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
255 bool selectionHasOnlyDirs
= true;
256 foreach (const KFileItem
& item
, m_selectedItems
) {
257 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
259 selectionHasOnlyDirs
= false;
264 if (selectionHasOnlyDirs
) {
265 // insert 'Open in new tab' entry
266 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
268 // Insert 'Open With" entries
269 addOpenWithActions(fileItemActions
);
272 insertDefaultItemActions(selectedItemsProps
);
274 // insert 'Add to Places' entry if appropriate
275 if (m_selectedItems
.count() == 1) {
276 if (m_fileInfo
.isDir()) {
277 if (!placeExists(m_fileInfo
.url())) {
278 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
285 fileItemActions
.addServiceActionsTo(this);
286 fileItemActions
.addPluginActionsTo(this);
288 addVersionControlPluginActions();
290 // insert 'Copy To' and 'Move To' sub menus
291 if (GeneralSettings::showCopyMoveMenu()) {
292 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
293 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
294 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
295 m_copyToMenu
.addActionsTo(this);
298 // insert 'Properties...' entry
300 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
301 addAction(propertiesAction
);
303 QAction
* activatedAction
= exec(m_pos
);
304 if (activatedAction
) {
305 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 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
319 // Insert 'Open With' entries
320 KFileItem baseItem
= view
->rootItem();
321 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
322 baseItem
= baseFileItem();
325 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
326 KFileItemActions fileItemActions
;
327 fileItemActions
.setParentWidget(m_mainWindow
);
328 fileItemActions
.setItemListProperties(baseUrlProperties
);
330 // Don't show "Open With" menu items if the current dir is empty, because there's
331 // generally no app that can do anything interesting with an empty directory
332 if (view
->itemsCount() != 0) {
333 addOpenWithActions(fileItemActions
);
336 // Set up and insert 'Create New' menu
337 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
338 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
339 newFileMenu
->checkUpToDate();
340 newFileMenu
->setPopupFiles(m_baseUrl
);
341 addMenu(newFileMenu
->menu());
343 QAction
* pasteAction
= createPasteAction();
344 addAction(pasteAction
);
346 // Insert 'Add to Places' entry if it's not already in the places panel
347 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
348 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
352 // Insert 'Sort By' and 'View Mode'
353 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
354 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
358 // Insert service actions
359 fileItemActions
.addServiceActionsTo(this);
360 fileItemActions
.addPluginActionsTo(this);
362 addVersionControlPluginActions();
368 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
369 addAction(propertiesAction
);
371 addShowMenuBarAction();
376 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
378 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
380 // Insert 'Cut', 'Copy' and 'Paste'
381 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
382 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
383 addAction(createPasteAction());
388 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
390 // Insert 'Move to Trash' and/or 'Delete'
391 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
392 !properties
.isLocal());
393 const bool showMoveToTrashAction
= (properties
.isLocal() &&
394 properties
.supportsMoving());
396 if (showDeleteAction
&& showMoveToTrashAction
) {
397 delete m_removeAction
;
398 m_removeAction
= nullptr;
399 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
400 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
401 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
402 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
404 if (!m_removeAction
) {
405 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
407 addAction(m_removeAction
);
408 m_removeAction
->update();
412 void DolphinContextMenu::addShowMenuBarAction()
414 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
415 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
416 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
418 addAction(showMenuBar
);
422 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
424 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
426 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
428 return !matchedPlaces
.isEmpty();
431 QAction
* DolphinContextMenu::createPasteAction()
433 QAction
* action
= nullptr;
434 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
435 if (isDir
&& (m_selectedItems
.count() == 1)) {
436 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
438 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
439 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
440 action
->setEnabled(canPaste
);
441 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
443 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
449 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
451 if (!m_selectedItemsProperties
) {
452 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
454 return *m_selectedItemsProperties
;
457 KFileItem
DolphinContextMenu::baseFileItem()
459 if (!m_baseFileItem
) {
460 m_baseFileItem
= new KFileItem(m_baseUrl
);
462 return *m_baseFileItem
;
465 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
467 // insert 'Open With...' action or sub menu
468 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
471 void DolphinContextMenu::addVersionControlPluginActions()
473 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
474 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
475 if (!versionControlActions
.isEmpty()) {
476 addActions(versionControlActions
);
481 void DolphinContextMenu::addCustomActions()
483 addActions(m_customActions
);