]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Changes to Undo/Redo in regard to ProgressIndicator
[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 assert(list != 0);
278
279 bool insertOpenWithItems = true;
280 const QString contextMimeType(m_fileInfo->mimetype());
281 QListIterator<KFileItem*> mimeIt(*list);
282 while (insertOpenWithItems && mimeIt.hasNext()) {
283 KFileItem* item = mimeIt.next();
284 insertOpenWithItems = (contextMimeType == item->mimetype());
285 }
286
287 QList<QAction*> openWithActions;
288
289 if (insertOpenWithItems) {
290 // fill the 'Open with' sub menu with application types
291 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
292 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
293 "Application",
294 "Type == 'Application'");
295 if (offers.count() > 0) {
296 KService::List::Iterator it;
297 KMenu* openWithMenu = new KMenu();
298 for(it = offers.begin(); it != offers.end(); ++it) {
299 // The offer list from the KTrader returns duplicate
300 // application entries. Although this seems to be a configuration
301 // problem outside the scope of Dolphin, duplicated entries just
302 // will be skipped here.
303 const QString appName((*it)->name());
304 if (!containsEntry(openWithMenu, appName)) {
305 QAction *action = openWithMenu->addAction((*it)->pixmap(K3Icon::Small),
306 appName);
307 openWithVector.append(*it);
308 openWithActions << action;
309 }
310 }
311
312 openWithMenu->insertSeparator();
313 QAction *action = openWithMenu->addAction(i18n("&Other..."));
314 openWithActions << action;
315 popup->insertItem(i18n("Open With"), openWithMenu);
316 }
317 else {
318 // No applications are registered, hence just offer
319 // a "Open With..." item instead of a sub menu containing
320 // only one entry.
321 QAction *action = popup->addAction(i18n("Open With..."));
322 openWithActions << action;
323 }
324 }
325 else {
326 // At least one of the selected items has a different MIME type. In this case
327 // just show a disabled "Open With..." entry.
328 QAction *action = popup->addAction(i18n("Open With..."));
329 action->setEnabled(false);
330 }
331
332 return openWithActions;
333 }
334
335 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
336 Q3ValueVector<KDEDesktopMimeType::Service>& actionsVector)
337 {
338 KMenu* actionsMenu = new KMenu();
339
340 QList<QAction*> serviceActions;
341
342 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
343
344 KMenu* menu = 0;
345 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
346 QDir dir(*dirIt);
347 QStringList entries = dir.entryList("*.desktop", QDir::Files);
348
349 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
350 KSimpleConfig cfg(*dirIt + *entryIt, true);
351 cfg.setDesktopGroup();
352 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
353 const QStringList types = cfg.readListEntry("ServiceTypes");
354 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
355 // check whether the mime type is equal or whether the
356 // mimegroup (e. g. image/*) is supported
357
358 bool insert = false;
359 if ((*it) == "all/allfiles") {
360 // The service type is valid for all files, but not for directories.
361 // Check whether the selected items only consist of files...
362 const KFileItemList* list = m_dolphinView->selectedItems();
363 assert(list != 0);
364
365 QListIterator<KFileItem*> mimeIt(*list);
366 insert = true;
367 while (insert && mimeIt.hasNext()) {
368 KFileItem* item = mimeIt.next();
369 insert = !item->isDir();
370 }
371 }
372
373 if (!insert) {
374 // Check whether the MIME types of all selected files match
375 // to the mimetype of the service action. As soon as one MIME
376 // type does not match, no service menu is shown at all.
377 const KFileItemList* list = m_dolphinView->selectedItems();
378 assert(list != 0);
379
380 QListIterator<KFileItem*> mimeIt(*list);
381 insert = true;
382 while (insert && mimeIt.hasNext()) {
383 KFileItem* item = mimeIt.next();
384 const QString mimeType(item->mimetype());
385 const QString mimeGroup(mimeType.left(mimeType.find('/')));
386
387 insert = (*it == mimeType) ||
388 ((*it).right(1) == "*") &&
389 ((*it).left((*it).find('/')) == mimeGroup);
390 }
391 }
392
393 if (insert) {
394 menu = actionsMenu;
395
396 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
397 if (!submenuName.isEmpty()) {
398 menu = new KMenu();
399 actionsMenu->insertItem(submenuName, menu, submenuID);
400 }
401
402 Q3ValueList<KDEDesktopMimeType::Service> userServices =
403 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
404
405 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
406 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
407 KDEDesktopMimeType::Service service = (*serviceIt);
408 if (!service.m_strIcon.isEmpty()) {
409 QAction *action = menu->addAction(SmallIcon(service.m_strIcon),
410 service.m_strName);
411 serviceActions << action;
412 }
413 else {
414 QAction *action = menu->addAction(service.m_strName);
415 serviceActions << action;
416 }
417 actionsVector.append(service);
418 }
419 }
420 }
421 }
422 }
423 }
424
425 const int itemsCount = actionsMenu->count();
426 if (itemsCount == 0) {
427 // no actions are available at all, hence show the "Actions"
428 // submenu disabled
429 actionsMenu->setEnabled(false);
430 }
431
432 if (itemsCount == 1) {
433 // Exactly one item is available. Instead of showing a sub menu with
434 // only one item, show the item directly in the root menu.
435 if (menu == actionsMenu) {
436 // The item is an action, hence show the action in the root menu.
437 const int id = actionsMenu->idAt(0);
438 const QString text(actionsMenu->text(id));
439 QIcon iconSet = actionsMenu->iconSet(id);
440 if (iconSet.isNull()) {
441 QAction *action = popup->addAction(text);
442 serviceActions.clear();
443 serviceActions << action;
444 }
445 else {
446 QAction *action = popup->addAction(iconSet, text);
447 serviceActions.clear();
448 serviceActions << action;
449 }
450 }
451 else {
452 // The item is a sub menu, hence show the sub menu in the root menu.
453 popup->insertItem(actionsMenu->text(submenuID), menu);
454 }
455 actionsMenu->deleteLater();
456 actionsMenu = 0;
457 }
458 else {
459 popup->insertItem(i18n("Actions"), actionsMenu);
460 }
461
462 return serviceActions;
463 }
464
465 bool DolphinContextMenu::containsEntry(const KMenu* menu,
466 const QString& entryName) const
467 {
468 assert(menu != 0);
469
470 const uint count = menu->count();
471 for (uint i = 0; i < count; ++i) {
472 const int id = menu->idAt(i);
473 if (menu->text(id) == entryName) {
474 return true;
475 }
476 }
477
478 return false;
479 }