]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
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"
27 #include <kactioncollection.h>
28 #include <kfileplacesmodel.h>
29 #include <kdesktopfile.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
34 #include <kmessagebox.h>
35 #include <kmimetypetrader.h>
37 #include <konqmimedata.h>
38 #include <konq_operations.h>
40 #include <kpropertiesdialog.h>
42 #include <kstandardaction.h>
43 #include <kstandarddirs.h>
45 #include <QApplication>
48 #include <Q3ValueList>
50 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
52 const KUrl
& baseUrl
) :
58 // The context menu either accesses the URLs of the selected items
59 // or the items itself. To increase the performance both lists are cached.
60 DolphinView
* view
= m_mainWindow
->activeView();
61 m_selectedUrls
= view
->selectedUrls();
62 m_selectedItems
= view
->selectedItems();
65 DolphinContextMenu::~DolphinContextMenu()
69 void DolphinContextMenu::open()
71 // get the context information
72 if (m_baseUrl
.protocol() == "trash") {
73 m_context
|= TrashContext
;
76 if (m_fileInfo
!= 0) {
77 m_context
|= ItemContext
;
78 // TODO: handle other use cases like devices + desktop files
81 // open the corresponding popup for the context
82 if (m_context
& TrashContext
) {
83 if (m_context
& ItemContext
) {
84 openTrashItemContextMenu();
87 openTrashContextMenu();
90 else if (m_context
& ItemContext
) {
91 openItemContextMenu();
94 Q_ASSERT(m_context
== NoContext
);
95 openViewportContextMenu();
100 void DolphinContextMenu::openTrashContextMenu()
102 Q_ASSERT(m_context
& TrashContext
);
104 KMenu
* popup
= new KMenu(m_mainWindow
);
106 QAction
* emptyTrashAction
= new QAction(KIcon("emptytrash"), i18n("Emtpy Trash"), popup
);
107 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
108 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
109 popup
->addAction(emptyTrashAction
);
111 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
112 popup
->addAction(propertiesAction
);
114 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
115 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
116 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
119 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
120 ) == KMessageBox::Continue
;
122 KonqOperations::emptyTrash(m_mainWindow
);
126 popup
->deleteLater();
129 void DolphinContextMenu::openTrashItemContextMenu()
131 Q_ASSERT(m_context
& TrashContext
);
132 Q_ASSERT(m_context
& ItemContext
);
134 KMenu
* popup
= new KMenu(m_mainWindow
);
136 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
137 popup
->addAction(restoreAction
);
139 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
140 popup
->addAction(deleteAction
);
142 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
143 popup
->addAction(propertiesAction
);
145 if (popup
->exec(QCursor::pos()) == restoreAction
) {
146 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
149 popup
->deleteLater();
152 void DolphinContextMenu::openItemContextMenu()
154 Q_ASSERT(m_fileInfo
!= 0);
156 KMenu
* popup
= new KMenu(m_mainWindow
);
157 insertDefaultItemActions(popup
);
159 popup
->addSeparator();
161 // insert 'Bookmark This Folder' entry if exactly one item is selected
162 QAction
* bookmarkAction
= 0;
163 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
164 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
167 // Insert 'Open With...' sub menu
168 QVector
<KService::Ptr
> openWithVector
;
169 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
171 // Insert 'Actions' sub menu
172 QVector
<KDesktopFileActions::Service
> actionsVector
;
173 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
174 popup
->addSeparator();
176 // insert 'Properties...' entry
177 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
178 popup
->addAction(propertiesAction
);
180 QAction
* activatedAction
= popup
->exec(QCursor::pos());
182 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
183 const KUrl
selectedUrl(m_fileInfo
->url());
184 if (selectedUrl
.isValid()) {
185 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
189 else if (serviceActions
.contains(activatedAction
)) {
190 // one of the 'Actions' items has been selected
191 int id
= serviceActions
.indexOf(activatedAction
);
192 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
194 else if (openWithActions
.contains(activatedAction
)) {
195 // one of the 'Open With' items has been selected
196 if (openWithActions
.last() == activatedAction
) {
197 // the item 'Other...' has been selected
198 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
201 int id
= openWithActions
.indexOf(activatedAction
);
202 KService::Ptr servicePtr
= openWithVector
[id
];
203 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
207 openWithVector
.clear();
208 actionsVector
.clear();
209 popup
->deleteLater();
212 void DolphinContextMenu::openViewportContextMenu()
214 Q_ASSERT(m_fileInfo
== 0);
215 KMenu
* popup
= new KMenu(m_mainWindow
);
217 // setup 'Create New' menu
218 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
219 newMenu
->slotCheckUpToDate();
220 newMenu
->setPopupFiles(m_baseUrl
);
221 popup
->addMenu(newMenu
->menu());
222 popup
->addSeparator();
224 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
225 popup
->addAction(pasteAction
);
227 // setup 'View Mode' menu
228 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
230 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
231 viewModeMenu
->addAction(iconsMode
);
233 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
234 viewModeMenu
->addAction(detailsMode
);
236 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
237 viewModeMenu
->addAction(previewsMode
);
239 popup
->addMenu(viewModeMenu
);
240 popup
->addSeparator();
242 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
243 popup
->addSeparator();
245 QAction
* propertiesAction
= popup
->addAction(i18n("Properties"));
247 QAction
* activatedAction
= popup
->exec(QCursor::pos());
248 if (activatedAction
== propertiesAction
) {
249 new KPropertiesDialog(m_mainWindow
->activeView()->url());
251 else if (activatedAction
== bookmarkAction
) {
252 const KUrl
& url
= m_mainWindow
->activeView()->url();
254 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
258 popup
->deleteLater();
261 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
263 Q_ASSERT(popup
!= 0);
264 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
266 // insert 'Cut', 'Copy' and 'Paste'
267 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
268 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
269 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
271 popup
->addAction(cutAction
);
272 popup
->addAction(copyAction
);
273 popup
->addAction(pasteAction
);
274 popup
->addSeparator();
277 QAction
* renameAction
= collection
->action("rename");
278 popup
->addAction(renameAction
);
280 // insert 'Move to Trash' and (optionally) 'Delete'
281 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
282 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
283 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
284 const KUrl
& url
= m_mainWindow
->activeView()->url();
285 if (url
.isLocalFile()) {
286 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
287 popup
->addAction(moveToTrashAction
);
290 showDeleteCommand
= true;
293 if (showDeleteCommand
) {
294 QAction
* deleteAction
= collection
->action("delete");
295 popup
->addAction(deleteAction
);
299 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
300 QVector
<KService::Ptr
>& openWithVector
)
302 // Parts of the following code have been taken
303 // from the class KonqOperations located in
304 // libqonq/konq_operations.h of Konqueror.
305 // (Copyright (C) 2000 David Faure <faure@kde.org>)
307 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
308 // are listed which are registered to open the item. As last entry "Other..." will be
309 // attached which allows to select a custom application. If no applications are registered
310 // no sub menu is created at all, only "Open With..." will be offered.
311 bool insertOpenWithItems
= true;
312 const QString
contextMimeType(m_fileInfo
->mimetype());
314 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
315 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
316 KFileItem
* item
= mimeIt
.next();
317 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
320 QList
<QAction
*> openWithActions
;
321 if (insertOpenWithItems
) {
322 // fill the 'Open with' sub menu with application types
323 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
324 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
326 "Type == 'Application'");
327 if (offers
.count() > 0) {
328 KService::List::Iterator it
;
329 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
330 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
331 // The offer list from the KTrader returns duplicate
332 // application entries. Although this seems to be a configuration
333 // problem outside the scope of Dolphin, duplicated entries just
334 // will be skipped here.
335 const QString
appName((*it
)->name());
336 if (!containsEntry(openWithMenu
, appName
)) {
337 const KIcon
icon((*it
)->icon());
338 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
339 openWithVector
.append(*it
);
340 openWithActions
<< action
;
344 openWithMenu
->addSeparator();
345 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
347 openWithActions
<< action
;
348 popup
->addMenu(openWithMenu
);
351 // No applications are registered, hence just offer
352 // a "Open With..." item instead of a sub menu containing
354 QAction
* action
= popup
->addAction(i18n("Open With..."));
355 openWithActions
<< action
;
359 // At least one of the selected items has a different MIME type. In this case
360 // just show a disabled "Open With..." entry.
361 QAction
* action
= popup
->addAction(i18n("Open With..."));
362 action
->setEnabled(false);
365 return openWithActions
;
368 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
369 QVector
<KDesktopFileActions::Service
>& actionsVector
)
371 // Parts of the following code have been taken
372 // from the class KonqOperations located in
373 // libqonq/konq_operations.h of Konqueror.
374 // (Copyright (C) 2000 David Faure <faure@kde.org>)
376 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
378 QList
<QAction
*> serviceActions
;
380 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
383 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
386 filters
<< "*.desktop";
387 dir
.setNameFilters(filters
);
388 QStringList entries
= dir
.entryList(QDir::Files
);
390 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
391 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
392 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
393 //const QStringList types = cfg.readListEntry("ServiceTypes");
395 types
= cfg
.readEntry("ServiceTypes", types
);
396 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
397 // check whether the mime type is equal or whether the
398 // mimegroup (e. g. image/*) is supported
401 if ((*it
) == "all/allfiles") {
402 // The service type is valid for all files, but not for directories.
403 // Check whether the selected items only consist of files...
404 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
406 while (insert
&& mimeIt
.hasNext()) {
407 KFileItem
* item
= mimeIt
.next();
408 insert
= !item
->isDir();
413 // Check whether the MIME types of all selected files match
414 // to the mimetype of the service action. As soon as one MIME
415 // type does not match, no service menu is shown at all.
416 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
418 while (insert
&& mimeIt
.hasNext()) {
419 KFileItem
* item
= mimeIt
.next();
420 const QString
mimeType(item
->mimetype());
421 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
423 insert
= (*it
== mimeType
) ||
424 ((*it
).right(1) == "*") &&
425 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
432 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
433 if (!submenuName
.isEmpty()) {
434 menu
= new KMenu(submenuName
);
435 actionsMenu
->addMenu(menu
);
438 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
439 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
441 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
442 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
443 KDesktopFileActions::Service service
= (*serviceIt
);
444 if (!service
.m_strIcon
.isEmpty()) {
445 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
447 serviceActions
<< action
;
450 QAction
*action
= menu
->addAction(service
.m_strName
);
451 serviceActions
<< action
;
453 actionsVector
.append(service
);
461 const int itemsCount
= actionsMenu
->actions().count();
462 if (itemsCount
== 0) {
463 // no actions are available at all, hence show the "Actions"
465 actionsMenu
->setEnabled(false);
468 if (itemsCount
== 1) {
469 // Exactly one item is available. Instead of showing a sub menu with
470 // only one item, show the item directly in the root menu.
471 if (menu
== actionsMenu
) {
472 // The item is an action, hence show the action in the root menu.
473 const QList
<QAction
*> actions
= actionsMenu
->actions();
474 Q_ASSERT(actions
.count() == 1);
476 const QString text
= actions
[0]->text();
477 const QIcon icon
= actions
[0]->icon();
479 QAction
* action
= popup
->addAction(text
);
480 serviceActions
.clear();
481 serviceActions
<< action
;
484 QAction
* action
= popup
->addAction(icon
, text
);
485 serviceActions
.clear();
486 serviceActions
<< action
;
490 // The item is a sub menu, hence show the sub menu in the root menu.
491 popup
->addMenu(menu
);
493 actionsMenu
->deleteLater();
497 popup
->addMenu(actionsMenu
);
500 return serviceActions
;
503 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
504 const QString
& entryName
) const
508 const QList
<QAction
*> list
= menu
->actions();
509 const uint count
= list
.count();
510 for (uint i
= 0; i
< count
; ++i
) {
511 const QAction
* action
= list
.at(i
);
512 if (action
->text() == entryName
) {
520 #include "dolphincontextmenu.moc"