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>
41 #include <KLocalizedString>
42 #include <KStandardAction>
45 #include <QApplication>
51 #include <panels/places/placesitem.h>
52 #include <panels/places/placesitemmodel.h>
55 #include "views/dolphinview.h"
56 #include "views/viewmodecontroller.h"
58 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
60 const KFileItem
& fileInfo
,
61 const QUrl
& baseUrl
) :
69 m_selectedItemsProperties(0),
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_selectedItemsProperties
;
85 m_selectedItemsProperties
= 0;
88 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
90 m_customActions
= actions
;
93 DolphinContextMenu::Command
DolphinContextMenu::open()
95 // get the context information
96 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
97 m_context
|= TrashContext
;
100 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
101 m_context
|= ItemContext
;
102 // TODO: handle other use cases like devices + desktop files
105 // open the corresponding popup for the context
106 if (m_context
& TrashContext
) {
107 if (m_context
& ItemContext
) {
108 openTrashItemContextMenu();
110 openTrashContextMenu();
112 } else if (m_context
& ItemContext
) {
113 openItemContextMenu();
115 Q_ASSERT(m_context
== NoContext
);
116 openViewportContextMenu();
122 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
124 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
125 m_removeAction
->update();
127 QMenu::keyPressEvent(ev
);
130 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
132 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
133 m_removeAction
->update();
135 QMenu::keyReleaseEvent(ev
);
138 void DolphinContextMenu::openTrashContextMenu()
140 Q_ASSERT(m_context
& TrashContext
);
142 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
143 KConfig
trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig
);
144 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
145 addAction(emptyTrashAction
);
149 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
150 addAction(propertiesAction
);
152 addShowMenuBarAction();
154 if (exec(m_pos
) == emptyTrashAction
) {
155 KIO::JobUiDelegate uiDelegate
;
156 uiDelegate
.setWindow(m_mainWindow
);
157 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
158 KIO::Job
* job
= KIO::emptyTrash();
159 KJobWidgets::setWindow(job
, m_mainWindow
);
160 job
->ui()->setAutoErrorHandlingEnabled(true);
165 void DolphinContextMenu::openTrashItemContextMenu()
167 Q_ASSERT(m_context
& TrashContext
);
168 Q_ASSERT(m_context
& ItemContext
);
170 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
171 addAction(restoreAction
);
173 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("delete"));
174 addAction(deleteAction
);
176 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
177 addAction(propertiesAction
);
179 if (exec(m_pos
) == restoreAction
) {
180 QList
<QUrl
> selectedUrls
;
181 selectedUrls
.reserve(m_selectedItems
.count());
182 foreach (const KFileItem
&item
, m_selectedItems
) {
183 selectedUrls
.append(item
.url());
186 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
187 KJobWidgets::setWindow(job
, m_mainWindow
);
188 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
192 void DolphinContextMenu::openItemContextMenu()
194 Q_ASSERT(!m_fileInfo
.isNull());
196 QAction
* openParentAction
= 0;
197 QAction
* openParentInNewWindowAction
= 0;
198 QAction
* openParentInNewTabAction
= 0;
199 QAction
* addToPlacesAction
= 0;
200 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
202 if (m_selectedItems
.count() == 1) {
203 if (m_fileInfo
.isDir()) {
204 // setup 'Create New' menu
205 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
206 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
207 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
208 newFileMenu
->checkUpToDate();
209 newFileMenu
->setPopupFiles(m_fileInfo
.url());
210 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
211 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
212 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
214 QMenu
* menu
= newFileMenu
->menu();
215 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
216 menu
->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
220 // insert 'Open in new window' and 'Open in new tab' entries
221 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
222 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
224 // insert 'Add to Places' entry
225 if (!placeExists(m_fileInfo
.url())) {
226 addToPlacesAction
= addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
227 i18nc("@action:inmenu Add selected folder to places",
232 } else if (m_baseUrl
.scheme().contains(QStringLiteral("search")) || m_baseUrl
.scheme().contains(QStringLiteral("timeline"))) {
233 openParentAction
= new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
234 i18nc("@action:inmenu",
237 addAction(openParentAction
);
239 openParentInNewWindowAction
= new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
240 i18nc("@action:inmenu",
241 "Open Path in New Window"),
243 addAction(openParentInNewWindowAction
);
245 openParentInNewTabAction
= new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
246 i18nc("@action:inmenu",
247 "Open Path in New Tab"),
249 addAction(openParentInNewTabAction
);
252 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
253 // insert 'Open in new window' and 'Open in new tab' entries
254 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_window")));
255 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tab")));
260 bool selectionHasOnlyDirs
= true;
261 foreach (const KFileItem
& item
, m_selectedItems
) {
262 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
264 selectionHasOnlyDirs
= false;
269 if (selectionHasOnlyDirs
) {
270 // insert 'Open in new tab' entry
271 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
276 insertDefaultItemActions(selectedItemsProps
);
280 KFileItemActions fileItemActions
;
281 fileItemActions
.setItemListProperties(selectedItemsProps
);
282 addServiceActions(fileItemActions
);
284 addFileItemPluginActions();
286 addVersionControlPluginActions();
288 // insert 'Copy To' and 'Move To' sub menus
289 if (GeneralSettings::showCopyMoveMenu()) {
290 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
291 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
292 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
293 m_copyToMenu
.addActionsTo(this);
296 // insert 'Properties...' entry
297 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action(QStringLiteral("properties"));
298 addAction(propertiesAction
);
300 QAction
* activatedAction
= exec(m_pos
);
301 if (activatedAction
) {
302 if (activatedAction
== addToPlacesAction
) {
303 const QUrl
selectedUrl(m_fileInfo
.url());
304 if (selectedUrl
.isValid()) {
305 PlacesItemModel model
;
306 const QString text
= selectedUrl
.fileName();
307 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
308 model
.appendItemToGroup(item
);
309 model
.saveBookmarks();
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
= 0;
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 addFileItemPluginActions();
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 if (container
->url().isValid()) {
371 PlacesItemModel model
;
372 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
374 model
.appendItemToGroup(item
);
375 model
.saveBookmarks();
380 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
382 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
384 // Insert 'Cut', 'Copy' and 'Paste'
385 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
386 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
387 addAction(createPasteAction());
392 QAction
* renameAction
= collection
->action(QStringLiteral("rename"));
393 addAction(renameAction
);
395 // Insert 'Move to Trash' and/or 'Delete'
396 if (properties
.supportsDeleting()) {
397 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
398 !properties
.isLocal());
399 const bool showMoveToTrashAction
= (properties
.isLocal() &&
400 properties
.supportsMoving());
402 if (showDeleteAction
&& showMoveToTrashAction
) {
403 delete m_removeAction
;
405 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("move_to_trash")));
406 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("delete")));
407 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
408 addAction(m_mainWindow
->actionCollection()->action(QStringLiteral("delete")));
410 if (!m_removeAction
) {
411 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
413 addAction(m_removeAction
);
414 m_removeAction
->update();
419 void DolphinContextMenu::addShowMenuBarAction()
421 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
422 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
423 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
425 addAction(showMenuBar
);
429 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
431 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
432 // can be expensive because the model asks Solid for the devices which are
433 // available, which can take some time.
434 // TODO: Consider restoring this check if the handling of Places and devices
435 // will be decoupled in the future.
439 QAction
* DolphinContextMenu::createPasteAction()
442 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
443 if (isDir
&& (m_selectedItems
.count() == 1)) {
444 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
446 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
447 action
= new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text
, this);
448 action
->setEnabled(canPaste
);
449 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
451 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
457 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
459 if (!m_selectedItemsProperties
) {
460 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
462 return *m_selectedItemsProperties
;
465 KFileItem
DolphinContextMenu::baseFileItem()
467 if (!m_baseFileItem
) {
468 m_baseFileItem
= new KFileItem(m_baseUrl
);
470 return *m_baseFileItem
;
473 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
475 fileItemActions
.setParentWidget(m_mainWindow
);
477 // insert 'Open With...' action or sub menu
478 fileItemActions
.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
480 // insert 'Actions' sub menu
481 fileItemActions
.addServiceActionsTo(this);
484 void DolphinContextMenu::addFileItemPluginActions()
486 KFileItemListProperties props
;
487 if (m_selectedItems
.isEmpty()) {
488 props
.setItems(KFileItemList() << baseFileItem());
490 props
= selectedItemsProperties();
493 QString commonMimeType
= props
.mimeType();
494 if (commonMimeType
.isEmpty()) {
495 commonMimeType
= QStringLiteral("application/octet-stream");
498 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, QStringLiteral("KFileItemAction/Plugin"), QStringLiteral("exist Library"));
499 if (pluginServices
.isEmpty()) {
503 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
504 const KConfigGroup showGroup
= config
.group("Show");
506 foreach (const KService::Ptr
& service
, pluginServices
) {
507 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
508 // The plugin has been disabled
512 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
513 if (abstractPlugin
) {
514 abstractPlugin
->setParent(this);
515 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
520 void DolphinContextMenu::addVersionControlPluginActions()
522 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
523 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
524 if (!versionControlActions
.isEmpty()) {
525 addActions(versionControlActions
);
530 void DolphinContextMenu::addCustomActions()
532 addActions(m_customActions
);