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(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' for local Urls, otherwise insert 'Delete'
155 const KUrl
& url
= dolphin
->activeView()->url();
156 if (url
.isLocalFile()) {
157 QAction
* moveToTrashAction
= dolphin
->actionCollection()->action("move_to_trash");
158 popup
->addAction(moveToTrashAction
);
161 QAction
* deleteAction
= dolphin
->actionCollection()->action("delete");
162 popup
->addAction(deleteAction
);
165 // insert 'Bookmark this folder...' entry
166 // urls is a list of selected items, so insert boolmark menu if
167 // urls contains only one item, i.e. no multiple selection made
168 QAction
* bookmarkAction
= 0;
169 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
170 bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
173 // Insert 'Open With...' sub menu
174 QVector
<KService::Ptr
> openWithVector
;
175 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
177 // Insert 'Actions' sub menu
178 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
179 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
180 popup
->addSeparator();
182 // insert 'Properties...' entry
183 QAction
* propertiesAction
= dolphin
->actionCollection()->action("properties");
184 popup
->addAction(propertiesAction
);
186 QAction
* activatedAction
= popup
->exec(QCursor::pos());
188 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
189 const KUrl
selectedUrl(m_fileInfo
->url());
190 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
191 selectedUrl
.fileName(),
194 if (!bookmark
.isNull()) {
195 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
196 KBookmarkGroup root
= manager
->root();
197 root
.addBookmark(manager
, bookmark
);
198 manager
->emitChanged(root
);
201 else if (serviceActions
.contains(activatedAction
)) {
202 // one of the 'Actions' items has been selected
203 int id
= serviceActions
.indexOf(activatedAction
);
204 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
206 else if (openWithActions
.contains(activatedAction
)) {
207 // one of the 'Open With' items has been selected
208 if (openWithActions
.last() == activatedAction
) {
209 // the item 'Other...' has been selected
210 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
213 int id
= openWithActions
.indexOf(activatedAction
);
214 KService::Ptr servicePtr
= openWithVector
[id
];
215 KRun::run(*servicePtr
, urls
, m_dolphinView
);
219 openWithVector
.clear();
220 actionsVector
.clear();
221 popup
->deleteLater();
224 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
225 QVector
<KService::Ptr
>& openWithVector
)
227 // Parts of the following code have been taken
228 // from the class KonqOperations located in
229 // libqonq/konq_operations.h of Konqueror.
230 // (Copyright (C) 2000 David Faure <faure@kde.org>)
232 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
233 // are listed which are registered to open the item. As last entry "Other..." will be
234 // attached which allows to select a custom application. If no applications are registered
235 // no sub menu is created at all, only "Open With..." will be offered.
236 const KFileItemList list
= m_dolphinView
->selectedItems();
238 bool insertOpenWithItems
= true;
239 const QString
contextMimeType(m_fileInfo
->mimetype());
241 QListIterator
<KFileItem
*> mimeIt(list
);
242 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
243 KFileItem
* item
= mimeIt
.next();
244 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
247 QList
<QAction
*> openWithActions
;
248 if (insertOpenWithItems
) {
249 // fill the 'Open with' sub menu with application types
250 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
251 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
253 "Type == 'Application'");
254 if (offers
.count() > 0) {
255 KService::List::Iterator it
;
256 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
257 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
258 // The offer list from the KTrader returns duplicate
259 // application entries. Although this seems to be a configuration
260 // problem outside the scope of Dolphin, duplicated entries just
261 // will be skipped here.
262 const QString
appName((*it
)->name());
263 if (!containsEntry(openWithMenu
, appName
)) {
264 const KIcon
icon((*it
)->icon());
265 QAction
*action
= openWithMenu
->addAction(icon
, appName
);
266 openWithVector
.append(*it
);
267 openWithActions
<< action
;
271 openWithMenu
->addSeparator();
272 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
274 openWithActions
<< action
;
275 popup
->addSeparator();
276 popup
->addMenu(openWithMenu
);
279 // No applications are registered, hence just offer
280 // a "Open With..." item instead of a sub menu containing
282 QAction
* action
= popup
->addAction(i18n("Open With..."));
283 openWithActions
<< action
;
287 // At least one of the selected items has a different MIME type. In this case
288 // just show a disabled "Open With..." entry.
289 popup
->addSeparator();
290 QAction
* action
= popup
->addAction(i18n("Open With..."));
291 action
->setEnabled(false);
294 return openWithActions
;
297 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
298 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
300 // Parts of the following code have been taken
301 // from the class KonqOperations located in
302 // libqonq/konq_operations.h of Konqueror.
303 // (Copyright (C) 2000 David Faure <faure@kde.org>)
305 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
307 QList
<QAction
*> serviceActions
;
309 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
312 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
315 filters
<< "*.desktop";
316 dir
.setNameFilters(filters
);
317 QStringList entries
= dir
.entryList(QDir::Files
);
319 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
320 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
321 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
322 //const QStringList types = cfg.readListEntry("ServiceTypes");
324 types
= cfg
.readEntry("ServiceTypes", types
);
325 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
326 // check whether the mime type is equal or whether the
327 // mimegroup (e. g. image/*) is supported
330 if ((*it
) == "all/allfiles") {
331 // The service type is valid for all files, but not for directories.
332 // Check whether the selected items only consist of files...
333 const KFileItemList list
= m_dolphinView
->selectedItems();
335 QListIterator
<KFileItem
*> mimeIt(list
);
337 while (insert
&& mimeIt
.hasNext()) {
338 KFileItem
* item
= mimeIt
.next();
339 insert
= !item
->isDir();
344 // Check whether the MIME types of all selected files match
345 // to the mimetype of the service action. As soon as one MIME
346 // type does not match, no service menu is shown at all.
347 const KFileItemList list
= m_dolphinView
->selectedItems();
349 QListIterator
<KFileItem
*> mimeIt(list
);
351 while (insert
&& mimeIt
.hasNext()) {
352 KFileItem
* item
= mimeIt
.next();
353 const QString
mimeType(item
->mimetype());
354 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
356 insert
= (*it
== mimeType
) ||
357 ((*it
).right(1) == "*") &&
358 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
365 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
366 if (!submenuName
.isEmpty()) {
367 menu
= new KMenu(submenuName
);
368 actionsMenu
->addMenu(menu
);
371 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
372 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
374 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
375 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
376 KDEDesktopMimeType::Service service
= (*serviceIt
);
377 if (!service
.m_strIcon
.isEmpty()) {
378 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
380 serviceActions
<< action
;
383 QAction
*action
= menu
->addAction(service
.m_strName
);
384 serviceActions
<< action
;
386 actionsVector
.append(service
);
394 const int itemsCount
= actionsMenu
->actions().count();
395 if (itemsCount
== 0) {
396 // no actions are available at all, hence show the "Actions"
398 actionsMenu
->setEnabled(false);
401 if (itemsCount
== 1) {
402 // Exactly one item is available. Instead of showing a sub menu with
403 // only one item, show the item directly in the root menu.
404 if (menu
== actionsMenu
) {
405 // The item is an action, hence show the action in the root menu.
406 const QList
<QAction
*> actions
= actionsMenu
->actions();
407 assert(actions
.count() == 1);
409 const QString text
= actions
[0]->text();
410 const QIcon icon
= actions
[0]->icon();
412 QAction
* action
= popup
->addAction(text
);
413 serviceActions
.clear();
414 serviceActions
<< action
;
417 QAction
* action
= popup
->addAction(icon
, text
);
418 serviceActions
.clear();
419 serviceActions
<< action
;
423 // The item is a sub menu, hence show the sub menu in the root menu.
424 popup
->addMenu(menu
);
426 actionsMenu
->deleteLater();
430 popup
->addMenu(actionsMenu
);
433 return serviceActions
;
436 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
437 const QString
& entryName
) const
441 const QList
<QAction
*> list
= menu
->actions();
442 const uint count
= list
.count();
443 for (uint i
= 0; i
< count
; ++i
) {
444 const QAction
* action
= list
.at(i
);
445 if (action
->text() == entryName
) {