]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Use a QLinkedList instead of Q3PtrList
[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 <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 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(KStdAction::stdName(KStdAction::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 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]));
184 if (action)
185 popup->addAction(action);
186 }
187 popup->insertSeparator();
188
189 // insert 'Rename'
190 QAction* renameAction = dolphin->actionCollection()->action("rename");
191 popup->addAction(renameAction);
192
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);
198 }
199 else {
200 QAction* deleteAction = dolphin->actionCollection()->action("delete");
201 popup->addAction(deleteAction);
202 }
203
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"));
210 }
211
212 popup->insertSeparator();
213
214 // Insert 'Open With...' sub menu
215 Q3ValueVector<KService::Ptr> openWithVector;
216 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
217
218 // Insert 'Actions' sub menu
219 Q3ValueVector<KDEDesktopMimeType::Service> actionsVector;
220 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
221
222 // insert 'Properties...' entry
223 popup->insertSeparator();
224 QAction* propertiesAction = dolphin->actionCollection()->action("properties");
225 popup->addAction(propertiesAction);
226
227 QAction *activatedAction = popup->exec(m_pos);
228
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(),
233 selectedUrl,
234 "bookmark");
235 if (!bookmark.isNull()) {
236 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
237 KBookmarkGroup root = manager->root();
238 root.addBookmark(manager, bookmark);
239 manager->emitChanged(root);
240 }
241 }
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]);
246 }
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);
252 }
253 else {
254 int id = openWithActions.indexOf(activatedAction);
255 KService::Ptr servicePtr = openWithVector[id];
256 KRun::run(*servicePtr, urls, m_dolphinView);
257 }
258 }
259
260 openWithVector.clear();
261 actionsVector.clear();
262 popup->deleteLater();
263 }
264
265 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
266 Q3ValueVector<KService::Ptr>& openWithVector)
267 {
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();
273
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());
280 }
281
282 QList<QAction*> openWithActions;
283
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(),
288 "Application",
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),
301 appName);
302 openWithVector.append(*it);
303 openWithActions << action;
304 }
305 }
306
307 openWithMenu->insertSeparator();
308 QAction *action = openWithMenu->addAction(i18n("&Other..."));
309 openWithActions << action;
310 popup->insertItem(i18n("Open With"), openWithMenu);
311 }
312 else {
313 // No applications are registered, hence just offer
314 // a "Open With..." item instead of a sub menu containing
315 // only one entry.
316 QAction *action = popup->addAction(i18n("Open With..."));
317 openWithActions << action;
318 }
319 }
320 else {
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);
325 }
326
327 return openWithActions;
328 }
329
330 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
331 Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
332 {
333 KMenu* actionsMenu = new KMenu();
334
335 QList<QAction*> serviceActions;
336
337 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
338
339 KMenu* menu = 0;
340 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
341 QDir dir(*dirIt);
342 QStringList entries = dir.entryList("*.desktop", QDir::Files);
343
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
352
353 bool insert = false;
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();
358
359 QListIterator<KFileItem*> mimeIt(list);
360 insert = true;
361 while (insert && mimeIt.hasNext()) {
362 KFileItem* item = mimeIt.next();
363 insert = !item->isDir();
364 }
365 }
366
367 if (!insert) {
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();
372
373 QListIterator<KFileItem*> mimeIt(list);
374 insert = true;
375 while (insert && mimeIt.hasNext()) {
376 KFileItem* item = mimeIt.next();
377 const QString mimeType(item->mimetype());
378 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
379
380 insert = (*it == mimeType) ||
381 ((*it).right(1) == "*") &&
382 ((*it).left((*it).indexOf('/')) == mimeGroup);
383 }
384 }
385
386 if (insert) {
387 menu = actionsMenu;
388
389 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
390 if (!submenuName.isEmpty()) {
391 menu = new KMenu();
392 actionsMenu->insertItem(submenuName, menu, submenuID);
393 }
394
395 Q3ValueList<KDEDesktopMimeType::Service> userServices =
396 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
397
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),
403 service.m_strName);
404 serviceActions << action;
405 }
406 else {
407 QAction *action = menu->addAction(service.m_strName);
408 serviceActions << action;
409 }
410 actionsVector.append(service);
411 }
412 }
413 }
414 }
415 }
416 }
417
418 const int itemsCount = actionsMenu->count();
419 if (itemsCount == 0) {
420 // no actions are available at all, hence show the "Actions"
421 // submenu disabled
422 actionsMenu->setEnabled(false);
423 }
424
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;
437 }
438 else {
439 QAction *action = popup->addAction(iconSet, text);
440 serviceActions.clear();
441 serviceActions << action;
442 }
443 }
444 else {
445 // The item is a sub menu, hence show the sub menu in the root menu.
446 popup->insertItem(actionsMenu->text(submenuID), menu);
447 }
448 actionsMenu->deleteLater();
449 actionsMenu = 0;
450 }
451 else {
452 popup->insertItem(i18n("Actions"), actionsMenu);
453 }
454
455 return serviceActions;
456 }
457
458 bool DolphinContextMenu::containsEntry(const KMenu* menu,
459 const QString& entryName) const
460 {
461 assert(menu != 0);
462
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) {
467 return true;
468 }
469 }
470
471 return false;
472 }