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 "settings/dolphinsettings.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
29 #include <KActionCollection>
30 #include <KDesktopFile>
31 #include <kfileitemactionplugin.h>
32 #include <kabstractfileitemactionplugin.h>
33 #include <KFileItemActions>
34 #include <KFileItemListProperties>
35 #include <KFilePlacesModel>
37 #include <KIconLoader>
38 #include <KIO/NetAccess>
41 #include <KMessageBox>
42 #include <KMimeTypeTrader>
43 #include <KModifierKeyInfo>
44 #include <KNewFileMenu>
45 #include <konqmimedata.h>
46 #include <konq_operations.h>
49 #include <KPropertiesDialog>
50 #include <KStandardAction>
51 #include <KStandardDirs>
53 #include <QApplication>
57 #include "views/dolphinview.h"
58 #include "views/viewmodecontroller.h"
60 K_GLOBAL_STATIC(KModifierKeyInfo
, m_keyInfo
)
62 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
63 const KFileItem
& fileInfo
,
64 const KUrl
& baseUrl
) :
70 m_selectedItemsProperties(0),
76 m_shiftPressed(false),
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();
85 if (m_keyInfo
->isKeyPressed(Qt::Key_Shift
) || m_keyInfo
->isKeyLatched(Qt::Key_Shift
)) {
86 m_shiftPressed
= true;
88 connect(m_keyInfo
, SIGNAL(keyPressed(Qt::Key
, bool)),
89 this, SLOT(slotKeyModifierPressed(Qt::Key
, bool)));
92 m_removeAction
= new QAction(this);
93 connect(m_removeAction
, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
95 m_popup
= new KMenu(m_mainWindow
);
98 DolphinContextMenu::~DolphinContextMenu()
100 delete m_selectedItemsProperties
;
101 m_selectedItemsProperties
= 0;
107 void DolphinContextMenu::setCustomActions(const QList
<QAction
*>& actions
)
109 m_customActions
= actions
;
112 DolphinContextMenu::Command
DolphinContextMenu::open()
114 // get the context information
115 if (m_baseUrl
.protocol() == QLatin1String("trash")) {
116 m_context
|= TrashContext
;
119 if (!m_fileInfo
.isNull() && !m_selectedItems
.isEmpty()) {
120 m_context
|= ItemContext
;
121 // TODO: handle other use cases like devices + desktop files
124 // open the corresponding popup for the context
125 if (m_context
& TrashContext
) {
126 if (m_context
& ItemContext
) {
127 openTrashItemContextMenu();
129 openTrashContextMenu();
131 } else if (m_context
& ItemContext
) {
132 openItemContextMenu();
134 Q_ASSERT(m_context
== NoContext
);
135 openViewportContextMenu();
141 void DolphinContextMenu::initializeModifierKeyInfo()
143 // Access m_keyInfo, so that it gets instantiated by
145 KModifierKeyInfo
* keyInfo
= m_keyInfo
;
149 void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key
, bool pressed
)
151 m_shiftPressed
= (key
== Qt::Key_Shift
) && pressed
;
152 updateRemoveAction();
155 void DolphinContextMenu::slotRemoveActionTriggered()
157 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
158 if (m_shiftPressed
) {
159 collection
->action("delete")->trigger();
161 collection
->action("move_to_trash")->trigger();
165 void DolphinContextMenu::openTrashContextMenu()
167 Q_ASSERT(m_context
& TrashContext
);
169 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup
);
170 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
171 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
172 m_popup
->addAction(emptyTrashAction
);
174 QAction
* addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
175 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
177 // Don't show if url is already in places
178 if (placeExists(m_mainWindow
->activeViewContainer()->url())) {
179 addToPlacesAction
->setVisible(false);
184 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
185 m_popup
->addAction(propertiesAction
);
187 QAction
*action
= m_popup
->exec(QCursor::pos());
188 if (action
== emptyTrashAction
) {
189 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
190 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
193 KGuiItem(i18nc("@action:button", "Empty Trash"),
195 ) == KMessageBox::Continue
;
197 KonqOperations::emptyTrash(m_mainWindow
);
199 } else if (action
== addToPlacesAction
) {
200 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
202 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
207 void DolphinContextMenu::openTrashItemContextMenu()
209 Q_ASSERT(m_context
& TrashContext
);
210 Q_ASSERT(m_context
& ItemContext
);
212 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
213 m_popup
->addAction(restoreAction
);
215 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
216 m_popup
->addAction(deleteAction
);
218 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
219 m_popup
->addAction(propertiesAction
);
221 if (m_popup
->exec(QCursor::pos()) == restoreAction
) {
222 KUrl::List selectedUrls
;
223 foreach (const KFileItem
&item
, m_selectedItems
) {
224 selectedUrls
.append(item
.url());
227 KonqOperations::restoreTrashedItems(selectedUrls
, m_mainWindow
);
231 void DolphinContextMenu::openItemContextMenu()
233 Q_ASSERT(!m_fileInfo
.isNull());
235 QAction
* openParentInNewWindowAction
= 0;
236 QAction
* openParentInNewTabAction
= 0;
237 QAction
* addToPlacesAction
= 0;
238 if (m_selectedItems
.count() == 1) {
239 if (m_fileInfo
.isDir()) {
240 // setup 'Create New' menu
241 DolphinNewFileMenu
* newFileMenu
= new DolphinNewFileMenu(m_mainWindow
);
242 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
243 newFileMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
244 newFileMenu
->checkUpToDate();
245 newFileMenu
->setPopupFiles(m_fileInfo
.url());
246 newFileMenu
->setEnabled(selectedItemsProperties().supportsWriting());
247 connect(newFileMenu
, SIGNAL(fileCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
248 connect(newFileMenu
, SIGNAL(directoryCreated(KUrl
)), newFileMenu
, SLOT(deleteLater()));
250 KMenu
* menu
= newFileMenu
->menu();
251 menu
->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
252 menu
->setIcon(KIcon("document-new"));
253 m_popup
->addMenu(menu
);
254 m_popup
->addSeparator();
256 // insert 'Open in new window' and 'Open in new tab' entries
257 m_popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_window"));
258 m_popup
->addAction(m_mainWindow
->actionCollection()->action("open_in_new_tab"));
260 // insert 'Add to Places' entry
261 if (!placeExists(m_fileInfo
.url())) {
262 addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
263 i18nc("@action:inmenu Add selected folder to places",
267 m_popup
->addSeparator();
268 } else if (m_baseUrl
.protocol().contains("search")) {
269 openParentInNewWindowAction
= new QAction(KIcon("window-new"),
270 i18nc("@action:inmenu",
271 "Open Path in New Window"),
273 m_popup
->addAction(openParentInNewWindowAction
);
275 openParentInNewTabAction
= new QAction(KIcon("tab-new"),
276 i18nc("@action:inmenu",
277 "Open Path in New Tab"),
279 m_popup
->addAction(openParentInNewTabAction
);
281 m_popup
->addSeparator();
285 insertDefaultItemActions();
287 m_popup
->addSeparator();
289 KFileItemActions fileItemActions
;
290 fileItemActions
.setItemListProperties(selectedItemsProperties());
291 addServiceActions(fileItemActions
);
293 addFileItemPluginActions();
295 addVersionControlPluginActions();
297 // insert 'Copy To' and 'Move To' sub menus
298 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
299 m_copyToMenu
.setItems(m_selectedItems
);
300 m_copyToMenu
.setReadOnly(!selectedItemsProperties().supportsWriting());
301 m_copyToMenu
.addActionsTo(m_popup
);
304 // insert 'Properties...' entry
305 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
306 m_popup
->addAction(propertiesAction
);
308 QAction
* activatedAction
= m_popup
->exec(QCursor::pos());
309 if (activatedAction
) {
310 if (activatedAction
== addToPlacesAction
) {
311 const KUrl
selectedUrl(m_fileInfo
.url());
312 if (selectedUrl
.isValid()) {
313 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
316 } else if (activatedAction
== openParentInNewWindowAction
) {
317 m_command
= OpenParentFolderInNewWindow
;
318 } else if (activatedAction
== openParentInNewTabAction
) {
319 m_command
= OpenParentFolderInNewTab
;
324 void DolphinContextMenu::openViewportContextMenu()
326 // setup 'Create New' menu
327 KNewFileMenu
* newFileMenu
= m_mainWindow
->newFileMenu();
328 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
329 newFileMenu
->setViewShowsHiddenFiles(view
->showHiddenFiles());
330 newFileMenu
->checkUpToDate();
331 newFileMenu
->setPopupFiles(m_baseUrl
);
332 m_popup
->addMenu(newFileMenu
->menu());
333 m_popup
->addSeparator();
335 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
336 // "open_in_new_tab" here, as the current selection should get ignored.
337 m_popup
->addAction(m_mainWindow
->actionCollection()->action("new_window"));
338 m_popup
->addAction(m_mainWindow
->actionCollection()->action("new_tab"));
340 // Insert 'Add to Places' entry if exactly one item is selected
341 QAction
* addToPlacesAction
= 0;
342 if (!placeExists(m_mainWindow
->activeViewContainer()->url())) {
343 addToPlacesAction
= m_popup
->addAction(KIcon("bookmark-new"),
344 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
347 m_popup
->addSeparator();
349 QAction
* pasteAction
= createPasteAction();
350 m_popup
->addAction(pasteAction
);
351 m_popup
->addSeparator();
353 // Insert service actions
354 const KFileItemListProperties
baseUrlProperties(KFileItemList() << baseFileItem());
355 KFileItemActions fileItemActions
;
356 fileItemActions
.setItemListProperties(baseUrlProperties
);
357 addServiceActions(fileItemActions
);
359 addFileItemPluginActions();
361 addVersionControlPluginActions();
365 QAction
* propertiesAction
= m_popup
->addAction(i18nc("@action:inmenu", "Properties"));
366 propertiesAction
->setIcon(KIcon("document-properties"));
368 QAction
* action
= m_popup
->exec(QCursor::pos());
369 if (action
== propertiesAction
) {
370 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
372 KPropertiesDialog
* dialog
= new KPropertiesDialog(url
, m_mainWindow
);
373 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
375 } else if (addToPlacesAction
&& (action
== addToPlacesAction
)) {
376 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
378 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
383 void DolphinContextMenu::insertDefaultItemActions()
385 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
387 // Insert 'Cut', 'Copy' and 'Paste'
388 m_popup
->addAction(collection
->action(KStandardAction::name(KStandardAction::Cut
)));
389 m_popup
->addAction(collection
->action(KStandardAction::name(KStandardAction::Copy
)));
390 m_popup
->addAction(createPasteAction());
392 m_popup
->addSeparator();
395 QAction
* renameAction
= collection
->action("rename");
396 m_popup
->addAction(renameAction
);
398 // Insert 'Move to Trash' and/or 'Delete'
399 if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
400 m_popup
->addAction(collection
->action("move_to_trash"));
401 m_popup
->addAction(collection
->action("delete"));
403 m_popup
->addAction(m_removeAction
);
404 updateRemoveAction();
408 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
410 QString name
= url
.fileName();
411 if (name
.isEmpty()) {
417 bool DolphinContextMenu::placeExists(const KUrl
& url
) const
419 const KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
420 const int count
= placesModel
->rowCount();
422 for (int i
= 0; i
< count
; ++i
) {
423 const QModelIndex index
= placesModel
->index(i
, 0);
425 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
432 QAction
* DolphinContextMenu::createPasteAction()
435 const bool isDir
= !m_fileInfo
.isNull() && m_fileInfo
.isDir();
436 if (isDir
&& (m_selectedItems
.count() == 1)) {
437 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
438 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
439 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
440 action
->setEnabled(!pasteData
.isEmpty() && selectedItemsProperties().supportsWriting());
441 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
443 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
449 KFileItemListProperties
& DolphinContextMenu::selectedItemsProperties()
451 if (!m_selectedItemsProperties
) {
452 m_selectedItemsProperties
= new KFileItemListProperties(m_selectedItems
);
454 return *m_selectedItemsProperties
;
457 KFileItem
DolphinContextMenu::baseFileItem()
459 if (!m_baseFileItem
) {
460 m_baseFileItem
= new KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_baseUrl
);
462 return *m_baseFileItem
;
465 void DolphinContextMenu::addServiceActions(KFileItemActions
& fileItemActions
)
467 fileItemActions
.setParentWidget(m_mainWindow
);
469 // insert 'Open With...' action or sub menu
470 fileItemActions
.addOpenWithActionsTo(m_popup
, "DesktopEntryName != 'dolphin'");
472 // insert 'Actions' sub menu
473 fileItemActions
.addServiceActionsTo(m_popup
);
476 void DolphinContextMenu::addFileItemPluginActions()
478 KFileItemListProperties props
;
479 if (m_selectedItems
.isEmpty()) {
480 props
.setItems(KFileItemList() << baseFileItem());
482 props
= selectedItemsProperties();
485 QString commonMimeType
= props
.mimeType();
486 if (commonMimeType
.isEmpty()) {
487 commonMimeType
= QLatin1String("application/octet-stream");
490 const KService::List pluginServices
= KMimeTypeTrader::self()->query(commonMimeType
, "KFileItemAction/Plugin", "exist Library");
491 if (pluginServices
.isEmpty()) {
495 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
496 const KConfigGroup showGroup
= config
.group("Show");
498 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
499 if (!showGroup
.readEntry(service
->desktopEntryName(), true)) {
500 // The plugin has been disabled
504 // Old API (kdelibs-4.6.0 only)
505 KFileItemActionPlugin
* plugin
= service
->createInstance
<KFileItemActionPlugin
>();
507 plugin
->setParent(m_popup
);
508 m_popup
->addActions(plugin
->actions(props
, m_mainWindow
));
510 // New API (kdelibs >= 4.6.1)
511 KAbstractFileItemActionPlugin
* abstractPlugin
= service
->createInstance
<KAbstractFileItemActionPlugin
>();
512 if (abstractPlugin
) {
513 abstractPlugin
->setParent(m_popup
);
514 m_popup
->addActions(abstractPlugin
->actions(props
, m_mainWindow
));
519 void DolphinContextMenu::addVersionControlPluginActions()
521 const DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
522 const QList
<QAction
*> versionControlActions
= view
->versionControlActions(m_selectedItems
);
523 if (!versionControlActions
.isEmpty()) {
524 foreach (QAction
* action
, versionControlActions
) {
525 m_popup
->addAction(action
);
527 m_popup
->addSeparator();
531 void DolphinContextMenu::addCustomActions()
533 foreach (QAction
* action
, m_customActions
) {
534 m_popup
->addAction(action
);
538 void DolphinContextMenu::updateRemoveAction()
540 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
541 const bool moveToTrash
= selectedItemsProperties().isLocal() && !m_shiftPressed
;
543 // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
544 // This is only done until the original action has been shown at least once. To
545 // bypass this issue, the text and &-shortcut is applied manually.
546 const QAction
* action
= 0;
548 action
= collection
->action("move_to_trash");
549 m_removeAction
->setText(i18nc("@action:inmenu", "&Move to Trash"));
551 action
= collection
->action("delete");
552 m_removeAction
->setText(i18nc("@action:inmenu", "&Delete"));
554 m_removeAction
->setIcon(action
->icon());
555 m_removeAction
->setShortcuts(action
->shortcuts());
558 #include "dolphincontextmenu.moc"