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>
40 #include <KNewFileMenu>
41 #include <konq_operations.h>
43 #include <KLocalizedString>
44 #include <KStandardAction>
48 #include <panels/places/placesitem.h>
49 #include <panels/places/placesitemmodel.h>
52 #include "views/dolphinview.h"
53 #include "views/viewmodecontroller.h"
55 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
57 const KFileItem
& fileInfo
,
58 const QUrl
& baseUrl
) :
66 m_selectedItemsProperties(0),
73 // The context menu either accesses the URLs of the selected items
74 // or the items itself. To increase the performance both lists are cached.
75 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
76 m_selectedItems
= view
->selectedItems();
79 DolphinContextMenu::~DolphinContextMenu()
81 delete m_selectedItemsProperties
;
82 m_selectedItemsProperties
= 0;
85 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
87 m_customActions
= actions
;
90 DolphinContextMenu::Command
DolphinContextMenu::open()
92 // get the context information
93 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
94 m_context
|= TrashContext
;
97 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
98 m_context
|= ItemContext
;
99 // TODO: handle other use cases like devices + desktop files
102 // open the corresponding popup for the context
103 if (m_context
& TrashContext
) {
104 if (m_context
& ItemContext
) {
105 openTrashItemContextMenu();
107 openTrashContextMenu();
109 } else if (m_context
& ItemContext
) {
110 openItemContextMenu();
112 Q_ASSERT(m_context
== NoContext
);
113 openViewportContextMenu();
119 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
121 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
122 m_removeAction
->update();
124 QMenu::keyPressEvent(ev
);
127 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
129 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
130 m_removeAction
->update();
132 QMenu::keyReleaseEvent(ev
);
135 void DolphinContextMenu::openTrashContextMenu()
137 Q_ASSERT(m_context
& TrashContext
);
139 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
140 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
141 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
142 addAction(emptyTrashAction
);
146 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
147 addAction(propertiesAction
);
149 addShowMenuBarAction();
151 if (exec(m_pos
) == emptyTrashAction
) {
152 KIO::JobUiDelegate uiDelegate
;
153 uiDelegate
.setWindow(m_mainWindow
);
154 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
155 KIO::Job
* job
= KIO::emptyTrash();
156 KJobWidgets::setWindow(job
, m_mainWindow
);
157 job
->ui()->setAutoErrorHandlingEnabled(true);
162 void DolphinContextMenu::openTrashItemContextMenu()
164 Q_ASSERT(m_context
& TrashContext
);
165 Q_ASSERT(m_context
& ItemContext
);
167 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
168 addAction(restoreAction
);
170 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
171 addAction(deleteAction
);
173 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
174 addAction(propertiesAction
);
176 if (exec(m_pos
) == restoreAction
) {
177 QList
<QUrl
> selectedUrls
;
178 foreach (const KFileItem
&item
, m_selectedItems
) {
179 selectedUrls
.append(item
.url());
182 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
183 KJobWidgets::setWindow(job
, m_mainWindow
);
184 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
188 void DolphinContextMenu::openItemContextMenu()
190 Q_ASSERT(!m_fileInfo
.isNull());
192 QAction
* openParentAction
= 0;
193 QAction
* openParentInNewWindowAction
= 0;
194 QAction
* openParentInNewTabAction
= 0;
195 QAction
* addToPlacesAction
= 0;
196 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
198 if (m_selectedItems
.count() == 1) {
199 if (m_fileInfo
.isDir()) {
200 // setup 'Create New' menu
201 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
202 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
203 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
204 newFileMenu
->checkUpToDate();
205 newFileMenu
->setPopupFiles(m_fileInfo
.url());
206 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
207 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
208 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
210 QMenu
* menu
= newFileMenu
->menu();
211 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
212 menu
->setIcon(QIcon::fromTheme("document-new"));
216 // insert 'Open in new window' and 'Open in new tab' entries
217 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
218 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
220 // insert 'Add to Places' entry
221 if (!placeExists(m_fileInfo
.url())) {
222 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
223 i18nc("@action:inmenu Add selected folder to places",
228 } else if (m_baseUrl
.scheme().contains("search") || m_baseUrl
.scheme().contains("timeline")) {
229 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
230 i18nc("@action:inmenu",
233 addAction(openParentAction
);
235 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
236 i18nc("@action:inmenu",
237 "Open Path in New Window"),
239 addAction(openParentInNewWindowAction
);
241 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
242 i18nc("@action:inmenu",
243 "Open Path in New Tab"),
245 addAction(openParentInNewTabAction
);
248 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
249 // insert 'Open in new window' and 'Open in new tab' entries
250 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
251 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
256 bool selectionHasOnlyDirs
= true;
257 foreach (const KFileItem
& item
, m_selectedItems
) {
258 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
260 selectionHasOnlyDirs
= false;
265 if (selectionHasOnlyDirs
) {
266 // insert 'Open in new tab' entry
267 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
272 insertDefaultItemActions(selectedItemsProps
);
276 KFileItemActions fileItemActions
;
277 fileItemActions
.setItemListProperties(selectedItemsProps
);
278 addServiceActions(fileItemActions
);
280 addFileItemPluginActions();
282 addVersionControlPluginActions();
284 // insert 'Copy To' and 'Move To' sub menus
285 if (GeneralSettings::showCopyMoveMenu()) {
286 m_copyToMenu
.setItems(m_selectedItems
);
287 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
288 m_copyToMenu
.addActionsTo(this);
291 // insert 'Properties...' entry
292 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
293 addAction(propertiesAction
);
295 QAction
* activatedAction
= exec(m_pos
);
296 if (activatedAction
) {
297 if (activatedAction
== addToPlacesAction
) {
298 const QUrl
selectedUrl(m_fileInfo
.url());
299 if (selectedUrl
.isValid()) {
300 PlacesItemModel model
;
301 const QString text
= selectedUrl
.fileName();
302 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
303 model
.appendItemToGroup(item
);
305 } else if (activatedAction
== openParentAction
) {
306 m_command
= OpenParentFolder
;
307 } else if (activatedAction
== openParentInNewWindowAction
) {
308 m_command
= OpenParentFolderInNewWindow
;
309 } else if (activatedAction
== openParentInNewTabAction
) {
310 m_command
= OpenParentFolderInNewTab
;
315 void DolphinContextMenu::openViewportContextMenu()
317 // setup 'Create New' menu
318 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
319 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
320 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
321 newFileMenu
->checkUpToDate();
322 newFileMenu
->setPopupFiles(m_baseUrl
);
323 addMenu(newFileMenu
->menu());
326 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
327 // "open_in_new_tab" here, as the current selection should get ignored.
328 addAction(m_mainWindow
->actionCollection()->action("new_window"));
329 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
331 // Insert 'Add to Places' entry if exactly one item is selected
332 QAction
* addToPlacesAction
= 0;
333 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
334 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
335 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
340 QAction
* pasteAction
= createPasteAction();
341 addAction(pasteAction
);
344 // Insert service actions
345 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
346 KFileItemActions fileItemActions
;
347 fileItemActions
.setItemListProperties(baseUrlProperties
);
348 addServiceActions(fileItemActions
);
350 addFileItemPluginActions();
352 addVersionControlPluginActions();
356 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
357 addAction(propertiesAction
);
359 addShowMenuBarAction();
361 QAction
* action
= exec(m_pos
);
362 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
363 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
364 if (container
->url().isValid()) {
365 PlacesItemModel model
;
366 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
368 model
.appendItemToGroup(item
);
373 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
375 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
377 // Insert 'Cut', 'Copy' and 'Paste'
378 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
379 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
380 addAction(createPasteAction());
385 QAction
* renameAction
= collection
->action("rename");
386 addAction(renameAction
);
388 // Insert 'Move to Trash' and/or 'Delete'
389 if (properties
.supportsDeleting()) {
390 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
391 !properties
.isLocal());
392 const bool showMoveToTrashAction
= (properties
.isLocal() &&
393 properties
.supportsMoving());
395 if (showDeleteAction
&& showMoveToTrashAction
) {
396 delete m_removeAction
;
398 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
399 addAction(m_mainWindow
->actionCollection()->action("delete"));
400 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
401 addAction(m_mainWindow
->actionCollection()->action("delete"));
403 if (!m_removeAction
) {
404 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
406 addAction(m_removeAction
);
407 m_removeAction
->update();
412 void DolphinContextMenu::addShowMenuBarAction()
414 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
415 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
416 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
418 addAction(showMenuBar
);
422 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
424 PlacesItemModel model
;
426 const int count
= model
.count();
427 for (int i
= 0; i
< count
; ++i
) {
428 const QUrl placeUrl
= model
.placesItem(i
)->url();
429 if (placeUrl
.matches(url
, QUrl::StripTrailingSlash
)) {
437 QAction
* DolphinContextMenu::createPasteAction()
440 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
441 if (isDir
&& (m_selectedItems
.count() == 1)) {
442 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileInfo
.url());
443 action
= new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
444 action
->setEnabled(pasteInfo
.first
);
445 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
447 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
453 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
455 if (!m_selectedItemsProperties
) {
456 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
458 return *m_selectedItemsProperties
;
461 KFileItem
DolphinContextMenu::baseFileItem()
463 if (!m_baseFileItem
) {
464 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
466 return *m_baseFileItem
;
469 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
471 fileItemActions
.setParentWidget(m_mainWindow
);
473 // insert 'Open With...' action or sub menu
474 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
476 // insert 'Actions' sub menu
477 fileItemActions
.addServiceActionsTo(this);
480 void DolphinContextMenu::addFileItemPluginActions()
482 KFileItemListProperties props
;
483 if (m_selectedItems
.isEmpty()) {
484 props
.setItems(KFileItemList() << baseFileItem());
486 props
= selectedItemsProperties();
489 QString commonMimeType
= props
.mimeType();
490 if (commonMimeType
.isEmpty()) {
491 commonMimeType
= QLatin1String("application/octet-stream");
494 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
495 if (pluginServices
.isEmpty()) {
499 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
500 const KConfigGroup showGroup
= config
.group("Show");
502 foreach (const KService::Ptr
& service
, pluginServices
) {
503 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
504 // The plugin has been disabled
508 // Old API (kdelibs-4.6.0 only)
509 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
511 plugin
->setParent(this);
512 addActions(plugin
->actions(props
, m_mainWindow
));
514 // New API (kdelibs >= 4.6.1)
515 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
516 if (abstractPlugin
) {
517 abstractPlugin
->setParent(this);
518 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
523 void DolphinContextMenu::addVersionControlPluginActions()
525 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
526 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
527 if (!versionControlActions
.isEmpty()) {
528 foreach (QAction
* action
, versionControlActions
) {
535 void DolphinContextMenu::addCustomActions()
537 foreach (QAction
* action
, m_customActions
) {