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 <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_baseFileItem
;
85 m_baseFileItem
= nullptr;
86 delete m_selectedItemsProperties
;
87 m_selectedItemsProperties
= nullptr;
90 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
92 m_customActions
= actions
;
95 DolphinContextMenu::Command
DolphinContextMenu::open()
97 // get the context information
98 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
99 m_context
|= TrashContext
;
102 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
103 m_context
|= ItemContext
;
104 // TODO: handle other use cases like devices + desktop files
107 // open the corresponding popup for the context
108 if (m_context
& TrashContext
) {
109 if (m_context
& ItemContext
) {
110 openTrashItemContextMenu();
112 openTrashContextMenu();
114 } else if (m_context
& ItemContext
) {
115 openItemContextMenu();
117 Q_ASSERT(m_context
== NoContext
);
118 openViewportContextMenu();
124 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
126 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
127 m_removeAction
->update(DolphinRemoveAction::ShiftState::Pressed
);
129 QMenu::keyPressEvent(ev
);
132 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
134 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
135 m_removeAction
->update(DolphinRemoveAction::ShiftState::Released
);
137 QMenu::keyReleaseEvent(ev
);
140 void DolphinContextMenu::openTrashContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
144 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
145 emptyTrashAction
->setEnabled(!Trash::isEmpty());
146 addAction(emptyTrashAction
);
150 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
151 addAction(propertiesAction
);
153 addShowMenuBarAction();
155 if (exec(m_pos
) == emptyTrashAction
) {
156 Trash::empty(m_mainWindow
);
160 void DolphinContextMenu::openTrashItemContextMenu()
162 Q_ASSERT(m_context
& TrashContext
);
163 Q_ASSERT(m_context
& ItemContext
);
165 QAction
* restoreAction
= new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow
);
166 addAction(restoreAction
);
168 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
169 addAction(deleteAction
);
171 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
172 addAction(propertiesAction
);
174 if (exec(m_pos
) == restoreAction
) {
175 QList
<QUrl
> selectedUrls
;
176 selectedUrls
.reserve(m_selectedItems
.count());
177 foreach (const KFileItem
&item
, m_selectedItems
) {
178 selectedUrls
.append(item
.url());
181 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
182 KJobWidgets::setWindow(job
, m_mainWindow
);
183 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
187 void DolphinContextMenu::openItemContextMenu()
189 Q_ASSERT(!m_fileInfo
.isNull());
191 QAction
* openParentAction
= nullptr;
192 QAction
* openParentInNewWindowAction
= nullptr;
193 QAction
* openParentInNewTabAction
= nullptr;
194 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
196 KFileItemActions fileItemActions
;
197 fileItemActions
.setParentWidget(m_mainWindow
);
198 fileItemActions
.setItemListProperties(selectedItemsProps
);
200 if (m_selectedItems
.count() == 1) {
201 if (m_fileInfo
.isDir()) {
202 // insert 'Open in new window' and 'Open in new tab' entries
203 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
204 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
206 // Insert 'Open With' entries
207 addOpenWithActions(fileItemActions
);
209 // set up 'Create New' menu
210 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
211 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
212 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
213 newFileMenu
->checkUpToDate();
214 newFileMenu
->setPopupFiles(m_fileInfo
.url());
215 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
216 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
217 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
219 QMenu
* menu
= newFileMenu
->menu();
220 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
221 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
225 } else if (m_baseUrl
.scheme().contains(QLatin1String("search")) || m_baseUrl
.scheme().contains(QLatin1String("timeline"))) {
226 addOpenWithActions(fileItemActions
);
228 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
229 i18nc("@action:inmenu",
232 addAction(openParentAction
);
234 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
235 i18nc("@action:inmenu",
236 "Open Path in New Window"),
238 addAction(openParentInNewWindowAction
);
240 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
241 i18nc("@action:inmenu",
242 "Open Path in New Tab"),
244 addAction(openParentInNewTabAction
);
248 // Insert 'Open With" entries
249 addOpenWithActions(fileItemActions
);
251 if (m_fileInfo
.isLink()) {
252 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
256 bool selectionHasOnlyDirs
= true;
257 foreach (const KFileItem
& item
, m_selectedItems
) {
258 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
260 selectionHasOnlyDirs
= false;
265 if (selectionHasOnlyDirs
) {
266 // insert 'Open in new tab' entry
267 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
269 // Insert 'Open With" entries
270 addOpenWithActions(fileItemActions
);
273 insertDefaultItemActions(selectedItemsProps
);
275 // insert 'Add to Places' entry if appropriate
276 if (m_selectedItems
.count() == 1) {
277 if (m_fileInfo
.isDir()) {
278 if (!placeExists(m_fileInfo
.url())) {
279 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
286 fileItemActions
.addServiceActionsTo(this);
287 fileItemActions
.addPluginActionsTo(this);
289 addVersionControlPluginActions();
291 // insert 'Copy To' and 'Move To' sub menus
292 if (GeneralSettings::showCopyMoveMenu()) {
293 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
294 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
295 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
296 m_copyToMenu
.addActionsTo(this);
299 // insert 'Properties...' entry
301 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
302 addAction(propertiesAction
);
304 QAction
* activatedAction
= exec(m_pos
);
305 if (activatedAction
) {
306 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 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
320 // Insert 'Open With' entries
321 KFileItem baseItem
= view
->rootItem();
322 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
323 baseItem
= baseFileItem();
326 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
327 KFileItemActions fileItemActions
;
328 fileItemActions
.setParentWidget(m_mainWindow
);
329 fileItemActions
.setItemListProperties(baseUrlProperties
);
331 // Don't show "Open With" menu items if the current dir is empty, because there's
332 // generally no app that can do anything interesting with an empty directory
333 if (view
->itemsCount() != 0) {
334 addOpenWithActions(fileItemActions
);
337 // Set up and insert 'Create New' menu
338 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
339 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
340 newFileMenu
->checkUpToDate();
341 newFileMenu
->setPopupFiles(m_baseUrl
);
342 addMenu(newFileMenu
->menu());
344 QAction
* pasteAction
= createPasteAction();
345 addAction(pasteAction
);
347 // Insert 'Add to Places' entry if it's not already in the places panel
348 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
349 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("add_to_places")));
353 // Insert 'Sort By' and 'View Mode'
354 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
355 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
359 // Insert service actions
360 fileItemActions
.addServiceActionsTo(this);
361 fileItemActions
.addPluginActionsTo(this);
363 addVersionControlPluginActions();
369 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
370 addAction(propertiesAction
);
372 addShowMenuBarAction();
377 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
379 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
381 // Insert 'Cut', 'Copy' and 'Paste'
382 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
383 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
384 addAction(createPasteAction());
389 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
391 // Insert 'Move to Trash' and/or 'Delete'
392 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
393 !properties
.isLocal());
394 const bool showMoveToTrashAction
= (properties
.isLocal() &&
395 properties
.supportsMoving());
397 if (showDeleteAction
&& showMoveToTrashAction
) {
398 delete m_removeAction
;
399 m_removeAction
= nullptr;
400 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
401 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
402 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
403 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
405 if (!m_removeAction
) {
406 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
408 addAction(m_removeAction
);
409 m_removeAction
->update();
413 void DolphinContextMenu::addShowMenuBarAction()
415 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
416 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
417 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
419 addAction(showMenuBar
);
423 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
425 const KFilePlacesModel
* placesModel
= DolphinPlacesModelSingleton::instance().placesModel();
427 const auto& matchedPlaces
= placesModel
->match(placesModel
->index(0,0), KFilePlacesModel::UrlRole
, url
, 1, Qt::MatchExactly
);
429 return !matchedPlaces
.isEmpty();
432 QAction
* DolphinContextMenu::createPasteAction()
434 QAction
* action
= nullptr;
435 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
436 if (isDir
&& (m_selectedItems
.count() == 1)) {
437 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
439 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
440 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
441 action
->setEnabled(canPaste
);
442 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
444 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
450 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
452 if (!m_selectedItemsProperties
) {
453 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
455 return *m_selectedItemsProperties
;
458 KFileItem
DolphinContextMenu::baseFileItem()
460 if (!m_baseFileItem
) {
461 m_baseFileItem
= new KFileItem(m_baseUrl
);
463 return *m_baseFileItem
;
466 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
468 // insert 'Open With...' action or sub menu
469 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
472 void DolphinContextMenu::addVersionControlPluginActions()
474 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
475 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
476 if (!versionControlActions
.isEmpty()) {
477 addActions(versionControlActions
);
482 void DolphinContextMenu::addCustomActions()
484 addActions(m_customActions
);