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/NetAccess>
40 #include <KMessageBox>
41 #include <KMimeTypeTrader>
42 #include <KNewFileMenu>
43 #include <konqmimedata.h>
44 #include <konq_operations.h>
47 #include <KPropertiesDialog>
48 #include <KStandardAction>
49 #include <KStandardDirs>
52 #include <panels/places/placesitem.h>
53 #include <panels/places/placesitemmodel.h>
55 #include <QApplication>
59 #include "views/dolphinview.h"
60 #include "views/viewmodecontroller.h"
62 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
64 const KFileItem
& fileInfo
,
65 const KUrl
& baseUrl
) :
73 m_selectedItemsProperties(0),
79 // The context menu either accesses the URLs of the selected items
80 // or the items itself. To increase the performance both lists are cached.
81 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
82 m_selectedItems
= view
->selectedItems();
84 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
87 DolphinContextMenu::~DolphinContextMenu()
89 delete m_selectedItemsProperties
;
90 m_selectedItemsProperties
= 0;
93 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
95 m_customActions
= actions
;
98 DolphinContextMenu::Command
DolphinContextMenu::open()
100 // get the context information
101 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
102 m_context
|= TrashContext
;
105 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
106 m_context
|= ItemContext
;
107 // TODO: handle other use cases like devices + desktop files
110 // open the corresponding popup for the context
111 if (m_context
& TrashContext
) {
112 if (m_context
& ItemContext
) {
113 openTrashItemContextMenu();
115 openTrashContextMenu();
117 } else if (m_context
& ItemContext
) {
118 openItemContextMenu();
120 Q_ASSERT(m_context
== NoContext
);
121 openViewportContextMenu();
127 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
129 if (ev
->key() == Qt::Key_Shift
) {
130 m_removeAction
->update();
132 KMenu::keyPressEvent(ev
);
135 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
137 if (ev
->key() == Qt::Key_Shift
) {
138 m_removeAction
->update();
140 KMenu::keyReleaseEvent(ev
);
143 void DolphinContextMenu::openTrashContextMenu()
145 Q_ASSERT(m_context
& TrashContext
);
147 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
148 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
149 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
150 addAction(emptyTrashAction
);
154 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
155 addAction(propertiesAction
);
157 addShowMenuBarAction();
159 if (exec(m_pos
) == emptyTrashAction
) {
160 KonqOperations::emptyTrash(m_mainWindow
);
164 void DolphinContextMenu::openTrashItemContextMenu()
166 Q_ASSERT(m_context
& TrashContext
);
167 Q_ASSERT(m_context
& ItemContext
);
169 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
170 addAction(restoreAction
);
172 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
173 addAction(deleteAction
);
175 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
176 addAction(propertiesAction
);
178 if (exec(m_pos
) == restoreAction
) {
179 KUrl::List selectedUrls
;
180 foreach (const KFileItem
&item
, m_selectedItems
) {
181 selectedUrls
.append(item
.url());
184 KonqOperations::restoreTrashedItems(selectedUrls
, m_mainWindow
);
188 void DolphinContextMenu::openItemContextMenu()
190 Q_ASSERT(!m_fileInfo
.isNull());
192 QAction
* openParentInNewWindowAction
= 0;
193 QAction
* openParentInNewTabAction
= 0;
194 QAction
* addToPlacesAction
= 0;
195 if (m_selectedItems
.count() == 1) {
196 if (m_fileInfo
.isDir()) {
197 // setup 'Create New' menu
198 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
);
199 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
200 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
201 newFileMenu
->checkUpToDate();
202 newFileMenu
->setPopupFiles(m_fileInfo
.url());
203 newFileMenu
->setEnabled(selectedItemsProperties().supportsWriting());
204 connect(newFileMenu
, SIGNAL(fileCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
205 connect(newFileMenu
, SIGNAL(directoryCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
207 KMenu
* menu
= newFileMenu
->menu();
208 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
209 menu
->setIcon(KIcon("document-new"));
213 // insert 'Open in new window' and 'Open in new tab' entries
214 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
215 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
217 // insert 'Add to Places' entry
218 if (!placeExists(m_fileInfo
.url())) {
219 addToPlacesAction
= addAction(KIcon("bookmark-new"),
220 i18nc("@action:inmenu Add selected folder to places",
225 } else if (m_baseUrl
.protocol().contains("search")) {
226 openParentInNewWindowAction
= new QAction(KIcon("window-new"),
227 i18nc("@action:inmenu",
228 "Open Path in New Window"),
230 addAction(openParentInNewWindowAction
);
232 openParentInNewTabAction
= new QAction(KIcon("tab-new"),
233 i18nc("@action:inmenu",
234 "Open Path in New Tab"),
236 addAction(openParentInNewTabAction
);
242 insertDefaultItemActions();
246 KFileItemActions fileItemActions
;
247 fileItemActions
.setItemListProperties(selectedItemsProperties());
248 addServiceActions(fileItemActions
);
250 addFileItemPluginActions();
252 addVersionControlPluginActions();
254 // insert 'Copy To' and 'Move To' sub menus
255 if (GeneralSettings::showCopyMoveMenu()) {
256 m_copyToMenu
.setItems(m_selectedItems
);
257 m_copyToMenu
.setReadOnly(!selectedItemsProperties().supportsWriting());
258 m_copyToMenu
.addActionsTo(this);
261 // insert 'Properties...' entry
262 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
263 addAction(propertiesAction
);
265 QAction
* activatedAction
= exec(m_pos
);
266 if (activatedAction
) {
267 if (activatedAction
== addToPlacesAction
) {
268 const KUrl
selectedUrl(m_fileInfo
.url());
269 if (selectedUrl
.isValid()) {
270 PlacesItemModel model
;
271 const QString text
= selectedUrl
.fileName();
272 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
273 model
.appendItemToGroup(item
);
275 } else if (activatedAction
== openParentInNewWindowAction
) {
276 m_command
= OpenParentFolderInNewWindow
;
277 } else if (activatedAction
== openParentInNewTabAction
) {
278 m_command
= OpenParentFolderInNewTab
;
283 void DolphinContextMenu::openViewportContextMenu()
285 // setup 'Create New' menu
286 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
287 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
288 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
289 newFileMenu
->checkUpToDate();
290 newFileMenu
->setPopupFiles(m_baseUrl
);
291 addMenu(newFileMenu
->menu());
294 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
295 // "open_in_new_tab" here, as the current selection should get ignored.
296 addAction(m_mainWindow
->actionCollection()->action("new_window"));
297 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
299 // Insert 'Add to Places' entry if exactly one item is selected
300 QAction
* addToPlacesAction
= 0;
301 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
302 addToPlacesAction
= addAction(KIcon("bookmark-new"),
303 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
308 QAction
* pasteAction
= createPasteAction();
309 addAction(pasteAction
);
312 // Insert service actions
313 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
314 KFileItemActions fileItemActions
;
315 fileItemActions
.setItemListProperties(baseUrlProperties
);
316 addServiceActions(fileItemActions
);
318 addFileItemPluginActions();
320 addVersionControlPluginActions();
324 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
325 addAction(propertiesAction
);
327 addShowMenuBarAction();
329 QAction
* action
= exec(m_pos
);
330 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
331 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
332 if (container
->url().isValid()) {
333 PlacesItemModel model
;
334 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
336 model
.appendItemToGroup(item
);
341 void DolphinContextMenu::insertDefaultItemActions()
343 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
345 // Insert 'Cut', 'Copy' and 'Paste'
346 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
347 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
348 addAction(createPasteAction());
353 QAction
* renameAction
= collection
->action("rename");
354 addAction(renameAction
);
356 // Insert 'Move to Trash' and/or 'Delete'
357 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
358 addAction(collection
->action("move_to_trash"));
359 addAction(collection
->action("delete"));
361 addAction(m_removeAction
);
362 m_removeAction
->update();
366 void DolphinContextMenu::addShowMenuBarAction()
368 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
369 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
370 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
372 addAction(showMenuBar
);
376 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
378 PlacesItemModel model
;
380 const int count
= model
.count();
381 for (int i
= 0; i
< count
; ++i
) {
382 const KUrl placeUrl
= model
.placesItem(i
)->url();
383 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
391 QAction
* DolphinContextMenu::createPasteAction()
394 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
395 if (isDir
&& (m_selectedItems
.count() == 1)) {
396 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
397 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
398 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
399 action
->setEnabled(!pasteData
.isEmpty() && selectedItemsProperties().supportsWriting());
400 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
402 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
408 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
410 if (!m_selectedItemsProperties
) {
411 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
413 return *m_selectedItemsProperties
;
416 KFileItem
DolphinContextMenu::baseFileItem()
418 if (!m_baseFileItem
) {
419 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
421 return *m_baseFileItem
;
424 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
426 fileItemActions
.setParentWidget(m_mainWindow
);
428 // insert 'Open With...' action or sub menu
429 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
431 // insert 'Actions' sub menu
432 fileItemActions
.addServiceActionsTo(this);
435 void DolphinContextMenu::addFileItemPluginActions()
437 KFileItemListProperties props
;
438 if (m_selectedItems
.isEmpty()) {
439 props
.setItems(KFileItemList() << baseFileItem());
441 props
= selectedItemsProperties();
444 QString commonMimeType
= props
.mimeType();
445 if (commonMimeType
.isEmpty()) {
446 commonMimeType
= QLatin1String("application/octet-stream");
449 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
450 if (pluginServices
.isEmpty()) {
454 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
455 const KConfigGroup showGroup
= config
.group("Show");
457 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
458 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
459 // The plugin has been disabled
463 // Old API (kdelibs-4.6.0 only)
464 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
466 plugin
->setParent(this);
467 addActions(plugin
->actions(props
, m_mainWindow
));
469 // New API (kdelibs >= 4.6.1)
470 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
471 if (abstractPlugin
) {
472 abstractPlugin
->setParent(this);
473 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
478 void DolphinContextMenu::addVersionControlPluginActions()
480 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
481 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
482 if (!versionControlActions
.isEmpty()) {
483 foreach (QAction
* action
, versionControlActions
) {
490 void DolphinContextMenu::addCustomActions()
492 foreach (QAction
* action
, m_customActions
) {
497 #include "dolphincontextmenu.moc"