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("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
143 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
144 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
145 addAction(emptyTrashAction
);
149 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("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("delete");
174 addAction(deleteAction
);
176 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
177 addAction(propertiesAction
);
179 if (exec(m_pos
) == restoreAction
) {
180 QList
<QUrl
> selectedUrls
;
181 foreach (const KFileItem
&item
, m_selectedItems
) {
182 selectedUrls
.append(item
.url());
185 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
186 KJobWidgets::setWindow(job
, m_mainWindow
);
187 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
191 void DolphinContextMenu::openItemContextMenu()
193 Q_ASSERT(!m_fileInfo
.isNull());
195 QAction
* openParentAction
= 0;
196 QAction
* openParentInNewWindowAction
= 0;
197 QAction
* openParentInNewTabAction
= 0;
198 QAction
* addToPlacesAction
= 0;
199 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
201 if (m_selectedItems
.count() == 1) {
202 if (m_fileInfo
.isDir()) {
203 // setup 'Create New' menu
204 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
205 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
206 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
207 newFileMenu
->checkUpToDate();
208 newFileMenu
->setPopupFiles(m_fileInfo
.url());
209 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
210 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
211 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
213 QMenu
* menu
= newFileMenu
->menu();
214 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
215 menu
->setIcon(QIcon::fromTheme("document-new"));
219 // insert 'Open in new window' and 'Open in new tab' entries
220 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
221 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
223 // insert 'Add to Places' entry
224 if (!placeExists(m_fileInfo
.url())) {
225 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
226 i18nc("@action:inmenu Add selected folder to places",
231 } else if (m_baseUrl
.scheme().contains("search") || m_baseUrl
.scheme().contains("timeline")) {
232 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
233 i18nc("@action:inmenu",
236 addAction(openParentAction
);
238 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
239 i18nc("@action:inmenu",
240 "Open Path in New Window"),
242 addAction(openParentInNewWindowAction
);
244 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
245 i18nc("@action:inmenu",
246 "Open Path in New Tab"),
248 addAction(openParentInNewTabAction
);
251 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
252 // insert 'Open in new window' and 'Open in new tab' entries
253 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
254 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
259 bool selectionHasOnlyDirs
= true;
260 foreach (const KFileItem
& item
, m_selectedItems
) {
261 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
263 selectionHasOnlyDirs
= false;
268 if (selectionHasOnlyDirs
) {
269 // insert 'Open in new tab' entry
270 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
275 insertDefaultItemActions(selectedItemsProps
);
279 KFileItemActions fileItemActions
;
280 fileItemActions
.setItemListProperties(selectedItemsProps
);
281 addServiceActions(fileItemActions
);
283 addFileItemPluginActions();
285 addVersionControlPluginActions();
287 // insert 'Copy To' and 'Move To' sub menus
288 if (GeneralSettings::showCopyMoveMenu()) {
289 m_copyToMenu
.setItems(m_selectedItems
);
290 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
291 m_copyToMenu
.addActionsTo(this);
294 // insert 'Properties...' entry
295 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
296 addAction(propertiesAction
);
298 QAction
* activatedAction
= exec(m_pos
);
299 if (activatedAction
) {
300 if (activatedAction
== addToPlacesAction
) {
301 const QUrl
selectedUrl(m_fileInfo
.url());
302 if (selectedUrl
.isValid()) {
303 PlacesItemModel model
;
304 const QString text
= selectedUrl
.fileName();
305 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
306 model
.appendItemToGroup(item
);
308 } else if (activatedAction
== openParentAction
) {
309 m_command
= OpenParentFolder
;
310 } else if (activatedAction
== openParentInNewWindowAction
) {
311 m_command
= OpenParentFolderInNewWindow
;
312 } else if (activatedAction
== openParentInNewTabAction
) {
313 m_command
= OpenParentFolderInNewTab
;
318 void DolphinContextMenu::openViewportContextMenu()
320 // setup 'Create New' menu
321 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
322 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
323 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
324 newFileMenu
->checkUpToDate();
325 newFileMenu
->setPopupFiles(m_baseUrl
);
326 addMenu(newFileMenu
->menu());
329 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
330 // "open_in_new_tab" here, as the current selection should get ignored.
331 addAction(m_mainWindow
->actionCollection()->action("new_window"));
332 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
334 // Insert 'Add to Places' entry if exactly one item is selected
335 QAction
* addToPlacesAction
= 0;
336 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
337 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
338 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
343 QAction
* pasteAction
= createPasteAction();
344 addAction(pasteAction
);
347 // Insert service actions
348 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
349 KFileItemActions fileItemActions
;
350 fileItemActions
.setItemListProperties(baseUrlProperties
);
351 addServiceActions(fileItemActions
);
353 addFileItemPluginActions();
355 addVersionControlPluginActions();
359 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
360 addAction(propertiesAction
);
362 addShowMenuBarAction();
364 QAction
* action
= exec(m_pos
);
365 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
366 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
367 if (container
->url().isValid()) {
368 PlacesItemModel model
;
369 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
371 model
.appendItemToGroup(item
);
376 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
378 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
380 // Insert 'Cut', 'Copy' and 'Paste'
381 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
382 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
383 addAction(createPasteAction());
388 QAction
* renameAction
= collection
->action("rename");
389 addAction(renameAction
);
391 // Insert 'Move to Trash' and/or 'Delete'
392 if (properties
.supportsDeleting()) {
393 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
394 !properties
.isLocal());
395 const bool showMoveToTrashAction
= (properties
.isLocal() &&
396 properties
.supportsMoving());
398 if (showDeleteAction
&& showMoveToTrashAction
) {
399 delete m_removeAction
;
401 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
402 addAction(m_mainWindow
->actionCollection()->action("delete"));
403 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
404 addAction(m_mainWindow
->actionCollection()->action("delete"));
406 if (!m_removeAction
) {
407 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
409 addAction(m_removeAction
);
410 m_removeAction
->update();
415 void DolphinContextMenu::addShowMenuBarAction()
417 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
418 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
419 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
421 addAction(showMenuBar
);
425 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
427 PlacesItemModel model
;
429 const int count
= model
.count();
430 for (int i
= 0; i
< count
; ++i
) {
431 const QUrl placeUrl
= model
.placesItem(i
)->url();
432 if (placeUrl
.matches(url
, QUrl::StripTrailingSlash
)) {
440 QAction
* DolphinContextMenu::createPasteAction()
443 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
444 if (isDir
&& (m_selectedItems
.count() == 1)) {
445 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
447 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
448 action
= new QAction(QIcon::fromTheme("edit-paste"), text
, this);
449 action
->setEnabled(canPaste
);
450 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
452 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
458 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
460 if (!m_selectedItemsProperties
) {
461 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
463 return *m_selectedItemsProperties
;
466 KFileItem
DolphinContextMenu::baseFileItem()
468 if (!m_baseFileItem
) {
469 m_baseFileItem
= new KFileItem(m_baseUrl
);
471 return *m_baseFileItem
;
474 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
476 fileItemActions
.setParentWidget(m_mainWindow
);
478 // insert 'Open With...' action or sub menu
479 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
481 // insert 'Actions' sub menu
482 fileItemActions
.addServiceActionsTo(this);
485 void DolphinContextMenu::addFileItemPluginActions()
487 KFileItemListProperties props
;
488 if (m_selectedItems
.isEmpty()) {
489 props
.setItems(KFileItemList() << baseFileItem());
491 props
= selectedItemsProperties();
494 QString commonMimeType
= props
.mimeType();
495 if (commonMimeType
.isEmpty()) {
496 commonMimeType
= QLatin1String("application/octet-stream");
499 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
500 if (pluginServices
.isEmpty()) {
504 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
505 const KConfigGroup showGroup
= config
.group("Show");
507 foreach (const KService::Ptr
& service
, pluginServices
) {
508 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
509 // The plugin has been disabled
513 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
514 if (abstractPlugin
) {
515 abstractPlugin
->setParent(this);
516 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
521 void DolphinContextMenu::addVersionControlPluginActions()
523 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
524 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
525 if (!versionControlActions
.isEmpty()) {
526 addActions(versionControlActions
);
531 void DolphinContextMenu::addCustomActions()
533 addActions(m_customActions
);