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
,
51 m_dolphinView(parent
),
57 void DolphinContextMenu::open()
59 if (m_fileInfo
== 0) {
60 openViewportContextMenu();
63 openItemContextMenu();
67 DolphinContextMenu::~DolphinContextMenu()
71 void DolphinContextMenu::openViewportContextMenu()
73 assert(m_fileInfo
== 0);
74 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
75 KMenu
* popup
= new KMenu(m_dolphinView
);
77 // setup 'Create New' menu
78 KNewMenu
* newMenu
= dolphin
->newMenu();
79 newMenu
->slotCheckUpToDate();
80 newMenu
->setPopupFiles(m_dolphinView
->url());
81 popup
->addMenu(newMenu
->menu());
82 popup
->addSeparator();
84 QAction
* pasteAction
= dolphin
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
85 popup
->addAction(pasteAction
);
87 // setup 'View Mode' menu
88 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
90 QAction
* iconsMode
= dolphin
->actionCollection()->action("icons");
91 viewModeMenu
->addAction(iconsMode
);
93 QAction
* detailsMode
= dolphin
->actionCollection()->action("details");
94 viewModeMenu
->addAction(detailsMode
);
96 QAction
* previewsMode
= dolphin
->actionCollection()->action("previews");
97 viewModeMenu
->addAction(previewsMode
);
99 popup
->addMenu(viewModeMenu
);
100 popup
->addSeparator();
102 QAction
* bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
103 popup
->addSeparator();
105 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
107 QAction
* activatedAction
= popup
->exec(m_pos
);
108 if (activatedAction
== propertiesAction
) {
109 new KPropertiesDialog(dolphin
->activeView()->url());
111 else if (activatedAction
== bookmarkAction
) {
112 const KUrl
& url
= dolphin
->activeView()->url();
113 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
117 if (!bookmark
.isNull()) {
118 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
119 KBookmarkGroup root
= manager
->root();
120 root
.addBookmark(manager
, bookmark
);
121 manager
->emitChanged(root
);
125 popup
->deleteLater();
128 void DolphinContextMenu::openItemContextMenu()
130 assert(m_fileInfo
!= 0);
132 KMenu
* popup
= new KMenu(m_dolphinView
);
133 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
134 const KUrl::List urls
= m_dolphinView
->selectedUrls();
136 // insert 'Cut', 'Copy' and 'Paste'
137 const KStandardAction::StandardAction actionNames
[] = {
138 KStandardAction::Cut
,
139 KStandardAction::Copy
,
140 KStandardAction::Paste
143 const int count
= sizeof(actionNames
) / sizeof(KStandardAction::StandardAction
);
144 for (int i
= 0; i
< count
; ++i
) {
145 QAction
* action
= dolphin
->actionCollection()->action(KStandardAction::stdName(actionNames
[i
]));
147 popup
->addAction(action
);
150 popup
->addSeparator();
153 QAction
* renameAction
= dolphin
->actionCollection()->action("rename");
154 popup
->addAction(renameAction
);
156 // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
157 const KUrl
& url
= dolphin
->activeView()->url();
158 if (url
.isLocalFile()) {
159 QAction
* moveToTrashAction
= dolphin
->actionCollection()->action("move_to_trash");
160 popup
->addAction(moveToTrashAction
);
163 QAction
* deleteAction
= dolphin
->actionCollection()->action("delete");
164 popup
->addAction(deleteAction
);
167 // insert 'Bookmark this folder...' entry
168 // urls is a list of selected items, so insert boolmark menu if
169 // urls contains only one item, i.e. no multiple selection made
170 QAction
* bookmarkAction
= 0;
171 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
172 bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
175 // Insert 'Open With...' sub menu
176 QVector
<KService::Ptr
> openWithVector
;
177 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
179 // Insert 'Actions' sub menu
180 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
181 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
182 popup
->addSeparator();
184 // insert 'Properties...' entry
185 QAction
* propertiesAction
= dolphin
->actionCollection()->action("properties");
186 popup
->addAction(propertiesAction
);
188 QAction
* activatedAction
= popup
->exec(m_pos
);
190 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
191 const KUrl
selectedUrl(m_fileInfo
->url());
192 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
193 selectedUrl
.fileName(),
196 if (!bookmark
.isNull()) {
197 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
198 KBookmarkGroup root
= manager
->root();
199 root
.addBookmark(manager
, bookmark
);
200 manager
->emitChanged(root
);
203 else if (serviceActions
.contains(activatedAction
)) {
204 // one of the 'Actions' items has been selected
205 int id
= serviceActions
.indexOf(activatedAction
);
206 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
208 else if (openWithActions
.contains(activatedAction
)) {
209 // one of the 'Open With' items has been selected
210 if (openWithActions
.last() == activatedAction
) {
211 // the item 'Other...' has been selected
212 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
215 int id
= openWithActions
.indexOf(activatedAction
);
216 KService::Ptr servicePtr
= openWithVector
[id
];
217 KRun::run(*servicePtr
, urls
, m_dolphinView
);
221 openWithVector
.clear();
222 actionsVector
.clear();
223 popup
->deleteLater();
226 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
227 QVector
<KService::Ptr
>& openWithVector
)
229 // Parts of the following code have been taken
230 // from the class KonqOperations located in
231 // libqonq/konq_operations.h of Konqueror.
232 // (Copyright (C) 2000 David Faure <faure@kde.org>)
234 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
235 // are listed which are registered to open the item. As last entry "Other..." will be
236 // attached which allows to select a custom application. If no applications are registered
237 // no sub menu is created at all, only "Open With..." will be offered.
238 const KFileItemList list
= m_dolphinView
->selectedItems();
240 bool insertOpenWithItems
= true;
241 const QString
contextMimeType(m_fileInfo
->mimetype());
243 QListIterator
<KFileItem
*> mimeIt(list
);
244 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
245 KFileItem
* item
= mimeIt
.next();
246 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
249 QList
<QAction
*> openWithActions
;
250 if (insertOpenWithItems
) {
251 // fill the 'Open with' sub menu with application types
252 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
253 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
255 "Type == 'Application'");
256 if (offers
.count() > 0) {
257 KService::List::Iterator it
;
258 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
259 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
260 // The offer list from the KTrader returns duplicate
261 // application entries. Although this seems to be a configuration
262 // problem outside the scope of Dolphin, duplicated entries just
263 // will be skipped here.
264 const QString
appName((*it
)->name());
265 if (!containsEntry(openWithMenu
, appName
)) {
266 const KIcon
icon((*it
)->icon());
267 QAction
*action
= openWithMenu
->addAction(icon
, appName
);
268 openWithVector
.append(*it
);
269 openWithActions
<< action
;
273 openWithMenu
->addSeparator();
274 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
276 openWithActions
<< action
;
277 popup
->addSeparator();
278 popup
->addMenu(openWithMenu
);
281 // No applications are registered, hence just offer
282 // a "Open With..." item instead of a sub menu containing
284 QAction
* action
= popup
->addAction(i18n("Open With..."));
285 openWithActions
<< action
;
289 // At least one of the selected items has a different MIME type. In this case
290 // just show a disabled "Open With..." entry.
291 popup
->addSeparator();
292 QAction
* action
= popup
->addAction(i18n("Open With..."));
293 action
->setEnabled(false);
296 return openWithActions
;
299 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
300 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
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 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
309 QList
<QAction
*> serviceActions
;
311 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
314 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
317 filters
<< "*.desktop";
318 dir
.setNameFilters(filters
);
319 QStringList entries
= dir
.entryList(QDir::Files
);
321 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
322 KSimpleConfig
cfg(*dirIt
+ *entryIt
, true);
323 cfg
.setDesktopGroup();
324 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
325 //const QStringList types = cfg.readListEntry("ServiceTypes");
327 types
= cfg
.readEntry("ServiceTypes", types
);
328 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
329 // check whether the mime type is equal or whether the
330 // mimegroup (e. g. image/*) is supported
333 if ((*it
) == "all/allfiles") {
334 // The service type is valid for all files, but not for directories.
335 // Check whether the selected items only consist of files...
336 const KFileItemList list
= m_dolphinView
->selectedItems();
338 QListIterator
<KFileItem
*> mimeIt(list
);
340 while (insert
&& mimeIt
.hasNext()) {
341 KFileItem
* item
= mimeIt
.next();
342 insert
= !item
->isDir();
347 // Check whether the MIME types of all selected files match
348 // to the mimetype of the service action. As soon as one MIME
349 // type does not match, no service menu is shown at all.
350 const KFileItemList list
= m_dolphinView
->selectedItems();
352 QListIterator
<KFileItem
*> mimeIt(list
);
354 while (insert
&& mimeIt
.hasNext()) {
355 KFileItem
* item
= mimeIt
.next();
356 const QString
mimeType(item
->mimetype());
357 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
359 insert
= (*it
== mimeType
) ||
360 ((*it
).right(1) == "*") &&
361 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
368 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
369 if (!submenuName
.isEmpty()) {
370 menu
= new KMenu(submenuName
);
371 actionsMenu
->addMenu(menu
);
374 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
375 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
377 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
378 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
379 KDEDesktopMimeType::Service service
= (*serviceIt
);
380 if (!service
.m_strIcon
.isEmpty()) {
381 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
383 serviceActions
<< action
;
386 QAction
*action
= menu
->addAction(service
.m_strName
);
387 serviceActions
<< action
;
389 actionsVector
.append(service
);
397 const int itemsCount
= actionsMenu
->actions().count();
398 if (itemsCount
== 0) {
399 // no actions are available at all, hence show the "Actions"
401 actionsMenu
->setEnabled(false);
404 if (itemsCount
== 1) {
405 // Exactly one item is available. Instead of showing a sub menu with
406 // only one item, show the item directly in the root menu.
407 if (menu
== actionsMenu
) {
408 // The item is an action, hence show the action in the root menu.
409 const QList
<QAction
*> actions
= actionsMenu
->actions();
410 assert(actions
.count() == 1);
412 const QString text
= actions
[0]->text();
413 const QIcon icon
= actions
[0]->icon();
415 QAction
* action
= popup
->addAction(text
);
416 serviceActions
.clear();
417 serviceActions
<< action
;
420 QAction
* action
= popup
->addAction(icon
, text
);
421 serviceActions
.clear();
422 serviceActions
<< action
;
426 // The item is a sub menu, hence show the sub menu in the root menu.
427 popup
->addMenu(menu
);
429 actionsMenu
->deleteLater();
433 popup
->addMenu(actionsMenu
);
436 return serviceActions
;
439 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
440 const QString
& entryName
) const
444 const QList
<QAction
*> list
= menu
->actions();
445 const uint count
= list
.count();
446 for (uint i
= 0; i
< count
; ++i
) {
447 const QAction
* action
= list
.at(i
);
448 if (action
->text() == entryName
) {