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"
28 #include <KActionCollection>
29 #include <KDesktopFile>
30 #include <kfileitemactionplugin.h>
31 #include <kabstractfileitemactionplugin.h>
32 #include <KFileItemActions>
33 #include <KFileItemListProperties>
35 #include <KIconLoader>
36 #include <KIO/NetAccess>
39 #include <KMessageBox>
40 #include <KMimeTypeTrader>
41 #include <KNewFileMenu>
42 #include <konqmimedata.h>
43 #include <konq_operations.h>
46 #include <KPropertiesDialog>
47 #include <KStandardAction>
48 #include <KStandardDirs>
51 #include <panels/places/placesitem.h>
52 #include <panels/places/placesitemmodel.h>
54 #include <QApplication>
58 #include "views/dolphinview.h"
59 #include "views/viewmodecontroller.h"
61 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
63 const KFileItem
& fileInfo
,
64 const KUrl
& baseUrl
) :
72 m_selectedItemsProperties(0),
77 m_shiftPressed(qApp
->keyboardModifiers() & Qt::ShiftModifier
),
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();
85 m_removeAction
= new QAction(this);
86 connect(m_removeAction
, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
89 DolphinContextMenu::~DolphinContextMenu()
91 delete m_selectedItemsProperties
;
92 m_selectedItemsProperties
= 0;
95 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
97 m_customActions
= actions
;
100 DolphinContextMenu::Command
DolphinContextMenu::open()
102 // get the context information
103 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
104 m_context
|= TrashContext
;
107 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
108 m_context
|= ItemContext
;
109 // TODO: handle other use cases like devices + desktop files
112 // open the corresponding popup for the context
113 if (m_context
& TrashContext
) {
114 if (m_context
& ItemContext
) {
115 openTrashItemContextMenu();
117 openTrashContextMenu();
119 } else if (m_context
& ItemContext
) {
120 openItemContextMenu();
122 Q_ASSERT(m_context
== NoContext
);
123 openViewportContextMenu();
129 void DolphinContextMenu::keyPressEvent(QKeyEvent
*ev
)
131 if (ev
->key() == Qt::Key_Shift
) {
132 m_shiftPressed
= true;
133 updateRemoveAction();
135 KMenu::keyPressEvent(ev
);
138 void DolphinContextMenu::keyReleaseEvent(QKeyEvent
*ev
)
140 if (ev
->key() == Qt::Key_Shift
) {
141 // not just "m_shiftPressed = false", the user could be playing with both Shift keys...
142 m_shiftPressed
= qApp
->keyboardModifiers() & Qt::ShiftModifier
;
143 updateRemoveAction();
145 KMenu::keyReleaseEvent(ev
);
148 void DolphinContextMenu::slotRemoveActionTriggered()
150 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
152 collection
->action("move_to_trash")->trigger();
154 collection
->action("delete")->trigger();
158 void DolphinContextMenu::openTrashContextMenu()
160 Q_ASSERT(m_context
& TrashContext
);
162 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
163 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
164 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
165 addAction(emptyTrashAction
);
169 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
170 addAction(propertiesAction
);
172 addShowMenuBarAction();
174 if (exec(m_pos
) == emptyTrashAction
) {
175 KonqOperations::emptyTrash(m_mainWindow
);
179 void DolphinContextMenu::openTrashItemContextMenu()
181 Q_ASSERT(m_context
& TrashContext
);
182 Q_ASSERT(m_context
& ItemContext
);
184 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
185 addAction(restoreAction
);
187 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
188 addAction(deleteAction
);
190 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
191 addAction(propertiesAction
);
193 if (exec(m_pos
) == restoreAction
) {
194 KUrl::List selectedUrls
;
195 foreach (const KFileItem
&item
, m_selectedItems
) {
196 selectedUrls
.append(item
.url());
199 KonqOperations::restoreTrashedItems(selectedUrls
, m_mainWindow
);
203 void DolphinContextMenu::openItemContextMenu()
205 Q_ASSERT(!m_fileInfo
.isNull());
207 QAction
* openParentInNewWindowAction
= 0;
208 QAction
* openParentInNewTabAction
= 0;
209 QAction
* addToPlacesAction
= 0;
210 if (m_selectedItems
.count() == 1) {
211 if (m_fileInfo
.isDir()) {
212 // setup 'Create New' menu
213 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
);
214 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
215 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
216 newFileMenu
->checkUpToDate();
217 newFileMenu
->setPopupFiles(m_fileInfo
.url());
218 newFileMenu
->setEnabled(selectedItemsProperties().supportsWriting());
219 connect(newFileMenu
, SIGNAL(fileCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
220 connect(newFileMenu
, SIGNAL(directoryCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
222 KMenu
* menu
= newFileMenu
->menu();
223 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
224 menu
->setIcon(KIcon("document-new"));
228 // insert 'Open in new window' and 'Open in new tab' entries
229 addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
230 addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
232 // insert 'Add to Places' entry
233 if (!placeExists(m_fileInfo
.url())) {
234 addToPlacesAction
= addAction(KIcon("bookmark-new"),
235 i18nc("@action:inmenu Add selected folder to places",
240 } else if (m_baseUrl
.protocol().contains("search")) {
241 openParentInNewWindowAction
= new QAction(KIcon("window-new"),
242 i18nc("@action:inmenu",
243 "Open Path in New Window"),
245 addAction(openParentInNewWindowAction
);
247 openParentInNewTabAction
= new QAction(KIcon("tab-new"),
248 i18nc("@action:inmenu",
249 "Open Path in New Tab"),
251 addAction(openParentInNewTabAction
);
257 insertDefaultItemActions();
261 KFileItemActions fileItemActions
;
262 fileItemActions
.setItemListProperties(selectedItemsProperties());
263 addServiceActions(fileItemActions
);
265 addFileItemPluginActions();
267 addVersionControlPluginActions();
269 // insert 'Copy To' and 'Move To' sub menus
270 if (GeneralSettings::showCopyMoveMenu()) {
271 m_copyToMenu
.setItems(m_selectedItems
);
272 m_copyToMenu
.setReadOnly(!selectedItemsProperties().supportsWriting());
273 m_copyToMenu
.addActionsTo(this);
276 // insert 'Properties...' entry
277 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
278 addAction(propertiesAction
);
280 QAction
* activatedAction
= exec(m_pos
);
281 if (activatedAction
) {
282 if (activatedAction
== addToPlacesAction
) {
283 const KUrl
selectedUrl(m_fileInfo
.url());
284 if (selectedUrl
.isValid()) {
285 PlacesItemModel model
;
286 const QString text
= selectedUrl
.fileName();
287 PlacesItem
* item
= model
.createPlacesItem(text
, selectedUrl
);
288 model
.appendItemToGroup(item
);
290 } else if (activatedAction
== openParentInNewWindowAction
) {
291 m_command
= OpenParentFolderInNewWindow
;
292 } else if (activatedAction
== openParentInNewTabAction
) {
293 m_command
= OpenParentFolderInNewTab
;
298 void DolphinContextMenu::openViewportContextMenu()
300 // setup 'Create New' menu
301 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
302 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
303 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
304 newFileMenu
->checkUpToDate();
305 newFileMenu
->setPopupFiles(m_baseUrl
);
306 addMenu(newFileMenu
->menu());
309 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
310 // "open_in_new_tab" here, as the current selection should get ignored.
311 addAction(m_mainWindow
->actionCollection()->action("new_window"));
312 addAction(m_mainWindow
->actionCollection()->action("new_tab"));
314 // Insert 'Add to Places' entry if exactly one item is selected
315 QAction
* addToPlacesAction
= 0;
316 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
317 addToPlacesAction
= addAction(KIcon("bookmark-new"),
318 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
323 QAction
* pasteAction
= createPasteAction();
324 addAction(pasteAction
);
327 // Insert service actions
328 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
329 KFileItemActions fileItemActions
;
330 fileItemActions
.setItemListProperties(baseUrlProperties
);
331 addServiceActions(fileItemActions
);
333 addFileItemPluginActions();
335 addVersionControlPluginActions();
339 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
340 addAction(propertiesAction
);
342 addShowMenuBarAction();
344 QAction
* action
= exec(m_pos
);
345 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
346 const DolphinViewContainer
* container
= m_mainWindow
->activeViewContainer();
347 if (container
->url().isValid()) {
348 PlacesItemModel model
;
349 PlacesItem
* item
= model
.createPlacesItem(container
->placesText(),
351 model
.appendItemToGroup(item
);
356 void DolphinContextMenu::insertDefaultItemActions()
358 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
360 // Insert 'Cut', 'Copy' and 'Paste'
361 addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
362 addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
363 addAction(createPasteAction());
368 QAction
* renameAction
= collection
->action("rename");
369 addAction(renameAction
);
371 // Insert 'Move to Trash' and/or 'Delete'
372 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
373 addAction(collection
->action("move_to_trash"));
374 addAction(collection
->action("delete"));
376 addAction(m_removeAction
);
377 updateRemoveAction();
381 void DolphinContextMenu::addShowMenuBarAction()
383 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
384 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
385 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
387 addAction(showMenuBar
);
391 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
393 PlacesItemModel model
;
395 const int count
= model
.count();
396 for (int i
= 0; i
< count
; ++i
) {
397 const KUrl placeUrl
= model
.placesItem(i
)->url();
398 if (placeUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
406 QAction
* DolphinContextMenu::createPasteAction()
409 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
410 if (isDir
&& (m_selectedItems
.count() == 1)) {
411 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
412 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
413 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
414 action
->setEnabled(!pasteData
.isEmpty() && selectedItemsProperties().supportsWriting());
415 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
417 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
423 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties() const
425 if (!m_selectedItemsProperties
) {
426 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
428 return *m_selectedItemsProperties
;
431 KFileItem
DolphinContextMenu::baseFileItem()
433 if (!m_baseFileItem
) {
434 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
436 return *m_baseFileItem
;
439 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
441 fileItemActions
.setParentWidget(m_mainWindow
);
443 // insert 'Open With...' action or sub menu
444 fileItemActions
.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
446 // insert 'Actions' sub menu
447 fileItemActions
.addServiceActionsTo(this);
450 void DolphinContextMenu::addFileItemPluginActions()
452 KFileItemListProperties props
;
453 if (m_selectedItems
.isEmpty()) {
454 props
.setItems(KFileItemList() << baseFileItem());
456 props
= selectedItemsProperties();
459 QString commonMimeType
= props
.mimeType();
460 if (commonMimeType
.isEmpty()) {
461 commonMimeType
= QLatin1String("application/octet-stream");
464 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
465 if (pluginServices
.isEmpty()) {
469 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
470 const KConfigGroup showGroup
= config
.group("Show");
472 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
473 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
474 // The plugin has been disabled
478 // Old API (kdelibs-4.6.0 only)
479 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
481 plugin
->setParent(this);
482 addActions(plugin
->actions(props
, m_mainWindow
));
484 // New API (kdelibs >= 4.6.1)
485 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
486 if (abstractPlugin
) {
487 abstractPlugin
->setParent(this);
488 addActions(abstractPlugin
->actions(props
, m_mainWindow
));
493 void DolphinContextMenu::addVersionControlPluginActions()
495 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
496 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
497 if (!versionControlActions
.isEmpty()) {
498 foreach (QAction
* action
, versionControlActions
) {
505 void DolphinContextMenu::addCustomActions()
507 foreach (QAction
* action
, m_customActions
) {
512 void DolphinContextMenu::updateRemoveAction()
514 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
516 // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
517 // This is only done until the original action has been shown at least once. To
518 // bypass this issue, the text and &-shortcut is applied manually.
519 const QAction
* action
= 0;
521 action
= collection
->action("move_to_trash");
522 m_removeAction
->setText(i18nc("@action:inmenu", "&Move to Trash"));
524 action
= collection
->action("delete");
525 m_removeAction
->setText(i18nc("@action:inmenu", "&Delete"));
527 m_removeAction
->setIcon(action
->icon());
528 m_removeAction
->setShortcuts(action
->shortcuts());
531 bool DolphinContextMenu::moveToTrash() const
533 return !m_shiftPressed
;
536 #include "dolphincontextmenu.moc"