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