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 <KDesktopFile>
31 #include <kfileitemactionplugin.h>
32 #include <kabstractfileitemactionplugin.h>
33 #include <KFileItemActions>
34 #include <KFileItemListProperties>
36 #include <KIconLoader>
37 #include <KIO/RestoreJob>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
40 #include <KJobUiDelegate>
41 #include <KJobWidgets>
44 #include <KMessageBox>
45 #include <KMimeTypeTrader>
47 #include <KNewFileMenu>
48 #include <konq_operations.h>
50 #include <KLocalizedString>
51 #include <KPropertiesDialog>
52 #include <KStandardAction>
55 #include <panels/places/placesitem.h>
56 #include <panels/places/placesitemmodel.h>
58 #include <QApplication>
62 #include "views/dolphinview.h"
63 #include "views/viewmodecontroller.h"
65 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
67 const KFileItem
& fileInfo
,
68 const KUrl
& baseUrl
) :
76 m_selectedItemsProperties(0),
83 // The context menu either accesses the URLs of the selected items
84 // or the items itself. To increase the performance both lists are cached.
85 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
86 m_selectedItems
= view
->selectedItems();
89 DolphinContextMenu::~DolphinContextMenu()
91 delete m_selectedItemsProperties
;
92 m_selectedItemsProperties
= 0;
95 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
97 m_customActions
= actions
;
100 DolphinContextMenu::Command
DolphinContextMenu::open()
102 // get the context information
103 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
104 m_context
|= TrashContext
;
107 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
108 m_context
|= ItemContext
;
109 // TODO: handle other use cases like devices + desktop files
112 // open the corresponding popup for the context
113 if (m_context
& TrashContext
) {
114 if (m_context
& ItemContext
) {
115 openTrashItemContextMenu();
117 openTrashContextMenu();
119 } else if (m_context
& ItemContext
) {
120 openItemContextMenu();
122 Q_ASSERT(m_context
== NoContext
);
123 openViewportContextMenu();
129 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
131 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
132 m_removeAction
->update();
134 QMenu::keyPressEvent(ev
);
137 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
139 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
140 m_removeAction
->update();
142 QMenu::keyReleaseEvent(ev
);
145 void DolphinContextMenu::openTrashContextMenu()
147 Q_ASSERT(m_context
& TrashContext
);
149 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
150 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
151 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
152 addAction(emptyTrashAction
);
156 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
157 addAction(propertiesAction
);
159 addShowMenuBarAction();
161 if (exec(m_pos
) == emptyTrashAction
) {
162 KIO::JobUiDelegate uiDelegate
;
163 uiDelegate
.setWindow(m_mainWindow
);
164 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
165 KIO::Job
* job
= KIO::emptyTrash();
166 KJobWidgets::setWindow(job
, m_mainWindow
);
167 job
->ui()->setAutoErrorHandlingEnabled(true);
172 void DolphinContextMenu::openTrashItemContextMenu()
174 Q_ASSERT(m_context
& TrashContext
);
175 Q_ASSERT(m_context
& ItemContext
);
177 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
178 addAction(restoreAction
);
180 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
181 addAction(deleteAction
);
183 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
184 addAction(propertiesAction
);
186 if (exec(m_pos
) == restoreAction
) {
187 KUrl::List selectedUrls
;
188 foreach (const KFileItem
&item
, m_selectedItems
) {
189 selectedUrls
.append(item
.url());
192 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
193 KJobWidgets::setWindow(job
, m_mainWindow
);
194 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
198 void DolphinContextMenu::openItemContextMenu()
200 Q_ASSERT(!m_fileInfo
.isNull());
202 QAction
* openParentAction
= 0;
203 QAction
* openParentInNewWindowAction
= 0;
204 QAction
* openParentInNewTabAction
= 0;
205 QAction
* addToPlacesAction
= 0;
206 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
208 if (m_selectedItems
.count() == 1) {
209 if (m_fileInfo
.isDir()) {
210 // setup 'Create New' menu
211 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
212 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
213 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
214 newFileMenu
->checkUpToDate();
215 newFileMenu
->setPopupFiles(m_fileInfo
.url());
216 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
217 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
218 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
220 QMenu
* menu
= newFileMenu
->menu();
221 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
222 menu
->setIcon(QIcon::fromTheme("document-new"));
226 // insert 'Open in new window' and 'Open in new tab' entries
227 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
228 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
230 // insert 'Add to Places' entry
231 if (!placeExists(m_fileInfo
.url())) {
232 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
233 i18nc("@action:inmenu Add selected folder to places",
238 } else if (m_baseUrl
.protocol().contains("search") || m_baseUrl
.protocol().contains("timeline")) {
239 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
240 i18nc("@action:inmenu",
243 addAction(openParentAction
);
245 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
246 i18nc("@action:inmenu",
247 "Open Path in New Window"),
249 addAction(openParentInNewWindowAction
);
251 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
252 i18nc("@action:inmenu",
253 "Open Path in New Tab"),
255 addAction(openParentInNewTabAction
);
258 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
259 // insert 'Open in new window' and 'Open in new tab' entries
260 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
261 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
266 bool selectionHasOnlyDirs
= true;
267 foreach (const KFileItem
& item
, m_selectedItems
) {
268 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
270 selectionHasOnlyDirs
= false;
275 if (selectionHasOnlyDirs
) {
276 // insert 'Open in new tab' entry
277 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
282 insertDefaultItemActions(selectedItemsProps
);
286 KFileItemActions fileItemActions
;
287 fileItemActions
.setItemListProperties(selectedItemsProps
);
288 addServiceActions(fileItemActions
);
290 addFileItemPluginActions();
292 addVersionControlPluginActions();
294 // insert 'Copy To' and 'Move To' sub menus
295 if (GeneralSettings::showCopyMoveMenu()) {
296 m_copyToMenu
.setItems(m_selectedItems
);
297 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
298 m_copyToMenu
.addActionsTo(this);
301 // insert 'Properties...' entry
302 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
303 addAction(propertiesAction
);
305 QAction
* activatedAction
= exec(m_pos
);
306 if (activatedAction
) {
307 if (activatedAction
== addToPlacesAction
) {
308 const KUrl
selectedUrl(m_fileInfo
.url());
309 if (selectedUrl
.isValid()) {
310 PlacesItemModel model
;
311 const QString text
= selectedUrl
.fileName();
312 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
313 model
.appendItemToGroup(item
);
315 } else if (activatedAction
== openParentAction
) {
316 m_command
= OpenParentFolder
;
317 } else if (activatedAction
== openParentInNewWindowAction
) {
318 m_command
= OpenParentFolderInNewWindow
;
319 } else if (activatedAction
== openParentInNewTabAction
) {
320 m_command
= OpenParentFolderInNewTab
;
325 void DolphinContextMenu::openViewportContextMenu()
327 // setup 'Create New' menu
328 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
329 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
330 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
331 newFileMenu
->checkUpToDate();
332 newFileMenu
->setPopupFiles(m_baseUrl
);
333 addMenu(newFileMenu
->menu());
336 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
337 // "open_in_new_tab" here, as the current selection should get ignored.
338 addAction(m_mainWindow
->actionCollection()->action("new_window"));
339 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
341 // Insert 'Add to Places' entry if exactly one item is selected
342 QAction
* addToPlacesAction
= 0;
343 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
344 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
345 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
350 QAction
* pasteAction
= createPasteAction();
351 addAction(pasteAction
);
354 // Insert service actions
355 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
356 KFileItemActions fileItemActions
;
357 fileItemActions
.setItemListProperties(baseUrlProperties
);
358 addServiceActions(fileItemActions
);
360 addFileItemPluginActions();
362 addVersionControlPluginActions();
366 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
367 addAction(propertiesAction
);
369 addShowMenuBarAction();
371 QAction
* action
= exec(m_pos
);
372 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
373 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
374 if (container
->url().isValid()) {
375 PlacesItemModel model
;
376 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
378 model
.appendItemToGroup(item
);
383 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
385 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
387 // Insert 'Cut', 'Copy' and 'Paste'
388 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
389 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
390 addAction(createPasteAction());
395 QAction
* renameAction
= collection
->action("rename");
396 addAction(renameAction
);
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
;
408 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
409 addAction(m_mainWindow
->actionCollection()->action("delete"));
410 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
411 addAction(m_mainWindow
->actionCollection()->action("delete"));
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 KUrl
& url
) const
434 PlacesItemModel model
;
436 const int count
= model
.count();
437 for (int i
= 0; i
< count
; ++i
) {
438 const KUrl placeUrl
= model
.placesItem(i
)->url();
439 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
447 QAction
* DolphinContextMenu::createPasteAction()
450 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
451 if (isDir
&& (m_selectedItems
.count() == 1)) {
452 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileInfo
.url());
453 action
= new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
454 action
->setEnabled(pasteInfo
.first
);
455 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
457 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
463 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
465 if (!m_selectedItemsProperties
) {
466 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
468 return *m_selectedItemsProperties
;
471 KFileItem
DolphinContextMenu::baseFileItem()
473 if (!m_baseFileItem
) {
474 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
476 return *m_baseFileItem
;
479 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
481 fileItemActions
.setParentWidget(m_mainWindow
);
483 // insert 'Open With...' action or sub menu
484 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
486 // insert 'Actions' sub menu
487 fileItemActions
.addServiceActionsTo(this);
490 void DolphinContextMenu::addFileItemPluginActions()
492 KFileItemListProperties props
;
493 if (m_selectedItems
.isEmpty()) {
494 props
.setItems(KFileItemList() << baseFileItem());
496 props
= selectedItemsProperties();
499 QString commonMimeType
= props
.mimeType();
500 if (commonMimeType
.isEmpty()) {
501 commonMimeType
= QLatin1String("application/octet-stream");
504 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
505 if (pluginServices
.isEmpty()) {
509 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
510 const KConfigGroup showGroup
= config
.group("Show");
512 foreach (const KService::Ptr
& service
, pluginServices
) {
513 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
514 // The plugin has been disabled
518 // Old API (kdelibs-4.6.0 only)
519 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
521 plugin
->setParent(this);
522 addActions(plugin
->actions(props
, m_mainWindow
));
524 // New API (kdelibs >= 4.6.1)
525 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
526 if (abstractPlugin
) {
527 abstractPlugin
->setParent(this);
528 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
533 void DolphinContextMenu::addVersionControlPluginActions()
535 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
536 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
537 if (!versionControlActions
.isEmpty()) {
538 foreach (QAction
* action
, versionControlActions
) {
545 void DolphinContextMenu::addCustomActions()
547 foreach (QAction
* action
, m_customActions
) {