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 "editbookmarkdialog.h"
30 #include <kactioncollection.h>
31 #include <kbookmarkmanager.h>
32 #include <kbookmark.h>
33 #include <kdesktopfile.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
38 #include <kmimetypetrader.h>
41 #include <kpropertiesdialog.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
48 DolphinContextMenu::DolphinContextMenu(DolphinView
* parent
,
49 KFileItem
* fileInfo
) :
50 m_dolphinView(parent
),
55 void DolphinContextMenu::open()
57 if (m_fileInfo
== 0) {
58 openViewportContextMenu();
61 openItemContextMenu();
65 DolphinContextMenu::~DolphinContextMenu()
69 void DolphinContextMenu::openViewportContextMenu()
71 assert(m_fileInfo
== 0);
72 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
73 KMenu
* popup
= new KMenu(m_dolphinView
);
75 // setup 'Create New' menu
76 KNewMenu
* newMenu
= dolphin
->newMenu();
77 newMenu
->slotCheckUpToDate();
78 newMenu
->setPopupFiles(m_dolphinView
->url());
79 popup
->addMenu(newMenu
->menu());
80 popup
->addSeparator();
82 QAction
* pasteAction
= dolphin
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
83 popup
->addAction(pasteAction
);
85 // setup 'View Mode' menu
86 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
88 QAction
* iconsMode
= dolphin
->actionCollection()->action("icons");
89 viewModeMenu
->addAction(iconsMode
);
91 QAction
* detailsMode
= dolphin
->actionCollection()->action("details");
92 viewModeMenu
->addAction(detailsMode
);
94 QAction
* previewsMode
= dolphin
->actionCollection()->action("previews");
95 viewModeMenu
->addAction(previewsMode
);
97 popup
->addMenu(viewModeMenu
);
98 popup
->addSeparator();
100 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
101 popup
->addSeparator();
103 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
105 QAction
* activatedAction
= popup
->exec(QCursor::pos());
106 if (activatedAction
== propertiesAction
) {
107 new KPropertiesDialog(dolphin
->activeView()->url());
109 else if (activatedAction
== bookmarkAction
) {
110 const KUrl
& url
= dolphin
->activeView()->url();
111 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
115 if (!bookmark
.isNull()) {
116 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
117 KBookmarkGroup root
= manager
->root();
118 root
.addBookmark(manager
, bookmark
);
119 manager
->emitChanged(root
);
123 popup
->deleteLater();
126 void DolphinContextMenu::openItemContextMenu()
128 assert(m_fileInfo
!= 0);
130 KMenu
* popup
= new KMenu(m_dolphinView
);
131 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
132 const KUrl::List urls
= m_dolphinView
->selectedUrls();
134 // insert 'Cut', 'Copy' and 'Paste'
135 const KStandardAction::StandardAction actionNames
[] = {
136 KStandardAction::Cut
,
137 KStandardAction::Copy
,
138 KStandardAction::Paste
141 const int count
= sizeof(actionNames
) / sizeof(KStandardAction::StandardAction
);
142 for (int i
= 0; i
< count
; ++i
) {
143 QAction
* action
= dolphin
->actionCollection()->action(KStandardAction::stdName(actionNames
[i
]));
145 popup
->addAction(action
);
148 popup
->addSeparator();
151 QAction
* renameAction
= dolphin
->actionCollection()->action("rename");
152 popup
->addAction(renameAction
);
154 // insert 'Move to Trash' and (optionally) 'Delete'
155 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
156 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
157 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
158 const KUrl
& url
= dolphin
->activeView()->url();
159 if (url
.isLocalFile()) {
160 QAction
* moveToTrashAction
= dolphin
->actionCollection()->action("move_to_trash");
161 popup
->addAction(moveToTrashAction
);
164 showDeleteCommand
= true;
167 if (showDeleteCommand
) {
168 QAction
* deleteAction
= dolphin
->actionCollection()->action("delete");
169 popup
->addAction(deleteAction
);
172 // insert 'Bookmark this folder...' entry
173 // urls is a list of selected items, so insert boolmark menu if
174 // urls contains only one item, i.e. no multiple selection made
175 QAction
* bookmarkAction
= 0;
176 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
177 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
180 // Insert 'Open With...' sub menu
181 QVector
<KService::Ptr
> openWithVector
;
182 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
184 // Insert 'Actions' sub menu
185 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
186 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
187 popup
->addSeparator();
189 // insert 'Properties...' entry
190 QAction
* propertiesAction
= dolphin
->actionCollection()->action("properties");
191 popup
->addAction(propertiesAction
);
193 QAction
* activatedAction
= popup
->exec(QCursor::pos());
195 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
196 const KUrl
selectedUrl(m_fileInfo
->url());
197 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
198 selectedUrl
.fileName(),
201 if (!bookmark
.isNull()) {
202 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
203 KBookmarkGroup root
= manager
->root();
204 root
.addBookmark(manager
, bookmark
);
205 manager
->emitChanged(root
);
208 else if (serviceActions
.contains(activatedAction
)) {
209 // one of the 'Actions' items has been selected
210 int id
= serviceActions
.indexOf(activatedAction
);
211 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
213 else if (openWithActions
.contains(activatedAction
)) {
214 // one of the 'Open With' items has been selected
215 if (openWithActions
.last() == activatedAction
) {
216 // the item 'Other...' has been selected
217 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
220 int id
= openWithActions
.indexOf(activatedAction
);
221 KService::Ptr servicePtr
= openWithVector
[id
];
222 KRun::run(*servicePtr
, urls
, m_dolphinView
);
226 openWithVector
.clear();
227 actionsVector
.clear();
228 popup
->deleteLater();
231 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
232 QVector
<KService::Ptr
>& openWithVector
)
234 // Parts of the following code have been taken
235 // from the class KonqOperations located in
236 // libqonq/konq_operations.h of Konqueror.
237 // (Copyright (C) 2000 David Faure <faure@kde.org>)
239 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
240 // are listed which are registered to open the item. As last entry "Other..." will be
241 // attached which allows to select a custom application. If no applications are registered
242 // no sub menu is created at all, only "Open With..." will be offered.
243 const KFileItemList list
= m_dolphinView
->selectedItems();
245 bool insertOpenWithItems
= true;
246 const QString
contextMimeType(m_fileInfo
->mimetype());
248 QListIterator
<KFileItem
*> mimeIt(list
);
249 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
250 KFileItem
* item
= mimeIt
.next();
251 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
254 QList
<QAction
*> openWithActions
;
255 if (insertOpenWithItems
) {
256 // fill the 'Open with' sub menu with application types
257 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
258 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
260 "Type == 'Application'");
261 if (offers
.count() > 0) {
262 KService::List::Iterator it
;
263 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
264 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
265 // The offer list from the KTrader returns duplicate
266 // application entries. Although this seems to be a configuration
267 // problem outside the scope of Dolphin, duplicated entries just
268 // will be skipped here.
269 const QString
appName((*it
)->name());
270 if (!containsEntry(openWithMenu
, appName
)) {
271 const KIcon
icon((*it
)->icon());
272 QAction
*action
= openWithMenu
->addAction(icon
, appName
);
273 openWithVector
.append(*it
);
274 openWithActions
<< action
;
278 openWithMenu
->addSeparator();
279 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
281 openWithActions
<< action
;
282 popup
->addSeparator();
283 popup
->addMenu(openWithMenu
);
286 // No applications are registered, hence just offer
287 // a "Open With..." item instead of a sub menu containing
289 QAction
* action
= popup
->addAction(i18n("Open With..."));
290 openWithActions
<< action
;
294 // At least one of the selected items has a different MIME type. In this case
295 // just show a disabled "Open With..." entry.
296 popup
->addSeparator();
297 QAction
* action
= popup
->addAction(i18n("Open With..."));
298 action
->setEnabled(false);
301 return openWithActions
;
304 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
305 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
307 // Parts of the following code have been taken
308 // from the class KonqOperations located in
309 // libqonq/konq_operations.h of Konqueror.
310 // (Copyright (C) 2000 David Faure <faure@kde.org>)
312 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
314 QList
<QAction
*> serviceActions
;
316 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
319 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
322 filters
<< "*.desktop";
323 dir
.setNameFilters(filters
);
324 QStringList entries
= dir
.entryList(QDir::Files
);
326 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
327 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
328 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
329 //const QStringList types = cfg.readListEntry("ServiceTypes");
331 types
= cfg
.readEntry("ServiceTypes", types
);
332 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
333 // check whether the mime type is equal or whether the
334 // mimegroup (e. g. image/*) is supported
337 if ((*it
) == "all/allfiles") {
338 // The service type is valid for all files, but not for directories.
339 // Check whether the selected items only consist of files...
340 const KFileItemList list
= m_dolphinView
->selectedItems();
342 QListIterator
<KFileItem
*> mimeIt(list
);
344 while (insert
&& mimeIt
.hasNext()) {
345 KFileItem
* item
= mimeIt
.next();
346 insert
= !item
->isDir();
351 // Check whether the MIME types of all selected files match
352 // to the mimetype of the service action. As soon as one MIME
353 // type does not match, no service menu is shown at all.
354 const KFileItemList list
= m_dolphinView
->selectedItems();
356 QListIterator
<KFileItem
*> mimeIt(list
);
358 while (insert
&& mimeIt
.hasNext()) {
359 KFileItem
* item
= mimeIt
.next();
360 const QString
mimeType(item
->mimetype());
361 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
363 insert
= (*it
== mimeType
) ||
364 ((*it
).right(1) == "*") &&
365 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
372 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
373 if (!submenuName
.isEmpty()) {
374 menu
= new KMenu(submenuName
);
375 actionsMenu
->addMenu(menu
);
378 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
379 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
381 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
382 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
383 KDEDesktopMimeType::Service service
= (*serviceIt
);
384 if (!service
.m_strIcon
.isEmpty()) {
385 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
387 serviceActions
<< action
;
390 QAction
*action
= menu
->addAction(service
.m_strName
);
391 serviceActions
<< action
;
393 actionsVector
.append(service
);
401 const int itemsCount
= actionsMenu
->actions().count();
402 if (itemsCount
== 0) {
403 // no actions are available at all, hence show the "Actions"
405 actionsMenu
->setEnabled(false);
408 if (itemsCount
== 1) {
409 // Exactly one item is available. Instead of showing a sub menu with
410 // only one item, show the item directly in the root menu.
411 if (menu
== actionsMenu
) {
412 // The item is an action, hence show the action in the root menu.
413 const QList
<QAction
*> actions
= actionsMenu
->actions();
414 assert(actions
.count() == 1);
416 const QString text
= actions
[0]->text();
417 const QIcon icon
= actions
[0]->icon();
419 QAction
* action
= popup
->addAction(text
);
420 serviceActions
.clear();
421 serviceActions
<< action
;
424 QAction
* action
= popup
->addAction(icon
, text
);
425 serviceActions
.clear();
426 serviceActions
<< action
;
430 // The item is a sub menu, hence show the sub menu in the root menu.
431 popup
->addMenu(menu
);
433 actionsMenu
->deleteLater();
437 popup
->addMenu(actionsMenu
);
440 return serviceActions
;
443 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
444 const QString
& entryName
) const
448 const QList
<QAction
*> list
= menu
->actions();
449 const uint count
= list
.count();
450 for (uint i
= 0; i
< count
; ++i
) {
451 const QAction
* action
= list
.at(i
);
452 if (action
->text() == entryName
) {