]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Add libkmetadata detection and minor fixes
[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(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' for local Urls, otherwise insert 'Delete'
155 const KUrl& url = dolphin->activeView()->url();
156 if (url.isLocalFile()) {
157 QAction* moveToTrashAction = dolphin->actionCollection()->action("move_to_trash");
158 popup->addAction(moveToTrashAction);
159 }
160 else {
161 QAction* deleteAction = dolphin->actionCollection()->action("delete");
162 popup->addAction(deleteAction);
163 }
164
165 // insert 'Bookmark this folder...' entry
166 // urls is a list of selected items, so insert boolmark menu if
167 // urls contains only one item, i.e. no multiple selection made
168 QAction* bookmarkAction = 0;
169 if (m_fileInfo->isDir() && (urls.count() == 1)) {
170 bookmarkAction = popup->addAction(i18n("Bookmark this folder"));
171 }
172
173 // Insert 'Open With...' sub menu
174 QVector<KService::Ptr> openWithVector;
175 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
176
177 // Insert 'Actions' sub menu
178 QVector<KDEDesktopMimeType::Service> actionsVector;
179 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
180 popup->addSeparator();
181
182 // insert 'Properties...' entry
183 QAction* propertiesAction = dolphin->actionCollection()->action("properties");
184 popup->addAction(propertiesAction);
185
186 QAction* activatedAction = popup->exec(QCursor::pos());
187
188 if ((bookmarkAction!= 0) && (activatedAction == bookmarkAction)) {
189 const KUrl selectedUrl(m_fileInfo->url());
190 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
191 selectedUrl.fileName(),
192 selectedUrl,
193 "bookmark");
194 if (!bookmark.isNull()) {
195 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
196 KBookmarkGroup root = manager->root();
197 root.addBookmark(manager, bookmark);
198 manager->emitChanged(root);
199 }
200 }
201 else if (serviceActions.contains(activatedAction)) {
202 // one of the 'Actions' items has been selected
203 int id = serviceActions.indexOf(activatedAction);
204 KDEDesktopMimeType::executeService(urls, actionsVector[id]);
205 }
206 else if (openWithActions.contains(activatedAction)) {
207 // one of the 'Open With' items has been selected
208 if (openWithActions.last() == activatedAction) {
209 // the item 'Other...' has been selected
210 KRun::displayOpenWithDialog(urls, m_dolphinView);
211 }
212 else {
213 int id = openWithActions.indexOf(activatedAction);
214 KService::Ptr servicePtr = openWithVector[id];
215 KRun::run(*servicePtr, urls, m_dolphinView);
216 }
217 }
218
219 openWithVector.clear();
220 actionsVector.clear();
221 popup->deleteLater();
222 }
223
224 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
225 QVector<KService::Ptr>& openWithVector)
226 {
227 // Parts of the following code have been taken
228 // from the class KonqOperations located in
229 // libqonq/konq_operations.h of Konqueror.
230 // (Copyright (C) 2000 David Faure <faure@kde.org>)
231
232 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
233 // are listed which are registered to open the item. As last entry "Other..." will be
234 // attached which allows to select a custom application. If no applications are registered
235 // no sub menu is created at all, only "Open With..." will be offered.
236 const KFileItemList list = m_dolphinView->selectedItems();
237
238 bool insertOpenWithItems = true;
239 const QString contextMimeType(m_fileInfo->mimetype());
240
241 QListIterator<KFileItem*> mimeIt(list);
242 while (insertOpenWithItems && mimeIt.hasNext()) {
243 KFileItem* item = mimeIt.next();
244 insertOpenWithItems = (contextMimeType == item->mimetype());
245 }
246
247 QList<QAction*> openWithActions;
248 if (insertOpenWithItems) {
249 // fill the 'Open with' sub menu with application types
250 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
251 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
252 "Application",
253 "Type == 'Application'");
254 if (offers.count() > 0) {
255 KService::List::Iterator it;
256 KMenu* openWithMenu = new KMenu(i18n("Open With"));
257 for(it = offers.begin(); it != offers.end(); ++it) {
258 // The offer list from the KTrader returns duplicate
259 // application entries. Although this seems to be a configuration
260 // problem outside the scope of Dolphin, duplicated entries just
261 // will be skipped here.
262 const QString appName((*it)->name());
263 if (!containsEntry(openWithMenu, appName)) {
264 const KIcon icon((*it)->icon());
265 QAction *action = openWithMenu->addAction(icon, appName);
266 openWithVector.append(*it);
267 openWithActions << action;
268 }
269 }
270
271 openWithMenu->addSeparator();
272 QAction* action = openWithMenu->addAction(i18n("&Other..."));
273
274 openWithActions << action;
275 popup->addSeparator();
276 popup->addMenu(openWithMenu);
277 }
278 else {
279 // No applications are registered, hence just offer
280 // a "Open With..." item instead of a sub menu containing
281 // only one entry.
282 QAction* action = popup->addAction(i18n("Open With..."));
283 openWithActions << action;
284 }
285 }
286 else {
287 // At least one of the selected items has a different MIME type. In this case
288 // just show a disabled "Open With..." entry.
289 popup->addSeparator();
290 QAction* action = popup->addAction(i18n("Open With..."));
291 action->setEnabled(false);
292 }
293
294 return openWithActions;
295 }
296
297 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
298 QVector<KDEDesktopMimeType::Service>& actionsVector)
299 {
300 // Parts of the following code have been taken
301 // from the class KonqOperations located in
302 // libqonq/konq_operations.h of Konqueror.
303 // (Copyright (C) 2000 David Faure <faure@kde.org>)
304
305 KMenu* actionsMenu = new KMenu(i18n("Actions"));
306
307 QList<QAction*> serviceActions;
308
309 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
310
311 KMenu* menu = 0;
312 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
313 QDir dir(*dirIt);
314 QStringList filters;
315 filters << "*.desktop";
316 dir.setNameFilters(filters);
317 QStringList entries = dir.entryList(QDir::Files);
318
319 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
320 KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry" );
321 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
322 //const QStringList types = cfg.readListEntry("ServiceTypes");
323 QStringList types;
324 types = cfg.readEntry("ServiceTypes", types);
325 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
326 // check whether the mime type is equal or whether the
327 // mimegroup (e. g. image/*) is supported
328
329 bool insert = false;
330 if ((*it) == "all/allfiles") {
331 // The service type is valid for all files, but not for directories.
332 // Check whether the selected items only consist of files...
333 const KFileItemList list = m_dolphinView->selectedItems();
334
335 QListIterator<KFileItem*> mimeIt(list);
336 insert = true;
337 while (insert && mimeIt.hasNext()) {
338 KFileItem* item = mimeIt.next();
339 insert = !item->isDir();
340 }
341 }
342
343 if (!insert) {
344 // Check whether the MIME types of all selected files match
345 // to the mimetype of the service action. As soon as one MIME
346 // type does not match, no service menu is shown at all.
347 const KFileItemList list = m_dolphinView->selectedItems();
348
349 QListIterator<KFileItem*> mimeIt(list);
350 insert = true;
351 while (insert && mimeIt.hasNext()) {
352 KFileItem* item = mimeIt.next();
353 const QString mimeType(item->mimetype());
354 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
355
356 insert = (*it == mimeType) ||
357 ((*it).right(1) == "*") &&
358 ((*it).left((*it).indexOf('/')) == mimeGroup);
359 }
360 }
361
362 if (insert) {
363 menu = actionsMenu;
364
365 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
366 if (!submenuName.isEmpty()) {
367 menu = new KMenu(submenuName);
368 actionsMenu->addMenu(menu);
369 }
370
371 Q3ValueList<KDEDesktopMimeType::Service> userServices =
372 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
373
374 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
375 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
376 KDEDesktopMimeType::Service service = (*serviceIt);
377 if (!service.m_strIcon.isEmpty()) {
378 QAction* action = menu->addAction(SmallIcon(service.m_strIcon),
379 service.m_strName);
380 serviceActions << action;
381 }
382 else {
383 QAction *action = menu->addAction(service.m_strName);
384 serviceActions << action;
385 }
386 actionsVector.append(service);
387 }
388 }
389 }
390 }
391 }
392 }
393
394 const int itemsCount = actionsMenu->actions().count();
395 if (itemsCount == 0) {
396 // no actions are available at all, hence show the "Actions"
397 // submenu disabled
398 actionsMenu->setEnabled(false);
399 }
400
401 if (itemsCount == 1) {
402 // Exactly one item is available. Instead of showing a sub menu with
403 // only one item, show the item directly in the root menu.
404 if (menu == actionsMenu) {
405 // The item is an action, hence show the action in the root menu.
406 const QList<QAction*> actions = actionsMenu->actions();
407 assert(actions.count() == 1);
408
409 const QString text = actions[0]->text();
410 const QIcon icon = actions[0]->icon();
411 if (icon.isNull()) {
412 QAction* action = popup->addAction(text);
413 serviceActions.clear();
414 serviceActions << action;
415 }
416 else {
417 QAction* action = popup->addAction(icon, text);
418 serviceActions.clear();
419 serviceActions << action;
420 }
421 }
422 else {
423 // The item is a sub menu, hence show the sub menu in the root menu.
424 popup->addMenu(menu);
425 }
426 actionsMenu->deleteLater();
427 actionsMenu = 0;
428 }
429 else {
430 popup->addMenu(actionsMenu);
431 }
432
433 return serviceActions;
434 }
435
436 bool DolphinContextMenu::containsEntry(const KMenu* menu,
437 const QString& entryName) const
438 {
439 assert(menu != 0);
440
441 const QList<QAction*> list = menu->actions();
442 const uint count = list.count();
443 for (uint i = 0; i < count; ++i) {
444 const QAction* action = list.at(i);
445 if (action->text() == entryName) {
446 return true;
447 }
448 }
449
450 return false;
451 }