]>
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 KAction
* createFolderAction
= dolphin
->actionCollection()->action("create_folder");
88 if (createFolderAction
!= 0) {
89 createFolderAction
->plug(createNewMenu
);
92 createNewMenu
->insertSeparator();
96 Q3PtrListIterator
<KAction
> fileGrouptIt(dolphin
->fileGroupActions());
97 while ((action
= fileGrouptIt
.current()) != 0) {
98 action
->plug(createNewMenu
);
102 // TODO: not used yet. See documentation of Dolphin::linkGroupActions()
103 // and Dolphin::linkToDeviceActions() in the header file for details.
105 //createNewMenu->insertSeparator();
107 //QPtrListIterator<KAction> linkGroupIt(dolphin->linkGroupActions());
108 //while ((action = linkGroupIt.current()) != 0) {
109 // action->plug(createNewMenu);
113 //KMenu* linkToDeviceMenu = new KMenu();
114 //QPtrListIterator<KAction> linkToDeviceIt(dolphin->linkToDeviceActions());
115 //while ((action = linkToDeviceIt.current()) != 0) {
116 // action->plug(linkToDeviceMenu);
120 //createNewMenu->insertItem(i18n("Link to Device"), linkToDeviceMenu);
122 popup
->insertItem(SmallIcon("filenew"), i18n("Create New"), createNewMenu
);
123 popup
->insertSeparator();
125 KAction
* pasteAction
= dolphin
->actionCollection()->action(KStdAction::stdName(KStdAction::Paste
));
126 pasteAction
->plug(popup
);
128 // setup 'View Mode' menu
129 KMenu
* viewModeMenu
= new KMenu();
131 KAction
* iconsMode
= dolphin
->actionCollection()->action("icons");
132 iconsMode
->plug(viewModeMenu
);
134 KAction
* detailsMode
= dolphin
->actionCollection()->action("details");
135 detailsMode
->plug(viewModeMenu
);
137 KAction
* previewsMode
= dolphin
->actionCollection()->action("previews");
138 previewsMode
->plug(viewModeMenu
);
140 popup
->insertItem(i18n("View Mode"), viewModeMenu
);
141 popup
->insertSeparator();
143 QAction
*bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
144 popup
->insertSeparator();
146 QAction
*propertiesAction
= popup
->addAction(i18n("Properties..."));
148 QAction
*activatedAction
= popup
->exec(m_pos
);
149 if (activatedAction
== propertiesAction
) {
150 new KPropertiesDialog(dolphin
->activeView()->url());
152 else if (activatedAction
== bookmarkAction
) {
153 const KUrl
& url
= dolphin
->activeView()->url();
154 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
158 if (!bookmark
.isNull()) {
159 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
160 KBookmarkGroup root
= manager
->root();
161 root
.addBookmark(manager
, bookmark
);
162 manager
->emitChanged(root
);
166 popup
->deleteLater();
169 void DolphinContextMenu::openItemContextMenu()
171 // Parts of the following code have been taken
172 // from the class KonqOperations located in
173 // libqonq/konq_operations.h of Konqueror.
174 // (Copyright (C) 2000 David Faure <faure@kde.org>)
176 assert(m_fileInfo
!= 0);
178 KMenu
* popup
= new KMenu(m_dolphinView
);
179 DolphinMainWindow
* dolphin
= m_dolphinView
->mainWindow();
180 const KUrl::List urls
= m_dolphinView
->selectedUrls();
182 // insert 'Cut', 'Copy' and 'Paste'
183 const KStdAction::StdAction actionNames
[] = { KStdAction::Cut
, KStdAction::Copy
, KStdAction::Paste
};
184 const int count
= sizeof(actionNames
) / sizeof(KStdAction::StdAction
);
185 for (int i
= 0; i
< count
; ++i
) {
186 KAction
* action
= dolphin
->actionCollection()->action(KStdAction::stdName(actionNames
[i
]));
191 popup
->insertSeparator();
194 KAction
* renameAction
= dolphin
->actionCollection()->action("rename");
195 renameAction
->plug(popup
);
197 // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
198 const KUrl
& url
= dolphin
->activeView()->url();
199 if (url
.isLocalFile()) {
200 KAction
* moveToTrashAction
= dolphin
->actionCollection()->action("move_to_trash");
201 moveToTrashAction
->plug(popup
);
204 KAction
* deleteAction
= dolphin
->actionCollection()->action("delete");
205 deleteAction
->plug(popup
);
208 // insert 'Bookmark this folder...' entry
209 // urls is a list of selected items, so insert boolmark menu if
210 // urls contains only one item, i.e. no multiple selection made
211 QAction
*bookmarkAction
= 0;
212 if (m_fileInfo
->isDir() && (urls
.count() == 1)) {
213 bookmarkAction
= popup
->addAction(i18n("Bookmark this folder"));
216 popup
->insertSeparator();
218 // Insert 'Open With...' sub menu
219 Q3ValueVector
<KService::Ptr
> openWithVector
;
220 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
222 // Insert 'Actions' sub menu
223 Q3ValueVector
<KDEDesktopMimeType::Service
> actionsVector
;
224 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
226 // insert 'Properties...' entry
227 popup
->insertSeparator();
228 KAction
* propertiesAction
= dolphin
->actionCollection()->action("properties");
229 propertiesAction
->plug(popup
);
231 QAction
*activatedAction
= popup
->exec(m_pos
);
233 if (bookmarkAction
!=0 && activatedAction
== bookmarkAction
) {
234 const KUrl
selectedUrl(m_fileInfo
->url());
235 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
236 selectedUrl
.fileName(),
239 if (!bookmark
.isNull()) {
240 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
241 KBookmarkGroup root
= manager
->root();
242 root
.addBookmark(manager
, bookmark
);
243 manager
->emitChanged(root
);
246 else if (serviceActions
.contains(activatedAction
)) {
247 // one of the 'Actions' items has been selected
248 int id
= serviceActions
.indexOf(activatedAction
);
249 KDEDesktopMimeType::executeService(urls
, actionsVector
[id
]);
251 else if (openWithActions
.contains(activatedAction
)) {
252 // one of the 'Open With' items has been selected
253 if (openWithActions
.last()==activatedAction
) {
254 // the item 'Other...' has been selected
255 KRun::displayOpenWithDialog(urls
, m_dolphinView
);
258 int id
= openWithActions
.indexOf(activatedAction
);
259 KService::Ptr servicePtr
= openWithVector
[id
];
260 KRun::run(*servicePtr
, urls
, m_dolphinView
);
264 openWithVector
.clear();
265 actionsVector
.clear();
266 popup
->deleteLater();
269 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
270 Q3ValueVector
<KService::Ptr
>& openWithVector
)
272 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
273 // are listed which are registered to open the item. As last entry "Other..." will be
274 // attached which allows to select a custom application. If no applications are registered
275 // no sub menu is created at all, only "Open With..." will be offered.
276 const KFileItemList list
= m_dolphinView
->selectedItems();
278 bool insertOpenWithItems
= true;
279 const QString
contextMimeType(m_fileInfo
->mimetype());
280 QListIterator
<KFileItem
*> mimeIt(list
);
281 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
282 KFileItem
* item
= mimeIt
.next();
283 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
286 QList
<QAction
*> openWithActions
;
288 if (insertOpenWithItems
) {
289 // fill the 'Open with' sub menu with application types
290 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
291 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
293 "Type == 'Application'");
294 if (offers
.count() > 0) {
295 KService::List::Iterator it
;
296 KMenu
* openWithMenu
= new KMenu();
297 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
298 // The offer list from the KTrader returns duplicate
299 // application entries. Although this seems to be a configuration
300 // problem outside the scope of Dolphin, duplicated entries just
301 // will be skipped here.
302 const QString
appName((*it
)->name());
303 if (!containsEntry(openWithMenu
, appName
)) {
304 QAction
*action
= openWithMenu
->addAction((*it
)->pixmap(K3Icon::Small
),
306 openWithVector
.append(*it
);
307 openWithActions
<< action
;
311 openWithMenu
->insertSeparator();
312 QAction
*action
= openWithMenu
->addAction(i18n("&Other..."));
313 openWithActions
<< action
;
314 popup
->insertItem(i18n("Open With"), openWithMenu
);
317 // No applications are registered, hence just offer
318 // a "Open With..." item instead of a sub menu containing
320 QAction
*action
= popup
->addAction(i18n("Open With..."));
321 openWithActions
<< action
;
325 // At least one of the selected items has a different MIME type. In this case
326 // just show a disabled "Open With..." entry.
327 QAction
*action
= popup
->addAction(i18n("Open With..."));
328 action
->setEnabled(false);
331 return openWithActions
;
334 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
335 Q3ValueVector
<KDEDesktopMimeType::Service
>& actionsVector
)
337 KMenu
* actionsMenu
= new KMenu();
339 QList
<QAction
*> serviceActions
;
341 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
344 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
346 QStringList entries
= dir
.entryList("*.desktop", QDir::Files
);
348 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
349 KSimpleConfig
cfg(*dirIt
+ *entryIt
, true);
350 cfg
.setDesktopGroup();
351 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
352 const QStringList types
= cfg
.readListEntry("ServiceTypes");
353 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
354 // check whether the mime type is equal or whether the
355 // mimegroup (e. g. image/*) is supported
358 if ((*it
) == "all/allfiles") {
359 // The service type is valid for all files, but not for directories.
360 // Check whether the selected items only consist of files...
361 const KFileItemList list
= m_dolphinView
->selectedItems();
363 QListIterator
<KFileItem
*> mimeIt(list
);
365 while (insert
&& mimeIt
.hasNext()) {
366 KFileItem
* item
= mimeIt
.next();
367 insert
= !item
->isDir();
372 // Check whether the MIME types of all selected files match
373 // to the mimetype of the service action. As soon as one MIME
374 // type does not match, no service menu is shown at all.
375 const KFileItemList list
= m_dolphinView
->selectedItems();
377 QListIterator
<KFileItem
*> mimeIt(list
);
379 while (insert
&& mimeIt
.hasNext()) {
380 KFileItem
* item
= mimeIt
.next();
381 const QString
mimeType(item
->mimetype());
382 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
384 insert
= (*it
== mimeType
) ||
385 ((*it
).right(1) == "*") &&
386 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
393 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
394 if (!submenuName
.isEmpty()) {
396 actionsMenu
->insertItem(submenuName
, menu
, submenuID
);
399 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
400 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
402 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
403 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
404 KDEDesktopMimeType::Service service
= (*serviceIt
);
405 if (!service
.m_strIcon
.isEmpty()) {
406 QAction
*action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
408 serviceActions
<< action
;
411 QAction
*action
= menu
->addAction(service
.m_strName
);
412 serviceActions
<< action
;
414 actionsVector
.append(service
);
422 const int itemsCount
= actionsMenu
->count();
423 if (itemsCount
== 0) {
424 // no actions are available at all, hence show the "Actions"
426 actionsMenu
->setEnabled(false);
429 if (itemsCount
== 1) {
430 // Exactly one item is available. Instead of showing a sub menu with
431 // only one item, show the item directly in the root menu.
432 if (menu
== actionsMenu
) {
433 // The item is an action, hence show the action in the root menu.
434 const int id
= actionsMenu
->idAt(0);
435 const QString
text(actionsMenu
->text(id
));
436 QIcon iconSet
= actionsMenu
->iconSet(id
);
437 if (iconSet
.isNull()) {
438 QAction
*action
= popup
->addAction(text
);
439 serviceActions
.clear();
440 serviceActions
<< action
;
443 QAction
*action
= popup
->addAction(iconSet
, text
);
444 serviceActions
.clear();
445 serviceActions
<< action
;
449 // The item is a sub menu, hence show the sub menu in the root menu.
450 popup
->insertItem(actionsMenu
->text(submenuID
), menu
);
452 actionsMenu
->deleteLater();
456 popup
->insertItem(i18n("Actions"), actionsMenu
);
459 return serviceActions
;
462 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
463 const QString
& entryName
) const
467 const uint count
= menu
->count();
468 for (uint i
= 0; i
< count
; ++i
) {
469 const int id
= menu
->idAt(i
);
470 if (menu
->text(id
) == entryName
) {