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 <KAbstractFileItemActionPlugin>
31 #include <KFileItemActions>
32 #include <KFileItemListProperties>
33 #include <KIO/RestoreJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
37 #include <KJobWidgets>
38 #include <KMimeTypeTrader>
39 #include <KNewFileMenu>
41 #include <KLocalizedString>
42 #include <KStandardAction>
45 #include <QApplication>
51 #include <panels/places/placesitem.h>
52 #include <panels/places/placesitemmodel.h>
55 #include "views/dolphinview.h"
56 #include "views/viewmodecontroller.h"
58 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
60 const KFileItem
& fileInfo
,
61 const QUrl
& baseUrl
) :
69 m_selectedItemsProperties(0),
76 // The context menu either accesses the URLs of the selected items
77 // or the items itself. To increase the performance both lists are cached.
78 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
79 m_selectedItems
= view
->selectedItems();
82 DolphinContextMenu::~DolphinContextMenu()
84 delete m_selectedItemsProperties
;
85 m_selectedItemsProperties
= 0;
88 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
90 m_customActions
= actions
;
93 DolphinContextMenu::Command
DolphinContextMenu::open()
95 // get the context information
96 if (m_baseUrl
.scheme() == QLatin1String("trash")) {
97 m_context
|= TrashContext
;
100 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
101 m_context
|= ItemContext
;
102 // TODO: handle other use cases like devices + desktop files
105 // open the corresponding popup for the context
106 if (m_context
& TrashContext
) {
107 if (m_context
& ItemContext
) {
108 openTrashItemContextMenu();
110 openTrashContextMenu();
112 } else if (m_context
& ItemContext
) {
113 openItemContextMenu();
115 Q_ASSERT(m_context
== NoContext
);
116 openViewportContextMenu();
122 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
124 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
125 m_removeAction
->update();
127 QMenu::keyPressEvent(ev
);
130 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
132 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
133 m_removeAction
->update();
135 QMenu::keyReleaseEvent(ev
);
138 void DolphinContextMenu::openTrashContextMenu()
140 Q_ASSERT(m_context
& TrashContext
);
142 QAction
* emptyTrashAction
= new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
143 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
144 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
145 addAction(emptyTrashAction
);
149 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
150 addAction(propertiesAction
);
152 addShowMenuBarAction();
154 if (exec(m_pos
) == emptyTrashAction
) {
155 KIO::JobUiDelegate uiDelegate
;
156 uiDelegate
.setWindow(m_mainWindow
);
157 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
158 KIO::Job
* job
= KIO::emptyTrash();
159 KJobWidgets::setWindow(job
, m_mainWindow
);
160 job
->ui()->setAutoErrorHandlingEnabled(true);
165 void DolphinContextMenu::openTrashItemContextMenu()
167 Q_ASSERT(m_context
& TrashContext
);
168 Q_ASSERT(m_context
& ItemContext
);
170 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
171 addAction(restoreAction
);
173 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
174 addAction(deleteAction
);
176 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
177 addAction(propertiesAction
);
179 if (exec(m_pos
) == restoreAction
) {
180 QList
<QUrl
> selectedUrls
;
181 foreach (const KFileItem
&item
, m_selectedItems
) {
182 selectedUrls
.append(item
.url());
185 KIO::RestoreJob
*job
= KIO::restoreFromTrash(selectedUrls
);
186 KJobWidgets::setWindow(job
, m_mainWindow
);
187 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
191 void DolphinContextMenu::openItemContextMenu()
193 Q_ASSERT(!m_fileInfo
.isNull());
195 QAction
* openParentAction
= 0;
196 QAction
* openParentInNewWindowAction
= 0;
197 QAction
* openParentInNewTabAction
= 0;
198 QAction
* addToPlacesAction
= 0;
199 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
201 if (m_selectedItems
.count() == 1) {
202 if (m_fileInfo
.isDir()) {
203 // setup 'Create New' menu
204 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
205 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
206 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
207 newFileMenu
->checkUpToDate();
208 newFileMenu
->setPopupFiles(m_fileInfo
.url());
209 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
210 connect(newFileMenu
, &DolphinNewFileMenu::fileCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
211 connect(newFileMenu
, &DolphinNewFileMenu::directoryCreated
, newFileMenu
, &DolphinNewFileMenu::deleteLater
);
213 QMenu
* menu
= newFileMenu
->menu();
214 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
215 menu
->setIcon(QIcon::fromTheme("document-new"));
219 // insert 'Open in new window' and 'Open in new tab' entries
220 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
221 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
223 // insert 'Add to Places' entry
224 if (!placeExists(m_fileInfo
.url())) {
225 addToPlacesAction
= addAction(QIcon::fromTheme("bookmark-new"),
226 i18nc("@action:inmenu Add selected folder to places",
231 } else if (m_baseUrl
.scheme().contains("search") || m_baseUrl
.scheme().contains("timeline")) {
232 openParentAction
= new QAction(QIcon::fromTheme("document-open-folder"),
233 i18nc("@action:inmenu",
236 addAction(openParentAction
);
238 openParentInNewWindowAction
= new QAction(QIcon::fromTheme("window-new"),
239 i18nc("@action:inmenu",
240 "Open Path in New Window"),
242 addAction(openParentInNewWindowAction
);
244 openParentInNewTabAction
= new QAction(QIcon::fromTheme("tab-new"),
245 i18nc("@action:inmenu",
246 "Open Path in New Tab"),
248 addAction(openParentInNewTabAction
);
251 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
252 // insert 'Open in new window' and 'Open in new tab' entries
253 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
254 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
259 bool selectionHasOnlyDirs
= true;
260 foreach (const KFileItem
& item
, m_selectedItems
) {
261 const QUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
263 selectionHasOnlyDirs
= false;
268 if (selectionHasOnlyDirs
) {
269 // insert 'Open in new tab' entry
270 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
275 insertDefaultItemActions(selectedItemsProps
);
279 KFileItemActions fileItemActions
;
280 fileItemActions
.setItemListProperties(selectedItemsProps
);
281 addServiceActions(fileItemActions
);
283 addFileItemPluginActions();
285 addVersionControlPluginActions();
287 // insert 'Copy To' and 'Move To' sub menus
288 if (GeneralSettings::showCopyMoveMenu()) {
289 m_copyToMenu
.setUrls(m_selectedItems
.urlList());
290 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
291 m_copyToMenu
.setAutoErrorHandlingEnabled(true);
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 QUrl
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
);
308 model
.saveBookmarks();
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
);
374 model
.saveBookmarks();
379 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
381 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
383 // Insert 'Cut', 'Copy' and 'Paste'
384 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
385 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
386 addAction(createPasteAction());
391 QAction
* renameAction
= collection
->action("rename");
392 addAction(renameAction
);
394 // Insert 'Move to Trash' and/or 'Delete'
395 if (properties
.supportsDeleting()) {
396 const bool showDeleteAction
= (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
397 !properties
.isLocal());
398 const bool showMoveToTrashAction
= (properties
.isLocal() &&
399 properties
.supportsMoving());
401 if (showDeleteAction
&& showMoveToTrashAction
) {
402 delete m_removeAction
;
404 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
405 addAction(m_mainWindow
->actionCollection()->action("delete"));
406 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
407 addAction(m_mainWindow
->actionCollection()->action("delete"));
409 if (!m_removeAction
) {
410 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
412 addAction(m_removeAction
);
413 m_removeAction
->update();
418 void DolphinContextMenu::addShowMenuBarAction()
420 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
421 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
422 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
424 addAction(showMenuBar
);
428 bool DolphinContextMenu::placeExists(const QUrl
& url
) const
430 PlacesItemModel model
;
432 const int count
= model
.count();
433 for (int i
= 0; i
< count
; ++i
) {
434 const QUrl placeUrl
= model
.placesItem(i
)->url();
435 if (placeUrl
.matches(url
, QUrl::StripTrailingSlash
)) {
443 QAction
* DolphinContextMenu::createPasteAction()
446 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
447 if (isDir
&& (m_selectedItems
.count() == 1)) {
448 const QMimeData
*mimeData
= QApplication::clipboard()->mimeData();
450 const QString text
= KIO::pasteActionText(mimeData
, &canPaste
, m_fileInfo
);
451 action
= new QAction(QIcon::fromTheme("edit-paste"), text
, this);
452 action
->setEnabled(canPaste
);
453 connect(action
, &QAction::triggered
, m_mainWindow
, &DolphinMainWindow::pasteIntoFolder
);
455 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
461 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
463 if (!m_selectedItemsProperties
) {
464 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
466 return *m_selectedItemsProperties
;
469 KFileItem
DolphinContextMenu::baseFileItem()
471 if (!m_baseFileItem
) {
472 m_baseFileItem
= new KFileItem(m_baseUrl
);
474 return *m_baseFileItem
;
477 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
479 fileItemActions
.setParentWidget(m_mainWindow
);
481 // insert 'Open With...' action or sub menu
482 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
484 // insert 'Actions' sub menu
485 fileItemActions
.addServiceActionsTo(this);
488 void DolphinContextMenu::addFileItemPluginActions()
490 KFileItemListProperties props
;
491 if (m_selectedItems
.isEmpty()) {
492 props
.setItems(KFileItemList() << baseFileItem());
494 props
= selectedItemsProperties();
497 QString commonMimeType
= props
.mimeType();
498 if (commonMimeType
.isEmpty()) {
499 commonMimeType
= QLatin1String("application/octet-stream");
502 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
503 if (pluginServices
.isEmpty()) {
507 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
508 const KConfigGroup showGroup
= config
.group("Show");
510 foreach (const KService::Ptr
& service
, pluginServices
) {
511 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
512 // The plugin has been disabled
516 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
517 if (abstractPlugin
) {
518 abstractPlugin
->setParent(this);
519 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
524 void DolphinContextMenu::addVersionControlPluginActions()
526 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
527 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
528 if (!versionControlActions
.isEmpty()) {
529 addActions(versionControlActions
);
534 void DolphinContextMenu::addCustomActions()
536 addActions(m_customActions
);