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 "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "dolphinviewcontainer.h"
27 #include "dolphin_generalsettings.h"
28 #include "fileitemcapabilities.h"
30 #include <kactioncollection.h>
31 #include <kfileplacesmodel.h>
32 #include <kdesktopfile.h>
34 #include <kiconloader.h>
35 #include <kio/netaccess.h>
38 #include <kmessagebox.h>
39 #include <kmimetypetrader.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <konq_menuactions.h>
45 #include <kpropertiesdialog.h>
47 #include <kstandardaction.h>
48 #include <kstandarddirs.h>
50 #include <QtGui/QApplication>
51 #include <QtGui/QClipboard>
52 #include <QtCore/QDir>
54 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
55 const KFileItem
& fileInfo
,
56 const KUrl
& baseUrl
) :
63 // The context menu either accesses the URLs of the selected items
64 // or the items itself. To increase the performance both lists are cached.
65 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
66 m_selectedUrls
= view
->selectedUrls();
67 m_selectedItems
= view
->selectedItems();
70 DolphinContextMenu::~DolphinContextMenu()
72 delete m_capabilities
;
76 void DolphinContextMenu::open()
78 // get the context information
79 if (m_baseUrl
.protocol() == "trash") {
80 m_context
|= TrashContext
;
83 if (!m_fileInfo
.isNull() && (m_selectedItems
.count() > 0)) {
84 m_context
|= ItemContext
;
85 // TODO: handle other use cases like devices + desktop files
88 // open the corresponding popup for the context
89 if (m_context
& TrashContext
) {
90 if (m_context
& ItemContext
) {
91 openTrashItemContextMenu();
93 openTrashContextMenu();
95 } else if (m_context
& ItemContext
) {
96 openItemContextMenu();
98 Q_ASSERT(m_context
== NoContext
);
99 openViewportContextMenu();
103 void DolphinContextMenu::openTrashContextMenu()
105 Q_ASSERT(m_context
& TrashContext
);
107 KMenu
* popup
= new KMenu(m_mainWindow
);
109 addShowMenubarAction(popup
);
111 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
112 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
113 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
114 popup
->addAction(emptyTrashAction
);
116 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
117 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
119 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
120 popup
->addAction(propertiesAction
);
122 QAction
*action
= popup
->exec(QCursor::pos());
123 if (action
== emptyTrashAction
) {
124 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted."));
125 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
128 KGuiItem(i18nc("@action:button", "Empty Trash"),
130 ) == KMessageBox::Continue
;
132 KonqOperations::emptyTrash(m_mainWindow
);
134 } else if (action
== addToPlacesAction
) {
135 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
137 DolphinSettings::instance().placesModel()->addPlace(i18nc("@label", "Trash"), url
);
141 popup
->deleteLater();
144 void DolphinContextMenu::openTrashItemContextMenu()
146 Q_ASSERT(m_context
& TrashContext
);
147 Q_ASSERT(m_context
& ItemContext
);
149 KMenu
* popup
= new KMenu(m_mainWindow
);
151 addShowMenubarAction(popup
);
153 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
154 popup
->addAction(restoreAction
);
156 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
157 popup
->addAction(deleteAction
);
159 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
160 popup
->addAction(propertiesAction
);
162 if (popup
->exec(QCursor::pos()) == restoreAction
) {
163 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
166 popup
->deleteLater();
169 void DolphinContextMenu::openItemContextMenu()
171 Q_ASSERT(!m_fileInfo
.isNull());
173 KMenu
* popup
= new KMenu(m_mainWindow
);
174 addShowMenubarAction(popup
);
175 insertDefaultItemActions(popup
);
177 popup
->addSeparator();
179 // insert 'Bookmark This Folder' entry if exactly one item is selected
180 QAction
* addToPlacesAction
= 0;
181 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
182 addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
183 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
186 // Insert 'Open With...' sub menu
187 QVector
<KService::Ptr
> openWithVector
;
188 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
190 // Insert 'Actions' sub menu
191 KonqMenuActions menuActions
;
192 menuActions
.setItems(m_selectedItems
);
193 if (menuActions
.addActionsTo(popup
)) {
194 popup
->addSeparator();
197 // Insert 'Copy To' and 'Move To' sub menus
198 if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) {
199 m_copyToMenu
.setItems(m_selectedItems
);
200 m_copyToMenu
.setReadOnly(!capabilities().supportsMoving());
201 m_copyToMenu
.addActionsTo(popup
);
202 popup
->addSeparator();
205 // insert 'Properties...' entry
206 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
207 popup
->addAction(propertiesAction
);
209 QAction
* activatedAction
= popup
->exec(QCursor::pos());
211 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
212 const KUrl
selectedUrl(m_fileInfo
.url());
213 if (selectedUrl
.isValid()) {
214 DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl
),
217 } else if (openWithActions
.contains(activatedAction
)) {
218 // one of the 'Open With' items has been selected
219 if (openWithActions
.last() == activatedAction
) {
220 // the item 'Other...' has been selected
221 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
223 int id
= openWithActions
.indexOf(activatedAction
);
224 KService::Ptr servicePtr
= openWithVector
[id
];
225 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
229 openWithVector
.clear();
230 popup
->deleteLater();
233 void DolphinContextMenu::openViewportContextMenu()
235 KMenu
* popup
= new KMenu(m_mainWindow
);
237 addShowMenubarAction(popup
);
239 // setup 'Create New' menu
240 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
241 newMenu
->slotCheckUpToDate();
242 newMenu
->setPopupFiles(m_baseUrl
);
243 popup
->addMenu(newMenu
->menu());
244 popup
->addSeparator();
246 QAction
* pasteAction
= createPasteAction();
247 popup
->addAction(pasteAction
);
249 // setup 'View Mode' menu
250 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
252 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
253 viewModeMenu
->addAction(iconsMode
);
255 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
256 viewModeMenu
->addAction(detailsMode
);
258 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
259 viewModeMenu
->addAction(columnsMode
);
261 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
262 viewModeMenu
->addAction(previewsMode
);
264 popup
->addMenu(viewModeMenu
);
266 popup
->addSeparator();
268 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
269 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
270 popup
->addSeparator();
272 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
274 QAction
* action
= popup
->exec(QCursor::pos());
275 if (action
== propertiesAction
) {
276 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
277 KPropertiesDialog
dialog(url
, m_mainWindow
);
279 } else if (action
== addToPlacesAction
) {
280 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
282 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
286 popup
->deleteLater();
289 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
291 Q_ASSERT(popup
!= 0);
292 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
294 // insert 'Cut', 'Copy' and 'Paste'
295 QAction
* cutAction
= collection
->action(KStandardAction::name(KStandardAction::Cut
));
296 QAction
* copyAction
= collection
->action(KStandardAction::name(KStandardAction::Copy
));
297 QAction
* pasteAction
= createPasteAction();
299 popup
->addAction(cutAction
);
300 popup
->addAction(copyAction
);
301 popup
->addAction(pasteAction
);
302 popup
->addSeparator();
305 QAction
* renameAction
= collection
->action("rename");
306 popup
->addAction(renameAction
);
308 // insert 'Move to Trash' and (optionally) 'Delete'
309 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
310 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
311 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
312 if (url
.isLocalFile()) {
313 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
314 popup
->addAction(moveToTrashAction
);
316 showDeleteCommand
= true;
319 if (showDeleteCommand
) {
320 QAction
* deleteAction
= collection
->action("delete");
321 popup
->addAction(deleteAction
);
325 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
326 QVector
<KService::Ptr
>& openWithVector
)
328 // Parts of the following code have been taken
329 // from the class KonqOperations located in
330 // libqonq/konq_operations.h of Konqueror.
331 // (Copyright (C) 2000 David Faure <faure@kde.org>)
333 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
334 // are listed which are registered to open the item. As last entry "Other..." will be
335 // attached which allows to select a custom application. If no applications are registered
336 // no sub menu is created at all, only "Open With..." will be offered.
337 bool insertOpenWithItems
= true;
338 const QString
contextMimeType(m_fileInfo
.mimetype());
340 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
341 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
342 KFileItem item
= mimeIt
.next();
343 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
346 QList
<QAction
*> openWithActions
;
347 if (insertOpenWithItems
) {
348 // fill the 'Open with' sub menu with application types
349 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
350 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
352 "Type == 'Application'");
353 if (offers
.count() > 0) {
354 KService::List::Iterator it
;
355 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
356 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
357 // The offer list from the KTrader returns duplicate
358 // application entries. Although this seems to be a configuration
359 // problem outside the scope of Dolphin, duplicated entries just
360 // will be skipped here.
361 const QString
appName((*it
)->name());
362 if (!containsEntry(openWithMenu
, appName
)) {
363 const KIcon
icon((*it
)->icon());
364 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
365 openWithVector
.append(*it
);
366 openWithActions
<< action
;
370 openWithMenu
->addSeparator();
371 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
373 openWithActions
<< action
;
374 popup
->addMenu(openWithMenu
);
376 // No applications are registered, hence just offer
377 // a "Open With..." item instead of a sub menu containing
379 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
380 openWithActions
<< action
;
383 // At least one of the selected items has a different MIME type. In this case
384 // just show a disabled "Open With..." entry.
385 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
386 action
->setEnabled(false);
389 return openWithActions
;
392 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
393 const QString
& entryName
) const
397 const QList
<QAction
*> list
= menu
->actions();
398 const uint count
= list
.count();
399 for (uint i
= 0; i
< count
; ++i
) {
400 const QAction
* action
= list
.at(i
);
401 if (action
->text() == entryName
) {
409 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
411 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
412 if (!m_mainWindow
->menuBar()->isVisible()) {
413 // TODO: it should not be necessary to uncheck the menu
414 // bar action, but currently the action states don't get
415 // updated if the menu is disabled
416 showMenuBar
->setChecked(false);
417 menu
->addAction(showMenuBar
);
418 menu
->addSeparator();
422 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
424 QString name
= url
.fileName();
425 if (name
.isEmpty()) {
431 QAction
* DolphinContextMenu::createPasteAction()
434 if ((m_selectedItems
.count() == 1) && m_fileInfo
.isDir()) {
435 action
= new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
436 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
437 const KUrl::List pasteData
= KUrl::List::fromMimeData(mimeData
);
438 action
->setEnabled(!pasteData
.isEmpty() && capabilities().supportsWriting());
439 connect(action
, SIGNAL(triggered()), m_mainWindow
, SLOT(pasteIntoFolder()));
441 action
= m_mainWindow
->actionCollection()->action(KStandardAction::name(KStandardAction::Paste
));
447 FileItemCapabilities
& DolphinContextMenu::capabilities()
449 if (m_capabilities
== 0) {
450 m_capabilities
= new FileItemCapabilities(m_selectedItems
);
452 return *m_capabilities
;
455 #include "dolphincontextmenu.moc"