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>
35 #include <kmessagebox.h>
36 #include <kmimetypetrader.h>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
40 #include <konq_menuactions.h>
42 #include <kpropertiesdialog.h>
44 #include <kstandardaction.h>
45 #include <kstandarddirs.h>
47 #include <QtGui/QApplication>
48 #include <QtGui/QClipboard>
49 #include <QtCore/QDir>
51 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
52 const KFileItem
& fileInfo
,
53 const KUrl
& baseUrl
) :
59 // The context menu either accesses the URLs of the selected items
60 // or the items itself. To increase the performance both lists are cached.
61 DolphinView
* view
= m_mainWindow
->activeViewContainer()->view();
62 m_selectedUrls
= view
->selectedUrls();
63 m_selectedItems
= view
->selectedItems();
66 DolphinContextMenu::~DolphinContextMenu()
70 void DolphinContextMenu::open()
72 // get the context information
73 if (m_baseUrl
.protocol() == "trash") {
74 m_context
|= TrashContext
;
77 if (!m_fileInfo
.isNull()) {
78 m_context
|= ItemContext
;
79 // TODO: handle other use cases like devices + desktop files
82 // open the corresponding popup for the context
83 if (m_context
& TrashContext
) {
84 if (m_context
& ItemContext
) {
85 openTrashItemContextMenu();
87 openTrashContextMenu();
89 } else if (m_context
& ItemContext
) {
90 openItemContextMenu();
92 Q_ASSERT(m_context
== NoContext
);
93 openViewportContextMenu();
98 void DolphinContextMenu::openTrashContextMenu()
100 Q_ASSERT(m_context
& TrashContext
);
102 KMenu
* popup
= new KMenu(m_mainWindow
);
104 QAction
* emptyTrashAction
= new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), popup
);
105 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
106 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
107 popup
->addAction(emptyTrashAction
);
109 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
110 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
112 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
113 popup
->addAction(propertiesAction
);
115 QAction
*action
= popup
->exec(QCursor::pos());
116 if (action
== emptyTrashAction
) {
117 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted."));
118 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
121 KGuiItem(i18nc("@action:button", "Empty Trash"),
123 ) == KMessageBox::Continue
;
125 KonqOperations::emptyTrash(m_mainWindow
);
127 } else if (action
== addToPlacesAction
) {
128 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
130 DolphinSettings::instance().placesModel()->addPlace(i18n("Trash"), url
);
134 popup
->deleteLater();
137 void DolphinContextMenu::openTrashItemContextMenu()
139 Q_ASSERT(m_context
& TrashContext
);
140 Q_ASSERT(m_context
& ItemContext
);
142 KMenu
* popup
= new KMenu(m_mainWindow
);
144 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
145 popup
->addAction(restoreAction
);
147 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
148 popup
->addAction(deleteAction
);
150 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
151 popup
->addAction(propertiesAction
);
153 if (popup
->exec(QCursor::pos()) == restoreAction
) {
154 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
157 popup
->deleteLater();
160 void DolphinContextMenu::openItemContextMenu()
162 Q_ASSERT(!m_fileInfo
.isNull());
164 KMenu
* popup
= new KMenu(m_mainWindow
);
165 insertDefaultItemActions(popup
);
167 popup
->addSeparator();
169 // insert 'Bookmark This Folder' entry if exactly one item is selected
170 QAction
* addToPlacesAction
= 0;
171 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
172 addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
173 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
176 // Insert 'Open With...' sub menu
177 QVector
<KService::Ptr
> openWithVector
;
178 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
180 // Insert 'Actions' sub menu
181 KonqMenuActions menuActions
;
182 menuActions
.setItems(m_selectedItems
);
183 if (menuActions
.addActionsTo(popup
))
184 popup
->addSeparator();
186 // insert 'Properties...' entry
187 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
188 popup
->addAction(propertiesAction
);
190 QAction
* activatedAction
= popup
->exec(QCursor::pos());
192 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
193 const KUrl
selectedUrl(m_fileInfo
.url());
194 if (selectedUrl
.isValid()) {
195 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
198 } else if (openWithActions
.contains(activatedAction
)) {
199 // one of the 'Open With' items has been selected
200 if (openWithActions
.last() == activatedAction
) {
201 // the item 'Other...' has been selected
202 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
204 int id
= openWithActions
.indexOf(activatedAction
);
205 KService::Ptr servicePtr
= openWithVector
[id
];
206 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
210 openWithVector
.clear();
211 popup
->deleteLater();
214 void DolphinContextMenu::openViewportContextMenu()
216 Q_ASSERT(m_fileInfo
.isNull());
217 KMenu
* popup
= new KMenu(m_mainWindow
);
219 // setup 'Create New' menu
220 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
221 newMenu
->slotCheckUpToDate();
222 newMenu
->setPopupFiles(m_baseUrl
);
223 popup
->addMenu(newMenu
->menu());
224 popup
->addSeparator();
226 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
227 popup
->addAction(pasteAction
);
229 // setup 'View Mode' menu
230 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
232 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
233 viewModeMenu
->addAction(iconsMode
);
235 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
236 viewModeMenu
->addAction(detailsMode
);
238 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
239 viewModeMenu
->addAction(columnsMode
);
241 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
242 viewModeMenu
->addAction(previewsMode
);
244 popup
->addMenu(viewModeMenu
);
246 popup
->addSeparator();
248 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
249 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
250 popup
->addSeparator();
252 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
254 QAction
* action
= popup
->exec(QCursor::pos());
255 if (action
== propertiesAction
) {
256 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
257 KPropertiesDialog
dialog(url
);
259 } else if (action
== addToPlacesAction
) {
260 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
262 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
266 popup
->deleteLater();
269 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
271 Q_ASSERT(popup
!= 0);
272 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
274 // insert 'Cut', 'Copy' and 'Paste'
275 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
276 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
277 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
279 popup
->addAction(cutAction
);
280 popup
->addAction(copyAction
);
281 popup
->addAction(pasteAction
);
282 popup
->addSeparator();
285 QAction
* renameAction
= collection
->action("rename");
286 popup
->addAction(renameAction
);
288 // insert 'Move to Trash' and (optionally) 'Delete'
289 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
290 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
291 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
292 if (url
.isLocalFile()) {
293 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
294 popup
->addAction(moveToTrashAction
);
296 showDeleteCommand
= true;
299 if (showDeleteCommand
) {
300 QAction
* deleteAction
= collection
->action("delete");
301 popup
->addAction(deleteAction
);
305 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
306 QVector
<KService::Ptr
>& openWithVector
)
308 // Parts of the following code have been taken
309 // from the class KonqOperations located in
310 // libqonq/konq_operations.h of Konqueror.
311 // (Copyright (C) 2000 David Faure <faure@kde.org>)
313 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
314 // are listed which are registered to open the item. As last entry "Other..." will be
315 // attached which allows to select a custom application. If no applications are registered
316 // no sub menu is created at all, only "Open With..." will be offered.
317 bool insertOpenWithItems
= true;
318 const QString
contextMimeType(m_fileInfo
.mimetype());
320 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
321 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
322 KFileItem item
= mimeIt
.next();
323 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
326 QList
<QAction
*> openWithActions
;
327 if (insertOpenWithItems
) {
328 // fill the 'Open with' sub menu with application types
329 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
330 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
332 "Type == 'Application'");
333 if (offers
.count() > 0) {
334 KService::List::Iterator it
;
335 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
336 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
337 // The offer list from the KTrader returns duplicate
338 // application entries. Although this seems to be a configuration
339 // problem outside the scope of Dolphin, duplicated entries just
340 // will be skipped here.
341 const QString
appName((*it
)->name());
342 if (!containsEntry(openWithMenu
, appName
)) {
343 const KIcon
icon((*it
)->icon());
344 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
345 openWithVector
.append(*it
);
346 openWithActions
<< action
;
350 openWithMenu
->addSeparator();
351 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
353 openWithActions
<< action
;
354 popup
->addMenu(openWithMenu
);
356 // No applications are registered, hence just offer
357 // a "Open With..." item instead of a sub menu containing
359 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
360 openWithActions
<< action
;
363 // At least one of the selected items has a different MIME type. In this case
364 // just show a disabled "Open With..." entry.
365 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
366 action
->setEnabled(false);
369 return openWithActions
;
372 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
373 const QString
& entryName
) const
377 const QList
<QAction
*> list
= menu
->actions();
378 const uint count
= list
.count();
379 for (uint i
= 0; i
< count
; ++i
) {
380 const QAction
* action
= list
.at(i
);
381 if (action
->text() == entryName
) {
389 #include "dolphincontextmenu.moc"