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"
28 #include <kactioncollection.h>
29 #include <kfileplacesmodel.h>
30 #include <kdesktopfile.h>
32 #include <kiconloader.h>
33 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
41 #include <konq_menuactions.h>
43 #include <kpropertiesdialog.h>
45 #include <kstandardaction.h>
46 #include <kstandarddirs.h>
48 #include <QtGui/QApplication>
49 #include <QtGui/QClipboard>
50 #include <QtCore/QDir>
52 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
53 const KFileItem
& fileInfo
,
54 const KUrl
& baseUrl
) :
60 // The context menu either accesses the URLs of the selected items
61 // or the items itself. To increase the performance both lists are cached.
62 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
63 m_selectedUrls
= view
->selectedUrls();
64 m_selectedItems
= view
->selectedItems();
67 DolphinContextMenu::~DolphinContextMenu()
71 void DolphinContextMenu::open()
73 // get the context information
74 if (m_baseUrl
.protocol() == "trash") {
75 m_context
|= TrashContext
;
78 if (!m_fileInfo
.isNull()) {
79 m_context
|= ItemContext
;
80 // TODO: handle other use cases like devices + desktop files
83 // open the corresponding popup for the context
84 if (m_context
& TrashContext
) {
85 if (m_context
& ItemContext
) {
86 openTrashItemContextMenu();
88 openTrashContextMenu();
90 } else if (m_context
& ItemContext
) {
91 openItemContextMenu();
93 Q_ASSERT(m_context
== NoContext
);
94 openViewportContextMenu();
99 void DolphinContextMenu::openTrashContextMenu()
101 Q_ASSERT(m_context
& TrashContext
);
103 KMenu
* popup
= new KMenu(m_mainWindow
);
105 addShowMenubarAction(popup
);
107 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
108 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
109 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
110 popup
->addAction(emptyTrashAction
);
112 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
113 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
115 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
116 popup
->addAction(propertiesAction
);
118 QAction
*action
= popup
->exec(QCursor::pos());
119 if (action
== emptyTrashAction
) {
120 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted."));
121 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
124 KGuiItem(i18nc("@action:button", "Empty Trash"),
126 ) == KMessageBox::Continue
;
128 KonqOperations::emptyTrash(m_mainWindow
);
130 } else if (action
== addToPlacesAction
) {
131 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
133 DolphinSettings::instance().placesModel()->addPlace(i18n("Trash"), url
);
137 popup
->deleteLater();
140 void DolphinContextMenu::openTrashItemContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
143 Q_ASSERT(m_context
& ItemContext
);
145 KMenu
* popup
= new KMenu(m_mainWindow
);
147 addShowMenubarAction(popup
);
149 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
150 popup
->addAction(restoreAction
);
152 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
153 popup
->addAction(deleteAction
);
155 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
156 popup
->addAction(propertiesAction
);
158 if (popup
->exec(QCursor::pos()) == restoreAction
) {
159 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
162 popup
->deleteLater();
165 void DolphinContextMenu::openItemContextMenu()
167 Q_ASSERT(!m_fileInfo
.isNull());
169 KMenu
* popup
= new KMenu(m_mainWindow
);
170 addShowMenubarAction(popup
);
171 insertDefaultItemActions(popup
);
173 popup
->addSeparator();
175 // insert 'Bookmark This Folder' entry if exactly one item is selected
176 QAction
* addToPlacesAction
= 0;
177 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
178 addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
179 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
182 // Insert 'Open With...' sub menu
183 QVector
<KService::Ptr
> openWithVector
;
184 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
186 // Insert 'Actions' sub menu
187 KonqMenuActions menuActions
;
188 menuActions
.setItems(m_selectedItems
);
189 if (menuActions
.addActionsTo(popup
))
190 popup
->addSeparator();
192 // insert 'Properties...' entry
193 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
194 popup
->addAction(propertiesAction
);
196 QAction
* activatedAction
= popup
->exec(QCursor::pos());
198 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
199 const KUrl
selectedUrl(m_fileInfo
.url());
200 if (selectedUrl
.isValid()) {
201 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
204 } else if (openWithActions
.contains(activatedAction
)) {
205 // one of the 'Open With' items has been selected
206 if (openWithActions
.last() == activatedAction
) {
207 // the item 'Other...' has been selected
208 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
210 int id
= openWithActions
.indexOf(activatedAction
);
211 KService::Ptr servicePtr
= openWithVector
[id
];
212 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
216 openWithVector
.clear();
217 popup
->deleteLater();
220 void DolphinContextMenu::openViewportContextMenu()
222 Q_ASSERT(m_fileInfo
.isNull());
223 KMenu
* popup
= new KMenu(m_mainWindow
);
225 addShowMenubarAction(popup
);
227 // setup 'Create New' menu
228 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
229 newMenu
->slotCheckUpToDate();
230 newMenu
->setPopupFiles(m_baseUrl
);
231 popup
->addMenu(newMenu
->menu());
232 popup
->addSeparator();
234 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
235 popup
->addAction(pasteAction
);
237 // setup 'View Mode' menu
238 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
240 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
241 viewModeMenu
->addAction(iconsMode
);
243 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
244 viewModeMenu
->addAction(detailsMode
);
246 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
247 viewModeMenu
->addAction(columnsMode
);
249 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
250 viewModeMenu
->addAction(previewsMode
);
252 popup
->addMenu(viewModeMenu
);
254 popup
->addSeparator();
256 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
257 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
258 popup
->addSeparator();
260 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
262 QAction
* action
= popup
->exec(QCursor::pos());
263 if (action
== propertiesAction
) {
264 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
265 KPropertiesDialog
dialog(url
);
267 } else if (action
== addToPlacesAction
) {
268 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
270 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
274 popup
->deleteLater();
277 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
279 Q_ASSERT(popup
!= 0);
280 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
282 // insert 'Cut', 'Copy' and 'Paste'
283 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
284 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
285 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
287 popup
->addAction(cutAction
);
288 popup
->addAction(copyAction
);
289 popup
->addAction(pasteAction
);
290 popup
->addSeparator();
293 QAction
* renameAction
= collection
->action("rename");
294 popup
->addAction(renameAction
);
296 // insert 'Move to Trash' and (optionally) 'Delete'
297 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
298 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
299 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
300 if (url
.isLocalFile()) {
301 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
302 popup
->addAction(moveToTrashAction
);
304 showDeleteCommand
= true;
307 if (showDeleteCommand
) {
308 QAction
* deleteAction
= collection
->action("delete");
309 popup
->addAction(deleteAction
);
313 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
314 QVector
<KService::Ptr
>& openWithVector
)
316 // Parts of the following code have been taken
317 // from the class KonqOperations located in
318 // libqonq/konq_operations.h of Konqueror.
319 // (Copyright (C) 2000 David Faure <faure@kde.org>)
321 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
322 // are listed which are registered to open the item. As last entry "Other..." will be
323 // attached which allows to select a custom application. If no applications are registered
324 // no sub menu is created at all, only "Open With..." will be offered.
325 bool insertOpenWithItems
= true;
326 const QString
contextMimeType(m_fileInfo
.mimetype());
328 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
329 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
330 KFileItem item
= mimeIt
.next();
331 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
334 QList
<QAction
*> openWithActions
;
335 if (insertOpenWithItems
) {
336 // fill the 'Open with' sub menu with application types
337 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
338 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
340 "Type == 'Application'");
341 if (offers
.count() > 0) {
342 KService::List::Iterator it
;
343 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
344 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
345 // The offer list from the KTrader returns duplicate
346 // application entries. Although this seems to be a configuration
347 // problem outside the scope of Dolphin, duplicated entries just
348 // will be skipped here.
349 const QString
appName((*it
)->name());
350 if (!containsEntry(openWithMenu
, appName
)) {
351 const KIcon
icon((*it
)->icon());
352 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
353 openWithVector
.append(*it
);
354 openWithActions
<< action
;
358 openWithMenu
->addSeparator();
359 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
361 openWithActions
<< action
;
362 popup
->addMenu(openWithMenu
);
364 // No applications are registered, hence just offer
365 // a "Open With..." item instead of a sub menu containing
367 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
368 openWithActions
<< action
;
371 // At least one of the selected items has a different MIME type. In this case
372 // just show a disabled "Open With..." entry.
373 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
374 action
->setEnabled(false);
377 return openWithActions
;
380 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
381 const QString
& entryName
) const
385 const QList
<QAction
*> list
= menu
->actions();
386 const uint count
= list
.count();
387 for (uint i
= 0; i
< count
; ++i
) {
388 const QAction
* action
= list
.at(i
);
389 if (action
->text() == entryName
) {
397 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
399 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
400 if (!m_mainWindow
->menuBar()->isVisible()) {
401 // TODO: it should not be necessary to uncheck the menu
402 // bar action, but currently the action states don't get
403 // updated if the menu is disabled
404 showMenuBar
->setChecked(false);
405 menu
->addAction(showMenuBar
);
406 menu
->addSeparator();
410 #include "dolphincontextmenu.moc"