]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
fixing more warnings
[dolphin.git] / src / dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "dolphincontextmenu.h"
22
23 #include <kactioncollection.h>
24 #include <kbookmarkmanager.h>
25 #include <kbookmark.h>
26 #include <kmimetypetrader.h>
27 #include <klocale.h>
28 #include <krun.h>
29 #include <qdir.h>
30 //Added by qt3to4:
31 #include <Q3ValueList>
32 #include <kglobal.h>
33 #include <kstandarddirs.h>
34 #include <kiconloader.h>
35 #include <kaction.h>
36 #include <kpropertiesdialog.h>
37 #include <kdesktopfile.h>
38 #include <assert.h>
39 #include <kio/netaccess.h>
40 #include <kmenu.h>
41 #include <kstdaction.h>
42
43 #include "dolphinmainwindow.h"
44 #include "dolphinview.h"
45 #include "editbookmarkdialog.h"
46 #include "dolphinsettings.h"
47
48
49 DolphinContextMenu::DolphinContextMenu(DolphinView* parent,
50 KFileItem* fileInfo,
51 const QPoint& pos) :
52 m_dolphinView(parent),
53 m_fileInfo(fileInfo),
54 m_pos(pos)
55 {
56 }
57
58 void DolphinContextMenu::open()
59 {
60 if (m_fileInfo == 0) {
61 openViewportContextMenu();
62 }
63 else {
64 openItemContextMenu();
65 }
66 }
67
68 DolphinContextMenu::~DolphinContextMenu()
69 {
70 }
71
72 void DolphinContextMenu::openViewportContextMenu()
73 {
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>)
78
79 assert(m_fileInfo == 0);
80
81 KMenu* popup = new KMenu(m_dolphinView);
82 DolphinMainWindow *dolphin = m_dolphinView->mainWindow();
83
84 // setup 'Create New' menu
85 KMenu* createNewMenu = new KMenu();
86
87 KAction* createFolderAction = dolphin->actionCollection()->action("create_folder");
88 if (createFolderAction != 0) {
89 createFolderAction->plug(createNewMenu);
90 }
91
92 createNewMenu->insertSeparator();
93
94 KAction* action = 0;
95
96 Q3PtrListIterator<KAction> fileGrouptIt(dolphin->fileGroupActions());
97 while ((action = fileGrouptIt.current()) != 0) {
98 action->plug(createNewMenu);
99 ++fileGrouptIt;
100 }
101
102 // TODO: not used yet. See documentation of Dolphin::linkGroupActions()
103 // and Dolphin::linkToDeviceActions() in the header file for details.
104 //
105 //createNewMenu->insertSeparator();
106 //
107 //QPtrListIterator<KAction> linkGroupIt(dolphin->linkGroupActions());
108 //while ((action = linkGroupIt.current()) != 0) {
109 // action->plug(createNewMenu);
110 // ++linkGroupIt;
111 //}
112 //
113 //KMenu* linkToDeviceMenu = new KMenu();
114 //QPtrListIterator<KAction> linkToDeviceIt(dolphin->linkToDeviceActions());
115 //while ((action = linkToDeviceIt.current()) != 0) {
116 // action->plug(linkToDeviceMenu);
117 // ++linkToDeviceIt;
118 //}
119 //
120 //createNewMenu->insertItem(i18n("Link to Device"), linkToDeviceMenu);
121
122 popup->insertItem(SmallIcon("filenew"), i18n("Create New"), createNewMenu);
123 popup->insertSeparator();
124
125 KAction* pasteAction = dolphin->actionCollection()->action(KStdAction::stdName(KStdAction::Paste));
126 pasteAction->plug(popup);
127
128 // setup 'View Mode' menu
129 KMenu* viewModeMenu = new KMenu();
130
131 KAction* iconsMode = dolphin->actionCollection()->action("icons");
132 iconsMode->plug(viewModeMenu);
133
134 KAction* detailsMode = dolphin->actionCollection()->action("details");
135 detailsMode->plug(viewModeMenu);
136
137 KAction* previewsMode = dolphin->actionCollection()->action("previews");
138 previewsMode->plug(viewModeMenu);
139
140 popup->insertItem(i18n("View Mode"), viewModeMenu);
141 popup->insertSeparator();
142
143 QAction *bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
144 popup->insertSeparator();
145
146 QAction *propertiesAction = popup->addAction(i18n("Properties..."));
147
148 QAction *activatedAction = popup->exec(m_pos);
149 if (activatedAction == propertiesAction) {
150 new KPropertiesDialog(dolphin->activeView()->url());
151 }
152 else if (activatedAction == bookmarkAction) {
153 const KUrl& url = dolphin->activeView()->url();
154 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
155 url.fileName(),
156 url,
157 "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);
163 }
164 }
165
166 popup->deleteLater();
167 }
168
169 void DolphinContextMenu::openItemContextMenu()
170 {
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>)
175
176 assert(m_fileInfo != 0);
177
178 KMenu* popup = new KMenu(m_dolphinView);
179 DolphinMainWindow* dolphin = m_dolphinView->mainWindow();
180 const KUrl::List urls = m_dolphinView->selectedUrls();
181
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]));
187 if (action != 0) {
188 action->plug(popup);
189 }
190 }
191 popup->insertSeparator();
192
193 // insert 'Rename'
194 KAction* renameAction = dolphin->actionCollection()->action("rename");
195 renameAction->plug(popup);
196
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);
202 }
203 else {
204 KAction* deleteAction = dolphin->actionCollection()->action("delete");
205 deleteAction->plug(popup);
206 }
207
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"));
214 }
215
216 popup->insertSeparator();
217
218 // Insert 'Open With...' sub menu
219 Q3ValueVector<KService::Ptr> openWithVector;
220 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
221
222 // Insert 'Actions' sub menu
223 Q3ValueVector<KDEDesktopMimeType::Service> actionsVector;
224 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
225
226 // insert 'Properties...' entry
227 popup->insertSeparator();
228 KAction* propertiesAction = dolphin->actionCollection()->action("properties");
229 propertiesAction->plug(popup);
230
231 QAction *activatedAction = popup->exec(m_pos);
232
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(),
237 selectedUrl,
238 "bookmark");
239 if (!bookmark.isNull()) {
240 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
241 KBookmarkGroup root = manager->root();
242 root.addBookmark(manager, bookmark);
243 manager->emitChanged(root);
244 }
245 }
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]);
250 }
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);
256 }
257 else {
258 int id = openWithActions.indexOf(activatedAction);
259 KService::Ptr servicePtr = openWithVector[id];
260 KRun::run(*servicePtr, urls, m_dolphinView);
261 }
262 }
263
264 openWithVector.clear();
265 actionsVector.clear();
266 popup->deleteLater();
267 }
268
269 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
270 Q3ValueVector<KService::Ptr>& openWithVector)
271 {
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();
277
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());
284 }
285
286 QList<QAction*> openWithActions;
287
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(),
292 "Application",
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),
305 appName);
306 openWithVector.append(*it);
307 openWithActions << action;
308 }
309 }
310
311 openWithMenu->insertSeparator();
312 QAction *action = openWithMenu->addAction(i18n("&Other..."));
313 openWithActions << action;
314 popup->insertItem(i18n("Open With"), openWithMenu);
315 }
316 else {
317 // No applications are registered, hence just offer
318 // a "Open With..." item instead of a sub menu containing
319 // only one entry.
320 QAction *action = popup->addAction(i18n("Open With..."));
321 openWithActions << action;
322 }
323 }
324 else {
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);
329 }
330
331 return openWithActions;
332 }
333
334 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
335 Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
336 {
337 KMenu* actionsMenu = new KMenu();
338
339 QList<QAction*> serviceActions;
340
341 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
342
343 KMenu* menu = 0;
344 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
345 QDir dir(*dirIt);
346 QStringList entries = dir.entryList("*.desktop", QDir::Files);
347
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
356
357 bool insert = false;
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();
362
363 QListIterator<KFileItem*> mimeIt(list);
364 insert = true;
365 while (insert && mimeIt.hasNext()) {
366 KFileItem* item = mimeIt.next();
367 insert = !item->isDir();
368 }
369 }
370
371 if (!insert) {
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();
376
377 QListIterator<KFileItem*> mimeIt(list);
378 insert = true;
379 while (insert && mimeIt.hasNext()) {
380 KFileItem* item = mimeIt.next();
381 const QString mimeType(item->mimetype());
382 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
383
384 insert = (*it == mimeType) ||
385 ((*it).right(1) == "*") &&
386 ((*it).left((*it).indexOf('/')) == mimeGroup);
387 }
388 }
389
390 if (insert) {
391 menu = actionsMenu;
392
393 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
394 if (!submenuName.isEmpty()) {
395 menu = new KMenu();
396 actionsMenu->insertItem(submenuName, menu, submenuID);
397 }
398
399 Q3ValueList<KDEDesktopMimeType::Service> userServices =
400 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
401
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),
407 service.m_strName);
408 serviceActions << action;
409 }
410 else {
411 QAction *action = menu->addAction(service.m_strName);
412 serviceActions << action;
413 }
414 actionsVector.append(service);
415 }
416 }
417 }
418 }
419 }
420 }
421
422 const int itemsCount = actionsMenu->count();
423 if (itemsCount == 0) {
424 // no actions are available at all, hence show the "Actions"
425 // submenu disabled
426 actionsMenu->setEnabled(false);
427 }
428
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;
441 }
442 else {
443 QAction *action = popup->addAction(iconSet, text);
444 serviceActions.clear();
445 serviceActions << action;
446 }
447 }
448 else {
449 // The item is a sub menu, hence show the sub menu in the root menu.
450 popup->insertItem(actionsMenu->text(submenuID), menu);
451 }
452 actionsMenu->deleteLater();
453 actionsMenu = 0;
454 }
455 else {
456 popup->insertItem(i18n("Actions"), actionsMenu);
457 }
458
459 return serviceActions;
460 }
461
462 bool DolphinContextMenu::containsEntry(const KMenu* menu,
463 const QString& entryName) const
464 {
465 assert(menu != 0);
466
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) {
471 return true;
472 }
473 }
474
475 return false;
476 }