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 <kfileitemactionplugin.h>
31 #include <kabstractfileitemactionplugin.h>
32 #include <KFileItemActions>
33 #include <KFileItemListProperties>
34 #include <KIO/RestoreJob>
35 #include <KIO/EmptyTrashJob>
36 #include <KIO/JobUiDelegate>
37 #include <KJobWidgets>
39 #include <KMimeTypeTrader>
41 #include <KNewFileMenu>
42 #include <konq_operations.h>
44 #include <KLocalizedString>
45 #include <KStandardAction>
49 #include <panels/places/placesitem.h>
50 #include <panels/places/placesitemmodel.h>
53 #include "views/dolphinview.h"
54 #include "views/viewmodecontroller.h"
56 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
58 const KFileItem
& fileInfo
,
59 const KUrl
& baseUrl
) :
67 m_selectedItemsProperties(0),
74 // The context menu either accesses the URLs of the selected items
75 // or the items itself. To increase the performance both lists are cached.
76 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
77 m_selectedItems
= view
->selectedItems();
80 DolphinContextMenu::~DolphinContextMenu()
82 delete m_selectedItemsProperties
;
83 m_selectedItemsProperties
= 0;
86 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
88 m_customActions
= actions
;
91 DolphinContextMenu::Command
DolphinContextMenu::open()
93 // get the context information
94 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
95 m_context
|= TrashContext
;
98 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
99 m_context
|= ItemContext
;
100 // TODO: handle other use cases like devices + desktop files
103 // open the corresponding popup for the context
104 if (m_context
& TrashContext
) {
105 if (m_context
& ItemContext
) {
106 openTrashItemContextMenu();
108 openTrashContextMenu();
110 } else if (m_context
& ItemContext
) {
111 openItemContextMenu();
113 Q_ASSERT(m_context
== NoContext
);
114 openViewportContextMenu();
120 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
122 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
123 m_removeAction
->update();
125 QMenu::keyPressEvent(ev
);
128 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
130 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
131 m_removeAction
->update();
133 QMenu::keyReleaseEvent(ev
);
136 void DolphinContextMenu::openTrashContextMenu()
138 Q_ASSERT(m_context
& TrashContext
);
140 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
141 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
142 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
143 addAction(emptyTrashAction
);
147 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
148 addAction(propertiesAction
);
150 addShowMenuBarAction();
152 if (exec(m_pos
) == emptyTrashAction
) {
153 KIO::JobUiDelegate uiDelegate
;
154 uiDelegate
.setWindow(m_mainWindow
);
155 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
156 KIO::Job
* job
= KIO::emptyTrash();
157 KJobWidgets::setWindow(job
, m_mainWindow
);
158 job
->ui()->setAutoErrorHandlingEnabled(true);
163 void DolphinContextMenu::openTrashItemContextMenu()
165 Q_ASSERT(m_context
& TrashContext
);
166 Q_ASSERT(m_context
& ItemContext
);
168 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
169 addAction(restoreAction
);
171 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
172 addAction(deleteAction
);
174 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
175 addAction(propertiesAction
);
177 if (exec(m_pos
) == restoreAction
) {
178 KUrl::List selectedUrls
;
179 foreach (const KFileItem
&item
, m_selectedItems
) {
180 selectedUrls
.append(item
.url());
183 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
184 KJobWidgets::setWindow(job
, m_mainWindow
);
185 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
189 void DolphinContextMenu::openItemContextMenu()
191 Q_ASSERT(!m_fileInfo
.isNull());
193 QAction
* openParentAction
= 0;
194 QAction
* openParentInNewWindowAction
= 0;
195 QAction
* openParentInNewTabAction
= 0;
196 QAction
* addToPlacesAction
= 0;
197 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
199 if (m_selectedItems
.count() == 1) {
200 if (m_fileInfo
.isDir()) {
201 // setup 'Create New' menu
202 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
203 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
204 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
205 newFileMenu
->checkUpToDate();
206 newFileMenu
->setPopupFiles(m_fileInfo
.url());
207 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
208 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
209 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
211 QMenu
* menu
= newFileMenu
->menu();
212 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
213 menu
->setIcon(QIcon::fromTheme("document-new"));
217 // insert 'Open in new window' and 'Open in new tab' entries
218 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
219 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
221 // insert 'Add to Places' entry
222 if (!placeExists(m_fileInfo
.url())) {
223 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
224 i18nc("@action:inmenu Add selected folder to places",
229 } else if (m_baseUrl
.protocol().contains("search") || m_baseUrl
.protocol().contains("timeline")) {
230 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
231 i18nc("@action:inmenu",
234 addAction(openParentAction
);
236 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
237 i18nc("@action:inmenu",
238 "Open Path in New Window"),
240 addAction(openParentInNewWindowAction
);
242 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
243 i18nc("@action:inmenu",
244 "Open Path in New Tab"),
246 addAction(openParentInNewTabAction
);
249 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
250 // insert 'Open in new window' and 'Open in new tab' entries
251 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
252 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
257 bool selectionHasOnlyDirs
= true;
258 foreach (const KFileItem
& item
, m_selectedItems
) {
259 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
261 selectionHasOnlyDirs
= false;
266 if (selectionHasOnlyDirs
) {
267 // insert 'Open in new tab' entry
268 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
273 insertDefaultItemActions(selectedItemsProps
);
277 KFileItemActions fileItemActions
;
278 fileItemActions
.setItemListProperties(selectedItemsProps
);
279 addServiceActions(fileItemActions
);
281 addFileItemPluginActions();
283 addVersionControlPluginActions();
285 // insert 'Copy To' and 'Move To' sub menus
286 if (GeneralSettings::showCopyMoveMenu()) {
287 m_copyToMenu
.setItems(m_selectedItems
);
288 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
289 m_copyToMenu
.addActionsTo(this);
292 // insert 'Properties...' entry
293 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
294 addAction(propertiesAction
);
296 QAction
* activatedAction
= exec(m_pos
);
297 if (activatedAction
) {
298 if (activatedAction
== addToPlacesAction
) {
299 const KUrl
selectedUrl(m_fileInfo
.url());
300 if (selectedUrl
.isValid()) {
301 PlacesItemModel model
;
302 const QString text
= selectedUrl
.fileName();
303 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
304 model
.appendItemToGroup(item
);
306 } else if (activatedAction
== openParentAction
) {
307 m_command
= OpenParentFolder
;
308 } else if (activatedAction
== openParentInNewWindowAction
) {
309 m_command
= OpenParentFolderInNewWindow
;
310 } else if (activatedAction
== openParentInNewTabAction
) {
311 m_command
= OpenParentFolderInNewTab
;
316 void DolphinContextMenu::openViewportContextMenu()
318 // setup 'Create New' menu
319 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
320 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
321 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
322 newFileMenu
->checkUpToDate();
323 newFileMenu
->setPopupFiles(m_baseUrl
);
324 addMenu(newFileMenu
->menu());
327 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
328 // "open_in_new_tab" here, as the current selection should get ignored.
329 addAction(m_mainWindow
->actionCollection()->action("new_window"));
330 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
332 // Insert 'Add to Places' entry if exactly one item is selected
333 QAction
* addToPlacesAction
= 0;
334 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
335 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
336 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
341 QAction
* pasteAction
= createPasteAction();
342 addAction(pasteAction
);
345 // Insert service actions
346 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
347 KFileItemActions fileItemActions
;
348 fileItemActions
.setItemListProperties(baseUrlProperties
);
349 addServiceActions(fileItemActions
);
351 addFileItemPluginActions();
353 addVersionControlPluginActions();
357 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
358 addAction(propertiesAction
);
360 addShowMenuBarAction();
362 QAction
* action
= exec(m_pos
);
363 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
364 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
365 if (container
->url().isValid()) {
366 PlacesItemModel model
;
367 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
369 model
.appendItemToGroup(item
);
374 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
376 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
378 // Insert 'Cut', 'Copy' and 'Paste'
379 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
380 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
381 addAction(createPasteAction());
386 QAction
* renameAction
= collection
->action("rename");
387 addAction(renameAction
);
389 // Insert 'Move to Trash' and/or 'Delete'
390 if (properties
.supportsDeleting()) {
391 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
392 !properties
.isLocal());
393 const bool showMoveToTrashAction
= (properties
.isLocal() &&
394 properties
.supportsMoving());
396 if (showDeleteAction
&& showMoveToTrashAction
) {
397 delete m_removeAction
;
399 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
400 addAction(m_mainWindow
->actionCollection()->action("delete"));
401 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
402 addAction(m_mainWindow
->actionCollection()->action("delete"));
404 if (!m_removeAction
) {
405 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
407 addAction(m_removeAction
);
408 m_removeAction
->update();
413 void DolphinContextMenu::addShowMenuBarAction()
415 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
416 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
417 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
419 addAction(showMenuBar
);
423 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
425 PlacesItemModel model
;
427 const int count
= model
.count();
428 for (int i
= 0; i
< count
; ++i
) {
429 const KUrl placeUrl
= model
.placesItem(i
)->url();
430 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
438 QAction
* DolphinContextMenu::createPasteAction()
441 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
442 if (isDir
&& (m_selectedItems
.count() == 1)) {
443 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileInfo
.url());
444 action
= new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
445 action
->setEnabled(pasteInfo
.first
);
446 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
448 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
454 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
456 if (!m_selectedItemsProperties
) {
457 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
459 return *m_selectedItemsProperties
;
462 KFileItem
DolphinContextMenu::baseFileItem()
464 if (!m_baseFileItem
) {
465 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
467 return *m_baseFileItem
;
470 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
472 fileItemActions
.setParentWidget(m_mainWindow
);
474 // insert 'Open With...' action or sub menu
475 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
477 // insert 'Actions' sub menu
478 fileItemActions
.addServiceActionsTo(this);
481 void DolphinContextMenu::addFileItemPluginActions()
483 KFileItemListProperties props
;
484 if (m_selectedItems
.isEmpty()) {
485 props
.setItems(KFileItemList() << baseFileItem());
487 props
= selectedItemsProperties();
490 QString commonMimeType
= props
.mimeType();
491 if (commonMimeType
.isEmpty()) {
492 commonMimeType
= QLatin1String("application/octet-stream");
495 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
496 if (pluginServices
.isEmpty()) {
500 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
501 const KConfigGroup showGroup
= config
.group("Show");
503 foreach (const KService::Ptr
& service
, pluginServices
) {
504 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
505 // The plugin has been disabled
509 // Old API (kdelibs-4.6.0 only)
510 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
512 plugin
->setParent(this);
513 addActions(plugin
->actions(props
, m_mainWindow
));
515 // New API (kdelibs >= 4.6.1)
516 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
517 if (abstractPlugin
) {
518 abstractPlugin
->setParent(this);
519 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
524 void DolphinContextMenu::addVersionControlPluginActions()
526 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
527 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
528 if (!versionControlActions
.isEmpty()) {
529 foreach (QAction
* action
, versionControlActions
) {
536 void DolphinContextMenu::addCustomActions()
538 foreach (QAction
* action
, m_customActions
) {