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>
51 #include <KPropertiesDialog>
52 #include <KStandardAction>
53 #include <KStandardDirs>
56 #include <panels/places/placesitem.h>
57 #include <panels/places/placesitemmodel.h>
59 #include <QApplication>
63 #include "views/dolphinview.h"
64 #include "views/viewmodecontroller.h"
66 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
68 const KFileItem
& fileInfo
,
69 const KUrl
& baseUrl
) :
77 m_selectedItemsProperties(0),
84 // The context menu either accesses the URLs of the selected items
85 // or the items itself. To increase the performance both lists are cached.
86 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
87 m_selectedItems
= view
->selectedItems();
90 DolphinContextMenu::~DolphinContextMenu()
92 delete m_selectedItemsProperties
;
93 m_selectedItemsProperties
= 0;
96 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
98 m_customActions
= actions
;
101 DolphinContextMenu::Command
DolphinContextMenu::open()
103 // get the context information
104 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
105 m_context
|= TrashContext
;
108 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
109 m_context
|= ItemContext
;
110 // TODO: handle other use cases like devices + desktop files
113 // open the corresponding popup for the context
114 if (m_context
& TrashContext
) {
115 if (m_context
& ItemContext
) {
116 openTrashItemContextMenu();
118 openTrashContextMenu();
120 } else if (m_context
& ItemContext
) {
121 openItemContextMenu();
123 Q_ASSERT(m_context
== NoContext
);
124 openViewportContextMenu();
130 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
132 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
133 m_removeAction
->update();
135 KMenu::keyPressEvent(ev
);
138 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
140 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
141 m_removeAction
->update();
143 KMenu::keyReleaseEvent(ev
);
146 void DolphinContextMenu::openTrashContextMenu()
148 Q_ASSERT(m_context
& TrashContext
);
150 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
151 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
152 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
153 addAction(emptyTrashAction
);
157 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
158 addAction(propertiesAction
);
160 addShowMenuBarAction();
162 if (exec(m_pos
) == emptyTrashAction
) {
163 KIO::JobUiDelegate uiDelegate
;
164 uiDelegate
.setWindow(m_mainWindow
);
165 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
166 KIO::Job
* job
= KIO::emptyTrash();
167 KJobWidgets::setWindow(job
, m_mainWindow
);
168 job
->ui()->setAutoErrorHandlingEnabled(true);
173 void DolphinContextMenu::openTrashItemContextMenu()
175 Q_ASSERT(m_context
& TrashContext
);
176 Q_ASSERT(m_context
& ItemContext
);
178 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
179 addAction(restoreAction
);
181 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
182 addAction(deleteAction
);
184 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
185 addAction(propertiesAction
);
187 if (exec(m_pos
) == restoreAction
) {
188 KUrl::List selectedUrls
;
189 foreach (const KFileItem
&item
, m_selectedItems
) {
190 selectedUrls
.append(item
.url());
193 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
194 KJobWidgets::setWindow(job
, m_mainWindow
);
195 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
199 void DolphinContextMenu::openItemContextMenu()
201 Q_ASSERT(!m_fileInfo
.isNull());
203 QAction
* openParentAction
= 0;
204 QAction
* openParentInNewWindowAction
= 0;
205 QAction
* openParentInNewTabAction
= 0;
206 QAction
* addToPlacesAction
= 0;
207 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
209 if (m_selectedItems
.count() == 1) {
210 if (m_fileInfo
.isDir()) {
211 // setup 'Create New' menu
212 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
213 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
214 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
215 newFileMenu
->checkUpToDate();
216 newFileMenu
->setPopupFiles(m_fileInfo
.url());
217 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
218 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
219 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
221 QMenu
* menu
= newFileMenu
->menu();
222 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
223 menu
->setIcon(QIcon::fromTheme("document-new"));
227 // insert 'Open in new window' and 'Open in new tab' entries
228 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
229 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
231 // insert 'Add to Places' entry
232 if (!placeExists(m_fileInfo
.url())) {
233 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
234 i18nc("@action:inmenu Add selected folder to places",
239 } else if (m_baseUrl
.protocol().contains("search") || m_baseUrl
.protocol().contains("timeline")) {
240 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
241 i18nc("@action:inmenu",
244 addAction(openParentAction
);
246 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
247 i18nc("@action:inmenu",
248 "Open Path in New Window"),
250 addAction(openParentInNewWindowAction
);
252 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
253 i18nc("@action:inmenu",
254 "Open Path in New Tab"),
256 addAction(openParentInNewTabAction
);
259 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
260 // insert 'Open in new window' and 'Open in new tab' entries
261 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
262 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
267 bool selectionHasOnlyDirs
= true;
268 foreach (const KFileItem
& item
, m_selectedItems
) {
269 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
271 selectionHasOnlyDirs
= false;
276 if (selectionHasOnlyDirs
) {
277 // insert 'Open in new tab' entry
278 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
283 insertDefaultItemActions(selectedItemsProps
);
287 KFileItemActions fileItemActions
;
288 fileItemActions
.setItemListProperties(selectedItemsProps
);
289 addServiceActions(fileItemActions
);
291 addFileItemPluginActions();
293 addVersionControlPluginActions();
295 // insert 'Copy To' and 'Move To' sub menus
296 if (GeneralSettings::showCopyMoveMenu()) {
297 m_copyToMenu
.setItems(m_selectedItems
);
298 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
299 m_copyToMenu
.addActionsTo(this);
302 // insert 'Properties...' entry
303 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
304 addAction(propertiesAction
);
306 QAction
* activatedAction
= exec(m_pos
);
307 if (activatedAction
) {
308 if (activatedAction
== addToPlacesAction
) {
309 const KUrl
selectedUrl(m_fileInfo
.url());
310 if (selectedUrl
.isValid()) {
311 PlacesItemModel model
;
312 const QString text
= selectedUrl
.fileName();
313 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
314 model
.appendItemToGroup(item
);
316 } else if (activatedAction
== openParentAction
) {
317 m_command
= OpenParentFolder
;
318 } else if (activatedAction
== openParentInNewWindowAction
) {
319 m_command
= OpenParentFolderInNewWindow
;
320 } else if (activatedAction
== openParentInNewTabAction
) {
321 m_command
= OpenParentFolderInNewTab
;
326 void DolphinContextMenu::openViewportContextMenu()
328 // setup 'Create New' menu
329 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
330 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
331 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
332 newFileMenu
->checkUpToDate();
333 newFileMenu
->setPopupFiles(m_baseUrl
);
334 addMenu(newFileMenu
->menu());
337 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
338 // "open_in_new_tab" here, as the current selection should get ignored.
339 addAction(m_mainWindow
->actionCollection()->action("new_window"));
340 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
342 // Insert 'Add to Places' entry if exactly one item is selected
343 QAction
* addToPlacesAction
= 0;
344 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
345 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
346 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
351 QAction
* pasteAction
= createPasteAction();
352 addAction(pasteAction
);
355 // Insert service actions
356 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
357 KFileItemActions fileItemActions
;
358 fileItemActions
.setItemListProperties(baseUrlProperties
);
359 addServiceActions(fileItemActions
);
361 addFileItemPluginActions();
363 addVersionControlPluginActions();
367 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
368 addAction(propertiesAction
);
370 addShowMenuBarAction();
372 QAction
* action
= exec(m_pos
);
373 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
374 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
375 if (container
->url().isValid()) {
376 PlacesItemModel model
;
377 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
379 model
.appendItemToGroup(item
);
384 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
386 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
388 // Insert 'Cut', 'Copy' and 'Paste'
389 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
390 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
391 addAction(createPasteAction());
396 QAction
* renameAction
= collection
->action("rename");
397 addAction(renameAction
);
399 // Insert 'Move to Trash' and/or 'Delete'
400 if (properties
.supportsDeleting()) {
401 const bool showDeleteAction
= (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
402 !properties
.isLocal());
403 const bool showMoveToTrashAction
= (properties
.isLocal() &&
404 properties
.supportsMoving());
406 if (showDeleteAction
&& showMoveToTrashAction
) {
407 delete m_removeAction
;
409 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
410 addAction(m_mainWindow
->actionCollection()->action("delete"));
411 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
412 addAction(m_mainWindow
->actionCollection()->action("delete"));
414 if (!m_removeAction
) {
415 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
417 addAction(m_removeAction
);
418 m_removeAction
->update();
423 void DolphinContextMenu::addShowMenuBarAction()
425 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
426 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
427 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
429 addAction(showMenuBar
);
433 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
435 PlacesItemModel model
;
437 const int count
= model
.count();
438 for (int i
= 0; i
< count
; ++i
) {
439 const KUrl placeUrl
= model
.placesItem(i
)->url();
440 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
448 QAction
* DolphinContextMenu::createPasteAction()
451 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
452 if (isDir
&& (m_selectedItems
.count() == 1)) {
453 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileInfo
.url());
454 action
= new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
455 action
->setEnabled(pasteInfo
.first
);
456 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
458 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
464 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
466 if (!m_selectedItemsProperties
) {
467 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
469 return *m_selectedItemsProperties
;
472 KFileItem
DolphinContextMenu::baseFileItem()
474 if (!m_baseFileItem
) {
475 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
477 return *m_baseFileItem
;
480 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
482 fileItemActions
.setParentWidget(m_mainWindow
);
484 // insert 'Open With...' action or sub menu
485 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
487 // insert 'Actions' sub menu
488 fileItemActions
.addServiceActionsTo(this);
491 void DolphinContextMenu::addFileItemPluginActions()
493 KFileItemListProperties props
;
494 if (m_selectedItems
.isEmpty()) {
495 props
.setItems(KFileItemList() << baseFileItem());
497 props
= selectedItemsProperties();
500 QString commonMimeType
= props
.mimeType();
501 if (commonMimeType
.isEmpty()) {
502 commonMimeType
= QLatin1String("application/octet-stream");
505 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
506 if (pluginServices
.isEmpty()) {
510 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
511 const KConfigGroup showGroup
= config
.group("Show");
513 foreach (const KService::Ptr
& service
, pluginServices
) {
514 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
515 // The plugin has been disabled
519 // Old API (kdelibs-4.6.0 only)
520 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
522 plugin
->setParent(this);
523 addActions(plugin
->actions(props
, m_mainWindow
));
525 // New API (kdelibs >= 4.6.1)
526 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
527 if (abstractPlugin
) {
528 abstractPlugin
->setParent(this);
529 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
534 void DolphinContextMenu::addVersionControlPluginActions()
536 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
537 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
538 if (!versionControlActions
.isEmpty()) {
539 foreach (QAction
* action
, versionControlActions
) {
546 void DolphinContextMenu::addCustomActions()
548 foreach (QAction
* action
, m_customActions
) {