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>
34 #include <KFilePlacesModel>
36 #include <KIconLoader>
37 #include <KIO/NetAccess>
40 #include <KMessageBox>
41 #include <KMimeTypeTrader>
42 #include <KModifierKeyInfo>
43 #include <KNewFileMenu>
44 #include <konqmimedata.h>
45 #include <konq_operations.h>
48 #include <KPropertiesDialog>
49 #include <KStandardAction>
50 #include <KStandardDirs>
53 #include <QApplication>
57 #include "views/dolphinplacesmodel.h"
58 #include "views/dolphinview.h"
59 #include "views/viewmodecontroller.h"
61 K_GLOBAL_STATIC(KModifierKeyInfo
, m_keyInfo
)
63 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
65 const KFileItem
& fileInfo
,
66 const KUrl
& baseUrl
) :
74 m_selectedItemsProperties(0),
80 m_shiftPressed(false),
83 // The context menu either accesses the URLs of the selected items
84 // or the items itself. To increase the performance both lists are cached.
85 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
86 m_selectedItems
= view
->selectedItems();
89 if (m_keyInfo
->isKeyPressed(Qt::Key_Shift
) || m_keyInfo
->isKeyLatched(Qt::Key_Shift
)) {
90 m_shiftPressed
= true;
92 connect(m_keyInfo
, SIGNAL(keyPressed(Qt::Key
,bool)),
93 this, SLOT(slotKeyModifierPressed(Qt::Key
,bool)));
96 m_removeAction
= new QAction(this);
97 connect(m_removeAction
, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
99 m_popup
= new KMenu(m_mainWindow
);
102 DolphinContextMenu::~DolphinContextMenu()
104 delete m_selectedItemsProperties
;
105 m_selectedItemsProperties
= 0;
111 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
113 m_customActions
= actions
;
116 DolphinContextMenu::Command
DolphinContextMenu::open()
118 // get the context information
119 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
120 m_context
|= TrashContext
;
123 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
124 m_context
|= ItemContext
;
125 // TODO: handle other use cases like devices + desktop files
128 // open the corresponding popup for the context
129 if (m_context
& TrashContext
) {
130 if (m_context
& ItemContext
) {
131 openTrashItemContextMenu();
133 openTrashContextMenu();
135 } else if (m_context
& ItemContext
) {
136 openItemContextMenu();
138 Q_ASSERT(m_context
== NoContext
);
139 openViewportContextMenu();
145 void DolphinContextMenu::initializeModifierKeyInfo()
147 // Access m_keyInfo, so that it gets instantiated by
149 KModifierKeyInfo
* keyInfo
= m_keyInfo
;
153 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key
, bool pressed
)
155 m_shiftPressed
= (key
== Qt::Key_Shift
) && pressed
;
156 updateRemoveAction();
159 void DolphinContextMenu::slotRemoveActionTriggered()
161 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
162 if (m_shiftPressed
) {
163 collection
->action("delete")->trigger();
165 collection
->action("move_to_trash")->trigger();
169 void DolphinContextMenu::openTrashContextMenu()
171 Q_ASSERT(m_context
& TrashContext
);
173 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup
);
174 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
175 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
176 m_popup
->addAction(emptyTrashAction
);
178 QAction
* addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
179 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
181 // Don't show if url is already in places
182 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
183 addToPlacesAction
->setVisible(false);
188 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
189 m_popup
->addAction(propertiesAction
);
191 addShowMenuBarAction();
193 QAction
*action
= m_popup
->exec(m_pos
);
194 if (action
== emptyTrashAction
) {
195 KonqOperations::emptyTrash(m_mainWindow
);
196 } else if (action
== addToPlacesAction
) {
197 const KUrl url
= m_mainWindow
->activeViewContainer()->url();
199 DolphinPlacesModel::instance()->addPlace(i18nc("@label", "Trash"), url
);
204 void DolphinContextMenu::openTrashItemContextMenu()
206 Q_ASSERT(m_context
& TrashContext
);
207 Q_ASSERT(m_context
& ItemContext
);
209 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
210 m_popup
->addAction(restoreAction
);
212 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
213 m_popup
->addAction(deleteAction
);
215 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
216 m_popup
->addAction(propertiesAction
);
218 if (m_popup
->exec(m_pos
) == restoreAction
) {
219 KUrl::List selectedUrls
;
220 foreach (const KFileItem
&item
, m_selectedItems
) {
221 selectedUrls
.append(item
.url());
224 KonqOperations::restoreTrashedItems(selectedUrls
, m_mainWindow
);
228 void DolphinContextMenu::openItemContextMenu()
230 Q_ASSERT(!m_fileInfo
.isNull());
232 QAction
* openParentInNewWindowAction
= 0;
233 QAction
* openParentInNewTabAction
= 0;
234 QAction
* addToPlacesAction
= 0;
235 if (m_selectedItems
.count() == 1) {
236 if (m_fileInfo
.isDir()) {
237 // setup 'Create New' menu
238 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
);
239 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
240 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
241 newFileMenu
->checkUpToDate();
242 newFileMenu
->setPopupFiles(m_fileInfo
.url());
243 newFileMenu
->setEnabled(selectedItemsProperties().supportsWriting());
244 connect(newFileMenu
, SIGNAL(fileCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
245 connect(newFileMenu
, SIGNAL(directoryCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
247 KMenu
* menu
= newFileMenu
->menu();
248 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
249 menu
->setIcon(KIcon("document-new"));
250 m_popup
->addMenu(menu
);
251 m_popup
->addSeparator();
253 // insert 'Open in new window' and 'Open in new tab' entries
254 m_popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
255 m_popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
257 // insert 'Add to Places' entry
258 if (!placeExists(m_fileInfo
.url())) {
259 addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
260 i18nc("@action:inmenu Add selected folder to places",
264 m_popup
->addSeparator();
265 } else if (m_baseUrl
.protocol().contains("search")) {
266 openParentInNewWindowAction
= new QAction(KIcon("window-new"),
267 i18nc("@action:inmenu",
268 "Open Path in New Window"),
270 m_popup
->addAction(openParentInNewWindowAction
);
272 openParentInNewTabAction
= new QAction(KIcon("tab-new"),
273 i18nc("@action:inmenu",
274 "Open Path in New Tab"),
276 m_popup
->addAction(openParentInNewTabAction
);
278 m_popup
->addSeparator();
282 insertDefaultItemActions();
284 m_popup
->addSeparator();
286 KFileItemActions fileItemActions
;
287 fileItemActions
.setItemListProperties(selectedItemsProperties());
288 addServiceActions(fileItemActions
);
290 addFileItemPluginActions();
292 addVersionControlPluginActions();
294 // insert 'Copy To' and 'Move To' sub menus
295 if (GeneralSettings::showCopyMoveMenu()) {
296 m_copyToMenu
.setItems(m_selectedItems
);
297 m_copyToMenu
.setReadOnly(!selectedItemsProperties().supportsWriting());
298 m_copyToMenu
.addActionsTo(m_popup
);
301 // insert 'Properties...' entry
302 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
303 m_popup
->addAction(propertiesAction
);
305 QAction
* activatedAction
= m_popup
->exec(m_pos
);
306 if (activatedAction
) {
307 if (activatedAction
== addToPlacesAction
) {
308 const KUrl
selectedUrl(m_fileInfo
.url());
309 if (selectedUrl
.isValid()) {
310 DolphinPlacesModel::instance()->addPlace(placesName(selectedUrl
),
313 } else if (activatedAction
== openParentInNewWindowAction
) {
314 m_command
= OpenParentFolderInNewWindow
;
315 } else if (activatedAction
== openParentInNewTabAction
) {
316 m_command
= OpenParentFolderInNewTab
;
321 void DolphinContextMenu::openViewportContextMenu()
323 // setup 'Create New' menu
324 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
325 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
326 newFileMenu
->setViewShowsHiddenFiles(view
->hiddenFilesShown());
327 newFileMenu
->checkUpToDate();
328 newFileMenu
->setPopupFiles(m_baseUrl
);
329 m_popup
->addMenu(newFileMenu
->menu());
330 m_popup
->addSeparator();
332 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
333 // "open_in_new_tab" here, as the current selection should get ignored.
334 m_popup
->addAction(m_mainWindow
->actionCollection()->action("new_window"));
335 m_popup
->addAction(m_mainWindow
->actionCollection()->action("new_tab"));
337 // Insert 'Add to Places' entry if exactly one item is selected
338 QAction
* addToPlacesAction
= 0;
339 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
340 addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
341 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
344 m_popup
->addSeparator();
346 QAction
* pasteAction
= createPasteAction();
347 m_popup
->addAction(pasteAction
);
348 m_popup
->addSeparator();
350 // Insert service actions
351 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
352 KFileItemActions fileItemActions
;
353 fileItemActions
.setItemListProperties(baseUrlProperties
);
354 addServiceActions(fileItemActions
);
356 addFileItemPluginActions();
358 addVersionControlPluginActions();
362 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
363 m_popup
->addAction(propertiesAction
);
365 addShowMenuBarAction();
367 QAction
* action
= m_popup
->exec(m_pos
);
368 if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
369 const KUrl url
= m_mainWindow
->activeViewContainer()->url();
371 DolphinPlacesModel::instance()->addPlace(placesName(url
), url
);
376 void DolphinContextMenu::insertDefaultItemActions()
378 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
380 // Insert 'Cut', 'Copy' and 'Paste'
381 m_popup
->addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
382 m_popup
->addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
383 m_popup
->addAction(createPasteAction());
385 m_popup
->addSeparator();
388 QAction
* renameAction
= collection
->action("rename");
389 m_popup
->addAction(renameAction
);
391 // Insert 'Move to Trash' and/or 'Delete'
392 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
393 m_popup
->addAction(collection
->action("move_to_trash"));
394 m_popup
->addAction(collection
->action("delete"));
396 m_popup
->addAction(m_removeAction
);
397 updateRemoveAction();
401 void DolphinContextMenu::addShowMenuBarAction()
403 const KActionCollection
* ac
= m_mainWindow
->actionCollection();
404 QAction
* showMenuBar
= ac
->action(KStandardAction::name(KStandardAction::ShowMenubar
));
405 if (!m_mainWindow
->menuBar()->isVisible() && !m_mainWindow
->toolBar()->isVisible()) {
406 m_popup
->addSeparator();
407 m_popup
->addAction(showMenuBar
);
411 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
413 QString name
= url
.fileName();
414 if (name
.isEmpty()) {
420 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
422 const KFilePlacesModel
* placesModel
= DolphinPlacesModel::instance();
423 const int count
= placesModel
->rowCount();
425 for (int i
= 0; i
< count
; ++i
) {
426 const QModelIndex index
= placesModel
->index(i
, 0);
428 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
435 QAction
* DolphinContextMenu::createPasteAction()
438 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
439 if (isDir
&& (m_selectedItems
.count() == 1)) {
440 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
441 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
442 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
443 action
->setEnabled(!pasteData
.isEmpty() && selectedItemsProperties().supportsWriting());
444 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
446 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
452 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties()
454 if (!m_selectedItemsProperties
) {
455 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
457 return *m_selectedItemsProperties
;
460 KFileItem
DolphinContextMenu::baseFileItem()
462 if (!m_baseFileItem
) {
463 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
465 return *m_baseFileItem
;
468 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
470 fileItemActions
.setParentWidget(m_mainWindow
);
472 // insert 'Open With...' action or sub menu
473 fileItemActions
.addOpenWithActionsTo(m_popup
, "DesktopEntryName != 'dolphin'");
475 // insert 'Actions' sub menu
476 fileItemActions
.addServiceActionsTo(m_popup
);
479 void DolphinContextMenu::addFileItemPluginActions()
481 KFileItemListProperties props
;
482 if (m_selectedItems
.isEmpty()) {
483 props
.setItems(KFileItemList() << baseFileItem());
485 props
= selectedItemsProperties();
488 QString commonMimeType
= props
.mimeType();
489 if (commonMimeType
.isEmpty()) {
490 commonMimeType
= QLatin1String("application/octet-stream");
493 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
494 if (pluginServices
.isEmpty()) {
498 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
499 const KConfigGroup showGroup
= config
.group("Show");
501 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
502 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
503 // The plugin has been disabled
507 // Old API (kdelibs-4.6.0 only)
508 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
510 plugin
->setParent(m_popup
);
511 m_popup
->addActions(plugin
->actions(props
, m_mainWindow
));
513 // New API (kdelibs >= 4.6.1)
514 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
515 if (abstractPlugin
) {
516 abstractPlugin
->setParent(m_popup
);
517 m_popup
->addActions(abstractPlugin
->actions(props
, m_mainWindow
));
522 void DolphinContextMenu::addVersionControlPluginActions()
524 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
525 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
526 if (!versionControlActions
.isEmpty()) {
527 foreach (QAction
* action
, versionControlActions
) {
528 m_popup
->addAction(action
);
530 m_popup
->addSeparator();
534 void DolphinContextMenu::addCustomActions()
536 foreach (QAction
* action
, m_customActions
) {
537 m_popup
->addAction(action
);
541 void DolphinContextMenu::updateRemoveAction()
543 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
544 const bool moveToTrash
= selectedItemsProperties().isLocal() && !m_shiftPressed
;
546 // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
547 // This is only done until the original action has been shown at least once. To
548 // bypass this issue, the text and &-shortcut is applied manually.
549 const QAction
* action
= 0;
551 action
= collection
->action("move_to_trash");
552 m_removeAction
->setText(i18nc("@action:inmenu", "&Move to Trash"));
554 action
= collection
->action("delete");
555 m_removeAction
->setText(i18nc("@action:inmenu", "&Delete"));
557 m_removeAction
->setIcon(action
->icon());
558 m_removeAction
->setShortcuts(action
->shortcuts());
561 #include "dolphincontextmenu.moc"