]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Do a custom error handling in for the 'Create New...' submenu. Thanks to David for...
[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 "dolphinmainwindow.h"
24 #include "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "editbookmarkdialog.h"
27
28 #include <assert.h>
29
30 #include <kactioncollection.h>
31 #include <kbookmarkmanager.h>
32 #include <kbookmark.h>
33 #include <kdesktopfile.h>
34 #include <kglobal.h>
35 #include <kiconloader.h>
36 #include <kio/netaccess.h>
37 #include <kmenu.h>
38 #include <kmimetypetrader.h>
39 #include <knewmenu.h>
40 #include <klocale.h>
41 #include <kpropertiesdialog.h>
42 #include <krun.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
45
46 #include <QDir>
47
48 DolphinContextMenu::DolphinContextMenu(DolphinView* parent,
49 KFileItem* fileInfo,
50 const QPoint& pos) :
51 m_dolphinView(parent),
52 m_fileInfo(fileInfo),
53 m_pos(pos)
54 {
55 }
56
57 void DolphinContextMenu::open()
58 {
59 if (m_fileInfo == 0) {
60 openViewportContextMenu();
61 }
62 else {
63 openItemContextMenu();
64 }
65 }
66
67 DolphinContextMenu::~DolphinContextMenu()
68 {
69 }
70
71 void DolphinContextMenu::openViewportContextMenu()
72 {
73 assert(m_fileInfo == 0);
74 DolphinMainWindow* dolphin = m_dolphinView->mainWindow();
75 KMenu* popup = new KMenu(m_dolphinView);
76
77 // setup 'Create New' menu
78 KNewMenu* newMenu = dolphin->newMenu();
79 newMenu->slotCheckUpToDate();
80 newMenu->setPopupFiles(m_dolphinView->url());
81 popup->addMenu(newMenu->menu());
82 popup->addSeparator();
83
84 QAction* pasteAction = dolphin->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
85 popup->addAction(pasteAction);
86
87 // setup 'View Mode' menu
88 KMenu* viewModeMenu = new KMenu(i18n("View Mode"));
89
90 QAction* iconsMode = dolphin->actionCollection()->action("icons");
91 viewModeMenu->addAction(iconsMode);
92
93 QAction* detailsMode = dolphin->actionCollection()->action("details");
94 viewModeMenu->addAction(detailsMode);
95
96 QAction* previewsMode = dolphin->actionCollection()->action("previews");
97 viewModeMenu->addAction(previewsMode);
98
99 popup->addMenu(viewModeMenu);
100 popup->addSeparator();
101
102 QAction* bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
103 popup->addSeparator();
104
105 QAction* propertiesAction = popup->addAction(i18n("Properties..."));
106
107 QAction* activatedAction = popup->exec(m_pos);
108 if (activatedAction == propertiesAction) {
109 new KPropertiesDialog(dolphin->activeView()->url());
110 }
111 else if (activatedAction == bookmarkAction) {
112 const KUrl& url = dolphin->activeView()->url();
113 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
114 url.fileName(),
115 url,
116 "bookmark");
117 if (!bookmark.isNull()) {
118 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
119 KBookmarkGroup root = manager->root();
120 root.addBookmark(manager, bookmark);
121 manager->emitChanged(root);
122 }
123 }
124
125 popup->deleteLater();
126 }
127
128 void DolphinContextMenu::openItemContextMenu()
129 {
130 assert(m_fileInfo != 0);
131
132 KMenu* popup = new KMenu(m_dolphinView);
133 DolphinMainWindow* dolphin = m_dolphinView->mainWindow();
134 const KUrl::List urls = m_dolphinView->selectedUrls();
135
136 // insert 'Cut', 'Copy' and 'Paste'
137 const KStandardAction::StandardAction actionNames[] = {
138 KStandardAction::Cut,
139 KStandardAction::Copy,
140 KStandardAction::Paste
141 };
142
143 const int count = sizeof(actionNames) / sizeof(KStandardAction::StandardAction);
144 for (int i = 0; i < count; ++i) {
145 QAction* action = dolphin->actionCollection()->action(KStandardAction::stdName(actionNames[i]));
146 if (action != 0) {
147 popup->addAction(action);
148 }
149 }
150 popup->addSeparator();
151
152 // insert 'Rename'
153 QAction* renameAction = dolphin->actionCollection()->action("rename");
154 popup->addAction(renameAction);
155
156 // insert 'Move to Trash' for local Urls, otherwise insert 'Delete'
157 const KUrl& url = dolphin->activeView()->url();
158 if (url.isLocalFile()) {
159 QAction* moveToTrashAction = dolphin->actionCollection()->action("move_to_trash");
160 popup->addAction(moveToTrashAction);
161 }
162 else {
163 QAction* deleteAction = dolphin->actionCollection()->action("delete");
164 popup->addAction(deleteAction);
165 }
166
167 // insert 'Bookmark this folder...' entry
168 // urls is a list of selected items, so insert boolmark menu if
169 // urls contains only one item, i.e. no multiple selection made
170 QAction* bookmarkAction = 0;
171 if (m_fileInfo->isDir() && (urls.count() == 1)) {
172 bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
173 }
174
175 // Insert 'Open With...' sub menu
176 QVector<KService::Ptr> openWithVector;
177 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
178
179 // Insert 'Actions' sub menu
180 QVector<KDEDesktopMimeType::Service> actionsVector;
181 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
182 popup->addSeparator();
183
184 // insert 'Properties...' entry
185 QAction* propertiesAction = dolphin->actionCollection()->action("properties");
186 popup->addAction(propertiesAction);
187
188 QAction* activatedAction = popup->exec(m_pos);
189
190 if ((bookmarkAction!= 0) && (activatedAction == bookmarkAction)) {
191 const KUrl selectedUrl(m_fileInfo->url());
192 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
193 selectedUrl.fileName(),
194 selectedUrl,
195 "bookmark");
196 if (!bookmark.isNull()) {
197 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
198 KBookmarkGroup root = manager->root();
199 root.addBookmark(manager, bookmark);
200 manager->emitChanged(root);
201 }
202 }
203 else if (serviceActions.contains(activatedAction)) {
204 // one of the 'Actions' items has been selected
205 int id = serviceActions.indexOf(activatedAction);
206 KDEDesktopMimeType::executeService(urls, actionsVector[id]);
207 }
208 else if (openWithActions.contains(activatedAction)) {
209 // one of the 'Open With' items has been selected
210 if (openWithActions.last() == activatedAction) {
211 // the item 'Other...' has been selected
212 KRun::displayOpenWithDialog(urls, m_dolphinView);
213 }
214 else {
215 int id = openWithActions.indexOf(activatedAction);
216 KService::Ptr servicePtr = openWithVector[id];
217 KRun::run(*servicePtr, urls, m_dolphinView);
218 }
219 }
220
221 openWithVector.clear();
222 actionsVector.clear();
223 popup->deleteLater();
224 }
225
226 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
227 QVector<KService::Ptr>& openWithVector)
228 {
229 // Parts of the following code have been taken
230 // from the class KonqOperations located in
231 // libqonq/konq_operations.h of Konqueror.
232 // (Copyright (C) 2000 David Faure <faure@kde.org>)
233
234 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
235 // are listed which are registered to open the item. As last entry "Other..." will be
236 // attached which allows to select a custom application. If no applications are registered
237 // no sub menu is created at all, only "Open With..." will be offered.
238 const KFileItemList list = m_dolphinView->selectedItems();
239
240 bool insertOpenWithItems = true;
241 const QString contextMimeType(m_fileInfo->mimetype());
242
243 QListIterator<KFileItem*> mimeIt(list);
244 while (insertOpenWithItems && mimeIt.hasNext()) {
245 KFileItem* item = mimeIt.next();
246 insertOpenWithItems = (contextMimeType == item->mimetype());
247 }
248
249 QList<QAction*> openWithActions;
250 if (insertOpenWithItems) {
251 // fill the 'Open with' sub menu with application types
252 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
253 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
254 "Application",
255 "Type == 'Application'");
256 if (offers.count() > 0) {
257 KService::List::Iterator it;
258 KMenu* openWithMenu = new KMenu(i18n("Open With"));
259 for(it = offers.begin(); it != offers.end(); ++it) {
260 // The offer list from the KTrader returns duplicate
261 // application entries. Although this seems to be a configuration
262 // problem outside the scope of Dolphin, duplicated entries just
263 // will be skipped here.
264 const QString appName((*it)->name());
265 if (!containsEntry(openWithMenu, appName)) {
266 const KIcon icon((*it)->icon());
267 QAction *action = openWithMenu->addAction(icon, appName);
268 openWithVector.append(*it);
269 openWithActions << action;
270 }
271 }
272
273 openWithMenu->addSeparator();
274 QAction* action = openWithMenu->addAction(i18n("&Other..."));
275
276 openWithActions << action;
277 popup->addSeparator();
278 popup->addMenu(openWithMenu);
279 }
280 else {
281 // No applications are registered, hence just offer
282 // a "Open With..." item instead of a sub menu containing
283 // only one entry.
284 QAction* action = popup->addAction(i18n("Open With..."));
285 openWithActions << action;
286 }
287 }
288 else {
289 // At least one of the selected items has a different MIME type. In this case
290 // just show a disabled "Open With..." entry.
291 popup->addSeparator();
292 QAction* action = popup->addAction(i18n("Open With..."));
293 action->setEnabled(false);
294 }
295
296 return openWithActions;
297 }
298
299 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
300 QVector<KDEDesktopMimeType::Service>& actionsVector)
301 {
302 // Parts of the following code have been taken
303 // from the class KonqOperations located in
304 // libqonq/konq_operations.h of Konqueror.
305 // (Copyright (C) 2000 David Faure <faure@kde.org>)
306
307 KMenu* actionsMenu = new KMenu(i18n("Actions"));
308
309 QList<QAction*> serviceActions;
310
311 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
312
313 KMenu* menu = 0;
314 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
315 QDir dir(*dirIt);
316 QStringList filters;
317 filters << "*.desktop";
318 dir.setNameFilters(filters);
319 QStringList entries = dir.entryList(QDir::Files);
320
321 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
322 KSimpleConfig cfg(*dirIt + *entryIt, true);
323 cfg.setDesktopGroup();
324 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
325 //const QStringList types = cfg.readListEntry("ServiceTypes");
326 QStringList types;
327 types = cfg.readEntry("ServiceTypes", types);
328 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
329 // check whether the mime type is equal or whether the
330 // mimegroup (e. g. image/*) is supported
331
332 bool insert = false;
333 if ((*it) == "all/allfiles") {
334 // The service type is valid for all files, but not for directories.
335 // Check whether the selected items only consist of files...
336 const KFileItemList list = m_dolphinView->selectedItems();
337
338 QListIterator<KFileItem*> mimeIt(list);
339 insert = true;
340 while (insert && mimeIt.hasNext()) {
341 KFileItem* item = mimeIt.next();
342 insert = !item->isDir();
343 }
344 }
345
346 if (!insert) {
347 // Check whether the MIME types of all selected files match
348 // to the mimetype of the service action. As soon as one MIME
349 // type does not match, no service menu is shown at all.
350 const KFileItemList list = m_dolphinView->selectedItems();
351
352 QListIterator<KFileItem*> mimeIt(list);
353 insert = true;
354 while (insert && mimeIt.hasNext()) {
355 KFileItem* item = mimeIt.next();
356 const QString mimeType(item->mimetype());
357 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
358
359 insert = (*it == mimeType) ||
360 ((*it).right(1) == "*") &&
361 ((*it).left((*it).indexOf('/')) == mimeGroup);
362 }
363 }
364
365 if (insert) {
366 menu = actionsMenu;
367
368 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
369 if (!submenuName.isEmpty()) {
370 menu = new KMenu(submenuName);
371 actionsMenu->addMenu(menu);
372 }
373
374 Q3ValueList<KDEDesktopMimeType::Service> userServices =
375 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
376
377 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
378 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
379 KDEDesktopMimeType::Service service = (*serviceIt);
380 if (!service.m_strIcon.isEmpty()) {
381 QAction* action = menu->addAction(SmallIcon(service.m_strIcon),
382 service.m_strName);
383 serviceActions << action;
384 }
385 else {
386 QAction *action = menu->addAction(service.m_strName);
387 serviceActions << action;
388 }
389 actionsVector.append(service);
390 }
391 }
392 }
393 }
394 }
395 }
396
397 const int itemsCount = actionsMenu->actions().count();
398 if (itemsCount == 0) {
399 // no actions are available at all, hence show the "Actions"
400 // submenu disabled
401 actionsMenu->setEnabled(false);
402 }
403
404 if (itemsCount == 1) {
405 // Exactly one item is available. Instead of showing a sub menu with
406 // only one item, show the item directly in the root menu.
407 if (menu == actionsMenu) {
408 // The item is an action, hence show the action in the root menu.
409 const QList<QAction*> actions = actionsMenu->actions();
410 assert(actions.count() == 1);
411
412 const QString text = actions[0]->text();
413 const QIcon icon = actions[0]->icon();
414 if (icon.isNull()) {
415 QAction* action = popup->addAction(text);
416 serviceActions.clear();
417 serviceActions << action;
418 }
419 else {
420 QAction* action = popup->addAction(icon, text);
421 serviceActions.clear();
422 serviceActions << action;
423 }
424 }
425 else {
426 // The item is a sub menu, hence show the sub menu in the root menu.
427 popup->addMenu(menu);
428 }
429 actionsMenu->deleteLater();
430 actionsMenu = 0;
431 }
432 else {
433 popup->addMenu(actionsMenu);
434 }
435
436 return serviceActions;
437 }
438
439 bool DolphinContextMenu::containsEntry(const KMenu* menu,
440 const QString& entryName) const
441 {
442 assert(menu != 0);
443
444 const QList<QAction*> list = menu->actions();
445 const uint count = list.count();
446 for (uint i = 0; i < count; ++i) {
447 const QAction* action = list.at(i);
448 if (action->text() == entryName) {
449 return true;
450 }
451 }
452
453 return false;
454 }