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
) :
69 m_baseFileItem(nullptr),
71 m_selectedItemsProperties(nullptr),
76 m_removeAction(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 model
.createPlacesItem(text
, selectedUrl
, KIO::iconNameForUrl(selectedUrl
));
311 } else if (activatedAction
== openParentAction
) {
312 m_command
= OpenParentFolder
;
313 } else if (activatedAction
== openParentInNewWindowAction
) {
314 m_command
= OpenParentFolderInNewWindow
;
315 } else if (activatedAction
== openParentInNewTabAction
) {
316 m_command
= OpenParentFolderInNewTab
;
321 void DolphinContextMenu::openViewportContextMenu()
323 // setup 'Create New' menu
324 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
325 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
326 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
327 newFileMenu
->checkUpToDate();
328 newFileMenu
->setPopupFiles(m_baseUrl
);
329 addMenu(newFileMenu
->menu());
332 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
333 // "open_in_new_tab" here, as the current selection should get ignored.
334 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_window")));
335 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("new_tab")));
337 // Insert 'Add to Places' entry if exactly one item is selected
338 QAction
* addToPlacesAction
= nullptr;
339 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
340 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
341 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
346 QAction
* pasteAction
= createPasteAction();
347 addAction(pasteAction
);
350 // Insert service actions
351 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
352 KFileItemActions fileItemActions
;
353 fileItemActions
.setItemListProperties(baseUrlProperties
);
354 addServiceActions(fileItemActions
);
356 fileItemActions
.addPluginActionsTo(this);
358 addVersionControlPluginActions();
362 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
363 addAction(propertiesAction
);
365 addShowMenuBarAction();
367 QAction
* action
= exec(m_pos
);
368 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
369 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
370 const QUrl url
= container
->url();
372 PlacesItemModel model
;
374 if (container
->isSearchModeEnabled()) {
375 icon
= QStringLiteral("folder-saved-search-symbolic");
377 icon
= KIO::iconNameForUrl(url
);
379 model
.createPlacesItem(container
->placesText(), url
, icon
);
384 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
386 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
388 // Insert 'Cut', 'Copy' and 'Paste'
389 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
390 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
391 addAction(createPasteAction());
396 addAction(collection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
398 // Insert 'Move to Trash' and/or 'Delete'
399 if (properties
.supportsDeleting()) {
400 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
401 !properties
.isLocal());
402 const bool showMoveToTrashAction
= (properties
.isLocal() &&
403 properties
.supportsMoving());
405 if (showDeleteAction
&& showMoveToTrashAction
) {
406 delete m_removeAction
;
407 m_removeAction
= nullptr;
408 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
409 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
410 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
411 addAction(m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile
)));
413 if (!m_removeAction
) {
414 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
416 addAction(m_removeAction
);
417 m_removeAction
->update();
422 void DolphinContextMenu::addShowMenuBarAction()
424 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
425 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
426 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
428 addAction(showMenuBar
);
432 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
435 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
436 // can be expensive because the model asks Solid for the devices which are
437 // available, which can take some time.
438 // TODO: Consider restoring this check if the handling of Places and devices
439 // will be decoupled in the future.
443 QAction
* DolphinContextMenu::createPasteAction()
445 QAction
* action
= nullptr;
446 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
447 if (isDir
&& (m_selectedItems
.count() == 1)) {
448 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
450 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
451 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
452 action
->setEnabled(canPaste
);
453 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
455 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
461 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
463 if (!m_selectedItemsProperties
) {
464 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
466 return *m_selectedItemsProperties
;
469 KFileItem
DolphinContextMenu::baseFileItem()
471 if (!m_baseFileItem
) {
472 m_baseFileItem
= new KFileItem(m_baseUrl
);
474 return *m_baseFileItem
;
477 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
479 fileItemActions
.setParentWidget(m_mainWindow
);
481 // insert 'Open With...' action or sub menu
482 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
484 // insert 'Actions' sub menu
485 fileItemActions
.addServiceActionsTo(this);
488 void DolphinContextMenu::addVersionControlPluginActions()
490 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
491 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
492 if (!versionControlActions
.isEmpty()) {
493 addActions(versionControlActions
);
498 void DolphinContextMenu::addCustomActions()
500 addActions(m_customActions
);