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 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 QAction
* addToPlacesAction
= 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 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
213 i18nc("@action:inmenu Add selected folder to places",
219 // set up 'Create New' menu
220 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
221 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
222 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
223 newFileMenu
->checkUpToDate();
224 newFileMenu
->setPopupFiles(m_fileInfo
.url());
225 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
226 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
227 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
229 QMenu
* menu
= newFileMenu
->menu();
230 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
231 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
235 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
236 addOpenWithActions(fileItemActions
);
238 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
239 i18nc("@action:inmenu",
242 addAction(openParentAction
);
244 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
245 i18nc("@action:inmenu",
246 "Open Path in New Window"),
248 addAction(openParentInNewWindowAction
);
250 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
251 i18nc("@action:inmenu",
252 "Open Path in New Tab"),
254 addAction(openParentInNewTabAction
);
257 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
258 // Insert 'Open With" entries
259 addOpenWithActions(fileItemActions
);
261 // insert 'Open in new window' and 'Open in new tab' entries
262 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
263 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
267 // Insert 'Open With" entries
268 addOpenWithActions(fileItemActions
);
270 if (m_fileInfo
.isLink()) {
271 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("show_target")));
275 bool selectionHasOnlyDirs
= true;
276 foreach (const KFileItem
& item
, m_selectedItems
) {
277 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
279 selectionHasOnlyDirs
= false;
284 if (selectionHasOnlyDirs
) {
285 // insert 'Open in new tab' entry
286 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
288 // Insert 'Open With" entries
289 addOpenWithActions(fileItemActions
);
292 insertDefaultItemActions(selectedItemsProps
);
296 fileItemActions
.addServiceActionsTo(this);
297 fileItemActions
.addPluginActionsTo(this);
299 addVersionControlPluginActions();
301 // insert 'Copy To' and 'Move To' sub menus
302 if (GeneralSettings::showCopyMoveMenu()) {
303 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
304 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
305 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
306 m_copyToMenu
.addActionsTo(this);
309 // insert 'Properties...' entry
310 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
311 addAction(propertiesAction
);
313 QAction
* activatedAction
= exec(m_pos
);
314 if (activatedAction
) {
315 if (activatedAction
== addToPlacesAction
) {
316 const QUrl
selectedUrl(m_fileInfo
.url());
317 if (selectedUrl
.isValid()) {
318 PlacesItemModel model
;
319 const QString text
= selectedUrl
.fileName();
320 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
322 } else if (activatedAction
== openParentAction
) {
323 m_command
= OpenParentFolder
;
324 } else if (activatedAction
== openParentInNewWindowAction
) {
325 m_command
= OpenParentFolderInNewWindow
;
326 } else if (activatedAction
== openParentInNewTabAction
) {
327 m_command
= OpenParentFolderInNewTab
;
332 void DolphinContextMenu::openViewportContextMenu()
334 // setup 'Create New' menu
335 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
336 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
337 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
338 newFileMenu
->checkUpToDate();
339 newFileMenu
->setPopupFiles(m_baseUrl
);
340 addMenu(newFileMenu
->menu());
343 // Insert 'Open With' entries
344 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
345 KFileItemActions fileItemActions
;
346 fileItemActions
.setParentWidget(m_mainWindow
);
347 fileItemActions
.setItemListProperties(baseUrlProperties
);
348 addOpenWithActions(fileItemActions
);
350 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
351 // "open_in_new_tab" here, as the current selection should get ignored.
352 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("file_new")));
353 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
355 // Insert 'Add to Places' entry if exactly one item is selected
356 QAction
* addToPlacesAction
= nullptr;
357 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
358 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
359 i18nc("@action:inmenu Add current folder to places", "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();
387 QAction
* action
= exec(m_pos
);
388 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
389 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
390 const QUrl url
= container
->url();
392 PlacesItemModel model
;
394 if (container
->isSearchModeEnabled()) {
395 icon
= QStringLiteral("folder-saved-search-symbolic");
397 icon
= KIO::iconNameForUrl(url
);
399 model
.createPlacesItem(container
->placesText(), url
, icon
);
404 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
406 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
408 // Insert 'Cut', 'Copy' and 'Paste'
409 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
410 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
411 addAction(createPasteAction());
416 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
418 // Insert 'Move to Trash' and/or 'Delete'
419 if (properties
.supportsDeleting()) {
420 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
421 !properties
.isLocal());
422 const bool showMoveToTrashAction
= (properties
.isLocal() &&
423 properties
.supportsMoving());
425 if (showDeleteAction
&& showMoveToTrashAction
) {
426 delete m_removeAction
;
427 m_removeAction
= nullptr;
428 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
429 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
430 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
431 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
433 if (!m_removeAction
) {
434 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
436 addAction(m_removeAction
);
437 m_removeAction
->update();
442 void DolphinContextMenu::addShowMenuBarAction()
444 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
445 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
446 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
448 addAction(showMenuBar
);
452 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
455 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
456 // can be expensive because the model asks Solid for the devices which are
457 // available, which can take some time.
458 // TODO: Consider restoring this check if the handling of Places and devices
459 // will be decoupled in the future.
463 QAction
* DolphinContextMenu::createPasteAction()
465 QAction
* action
= nullptr;
466 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
467 if (isDir
&& (m_selectedItems
.count() == 1)) {
468 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
470 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
471 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
472 action
->setEnabled(canPaste
);
473 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
475 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
481 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
483 if (!m_selectedItemsProperties
) {
484 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
486 return *m_selectedItemsProperties
;
489 KFileItem
DolphinContextMenu::baseFileItem()
491 if (!m_baseFileItem
) {
492 m_baseFileItem
= new KFileItem(m_baseUrl
);
494 return *m_baseFileItem
;
497 void DolphinContextMenu::addOpenWithActions(KFileItemActions
& fileItemActions
)
499 // insert 'Open With...' action or sub menu
500 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp
->desktopFileName()));
503 void DolphinContextMenu::addVersionControlPluginActions()
505 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
506 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
507 if (!versionControlActions
.isEmpty()) {
508 addActions(versionControlActions
);
513 void DolphinContextMenu::addCustomActions()
515 addActions(m_customActions
);