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 "dolphinmainwindow.h"
24 #include "dolphinnewfilemenu.h"
25 #include "dolphinviewcontainer.h"
26 #include "dolphin_generalsettings.h"
27 #include "dolphinremoveaction.h"
29 #include <KActionCollection>
30 #include <KAbstractFileItemActionPlugin>
31 #include <KFileItemActions>
32 #include <KFileItemListProperties>
33 #include <KIO/RestoreJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
37 #include <KJobWidgets>
38 #include <KMimeTypeTrader>
39 #include <KNewFileMenu>
40 #include <KPluginMetaData>
42 #include <KLocalizedString>
43 #include <KStandardAction>
46 #include <QApplication>
51 #include <QMimeDatabase>
53 #include <panels/places/placesitem.h>
54 #include <panels/places/placesitemmodel.h>
57 #include "views/dolphinview.h"
58 #include "views/viewmodecontroller.h"
60 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
62 const KFileItem
& fileInfo
,
63 const QUrl
& baseUrl
) :
71 m_selectedItemsProperties(nullptr),
78 // The context menu either accesses the URLs of the selected items
79 // or the items itself. To increase the performance both lists are cached.
80 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
81 m_selectedItems
= view
->selectedItems();
84 DolphinContextMenu::~DolphinContextMenu()
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 KConfig
trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig
);
146 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
147 addAction(emptyTrashAction
);
151 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
152 addAction(propertiesAction
);
154 addShowMenuBarAction();
156 if (exec(m_pos
) == emptyTrashAction
) {
157 KIO::JobUiDelegate uiDelegate
;
158 uiDelegate
.setWindow(m_mainWindow
);
159 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
160 KIO::Job
* job
= KIO::emptyTrash();
161 KJobWidgets::setWindow(job
, m_mainWindow
);
162 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
167 void DolphinContextMenu::openTrashItemContextMenu()
169 Q_ASSERT(m_context
& TrashContext
);
170 Q_ASSERT(m_context
& ItemContext
);
172 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
173 addAction(restoreAction
);
175 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
));
176 addAction(deleteAction
);
178 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
179 addAction(propertiesAction
);
181 if (exec(m_pos
) == restoreAction
) {
182 QList
<QUrl
> selectedUrls
;
183 selectedUrls
.reserve(m_selectedItems
.count());
184 foreach (const KFileItem
&item
, m_selectedItems
) {
185 selectedUrls
.append(item
.url());
188 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
189 KJobWidgets::setWindow(job
, m_mainWindow
);
190 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
194 void DolphinContextMenu::openItemContextMenu()
196 Q_ASSERT(!m_fileInfo
.isNull());
198 QAction
* openParentAction
= nullptr;
199 QAction
* openParentInNewWindowAction
= nullptr;
200 QAction
* openParentInNewTabAction
= nullptr;
201 QAction
* addToPlacesAction
= nullptr;
202 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
204 if (m_selectedItems
.count() == 1) {
205 if (m_fileInfo
.isDir()) {
206 // setup 'Create New' menu
207 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
208 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
209 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
210 newFileMenu
->checkUpToDate();
211 newFileMenu
->setPopupFiles(m_fileInfo
.url());
212 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
213 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
214 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
216 QMenu
* menu
= newFileMenu
->menu();
217 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
218 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
222 // insert 'Open in new window' and 'Open in new tab' entries
223 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
224 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
226 // insert 'Add to Places' entry
227 if (!placeExists(m_fileInfo
.url())) {
228 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
229 i18nc("@action:inmenu Add selected folder to places",
234 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
235 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
236 i18nc("@action:inmenu",
239 addAction(openParentAction
);
241 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
242 i18nc("@action:inmenu",
243 "Open Path in New Window"),
245 addAction(openParentInNewWindowAction
);
247 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
248 i18nc("@action:inmenu",
249 "Open Path in New Tab"),
251 addAction(openParentInNewTabAction
);
254 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
255 // insert 'Open in new window' and 'Open in new tab' entries
256 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
257 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
262 bool selectionHasOnlyDirs
= true;
263 foreach (const KFileItem
& item
, m_selectedItems
) {
264 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
266 selectionHasOnlyDirs
= false;
271 if (selectionHasOnlyDirs
) {
272 // insert 'Open in new tab' entry
273 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
278 insertDefaultItemActions(selectedItemsProps
);
282 KFileItemActions fileItemActions
;
283 fileItemActions
.setItemListProperties(selectedItemsProps
);
284 addServiceActions(fileItemActions
);
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
299 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
300 addAction(propertiesAction
);
302 QAction
* activatedAction
= exec(m_pos
);
303 if (activatedAction
) {
304 if (activatedAction
== addToPlacesAction
) {
305 const QUrl
selectedUrl(m_fileInfo
.url());
306 if (selectedUrl
.isValid()) {
307 PlacesItemModel model
;
308 const QString text
= selectedUrl
.fileName();
309 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
310 model
.appendItemToGroup(item
);
311 model
.saveBookmarks();
313 } else if (activatedAction
== openParentAction
) {
314 m_command
= OpenParentFolder
;
315 } else if (activatedAction
== openParentInNewWindowAction
) {
316 m_command
= OpenParentFolderInNewWindow
;
317 } else if (activatedAction
== openParentInNewTabAction
) {
318 m_command
= OpenParentFolderInNewTab
;
323 void DolphinContextMenu::openViewportContextMenu()
325 // setup 'Create New' menu
326 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
327 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
328 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
329 newFileMenu
->checkUpToDate();
330 newFileMenu
->setPopupFiles(m_baseUrl
);
331 addMenu(newFileMenu
->menu());
334 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
335 // "open_in_new_tab" here, as the current selection should get ignored.
336 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_window")));
337 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
339 // Insert 'Add to Places' entry if exactly one item is selected
340 QAction
* addToPlacesAction
= 0;
341 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
342 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
343 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
348 QAction
* pasteAction
= createPasteAction();
349 addAction(pasteAction
);
352 // Insert service actions
353 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
354 KFileItemActions fileItemActions
;
355 fileItemActions
.setItemListProperties(baseUrlProperties
);
356 addServiceActions(fileItemActions
);
358 fileItemActions
.addPluginActionsTo(this);
360 addVersionControlPluginActions();
364 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
365 addAction(propertiesAction
);
367 addShowMenuBarAction();
369 QAction
* action
= exec(m_pos
);
370 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
371 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
372 const QUrl url
= container
->url();
374 PlacesItemModel model
;
376 if (container
->isSearchModeEnabled()) {
377 icon
= QStringLiteral("folder-saved-search-symbolic");
379 icon
= KIO::iconNameForUrl(url
);
381 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(), url
, icon
);
382 model
.appendItemToGroup(item
);
383 model
.saveBookmarks();
388 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
390 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
392 // Insert 'Cut', 'Copy' and 'Paste'
393 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
394 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
395 addAction(createPasteAction());
400 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
402 // Insert 'Move to Trash' and/or 'Delete'
403 if (properties
.supportsDeleting()) {
404 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
405 !properties
.isLocal());
406 const bool showMoveToTrashAction
= (properties
.isLocal() &&
407 properties
.supportsMoving());
409 if (showDeleteAction
&& showMoveToTrashAction
) {
410 delete m_removeAction
;
412 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
413 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
414 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
415 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
417 if (!m_removeAction
) {
418 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
420 addAction(m_removeAction
);
421 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 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
439 // can be expensive because the model asks Solid for the devices which are
440 // available, which can take some time.
441 // TODO: Consider restoring this check if the handling of Places and devices
442 // will be decoupled in the future.
446 QAction
* DolphinContextMenu::createPasteAction()
449 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
450 if (isDir
&& (m_selectedItems
.count() == 1)) {
451 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
453 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
454 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
455 action
->setEnabled(canPaste
);
456 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
458 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
464 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
466 if (!m_selectedItemsProperties
) {
467 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
469 return *m_selectedItemsProperties
;
472 KFileItem
DolphinContextMenu::baseFileItem()
474 if (!m_baseFileItem
) {
475 m_baseFileItem
= new KFileItem(m_baseUrl
);
477 return *m_baseFileItem
;
480 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
482 fileItemActions
.setParentWidget(m_mainWindow
);
484 // insert 'Open With...' action or sub menu
485 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
487 // insert 'Actions' sub menu
488 fileItemActions
.addServiceActionsTo(this);
491 void DolphinContextMenu::addVersionControlPluginActions()
493 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
494 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
495 if (!versionControlActions
.isEmpty()) {
496 addActions(versionControlActions
);
501 void DolphinContextMenu::addCustomActions()
503 addActions(m_customActions
);