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),
80 // The context menu either accesses the URLs of the selected items
81 // or the items itself. To increase the performance both lists are cached.
82 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
83 m_selectedItems
= view
->selectedItems();
86 DolphinContextMenu::~DolphinContextMenu()
88 delete m_selectedItemsProperties
;
89 m_selectedItemsProperties
= 0;
92 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
94 m_customActions
= actions
;
97 DolphinContextMenu::Command
DolphinContextMenu::open()
99 // get the context information
100 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
101 m_context
|= TrashContext
;
104 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
105 m_context
|= ItemContext
;
106 // TODO: handle other use cases like devices + desktop files
109 // open the corresponding popup for the context
110 if (m_context
& TrashContext
) {
111 if (m_context
& ItemContext
) {
112 openTrashItemContextMenu();
114 openTrashContextMenu();
116 } else if (m_context
& ItemContext
) {
117 openItemContextMenu();
119 Q_ASSERT(m_context
== NoContext
);
120 openViewportContextMenu();
126 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
128 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
129 m_removeAction
->update();
131 KMenu::keyPressEvent(ev
);
134 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
136 if (m_removeAction
&& ev
->key() == Qt::Key_Shift
) {
137 m_removeAction
->update();
139 KMenu::keyReleaseEvent(ev
);
142 void DolphinContextMenu::openTrashContextMenu()
144 Q_ASSERT(m_context
& TrashContext
);
146 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
147 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
148 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
149 addAction(emptyTrashAction
);
153 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
154 addAction(propertiesAction
);
156 addShowMenuBarAction();
158 if (exec(m_pos
) == emptyTrashAction
) {
159 KonqOperations::emptyTrash(m_mainWindow
);
163 void DolphinContextMenu::openTrashItemContextMenu()
165 Q_ASSERT(m_context
& TrashContext
);
166 Q_ASSERT(m_context
& ItemContext
);
168 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
169 addAction(restoreAction
);
171 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
172 addAction(deleteAction
);
174 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
175 addAction(propertiesAction
);
177 if (exec(m_pos
) == restoreAction
) {
178 KUrl::List selectedUrls
;
179 foreach (const KFileItem
&item
, m_selectedItems
) {
180 selectedUrls
.append(item
.url());
183 KonqOperations::restoreTrashedItems(selectedUrls
, m_mainWindow
);
187 void DolphinContextMenu::openItemContextMenu()
189 Q_ASSERT(!m_fileInfo
.isNull());
191 QAction
* openParentInNewWindowAction
= 0;
192 QAction
* openParentInNewTabAction
= 0;
193 QAction
* addToPlacesAction
= 0;
194 const KFileItemListProperties
& selectedItemsProps
= selectedItemsProperties();
196 if (m_selectedItems
.count() == 1) {
197 if (m_fileInfo
.isDir()) {
198 // setup 'Create New' menu
199 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
->actionCollection(), m_mainWindow
);
200 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
201 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
202 newFileMenu
->checkUpToDate();
203 newFileMenu
->setPopupFiles(m_fileInfo
.url());
204 newFileMenu
->setEnabled(selectedItemsProps
.supportsWriting());
205 connect(newFileMenu
, SIGNAL(fileCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
206 connect(newFileMenu
, SIGNAL(directoryCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
208 KMenu
* menu
= newFileMenu
->menu();
209 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
210 menu
->setIcon(KIcon("document-new"));
214 // insert 'Open in new window' and 'Open in new tab' entries
215 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
216 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
218 // insert 'Add to Places' entry
219 if (!placeExists(m_fileInfo
.url())) {
220 addToPlacesAction
= addAction(KIcon("bookmark-new"),
221 i18nc("@action:inmenu Add selected folder to places",
226 } else if (m_baseUrl
.protocol().contains("search")) {
227 openParentInNewWindowAction
= new QAction(KIcon("window-new"),
228 i18nc("@action:inmenu",
229 "Open Path in New Window"),
231 addAction(openParentInNewWindowAction
);
233 openParentInNewTabAction
= new QAction(KIcon("tab-new"),
234 i18nc("@action:inmenu",
235 "Open Path in New Tab"),
237 addAction(openParentInNewTabAction
);
240 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo
).isEmpty()) {
241 // insert 'Open in new window' and 'Open in new tab' entries
242 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
243 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
248 bool selectionHasOnlyDirs
= true;
249 foreach (const KFileItem
& item
, m_selectedItems
) {
250 const KUrl
& url
= DolphinView::openItemAsFolderUrl(item
);
252 selectionHasOnlyDirs
= false;
257 if (selectionHasOnlyDirs
) {
258 // insert 'Open in new tab' entry
259 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tabs"));
264 insertDefaultItemActions(selectedItemsProps
);
268 KFileItemActions fileItemActions
;
269 fileItemActions
.setItemListProperties(selectedItemsProps
);
270 addServiceActions(fileItemActions
);
272 addFileItemPluginActions();
274 addVersionControlPluginActions();
276 // insert 'Copy To' and 'Move To' sub menus
277 if (GeneralSettings::showCopyMoveMenu()) {
278 m_copyToMenu
.setItems(m_selectedItems
);
279 m_copyToMenu
.setReadOnly(!selectedItemsProps
.supportsWriting());
280 m_copyToMenu
.addActionsTo(this);
283 // insert 'Properties...' entry
284 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
285 addAction(propertiesAction
);
287 QAction
* activatedAction
= exec(m_pos
);
288 if (activatedAction
) {
289 if (activatedAction
== addToPlacesAction
) {
290 const KUrl
selectedUrl(m_fileInfo
.url());
291 if (selectedUrl
.isValid()) {
292 PlacesItemModel model
;
293 const QString text
= selectedUrl
.fileName();
294 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
295 model
.appendItemToGroup(item
);
297 } else if (activatedAction
== openParentInNewWindowAction
) {
298 m_command
= OpenParentFolderInNewWindow
;
299 } else if (activatedAction
== openParentInNewTabAction
) {
300 m_command
= OpenParentFolderInNewTab
;
305 void DolphinContextMenu::openViewportContextMenu()
307 // setup 'Create New' menu
308 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
309 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
310 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
311 newFileMenu
->checkUpToDate();
312 newFileMenu
->setPopupFiles(m_baseUrl
);
313 addMenu(newFileMenu
->menu());
316 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
317 // "open_in_new_tab" here, as the current selection should get ignored.
318 addAction(m_mainWindow
->actionCollection()->action("new_window"));
319 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
321 // Insert 'Add to Places' entry if exactly one item is selected
322 QAction
* addToPlacesAction
= 0;
323 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
324 addToPlacesAction
= addAction(KIcon("bookmark-new"),
325 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
330 QAction
* pasteAction
= createPasteAction();
331 addAction(pasteAction
);
334 // Insert service actions
335 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
336 KFileItemActions fileItemActions
;
337 fileItemActions
.setItemListProperties(baseUrlProperties
);
338 addServiceActions(fileItemActions
);
340 addFileItemPluginActions();
342 addVersionControlPluginActions();
346 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
347 addAction(propertiesAction
);
349 addShowMenuBarAction();
351 QAction
* action
= exec(m_pos
);
352 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
353 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
354 if (container
->url().isValid()) {
355 PlacesItemModel model
;
356 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
358 model
.appendItemToGroup(item
);
363 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties
& properties
)
365 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
367 // Insert 'Cut', 'Copy' and 'Paste'
368 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
369 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
370 addAction(createPasteAction());
375 QAction
* renameAction
= collection
->action("rename");
376 addAction(renameAction
);
378 // Insert 'Move to Trash' and/or 'Delete'
379 if (properties
.supportsDeleting()) {
380 const bool showDeleteAction
= (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false) ||
381 !properties
.isLocal());
382 const bool showMoveToTrashAction
= (properties
.isLocal() &&
383 properties
.supportsMoving());
385 if (showDeleteAction
&& showMoveToTrashAction
) {
386 delete m_removeAction
;
388 addAction(m_mainWindow
->actionCollection()->action("move_to_trash"));
389 addAction(m_mainWindow
->actionCollection()->action("delete"));
390 } else if (showDeleteAction
&& !showMoveToTrashAction
) {
391 addAction(m_mainWindow
->actionCollection()->action("delete"));
393 if (!m_removeAction
) {
394 m_removeAction
= new DolphinRemoveAction(this, m_mainWindow
->actionCollection());
396 addAction(m_removeAction
);
397 m_removeAction
->update();
402 void DolphinContextMenu::addShowMenuBarAction()
404 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
405 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
406 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
408 addAction(showMenuBar
);
412 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
414 PlacesItemModel model
;
416 const int count
= model
.count();
417 for (int i
= 0; i
< count
; ++i
) {
418 const KUrl placeUrl
= model
.placesItem(i
)->url();
419 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
427 QAction
* DolphinContextMenu::createPasteAction()
430 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
431 if (isDir
&& (m_selectedItems
.count() == 1)) {
432 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
433 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
434 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
435 action
->setEnabled(!pasteData
.isEmpty() && selectedItemsProperties().supportsWriting());
436 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
438 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
444 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
446 if (!m_selectedItemsProperties
) {
447 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
449 return *m_selectedItemsProperties
;
452 KFileItem
DolphinContextMenu::baseFileItem()
454 if (!m_baseFileItem
) {
455 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
457 return *m_baseFileItem
;
460 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
462 fileItemActions
.setParentWidget(m_mainWindow
);
464 // insert 'Open With...' action or sub menu
465 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
467 // insert 'Actions' sub menu
468 fileItemActions
.addServiceActionsTo(this);
471 void DolphinContextMenu::addFileItemPluginActions()
473 KFileItemListProperties props
;
474 if (m_selectedItems
.isEmpty()) {
475 props
.setItems(KFileItemList() << baseFileItem());
477 props
= selectedItemsProperties();
480 QString commonMimeType
= props
.mimeType();
481 if (commonMimeType
.isEmpty()) {
482 commonMimeType
= QLatin1String("application/octet-stream");
485 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
486 if (pluginServices
.isEmpty()) {
490 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
491 const KConfigGroup showGroup
= config
.group("Show");
493 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
494 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
495 // The plugin has been disabled
499 // Old API (kdelibs-4.6.0 only)
500 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
502 plugin
->setParent(this);
503 addActions(plugin
->actions(props
, m_mainWindow
));
505 // New API (kdelibs >= 4.6.1)
506 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
507 if (abstractPlugin
) {
508 abstractPlugin
->setParent(this);
509 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
514 void DolphinContextMenu::addVersionControlPluginActions()
516 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
517 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
518 if (!versionControlActions
.isEmpty()) {
519 foreach (QAction
* action
, versionControlActions
) {
526 void DolphinContextMenu::addCustomActions()
528 foreach (QAction
* action
, m_customActions
) {
533 #include "dolphincontextmenu.moc"