]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Applied some patches found by EBN (thanks to Nicolas Lécureuil for the patches!).
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 <kstandardaction.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 QAction* createFolderAction = dolphin->actionCollection()->action("create_folder");
88 if (createFolderAction != 0) {
89 createNewMenu->addAction(createFolderAction);
90 }
91
92 createNewMenu->insertSeparator();
93
94 QLinkedListIterator<QAction*> fileGrouptIt(dolphin->fileGroupActions());
95 while (fileGrouptIt.hasNext()) {
96 createNewMenu->addAction(fileGrouptIt.next());
97 }
98
99 // TODO: not used yet. See documentation of Dolphin::linkGroupActions()
100 // and Dolphin::linkToDeviceActions() in the header file for details.
101 //
102 //createNewMenu->insertSeparator();
103 //
104 //QPtrListIterator<KAction> linkGroupIt(dolphin->linkGroupActions());
105 //while ((action = linkGroupIt.current()) != 0) {
106 // action->plug(createNewMenu);
107 // ++linkGroupIt;
108 //}
109 //
110 //KMenu* linkToDeviceMenu = new KMenu();
111 //QPtrListIterator<KAction> linkToDeviceIt(dolphin->linkToDeviceActions());
112 //while ((action = linkToDeviceIt.current()) != 0) {
113 // action->plug(linkToDeviceMenu);
114 // ++linkToDeviceIt;
115 //}
116 //
117 //createNewMenu->insertItem(i18n("Link to Device"), linkToDeviceMenu);
118
119 popup->insertItem(SmallIcon("filenew"), i18n("Create New"), createNewMenu);
120 popup->insertSeparator();
121
122 QAction* pasteAction = dolphin->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
123 popup->addAction(pasteAction);
124
125 // setup 'View Mode' menu
126 KMenu* viewModeMenu = new KMenu();
127
128 QAction* iconsMode = dolphin->actionCollection()->action("icons");
129 viewModeMenu->addAction(iconsMode);
130
131 QAction* detailsMode = dolphin->actionCollection()->action("details");
132 viewModeMenu->addAction(detailsMode);
133
134 QAction* previewsMode = dolphin->actionCollection()->action("previews");
135 viewModeMenu->addAction(previewsMode);
136
137 popup->insertItem(i18n("View Mode"), viewModeMenu);
138 popup->insertSeparator();
139
140 QAction *bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
141 popup->insertSeparator();
142
143 QAction *propertiesAction = popup->addAction(i18n("Properties..."));
144
145 QAction *activatedAction = popup->exec(m_pos);
146 if (activatedAction == propertiesAction) {
147 new KPropertiesDialog(dolphin->activeView()->url());
148 }
149 else if (activatedAction == bookmarkAction) {
150 const KUrl& url = dolphin->activeView()->url();
151 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
152 url.fileName(),
153 url,
154 "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);
160 }
161 }
162
163 popup->deleteLater();
164 }
165
166 void DolphinContextMenu::openItemContextMenu()
167 {
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>)
172
173 assert(m_fileInfo != 0);
174
175 KMenu* popup = new KMenu(m_dolphinView);
176 DolphinMainWindow* dolphin = m_dolphinView->mainWindow();
177 const KUrl::List urls = m_dolphinView->selectedUrls();
178
179 // insert 'Cut', 'Copy' and 'Paste'
180 const KStandardAction::StandardAction actionNames[] = { KStandardAction::Cut, KStandardAction::Copy, KStandardAction::Paste };
181 const int count = sizeof(actionNames) / sizeof(KStandardAction::StandardAction);
182 for (int i = 0; i < count; ++i) {
183 QAction* action = dolphin->actionCollection()->action(KStandardAction::stdName(actionNames[i]));
184 if (action != 0) {
185 popup->addAction(action);
186 }
187 }
188 popup->insertSeparator();
189
190 // insert 'Rename'
191 QAction* renameAction = dolphin->actionCollection()->action("rename");
192 popup->addAction(renameAction);
193
194 // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
195 const KUrl& url = dolphin->activeView()->url();
196 if (url.isLocalFile()) {
197 QAction* moveToTrashAction = dolphin->actionCollection()->action("move_to_trash");
198 popup->addAction(moveToTrashAction);
199 }
200 else {
201 QAction* deleteAction = dolphin->actionCollection()->action("delete");
202 popup->addAction(deleteAction);
203 }
204
205 // insert 'Bookmark this folder...' entry
206 // urls is a list of selected items, so insert boolmark menu if
207 // urls contains only one item, i.e. no multiple selection made
208 QAction* bookmarkAction = 0;
209 if (m_fileInfo->isDir() && (urls.count() == 1)) {
210 bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
211 }
212
213 popup->insertSeparator();
214
215 // Insert 'Open With...' sub menu
216 Q3ValueVector<KService::Ptr> openWithVector;
217 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
218
219 // Insert 'Actions' sub menu
220 Q3ValueVector<KDEDesktopMimeType::Service> actionsVector;
221 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
222
223 // insert 'Properties...' entry
224 popup->insertSeparator();
225 QAction* propertiesAction = dolphin->actionCollection()->action("properties");
226 popup->addAction(propertiesAction);
227
228 QAction *activatedAction = popup->exec(m_pos);
229
230 if (bookmarkAction!=0 && activatedAction == bookmarkAction) {
231 const KUrl selectedUrl(m_fileInfo->url());
232 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
233 selectedUrl.fileName(),
234 selectedUrl,
235 "bookmark");
236 if (!bookmark.isNull()) {
237 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
238 KBookmarkGroup root = manager->root();
239 root.addBookmark(manager, bookmark);
240 manager->emitChanged(root);
241 }
242 }
243 else if (serviceActions.contains(activatedAction)) {
244 // one of the 'Actions' items has been selected
245 int id = serviceActions.indexOf(activatedAction);
246 KDEDesktopMimeType::executeService(urls, actionsVector[id]);
247 }
248 else if (openWithActions.contains(activatedAction)) {
249 // one of the 'Open With' items has been selected
250 if (openWithActions.last()==activatedAction) {
251 // the item 'Other...' has been selected
252 KRun::displayOpenWithDialog(urls, m_dolphinView);
253 }
254 else {
255 int id = openWithActions.indexOf(activatedAction);
256 KService::Ptr servicePtr = openWithVector[id];
257 KRun::run(*servicePtr, urls, m_dolphinView);
258 }
259 }
260
261 openWithVector.clear();
262 actionsVector.clear();
263 popup->deleteLater();
264 }
265
266 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
267 Q3ValueVector<KService::Ptr>& openWithVector)
268 {
269 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
270 // are listed which are registered to open the item. As last entry "Other..." will be
271 // attached which allows to select a custom application. If no applications are registered
272 // no sub menu is created at all, only "Open With..." will be offered.
273 const KFileItemList list = m_dolphinView->selectedItems();
274
275 bool insertOpenWithItems = true;
276 const QString contextMimeType(m_fileInfo->mimetype());
277 QListIterator<KFileItem*> mimeIt(list);
278 while (insertOpenWithItems && mimeIt.hasNext()) {
279 KFileItem* item = mimeIt.next();
280 insertOpenWithItems = (contextMimeType == item->mimetype());
281 }
282
283 QList<QAction*> openWithActions;
284
285 if (insertOpenWithItems) {
286 // fill the 'Open with' sub menu with application types
287 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
288 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
289 "Application",
290 "Type == 'Application'");
291 if (offers.count() > 0) {
292 KService::List::Iterator it;
293 KMenu* openWithMenu = new KMenu();
294 for(it = offers.begin(); it != offers.end(); ++it) {
295 // The offer list from the KTrader returns duplicate
296 // application entries. Although this seems to be a configuration
297 // problem outside the scope of Dolphin, duplicated entries just
298 // will be skipped here.
299 const QString appName((*it)->name());
300 if (!containsEntry(openWithMenu, appName)) {
301 const KIcon icon((*it)->icon());
302 QAction *action = openWithMenu->addAction(icon, appName);
303 openWithVector.append(*it);
304 openWithActions << action;
305 }
306 }
307
308 openWithMenu->insertSeparator();
309 QAction *action = openWithMenu->addAction(i18n("&Other..."));
310 openWithActions << action;
311 popup->insertItem(i18n("Open With"), openWithMenu);
312 }
313 else {
314 // No applications are registered, hence just offer
315 // a "Open With..." item instead of a sub menu containing
316 // only one entry.
317 QAction *action = popup->addAction(i18n("Open With..."));
318 openWithActions << action;
319 }
320 }
321 else {
322 // At least one of the selected items has a different MIME type. In this case
323 // just show a disabled "Open With..." entry.
324 QAction *action = popup->addAction(i18n("Open With..."));
325 action->setEnabled(false);
326 }
327
328 return openWithActions;
329 }
330
331 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
332 Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
333 {
334 KMenu* actionsMenu = new KMenu();
335
336 QList<QAction*> serviceActions;
337
338 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
339
340 KMenu* menu = 0;
341 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
342 QDir dir(*dirIt);
343 QStringList entries = dir.entryList("*.desktop", QDir::Files);
344
345 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
346 KSimpleConfig cfg(*dirIt + *entryIt, true);
347 cfg.setDesktopGroup();
348 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
349 const QStringList types = cfg.readListEntry("ServiceTypes");
350 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
351 // check whether the mime type is equal or whether the
352 // mimegroup (e. g. image/*) is supported
353
354 bool insert = false;
355 if ((*it) == "all/allfiles") {
356 // The service type is valid for all files, but not for directories.
357 // Check whether the selected items only consist of files...
358 const KFileItemList list = m_dolphinView->selectedItems();
359
360 QListIterator<KFileItem*> mimeIt(list);
361 insert = true;
362 while (insert && mimeIt.hasNext()) {
363 KFileItem* item = mimeIt.next();
364 insert = !item->isDir();
365 }
366 }
367
368 if (!insert) {
369 // Check whether the MIME types of all selected files match
370 // to the mimetype of the service action. As soon as one MIME
371 // type does not match, no service menu is shown at all.
372 const KFileItemList list = m_dolphinView->selectedItems();
373
374 QListIterator<KFileItem*> mimeIt(list);
375 insert = true;
376 while (insert && mimeIt.hasNext()) {
377 KFileItem* item = mimeIt.next();
378 const QString mimeType(item->mimetype());
379 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
380
381 insert = (*it == mimeType) ||
382 ((*it).right(1) == "*") &&
383 ((*it).left((*it).indexOf('/')) == mimeGroup);
384 }
385 }
386
387 if (insert) {
388 menu = actionsMenu;
389
390 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
391 if (!submenuName.isEmpty()) {
392 menu = new KMenu();
393 actionsMenu->insertItem(submenuName, menu, submenuID);
394 }
395
396 Q3ValueList<KDEDesktopMimeType::Service> userServices =
397 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
398
399 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
400 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
401 KDEDesktopMimeType::Service service = (*serviceIt);
402 if (!service.m_strIcon.isEmpty()) {
403 QAction *action = menu->addAction(SmallIcon(service.m_strIcon),
404 service.m_strName);
405 serviceActions << action;
406 }
407 else {
408 QAction *action = menu->addAction(service.m_strName);
409 serviceActions << action;
410 }
411 actionsVector.append(service);
412 }
413 }
414 }
415 }
416 }
417 }
418
419 const int itemsCount = actionsMenu->count();
420 if (itemsCount == 0) {
421 // no actions are available at all, hence show the "Actions"
422 // submenu disabled
423 actionsMenu->setEnabled(false);
424 }
425
426 if (itemsCount == 1) {
427 // Exactly one item is available. Instead of showing a sub menu with
428 // only one item, show the item directly in the root menu.
429 if (menu == actionsMenu) {
430 // The item is an action, hence show the action in the root menu.
431 const int id = actionsMenu->idAt(0);
432 const QString text(actionsMenu->text(id));
433 QIcon iconSet = actionsMenu->iconSet(id);
434 if (iconSet.isNull()) {
435 QAction *action = popup->addAction(text);
436 serviceActions.clear();
437 serviceActions << action;
438 }
439 else {
440 QAction *action = popup->addAction(iconSet, text);
441 serviceActions.clear();
442 serviceActions << action;
443 }
444 }
445 else {
446 // The item is a sub menu, hence show the sub menu in the root menu.
447 popup->insertItem(actionsMenu->text(submenuID), menu);
448 }
449 actionsMenu->deleteLater();
450 actionsMenu = 0;
451 }
452 else {
453 popup->insertItem(i18n("Actions"), actionsMenu);
454 }
455
456 return serviceActions;
457 }
458
459 bool DolphinContextMenu::containsEntry(const KMenu* menu,
460 const QString& entryName) const
461 {
462 assert(menu != 0);
463
464 const uint count = menu->count();
465 for (uint i = 0; i < count; ++i) {
466 const int id = menu->idAt(i);
467 if (menu->text(id) == entryName) {
468 return true;
469 }
470 }
471
472 return false;
473 }