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() && (m_selectedItems
.count() > 0)) {
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("bookmark-new"),
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("bookmark-new"),
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(placesName(selectedUrl
),
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 KMenu
* popup
= new KMenu(m_mainWindow
);
224 addShowMenubarAction(popup
);
226 // setup 'Create New' menu
227 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
228 newMenu
->slotCheckUpToDate();
229 newMenu
->setPopupFiles(m_baseUrl
);
230 popup
->addMenu(newMenu
->menu());
231 popup
->addSeparator();
233 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
234 popup
->addAction(pasteAction
);
236 // setup 'View Mode' menu
237 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
239 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
240 viewModeMenu
->addAction(iconsMode
);
242 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
243 viewModeMenu
->addAction(detailsMode
);
245 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
246 viewModeMenu
->addAction(columnsMode
);
248 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
249 viewModeMenu
->addAction(previewsMode
);
251 popup
->addMenu(viewModeMenu
);
253 popup
->addSeparator();
255 QAction
* addToPlacesAction
= popup
->addAction(KIcon("bookmark-new"),
256 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
257 popup
->addSeparator();
259 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
261 QAction
* action
= popup
->exec(QCursor::pos());
262 if (action
== propertiesAction
) {
263 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
264 KPropertiesDialog
dialog(url
, m_mainWindow
);
266 } else if (action
== addToPlacesAction
) {
267 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
269 DolphinSettings::instance().placesModel()->addPlace(placesName(url
), url
);
273 popup
->deleteLater();
276 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
278 Q_ASSERT(popup
!= 0);
279 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
281 // insert 'Cut', 'Copy' and 'Paste'
282 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
283 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
284 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
286 popup
->addAction(cutAction
);
287 popup
->addAction(copyAction
);
288 popup
->addAction(pasteAction
);
289 popup
->addSeparator();
292 QAction
* renameAction
= collection
->action("rename");
293 popup
->addAction(renameAction
);
295 // insert 'Move to Trash' and (optionally) 'Delete'
296 KConfigGroup
kdeConfig(KGlobal::config(), "KDE");
297 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
298 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
299 if (url
.isLocalFile()) {
300 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
301 popup
->addAction(moveToTrashAction
);
303 showDeleteCommand
= true;
306 if (showDeleteCommand
) {
307 QAction
* deleteAction
= collection
->action("delete");
308 popup
->addAction(deleteAction
);
312 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
313 QVector
<KService::Ptr
>& openWithVector
)
315 // Parts of the following code have been taken
316 // from the class KonqOperations located in
317 // libqonq/konq_operations.h of Konqueror.
318 // (Copyright (C) 2000 David Faure <faure@kde.org>)
320 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
321 // are listed which are registered to open the item. As last entry "Other..." will be
322 // attached which allows to select a custom application. If no applications are registered
323 // no sub menu is created at all, only "Open With..." will be offered.
324 bool insertOpenWithItems
= true;
325 const QString
contextMimeType(m_fileInfo
.mimetype());
327 QListIterator
<KFileItem
> mimeIt(m_selectedItems
);
328 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
329 KFileItem item
= mimeIt
.next();
330 insertOpenWithItems
= (contextMimeType
== item
.mimetype());
333 QList
<QAction
*> openWithActions
;
334 if (insertOpenWithItems
) {
335 // fill the 'Open with' sub menu with application types
336 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
.url());
337 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
339 "Type == 'Application'");
340 if (offers
.count() > 0) {
341 KService::List::Iterator it
;
342 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
343 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
344 // The offer list from the KTrader returns duplicate
345 // application entries. Although this seems to be a configuration
346 // problem outside the scope of Dolphin, duplicated entries just
347 // will be skipped here.
348 const QString
appName((*it
)->name());
349 if (!containsEntry(openWithMenu
, appName
)) {
350 const KIcon
icon((*it
)->icon());
351 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
352 openWithVector
.append(*it
);
353 openWithActions
<< action
;
357 openWithMenu
->addSeparator();
358 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
360 openWithActions
<< action
;
361 popup
->addMenu(openWithMenu
);
363 // No applications are registered, hence just offer
364 // a "Open With..." item instead of a sub menu containing
366 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
367 openWithActions
<< action
;
370 // At least one of the selected items has a different MIME type. In this case
371 // just show a disabled "Open With..." entry.
372 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
373 action
->setEnabled(false);
376 return openWithActions
;
379 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
380 const QString
& entryName
) const
384 const QList
<QAction
*> list
= menu
->actions();
385 const uint count
= list
.count();
386 for (uint i
= 0; i
< count
; ++i
) {
387 const QAction
* action
= list
.at(i
);
388 if (action
->text() == entryName
) {
396 void DolphinContextMenu::addShowMenubarAction(KMenu
* menu
)
398 KAction
* showMenuBar
= m_mainWindow
->showMenuBarAction();
399 if (!m_mainWindow
->menuBar()->isVisible()) {
400 // TODO: it should not be necessary to uncheck the menu
401 // bar action, but currently the action states don't get
402 // updated if the menu is disabled
403 showMenuBar
->setChecked(false);
404 menu
->addAction(showMenuBar
);
405 menu
->addSeparator();
409 QString
DolphinContextMenu::placesName(const KUrl
& url
) const
411 QString name
= url
.fileName();
412 if (name
.isEmpty()) {
418 #include "dolphincontextmenu.moc"