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_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(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
166 restoreAction
->setShortcut(Qt::CTRL
+ Qt::Key_R
);
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 QAction
* addToPlacesAction
= nullptr;
196 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
198 KFileItemActions fileItemActions
;
199 fileItemActions
.setParentWidget(m_mainWindow
);
200 fileItemActions
.setItemListProperties(selectedItemsProps
);
202 if (m_selectedItems
.count() == 1) {
203 if (m_fileInfo
.isDir()) {
204 // insert 'Open in new window' and 'Open in new tab' entries
205 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
206 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
208 // Insert 'Open With' entries
209 addOpenWithActions(fileItemActions
);
211 // insert 'Add to Places' entry
212 if (!placeExists(m_fileInfo
.url())) {
213 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
214 i18nc("@action:inmenu Add selected folder to places",
220 // set up 'Create New' menu
221 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
222 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
223 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
224 newFileMenu
->checkUpToDate();
225 newFileMenu
->setPopupFiles(m_fileInfo
.url());
226 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
227 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
228 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
230 QMenu
* menu
= newFileMenu
->menu();
231 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
232 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
236 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
237 addOpenWithActions(fileItemActions
);
239 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
240 i18nc("@action:inmenu",
243 addAction(openParentAction
);
245 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
246 i18nc("@action:inmenu",
247 "Open Path in New Window"),
249 addAction(openParentInNewWindowAction
);
251 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
252 i18nc("@action:inmenu",
253 "Open Path in New Tab"),
255 addAction(openParentInNewTabAction
);
258 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
259 // Insert 'Open With" entries
260 addOpenWithActions(fileItemActions
);
262 // insert 'Open in new window' and 'Open in new tab' entries
263 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
264 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
268 // Insert 'Open With" entries
269 addOpenWithActions(fileItemActions
);
271 if (m_fileInfo
.isLink()) {
272 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
276 bool selectionHasOnlyDirs
= true;
277 foreach (const KFileItem
& item
, m_selectedItems
) {
278 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
280 selectionHasOnlyDirs
= false;
285 if (selectionHasOnlyDirs
) {
286 // insert 'Open in new tab' entry
287 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
289 // Insert 'Open With" entries
290 addOpenWithActions(fileItemActions
);
293 insertDefaultItemActions(selectedItemsProps
);
297 fileItemActions
.addServiceActionsTo(this);
298 fileItemActions
.addPluginActionsTo(this);
300 addVersionControlPluginActions();
302 // insert 'Copy To' and 'Move To' sub menus
303 if (GeneralSettings::showCopyMoveMenu()) {
304 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
305 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
306 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
307 m_copyToMenu
.addActionsTo(this);
310 // insert 'Properties...' entry
312 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
313 addAction(propertiesAction
);
315 QAction
* activatedAction
= exec(m_pos
);
316 if (activatedAction
) {
317 if (activatedAction
== addToPlacesAction
) {
318 const QUrl
selectedUrl(m_fileInfo
.url());
319 if (selectedUrl
.isValid()) {
320 PlacesItemModel model
;
321 const QString text
= selectedUrl
.fileName();
322 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
324 } else if (activatedAction
== openParentAction
) {
325 m_command
= OpenParentFolder
;
326 } else if (activatedAction
== openParentInNewWindowAction
) {
327 m_command
= OpenParentFolderInNewWindow
;
328 } else if (activatedAction
== openParentInNewTabAction
) {
329 m_command
= OpenParentFolderInNewTab
;
334 void DolphinContextMenu::openViewportContextMenu()
336 // setup 'Create New' menu
337 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
338 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
339 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
340 newFileMenu
->checkUpToDate();
341 newFileMenu
->setPopupFiles(m_baseUrl
);
342 addMenu(newFileMenu
->menu());
345 // Insert 'Open With' entries
346 KFileItem baseItem
= view
->rootItem();
347 if (baseItem
.isNull() || baseItem
.url() != m_baseUrl
) {
348 baseItem
= baseFileItem();
351 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseItem
);
352 KFileItemActions fileItemActions
;
353 fileItemActions
.setParentWidget(m_mainWindow
);
354 fileItemActions
.setItemListProperties(baseUrlProperties
);
355 addOpenWithActions(fileItemActions
);
357 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
358 // "open_in_new_tab" here, as the current selection should get ignored.
359 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("file_new")));
360 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
362 // Insert 'Add to Places' entry if exactly one item is selected
363 QAction
* addToPlacesAction
= nullptr;
364 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
365 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
366 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
371 QAction
* pasteAction
= createPasteAction();
372 addAction(pasteAction
);
375 // Insert 'Sort By' and 'View Mode'
376 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("sort")));
377 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("view_mode")));
381 // Insert service actions
382 fileItemActions
.addServiceActionsTo(this);
383 fileItemActions
.addPluginActionsTo(this);
385 addVersionControlPluginActions();
389 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
390 addAction(propertiesAction
);
392 addShowMenuBarAction();
394 QAction
* action
= exec(m_pos
);
395 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
396 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
397 const QUrl url
= container
->url();
399 PlacesItemModel model
;
401 if (container
->isSearchModeEnabled()) {
402 icon
= QStringLiteral("folder-saved-search-symbolic");
404 icon
= KIO::iconNameForUrl(url
);
406 model
.createPlacesItem(container
->placesText(), url
, icon
);
411 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
413 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
415 // Insert 'Cut', 'Copy' and 'Paste'
416 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
417 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
418 addAction(createPasteAction());
423 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
425 // Insert 'Move to Trash' and/or 'Delete'
426 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
427 !properties
.isLocal());
428 const bool showMoveToTrashAction
= (properties
.isLocal() &&
429 properties
.supportsMoving());
431 if (showDeleteAction
&& showMoveToTrashAction
) {
432 delete m_removeAction
;
433 m_removeAction
= nullptr;
434 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
435 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
436 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
437 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
439 if (!m_removeAction
) {
440 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
442 addAction(m_removeAction
);
443 m_removeAction
->update();
447 void DolphinContextMenu::addShowMenuBarAction()
449 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
450 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
451 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
453 addAction(showMenuBar
);
457 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
460 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
461 // can be expensive because the model asks Solid for the devices which are
462 // available, which can take some time.
463 // TODO: Consider restoring this check if the handling of Places and devices
464 // will be decoupled in the future.
468 QAction
* DolphinContextMenu::createPasteAction()
470 QAction
* action
= nullptr;
471 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
472 if (isDir
&& (m_selectedItems
.count() == 1)) {
473 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
475 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
476 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
477 action
->setEnabled(canPaste
);
478 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
480 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
486 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
488 if (!m_selectedItemsProperties
) {
489 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
491 return *m_selectedItemsProperties
;
494 KFileItem
DolphinContextMenu::baseFileItem()
496 if (!m_baseFileItem
) {
497 m_baseFileItem
= new KFileItem(m_baseUrl
);
499 return *m_baseFileItem
;
502 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
504 // insert 'Open With...' action or sub menu
505 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
508 void DolphinContextMenu::addVersionControlPluginActions()
510 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
511 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
512 if (!versionControlActions
.isEmpty()) {
513 addActions(versionControlActions
);
518 void DolphinContextMenu::addCustomActions()
520 addActions(m_customActions
);