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
* 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")) {
239 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
240 i18nc("@action:inmenu",
241 "Open Path in New Window"),
243 addAction(openParentInNewWindowAction
);
245 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
246 i18nc("@action:inmenu",
247 "Open Path in New Tab"),
249 addAction(openParentInNewTabAction
);
252 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
253 // insert 'Open in new window' and 'Open in new tab' entries
254 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
255 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
260 bool selectionHasOnlyDirs
= true;
261 foreach (const KFileItem
& item
, m_selectedItems
) {
262 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
264 selectionHasOnlyDirs
= false;
269 if (selectionHasOnlyDirs
) {
270 // insert 'Open in new tab' entry
271 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
276 insertDefaultItemActions(selectedItemsProps
);
280 KFileItemActions fileItemActions
;
281 fileItemActions
.setItemListProperties(selectedItemsProps
);
282 addServiceActions(fileItemActions
);
284 addFileItemPluginActions();
286 addVersionControlPluginActions();
288 // insert 'Copy To' and 'Move To' sub menus
289 if (GeneralSettings::showCopyMoveMenu()) {
290 m_copyToMenu
.setItems(m_selectedItems
);
291 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
292 m_copyToMenu
.addActionsTo(this);
295 // insert 'Properties...' entry
296 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
297 addAction(propertiesAction
);
299 QAction
* activatedAction
= exec(m_pos
);
300 if (activatedAction
) {
301 if (activatedAction
== addToPlacesAction
) {
302 const KUrl
selectedUrl(m_fileInfo
.url());
303 if (selectedUrl
.isValid()) {
304 PlacesItemModel model
;
305 const QString text
= selectedUrl
.fileName();
306 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
307 model
.appendItemToGroup(item
);
309 } else if (activatedAction
== openParentInNewWindowAction
) {
310 m_command
= OpenParentFolderInNewWindow
;
311 } else if (activatedAction
== openParentInNewTabAction
) {
312 m_command
= OpenParentFolderInNewTab
;
317 void DolphinContextMenu::openViewportContextMenu()
319 // setup 'Create New' menu
320 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
321 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
322 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
323 newFileMenu
->checkUpToDate();
324 newFileMenu
->setPopupFiles(m_baseUrl
);
325 addMenu(newFileMenu
->menu());
328 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
329 // "open_in_new_tab" here, as the current selection should get ignored.
330 addAction(m_mainWindow
->actionCollection()->action("new_window"));
331 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
333 // Insert 'Add to Places' entry if exactly one item is selected
334 QAction
* addToPlacesAction
= 0;
335 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
336 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
337 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
342 QAction
* pasteAction
= createPasteAction();
343 addAction(pasteAction
);
346 // Insert service actions
347 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
348 KFileItemActions fileItemActions
;
349 fileItemActions
.setItemListProperties(baseUrlProperties
);
350 addServiceActions(fileItemActions
);
352 addFileItemPluginActions();
354 addVersionControlPluginActions();
358 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
359 addAction(propertiesAction
);
361 addShowMenuBarAction();
363 QAction
* action
= exec(m_pos
);
364 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
365 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
366 if (container
->url().isValid()) {
367 PlacesItemModel model
;
368 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
370 model
.appendItemToGroup(item
);
375 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
377 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
379 // Insert 'Cut', 'Copy' and 'Paste'
380 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
381 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
382 addAction(createPasteAction());
387 QAction
* renameAction
= collection
->action("rename");
388 addAction(renameAction
);
390 // Insert 'Move to Trash' and/or 'Delete'
391 if (properties
.supportsDeleting()) {
392 const bool showDeleteAction
= (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
393 !properties
.isLocal());
394 const bool showMoveToTrashAction
= (properties
.isLocal() &&
395 properties
.supportsMoving());
397 if (showDeleteAction
&& showMoveToTrashAction
) {
398 delete m_removeAction
;
400 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
401 addAction(m_mainWindow
->actionCollection()->action("delete"));
402 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
403 addAction(m_mainWindow
->actionCollection()->action("delete"));
405 if (!m_removeAction
) {
406 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
408 addAction(m_removeAction
);
409 m_removeAction
->update();
414 void DolphinContextMenu::addShowMenuBarAction()
416 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
417 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
418 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
420 addAction(showMenuBar
);
424 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
426 PlacesItemModel model
;
428 const int count
= model
.count();
429 for (int i
= 0; i
< count
; ++i
) {
430 const KUrl placeUrl
= model
.placesItem(i
)->url();
431 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
439 QAction
* DolphinContextMenu::createPasteAction()
442 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
443 if (isDir
&& (m_selectedItems
.count() == 1)) {
444 const QPair
<bool, QString
> pasteInfo
= KonqOperations::pasteInfo(m_fileInfo
.url());
445 action
= new QAction(QIcon::fromTheme("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
446 action
->setEnabled(pasteInfo
.first
);
447 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
449 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
455 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
457 if (!m_selectedItemsProperties
) {
458 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
460 return *m_selectedItemsProperties
;
463 KFileItem
DolphinContextMenu::baseFileItem()
465 if (!m_baseFileItem
) {
466 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
468 return *m_baseFileItem
;
471 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
473 fileItemActions
.setParentWidget(m_mainWindow
);
475 // insert 'Open With...' action or sub menu
476 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
478 // insert 'Actions' sub menu
479 fileItemActions
.addServiceActionsTo(this);
482 void DolphinContextMenu::addFileItemPluginActions()
484 KFileItemListProperties props
;
485 if (m_selectedItems
.isEmpty()) {
486 props
.setItems(KFileItemList() << baseFileItem());
488 props
= selectedItemsProperties();
491 QString commonMimeType
= props
.mimeType();
492 if (commonMimeType
.isEmpty()) {
493 commonMimeType
= QLatin1String("application/octet-stream");
496 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
497 if (pluginServices
.isEmpty()) {
501 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
502 const KConfigGroup showGroup
= config
.group("Show");
504 foreach (const KService::Ptr
& service
, pluginServices
) {
505 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
506 // The plugin has been disabled
510 // Old API (kdelibs-4.6.0 only)
511 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
513 plugin
->setParent(this);
514 addActions(plugin
->actions(props
, m_mainWindow
));
516 // New API (kdelibs >= 4.6.1)
517 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
518 if (abstractPlugin
) {
519 abstractPlugin
->setParent(this);
520 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
525 void DolphinContextMenu::addVersionControlPluginActions()
527 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
528 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
529 if (!versionControlActions
.isEmpty()) {
530 foreach (QAction
* action
, versionControlActions
) {
537 void DolphinContextMenu::addCustomActions()
539 foreach (QAction
* action
, m_customActions
) {