]>
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 <kactioncollection.h>
24 #include <kbookmarkmanager.h>
25 #include <kbookmark.h>
26 #include <kmimetypetrader.h>
31 #include <Q3ValueList>
33 #include <kstandarddirs.h>
34 #include <kiconloader.h>
36 #include <kpropertiesdialog.h>
37 #include <kdesktopfile.h>
39 #include <kio/netaccess.h>
41 #include <kstdaction.h>
43 #include "dolphinmainwindow.h"
44 #include "dolphinview.h"
45 #include "editbookmarkdialog.h"
46 #include "dolphinsettings.h"
49 DolphinContextMenu::DolphinContextMenu(DolphinView
* parent
,
52 m_dolphinView(parent
),
58 void DolphinContextMenu::open()
60 if (m_fileInfo
== 0) {
61 openViewportContextMenu();
64 openItemContextMenu();
68 DolphinContextMenu::~DolphinContextMenu()
72 void DolphinContextMenu::openViewportContextMenu()
74 // Parts of the following code have been taken
75 // from the class KonqOperations located in
76 // libqonq/konq_operations.h of Konqueror.
77 // (Copyright (C) 2000 David Faure <faure@kde.org>)
79 assert(m_fileInfo
== 0);
81 KMenu
* popup
= new KMenu(m_dolphinView
);
82 DolphinMainWindow
*dolphin
= m_dolphinView
->mainWindow();
84 // setup 'Create New' menu
85 KMenu
* createNewMenu
= new KMenu();
87 QAction
* createFolderAction
= dolphin
->actionCollection()->action("create_folder");
88 if (createFolderAction
!= 0) {
89 createNewMenu
->addAction(createFolderAction
);
92 createNewMenu
->insertSeparator();
94 QLinkedListIterator
<QAction
*> fileGrouptIt(dolphin
->fileGroupActions());
95 while (fileGrouptIt
.hasNext()) {
96 createNewMenu
->addAction(fileGrouptIt
.next());
99 // TODO: not used yet. See documentation of Dolphin::linkGroupActions()
100 // and Dolphin::linkToDeviceActions() in the header file for details.
102 //createNewMenu->insertSeparator();
104 //QPtrListIterator<KAction> linkGroupIt(dolphin->linkGroupActions());
105 //while ((action = linkGroupIt.current()) != 0) {
106 // action->plug(createNewMenu);
110 //KMenu* linkToDeviceMenu = new KMenu();
111 //QPtrListIterator<KAction> linkToDeviceIt(dolphin->linkToDeviceActions());
112 //while ((action = linkToDeviceIt.current()) != 0) {
113 // action->plug(linkToDeviceMenu);
117 //createNewMenu->insertItem(i18n("Link to Device"), linkToDeviceMenu);
119 popup
->insertItem(SmallIcon("filenew"), i18n("Create New"), createNewMenu
);
120 popup
->insertSeparator();
122 QAction
* pasteAction
= dolphin
->actionCollection()->action(KStdAction::stdName(KStdAction::Paste
));
123 popup
->addAction(pasteAction
);
125 // setup 'View Mode' menu
126 KMenu
* viewModeMenu
= new KMenu();
128 QAction
* iconsMode
= dolphin
->actionCollection()->action("icons");
129 viewModeMenu
->addAction(iconsMode
);
131 QAction
* detailsMode
= dolphin
->actionCollection()->action("details");
132 viewModeMenu
->addAction(detailsMode
);
134 QAction
* previewsMode
= dolphin
->actionCollection()->action("previews");
135 viewModeMenu
->addAction(previewsMode
);
137 popup
->insertItem(i18n("View Mode"), viewModeMenu
);
138 popup
->insertSeparator();
140 QAction
*bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
141 popup
->insertSeparator();
143 QAction
*propertiesAction
= popup
->addAction(i18n("Properties..."));
145 QAction
*activatedAction
= popup
->exec(m_pos
);
146 if (activatedAction
== propertiesAction
) {
147 new KPropertiesDialog(dolphin
->activeView()->url());
149 else if (activatedAction
== bookmarkAction
) {
150 const KUrl
& url
= dolphin
->activeView()->url();
151 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
155 if (!bookmark
.isNull()) {
156 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
157 KBookmarkGroup root
= manager
->root();
158 root
.addBookmark(manager
, bookmark
);
159 manager
->emitChanged(root
);
163 popup
->deleteLater();
166 void DolphinContextMenu::openItemContextMenu()
168 // Parts of the following code have been taken
169 // from the class KonqOperations located in
170 // libqonq/konq_operations.h of Konqueror.
171 // (Copyright (C) 2000 David Faure <faure@kde.org>)
173 assert(m_fileInfo
!= 0);
175 KMenu
* popup
= new KMenu(m_dolphinView
);
176 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
177 const KUrl::List urls
= m_dolphinView
->selectedUrls();
179 // insert 'Cut', 'Copy' and 'Paste'
180 const KStdAction::StdAction actionNames
[] = { KStdAction::Cut
, KStdAction::Copy
, KStdAction::Paste
};
181 const int count
= sizeof(actionNames
) / sizeof(KStdAction::StdAction
);
182 for (int i
= 0; i
< count
; ++i
) {
183 QAction
* action
= dolphin
->actionCollection()->action(KStdAction::stdName(actionNames
[i
]));
185 popup
->addAction(action
);
187 popup
->insertSeparator();
190 QAction
* renameAction
= dolphin
->actionCollection()->action("rename");
191 popup
->addAction(renameAction
);
193 // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
194 const KUrl
& url
= dolphin
->activeView()->url();
195 if (url
.isLocalFile()) {
196 QAction
* moveToTrashAction
= dolphin
->actionCollection()->action("move_to_trash");
197 popup
->addAction(moveToTrashAction
);
200 QAction
* deleteAction
= dolphin
->actionCollection()->action("delete");
201 popup
->addAction(deleteAction
);
204 // insert 'Bookmark this folder...' entry
205 // urls is a list of selected items, so insert boolmark menu if
206 // urls contains only one item, i.e. no multiple selection made
207 QAction
*bookmarkAction
= 0;
208 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
209 bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
212 popup
->insertSeparator();
214 // Insert 'Open With...' sub menu
215 Q3ValueVector
<KService::Ptr
> openWithVector
;
216 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
218 // Insert 'Actions' sub menu
219 Q3ValueVector
<KDEDesktopMimeType::Service
> actionsVector
;
220 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
222 // insert 'Properties...' entry
223 popup
->insertSeparator();
224 QAction
* propertiesAction
= dolphin
->actionCollection()->action("properties");
225 popup
->addAction(propertiesAction
);
227 QAction
*activatedAction
= popup
->exec(m_pos
);
229 if (bookmarkAction
!=0 && activatedAction
== bookmarkAction
) {
230 const KUrl
selectedUrl(m_fileInfo
->url());
231 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
232 selectedUrl
.fileName(),
235 if (!bookmark
.isNull()) {
236 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
237 KBookmarkGroup root
= manager
->root();
238 root
.addBookmark(manager
, bookmark
);
239 manager
->emitChanged(root
);
242 else if (serviceActions
.contains(activatedAction
)) {
243 // one of the 'Actions' items has been selected
244 int id
= serviceActions
.indexOf(activatedAction
);
245 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
247 else if (openWithActions
.contains(activatedAction
)) {
248 // one of the 'Open With' items has been selected
249 if (openWithActions
.last()==activatedAction
) {
250 // the item 'Other...' has been selected
251 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
254 int id
= openWithActions
.indexOf(activatedAction
);
255 KService::Ptr servicePtr
= openWithVector
[id
];
256 KRun::run(*servicePtr
, urls
, m_dolphinView
);
260 openWithVector
.clear();
261 actionsVector
.clear();
262 popup
->deleteLater();
265 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
266 Q3ValueVector
<KService::Ptr
>& openWithVector
)
268 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
269 // are listed which are registered to open the item. As last entry "Other..." will be
270 // attached which allows to select a custom application. If no applications are registered
271 // no sub menu is created at all, only "Open With..." will be offered.
272 const KFileItemList list
= m_dolphinView
->selectedItems();
274 bool insertOpenWithItems
= true;
275 const QString
contextMimeType(m_fileInfo
->mimetype());
276 QListIterator
<KFileItem
*> mimeIt(list
);
277 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
278 KFileItem
* item
= mimeIt
.next();
279 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
282 QList
<QAction
*> openWithActions
;
284 if (insertOpenWithItems
) {
285 // fill the 'Open with' sub menu with application types
286 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
287 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
289 "Type == 'Application'");
290 if (offers
.count() > 0) {
291 KService::List::Iterator it
;
292 KMenu
* openWithMenu
= new KMenu();
293 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
294 // The offer list from the KTrader returns duplicate
295 // application entries. Although this seems to be a configuration
296 // problem outside the scope of Dolphin, duplicated entries just
297 // will be skipped here.
298 const QString
appName((*it
)->name());
299 if (!containsEntry(openWithMenu
, appName
)) {
300 QAction
*action
= openWithMenu
->addAction((*it
)->pixmap(K3Icon::Small
),
302 openWithVector
.append(*it
);
303 openWithActions
<< action
;
307 openWithMenu
->insertSeparator();
308 QAction
*action
= openWithMenu
->addAction(i18n("&Other..."));
309 openWithActions
<< action
;
310 popup
->insertItem(i18n("Open With"), openWithMenu
);
313 // No applications are registered, hence just offer
314 // a "Open With..." item instead of a sub menu containing
316 QAction
*action
= popup
->addAction(i18n("Open With..."));
317 openWithActions
<< action
;
321 // At least one of the selected items has a different MIME type. In this case
322 // just show a disabled "Open With..." entry.
323 QAction
*action
= popup
->addAction(i18n("Open With..."));
324 action
->setEnabled(false);
327 return openWithActions
;
330 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
331 Q3ValueVector
<KDEDesktopMimeType::Service
>& actionsVector
)
333 KMenu
* actionsMenu
= new KMenu();
335 QList
<QAction
*> serviceActions
;
337 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
340 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
342 QStringList entries
= dir
.entryList("*.desktop", QDir::Files
);
344 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
345 KSimpleConfig
cfg(*dirIt
+ *entryIt
, true);
346 cfg
.setDesktopGroup();
347 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
348 const QStringList types
= cfg
.readListEntry("ServiceTypes");
349 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
350 // check whether the mime type is equal or whether the
351 // mimegroup (e. g. image/*) is supported
354 if ((*it
) == "all/allfiles") {
355 // The service type is valid for all files, but not for directories.
356 // Check whether the selected items only consist of files...
357 const KFileItemList list
= m_dolphinView
->selectedItems();
359 QListIterator
<KFileItem
*> mimeIt(list
);
361 while (insert
&& mimeIt
.hasNext()) {
362 KFileItem
* item
= mimeIt
.next();
363 insert
= !item
->isDir();
368 // Check whether the MIME types of all selected files match
369 // to the mimetype of the service action. As soon as one MIME
370 // type does not match, no service menu is shown at all.
371 const KFileItemList list
= m_dolphinView
->selectedItems();
373 QListIterator
<KFileItem
*> mimeIt(list
);
375 while (insert
&& mimeIt
.hasNext()) {
376 KFileItem
* item
= mimeIt
.next();
377 const QString
mimeType(item
->mimetype());
378 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
380 insert
= (*it
== mimeType
) ||
381 ((*it
).right(1) == "*") &&
382 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
389 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
390 if (!submenuName
.isEmpty()) {
392 actionsMenu
->insertItem(submenuName
, menu
, submenuID
);
395 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
396 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
398 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
399 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
400 KDEDesktopMimeType::Service service
= (*serviceIt
);
401 if (!service
.m_strIcon
.isEmpty()) {
402 QAction
*action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
404 serviceActions
<< action
;
407 QAction
*action
= menu
->addAction(service
.m_strName
);
408 serviceActions
<< action
;
410 actionsVector
.append(service
);
418 const int itemsCount
= actionsMenu
->count();
419 if (itemsCount
== 0) {
420 // no actions are available at all, hence show the "Actions"
422 actionsMenu
->setEnabled(false);
425 if (itemsCount
== 1) {
426 // Exactly one item is available. Instead of showing a sub menu with
427 // only one item, show the item directly in the root menu.
428 if (menu
== actionsMenu
) {
429 // The item is an action, hence show the action in the root menu.
430 const int id
= actionsMenu
->idAt(0);
431 const QString
text(actionsMenu
->text(id
));
432 QIcon iconSet
= actionsMenu
->iconSet(id
);
433 if (iconSet
.isNull()) {
434 QAction
*action
= popup
->addAction(text
);
435 serviceActions
.clear();
436 serviceActions
<< action
;
439 QAction
*action
= popup
->addAction(iconSet
, text
);
440 serviceActions
.clear();
441 serviceActions
<< action
;
445 // The item is a sub menu, hence show the sub menu in the root menu.
446 popup
->insertItem(actionsMenu
->text(submenuID
), menu
);
448 actionsMenu
->deleteLater();
452 popup
->insertItem(i18n("Actions"), actionsMenu
);
455 return serviceActions
;
458 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
459 const QString
& entryName
) const
463 const uint count
= menu
->count();
464 for (uint i
= 0; i
< count
; ++i
) {
465 const int id
= menu
->idAt(i
);
466 if (menu
->text(id
) == entryName
) {