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 popup
->addSeparator();
111 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
112 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
113 popup
->addSeparator();
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 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
148 popup
->addAction(restoreAction
);
150 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
151 popup
->addAction(deleteAction
);
153 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
154 popup
->addAction(propertiesAction
);
156 if (popup
->exec(QCursor::pos()) == restoreAction
) {
157 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
160 popup
->deleteLater();
163 void DolphinContextMenu::openItemContextMenu()
165 Q_ASSERT(!m_fileInfo
.isNull());
167 KMenu
* popup
= new KMenu(m_mainWindow
);
168 insertDefaultItemActions(popup
);
170 popup
->addSeparator();
172 // insert 'Bookmark This Folder' entry if exactly one item is selected
173 QAction
* addToPlacesAction
= 0;
174 if (m_fileInfo
.isDir() && (m_selectedUrls
.count() == 1)) {
175 addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
176 i18nc("@action:inmenu Add selected folder to places", "Add to Places"));
179 // Insert 'Open With...' sub menu
180 QVector
<KService::Ptr
> openWithVector
;
181 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
183 // Insert 'Actions' sub menu
184 KonqMenuActions menuActions
;
185 menuActions
.setItems(m_selectedItems
);
186 if (menuActions
.addActionsTo(popup
))
187 popup
->addSeparator();
189 // insert 'Properties...' entry
190 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
191 popup
->addAction(propertiesAction
);
193 QAction
* activatedAction
= popup
->exec(QCursor::pos());
195 if ((addToPlacesAction
!= 0) && (activatedAction
== addToPlacesAction
)) {
196 const KUrl
selectedUrl(m_fileInfo
.url());
197 if (selectedUrl
.isValid()) {
198 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
201 } else if (openWithActions
.contains(activatedAction
)) {
202 // one of the 'Open With' items has been selected
203 if (openWithActions
.last() == activatedAction
) {
204 // the item 'Other...' has been selected
205 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
207 int id
= openWithActions
.indexOf(activatedAction
);
208 KService::Ptr servicePtr
= openWithVector
[id
];
209 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
213 openWithVector
.clear();
214 popup
->deleteLater();
217 void DolphinContextMenu::openViewportContextMenu()
219 Q_ASSERT(m_fileInfo
.isNull());
220 KMenu
* popup
= new KMenu(m_mainWindow
);
222 // setup 'Create New' menu
223 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
224 newMenu
->slotCheckUpToDate();
225 newMenu
->setPopupFiles(m_baseUrl
);
226 popup
->addMenu(newMenu
->menu());
227 popup
->addSeparator();
229 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
230 popup
->addAction(pasteAction
);
232 // setup 'View Mode' menu
233 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
235 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
236 viewModeMenu
->addAction(iconsMode
);
238 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
239 viewModeMenu
->addAction(detailsMode
);
241 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
242 viewModeMenu
->addAction(columnsMode
);
244 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
245 viewModeMenu
->addAction(previewsMode
);
247 popup
->addMenu(viewModeMenu
);
249 popup
->addSeparator();
251 QAction
* addToPlacesAction
= popup
->addAction(KIcon("folder-bookmarks"),
252 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
253 popup
->addSeparator();
255 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
257 QAction
* action
= popup
->exec(QCursor::pos());
258 if (action
== propertiesAction
) {
259 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
260 KPropertiesDialog
dialog(url
);
262 } else if (action
== addToPlacesAction
) {
263 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
265 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
269 popup
->deleteLater();
272 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
274 Q_ASSERT(popup
!= 0);
275 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
277 // insert 'Cut', 'Copy' and 'Paste'
278 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
279 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
280 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
282 popup
->addAction(cutAction
);
283 popup
->addAction(copyAction
);
284 popup
->addAction(pasteAction
);
285 popup
->addSeparator();
288 QAction
* renameAction
= collection
->action("rename");
289 popup
->addAction(renameAction
);
291 // insert 'Move to Trash' and (optionally) 'Delete'
292 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
293 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
294 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
295 if (url
.isLocalFile()) {
296 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
297 popup
->addAction(moveToTrashAction
);
299 showDeleteCommand
= true;
302 if (showDeleteCommand
) {
303 QAction
* deleteAction
= collection
->action("delete");
304 popup
->addAction(deleteAction
);
308 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
309 QVector
<KService::Ptr
>& openWithVector
)
311 // Parts of the following code have been taken
312 // from the class KonqOperations located in
313 // libqonq/konq_operations.h of Konqueror.
314 // (Copyright (C) 2000 David Faure <faure@kde.org>)
316 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
317 // are listed which are registered to open the item. As last entry "Other..." will be
318 // attached which allows to select a custom application. If no applications are registered
319 // no sub menu is created at all, only "Open With..." will be offered.
320 bool insertOpenWithItems
= true;
321 const QString
contextMimeType(m_fileInfo
.mimetype());
323 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
324 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
325 KFileItem item
= mimeIt
.next();
326 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
329 QList
<QAction
*> openWithActions
;
330 if (insertOpenWithItems
) {
331 // fill the 'Open with' sub menu with application types
332 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
333 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
335 "Type == 'Application'");
336 if (offers
.count() > 0) {
337 KService::List::Iterator it
;
338 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
339 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
340 // The offer list from the KTrader returns duplicate
341 // application entries. Although this seems to be a configuration
342 // problem outside the scope of Dolphin, duplicated entries just
343 // will be skipped here.
344 const QString
appName((*it
)->name());
345 if (!containsEntry(openWithMenu
, appName
)) {
346 const KIcon
icon((*it
)->icon());
347 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
348 openWithVector
.append(*it
);
349 openWithActions
<< action
;
353 openWithMenu
->addSeparator();
354 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
356 openWithActions
<< action
;
357 popup
->addMenu(openWithMenu
);
359 // No applications are registered, hence just offer
360 // a "Open With..." item instead of a sub menu containing
362 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
363 openWithActions
<< action
;
366 // At least one of the selected items has a different MIME type. In this case
367 // just show a disabled "Open With..." entry.
368 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
369 action
->setEnabled(false);
372 return openWithActions
;
375 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
376 const QString
& entryName
) const
380 const QList
<QAction
*> list
= menu
->actions();
381 const uint count
= list
.count();
382 for (uint i
= 0; i
< count
; ++i
) {
383 const QAction
* action
= list
.at(i
);
384 if (action
->text() == entryName
) {
392 #include "dolphincontextmenu.moc"