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>
38 #include <KJobWidgets>
39 #include <KMimeTypeTrader>
40 #include <KNewFileMenu>
41 #include <konq_operations.h>
43 #include <KLocalizedString>
44 #include <KStandardAction>
47 #include <QApplication>
53 #include <panels/places/placesitem.h>
54 #include <panels/places/placesitemmodel.h>
57 #include "views/dolphinview.h"
58 #include "views/viewmodecontroller.h"
60 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
62 const KFileItem
& fileInfo
,
63 const QUrl
& baseUrl
) :
71 m_selectedItemsProperties(0),
78 // The context menu either accesses the URLs of the selected items
79 // or the items itself. To increase the performance both lists are cached.
80 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
81 m_selectedItems
= view
->selectedItems();
84 DolphinContextMenu::~DolphinContextMenu()
86 delete m_selectedItemsProperties
;
87 m_selectedItemsProperties
= 0;
90 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
92 m_customActions
= actions
;
95 DolphinContextMenu::Command
DolphinContextMenu::open()
97 // get the context information
98 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
99 m_context
|= TrashContext
;
102 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
103 m_context
|= ItemContext
;
104 // TODO: handle other use cases like devices + desktop files
107 // open the corresponding popup for the context
108 if (m_context
& TrashContext
) {
109 if (m_context
& ItemContext
) {
110 openTrashItemContextMenu();
112 openTrashContextMenu();
114 } else if (m_context
& ItemContext
) {
115 openItemContextMenu();
117 Q_ASSERT(m_context
== NoContext
);
118 openViewportContextMenu();
124 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
126 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
127 m_removeAction
->update();
129 QMenu::keyPressEvent(ev
);
132 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
134 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
135 m_removeAction
->update();
137 QMenu::keyReleaseEvent(ev
);
140 void DolphinContextMenu::openTrashContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
144 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
145 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
146 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
147 addAction(emptyTrashAction
);
151 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
152 addAction(propertiesAction
);
154 addShowMenuBarAction();
156 if (exec(m_pos
) == emptyTrashAction
) {
157 KIO::JobUiDelegate uiDelegate
;
158 uiDelegate
.setWindow(m_mainWindow
);
159 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
160 KIO::Job
* job
= KIO::emptyTrash();
161 KJobWidgets::setWindow(job
, m_mainWindow
);
162 job
->ui()->setAutoErrorHandlingEnabled(true);
167 void DolphinContextMenu::openTrashItemContextMenu()
169 Q_ASSERT(m_context
& TrashContext
);
170 Q_ASSERT(m_context
& ItemContext
);
172 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
173 addAction(restoreAction
);
175 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
176 addAction(deleteAction
);
178 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
179 addAction(propertiesAction
);
181 if (exec(m_pos
) == restoreAction
) {
182 QList
<QUrl
> selectedUrls
;
183 foreach (const KFileItem
&item
, m_selectedItems
) {
184 selectedUrls
.append(item
.url());
187 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
188 KJobWidgets::setWindow(job
, m_mainWindow
);
189 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
193 void DolphinContextMenu::openItemContextMenu()
195 Q_ASSERT(!m_fileInfo
.isNull());
197 QAction
* openParentAction
= 0;
198 QAction
* openParentInNewWindowAction
= 0;
199 QAction
* openParentInNewTabAction
= 0;
200 QAction
* addToPlacesAction
= 0;
201 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
203 if (m_selectedItems
.count() == 1) {
204 if (m_fileInfo
.isDir()) {
205 // setup 'Create New' menu
206 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
207 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
208 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
209 newFileMenu
->checkUpToDate();
210 newFileMenu
->setPopupFiles(m_fileInfo
.url());
211 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
212 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
213 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
215 QMenu
* menu
= newFileMenu
->menu();
216 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
217 menu
->setIcon(QIcon::fromTheme("document-new"));
221 // insert 'Open in new window' and 'Open in new tab' entries
222 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
223 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
225 // insert 'Add to Places' entry
226 if (!placeExists(m_fileInfo
.url())) {
227 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
228 i18nc("@action:inmenu Add selected folder to places",
233 } else if (m_baseUrl
.scheme().contains("search") || m_baseUrl
.scheme().contains("timeline")) {
234 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
235 i18nc("@action:inmenu",
238 addAction(openParentAction
);
240 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
241 i18nc("@action:inmenu",
242 "Open Path in New Window"),
244 addAction(openParentInNewWindowAction
);
246 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
247 i18nc("@action:inmenu",
248 "Open Path in New Tab"),
250 addAction(openParentInNewTabAction
);
253 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
254 // insert 'Open in new window' and 'Open in new tab' entries
255 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
256 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
261 bool selectionHasOnlyDirs
= true;
262 foreach (const KFileItem
& item
, m_selectedItems
) {
263 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
265 selectionHasOnlyDirs
= false;
270 if (selectionHasOnlyDirs
) {
271 // insert 'Open in new tab' entry
272 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
277 insertDefaultItemActions(selectedItemsProps
);
281 KFileItemActions fileItemActions
;
282 fileItemActions
.setItemListProperties(selectedItemsProps
);
283 addServiceActions(fileItemActions
);
285 addFileItemPluginActions();
287 addVersionControlPluginActions();
289 // insert 'Copy To' and 'Move To' sub menus
290 if (GeneralSettings::showCopyMoveMenu()) {
291 m_copyToMenu
.setItems(m_selectedItems
);
292 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
293 m_copyToMenu
.addActionsTo(this);
296 // insert 'Properties...' entry
297 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
298 addAction(propertiesAction
);
300 QAction
* activatedAction
= exec(m_pos
);
301 if (activatedAction
) {
302 if (activatedAction
== addToPlacesAction
) {
303 const QUrl
selectedUrl(m_fileInfo
.url());
304 if (selectedUrl
.isValid()) {
305 PlacesItemModel model
;
306 const QString text
= selectedUrl
.fileName();
307 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
308 model
.appendItemToGroup(item
);
310 } else if (activatedAction
== openParentAction
) {
311 m_command
= OpenParentFolder
;
312 } else if (activatedAction
== openParentInNewWindowAction
) {
313 m_command
= OpenParentFolderInNewWindow
;
314 } else if (activatedAction
== openParentInNewTabAction
) {
315 m_command
= OpenParentFolderInNewTab
;
320 void DolphinContextMenu::openViewportContextMenu()
322 // setup 'Create New' menu
323 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
324 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
325 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
326 newFileMenu
->checkUpToDate();
327 newFileMenu
->setPopupFiles(m_baseUrl
);
328 addMenu(newFileMenu
->menu());
331 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
332 // "open_in_new_tab" here, as the current selection should get ignored.
333 addAction(m_mainWindow
->actionCollection()->action("new_window"));
334 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
336 // Insert 'Add to Places' entry if exactly one item is selected
337 QAction
* addToPlacesAction
= 0;
338 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
339 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
340 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
345 QAction
* pasteAction
= createPasteAction();
346 addAction(pasteAction
);
349 // Insert service actions
350 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
351 KFileItemActions fileItemActions
;
352 fileItemActions
.setItemListProperties(baseUrlProperties
);
353 addServiceActions(fileItemActions
);
355 addFileItemPluginActions();
357 addVersionControlPluginActions();
361 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
362 addAction(propertiesAction
);
364 addShowMenuBarAction();
366 QAction
* action
= exec(m_pos
);
367 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
368 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
369 if (container
->url().isValid()) {
370 PlacesItemModel model
;
371 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
373 model
.appendItemToGroup(item
);
378 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
380 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
382 // Insert 'Cut', 'Copy' and 'Paste'
383 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
384 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
385 addAction(createPasteAction());
390 QAction
* renameAction
= collection
->action("rename");
391 addAction(renameAction
);
393 // Insert 'Move to Trash' and/or 'Delete'
394 if (properties
.supportsDeleting()) {
395 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
396 !properties
.isLocal());
397 const bool showMoveToTrashAction
= (properties
.isLocal() &&
398 properties
.supportsMoving());
400 if (showDeleteAction
&& showMoveToTrashAction
) {
401 delete m_removeAction
;
403 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
404 addAction(m_mainWindow
->actionCollection()->action("delete"));
405 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
406 addAction(m_mainWindow
->actionCollection()->action("delete"));
408 if (!m_removeAction
) {
409 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
411 addAction(m_removeAction
);
412 m_removeAction
->update();
417 void DolphinContextMenu::addShowMenuBarAction()
419 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
420 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
421 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
423 addAction(showMenuBar
);
427 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
429 PlacesItemModel model
;
431 const int count
= model
.count();
432 for (int i
= 0; i
< count
; ++i
) {
433 const QUrl placeUrl
= model
.placesItem(i
)->url();
434 if (placeUrl
.matches(url
, QUrl::StripTrailingSlash
)) {
442 QAction
* DolphinContextMenu::createPasteAction()
445 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
446 if (isDir
&& (m_selectedItems
.count() == 1)) {
447 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
449 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
450 action
= new QAction(QIcon::fromTheme("edit-paste"), text
, this);
451 action
->setEnabled(canPaste
);
452 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
454 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
460 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
462 if (!m_selectedItemsProperties
) {
463 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
465 return *m_selectedItemsProperties
;
468 KFileItem
DolphinContextMenu::baseFileItem()
470 if (!m_baseFileItem
) {
471 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
473 return *m_baseFileItem
;
476 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
478 fileItemActions
.setParentWidget(m_mainWindow
);
480 // insert 'Open With...' action or sub menu
481 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
483 // insert 'Actions' sub menu
484 fileItemActions
.addServiceActionsTo(this);
487 void DolphinContextMenu::addFileItemPluginActions()
489 KFileItemListProperties props
;
490 if (m_selectedItems
.isEmpty()) {
491 props
.setItems(KFileItemList() << baseFileItem());
493 props
= selectedItemsProperties();
496 QString commonMimeType
= props
.mimeType();
497 if (commonMimeType
.isEmpty()) {
498 commonMimeType
= QLatin1String("application/octet-stream");
501 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
502 if (pluginServices
.isEmpty()) {
506 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
507 const KConfigGroup showGroup
= config
.group("Show");
509 foreach (const KService::Ptr
& service
, pluginServices
) {
510 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
511 // The plugin has been disabled
515 // Old API (kdelibs-4.6.0 only)
516 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
518 plugin
->setParent(this);
519 addActions(plugin
->actions(props
, m_mainWindow
));
521 // New API (kdelibs >= 4.6.1)
522 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
523 if (abstractPlugin
) {
524 abstractPlugin
->setParent(this);
525 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
530 void DolphinContextMenu::addVersionControlPluginActions()
532 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
533 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
534 if (!versionControlActions
.isEmpty()) {
535 foreach (QAction
* action
, versionControlActions
) {
542 void DolphinContextMenu::addCustomActions()
544 foreach (QAction
* action
, m_customActions
) {