]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
fixed typo
[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 <kactioncollection.h>
29 #include <kbookmarkmanager.h>
30 #include <kbookmark.h>
31 #include <kdesktopfile.h>
32 #include <kglobal.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
35 #include <kmenu.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
38 #include <knewmenu.h>
39 #include <konq_operations.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(DolphinMainWindow* parent,
49 KFileItem* fileInfo,
50 const KUrl& baseUrl,
51 ViewType viewType) :
52 m_mainWindow(parent),
53 m_fileInfo(fileInfo),
54 m_baseUrl(baseUrl),
55 m_viewType(viewType),
56 m_context(NoContext)
57 {
58 if (viewType == ItemsView) {
59 // The context menu either accesses the URLs of the selected items
60 // or the items itself. To increase the performance both lists are cached.
61 DolphinView* view = m_mainWindow->activeView();
62 m_selectedUrls = view->selectedUrls();
63 m_selectedItems = view->selectedItems();
64 }
65 else if (fileInfo != 0) {
66 m_selectedUrls.append(fileInfo->url());
67 m_selectedItems.append(fileInfo);
68 }
69 }
70
71 void DolphinContextMenu::open()
72 {
73 // get the context information
74 if (m_baseUrl.protocol() == "trash") {
75 m_context |= TrashContext;
76 }
77
78 if (m_fileInfo != 0) {
79 m_context |= ItemContext;
80 // TODO: handle other use cases like devices + desktop files
81 }
82
83 // open the corresponding popup for the context
84 if (m_context & TrashContext) {
85 if (m_context & ItemContext) {
86 openTrashItemContextMenu();
87 }
88 else {
89 openTrashContextMenu();
90 }
91 }
92 else if (m_context & ItemContext) {
93 openItemContextMenu();
94 }
95 else {
96 Q_ASSERT(m_context == NoContext);
97 openViewportContextMenu();
98 }
99 }
100
101 DolphinContextMenu::~DolphinContextMenu()
102 {
103 }
104
105 void DolphinContextMenu::openTrashContextMenu()
106 {
107 Q_ASSERT(m_context & TrashContext);
108
109 KMenu* popup = new KMenu(m_mainWindow);
110
111 QAction* emptyTrashAction = new QAction(KIcon("user-trash"), i18n("Emtpy Trash"), popup);
112 KConfig trashConfig("trashrc", KConfig::OnlyLocal);
113 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
114 popup->addAction(emptyTrashAction);
115
116 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
117 popup->addAction(propertiesAction);
118
119 if (popup->exec(QCursor::pos()) == emptyTrashAction) {
120 const QString text(i18n("Do you really want to empty the Trash? All items will get deleted."));
121 const bool del = KMessageBox::warningContinueCancel(m_mainWindow,
122 text,
123 QString(),
124 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
125 ) == KMessageBox::Continue;
126 if (del) {
127 KonqOperations::emptyTrash(m_mainWindow);
128 }
129 }
130
131 popup->deleteLater();
132 }
133
134 void DolphinContextMenu::openTrashItemContextMenu()
135 {
136 Q_ASSERT(m_context & TrashContext);
137 Q_ASSERT(m_context & ItemContext);
138
139 KMenu* popup = new KMenu(m_mainWindow);
140
141 QAction* restoreAction = new QAction(i18n("Restore"), m_mainWindow);
142 popup->addAction(restoreAction);
143
144 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
145 popup->addAction(deleteAction);
146
147 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
148 popup->addAction(propertiesAction);
149
150 if (popup->exec(QCursor::pos()) == restoreAction) {
151 KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow);
152 }
153
154 popup->deleteLater();
155 }
156
157 void DolphinContextMenu::openItemContextMenu()
158 {
159 Q_ASSERT(m_fileInfo != 0);
160
161 KMenu* popup = new KMenu(m_mainWindow);
162 if (m_viewType == ItemsView) {
163 insertDefaultItemActions(popup);
164 }
165
166 popup->addSeparator();
167
168 // insert 'Bookmark this folder' entry if exactly one item is selected
169 QAction* bookmarkAction = 0;
170 if (m_fileInfo->isDir() && (m_selectedUrls.count() == 1)) {
171 bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark folder"));
172 }
173
174 // Insert 'Open With...' sub menu
175 QVector<KService::Ptr> openWithVector;
176 const QList<QAction*> openWithActions = insertOpenWithItems(popup, openWithVector);
177
178 // Insert 'Actions' sub menu
179 QVector<KDEDesktopMimeType::Service> actionsVector;
180 const QList<QAction*> serviceActions = insertActionItems(popup, actionsVector);
181 popup->addSeparator();
182
183 if (m_viewType == ItemsView) {
184 // insert 'Properties...' entry
185 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
186 popup->addAction(propertiesAction);
187 }
188
189 QAction* activatedAction = popup->exec(QCursor::pos());
190
191 if ((bookmarkAction!= 0) && (activatedAction == bookmarkAction)) {
192 const KUrl selectedUrl(m_fileInfo->url());
193 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
194 selectedUrl.fileName(),
195 selectedUrl,
196 "bookmark");
197 if (!bookmark.isNull()) {
198 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
199 KBookmarkGroup root = manager->root();
200 root.addBookmark(manager, bookmark);
201 manager->emitChanged(root);
202 }
203 }
204 else if (serviceActions.contains(activatedAction)) {
205 // one of the 'Actions' items has been selected
206 int id = serviceActions.indexOf(activatedAction);
207 KDEDesktopMimeType::executeService(m_selectedUrls, actionsVector[id]);
208 }
209 else if (openWithActions.contains(activatedAction)) {
210 // one of the 'Open With' items has been selected
211 if (openWithActions.last() == activatedAction) {
212 // the item 'Other...' has been selected
213 KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow);
214 }
215 else {
216 int id = openWithActions.indexOf(activatedAction);
217 KService::Ptr servicePtr = openWithVector[id];
218 KRun::run(*servicePtr, m_selectedUrls, m_mainWindow);
219 }
220 }
221
222 openWithVector.clear();
223 actionsVector.clear();
224 popup->deleteLater();
225 }
226
227 void DolphinContextMenu::openViewportContextMenu()
228 {
229 Q_ASSERT(m_fileInfo == 0);
230 KMenu* popup = new KMenu(m_mainWindow);
231
232 // setup 'Create New' menu
233 KNewMenu* newMenu = m_mainWindow->newMenu();
234 newMenu->slotCheckUpToDate();
235 newMenu->setPopupFiles(m_baseUrl);
236 popup->addMenu(newMenu->menu());
237 popup->addSeparator();
238
239 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
240 popup->addAction(pasteAction);
241
242 // setup 'View Mode' menu
243 KMenu* viewModeMenu = new KMenu(i18n("View Mode"));
244
245 QAction* iconsMode = m_mainWindow->actionCollection()->action("icons");
246 viewModeMenu->addAction(iconsMode);
247
248 QAction* detailsMode = m_mainWindow->actionCollection()->action("details");
249 viewModeMenu->addAction(detailsMode);
250
251 QAction* previewsMode = m_mainWindow->actionCollection()->action("previews");
252 viewModeMenu->addAction(previewsMode);
253
254 popup->addMenu(viewModeMenu);
255 popup->addSeparator();
256
257 QAction* bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
258 popup->addSeparator();
259
260 QAction* propertiesAction = popup->addAction(i18n("Properties..."));
261
262 QAction* activatedAction = popup->exec(QCursor::pos());
263 if (activatedAction == propertiesAction) {
264 new KPropertiesDialog(m_mainWindow->activeView()->url());
265 }
266 else if (activatedAction == bookmarkAction) {
267 const KUrl& url = m_mainWindow->activeView()->url();
268 KBookmark bookmark = EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
269 url.fileName(),
270 url,
271 "bookmark");
272 if (!bookmark.isNull()) {
273 KBookmarkManager* manager = DolphinSettings::instance().bookmarkManager();
274 KBookmarkGroup root = manager->root();
275 root.addBookmark(manager, bookmark);
276 manager->emitChanged(root);
277 }
278 }
279
280 popup->deleteLater();
281 }
282
283 void DolphinContextMenu::insertDefaultItemActions(KMenu* popup)
284 {
285 Q_ASSERT(popup != 0);
286
287 // insert 'Cut', 'Copy' and 'Paste'
288 QAction* cutAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Cut));
289 QAction* copyAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Copy));
290 QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste));
291 popup->addAction(cutAction);
292 popup->addAction(copyAction);
293 popup->addAction(pasteAction);
294 popup->addSeparator();
295
296 // insert 'Rename'
297 QAction* renameAction = m_mainWindow->actionCollection()->action("rename");
298 popup->addAction(renameAction);
299
300 // insert 'Move to Trash' and (optionally) 'Delete'
301 const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals);
302 const KConfigGroup kdeConfig(globalConfig, "KDE");
303 bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false);
304 const KUrl& url = m_mainWindow->activeView()->url();
305 if (url.isLocalFile()) {
306 QAction* moveToTrashAction = m_mainWindow->actionCollection()->action("move_to_trash");
307 popup->addAction(moveToTrashAction);
308 }
309 else {
310 showDeleteCommand = true;
311 }
312
313 if (showDeleteCommand) {
314 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
315 popup->addAction(deleteAction);
316 }
317 }
318
319 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
320 QVector<KService::Ptr>& openWithVector)
321 {
322 // Parts of the following code have been taken
323 // from the class KonqOperations located in
324 // libqonq/konq_operations.h of Konqueror.
325 // (Copyright (C) 2000 David Faure <faure@kde.org>)
326
327 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
328 // are listed which are registered to open the item. As last entry "Other..." will be
329 // attached which allows to select a custom application. If no applications are registered
330 // no sub menu is created at all, only "Open With..." will be offered.
331 bool insertOpenWithItems = true;
332 const QString contextMimeType(m_fileInfo->mimetype());
333
334 QListIterator<KFileItem*> mimeIt(m_selectedItems);
335 while (insertOpenWithItems && mimeIt.hasNext()) {
336 KFileItem* item = mimeIt.next();
337 insertOpenWithItems = (contextMimeType == item->mimetype());
338 }
339
340 QList<QAction*> openWithActions;
341 if (insertOpenWithItems) {
342 // fill the 'Open with' sub menu with application types
343 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
344 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
345 "Application",
346 "Type == 'Application'");
347 if (offers.count() > 0) {
348 KService::List::Iterator it;
349 KMenu* openWithMenu = new KMenu(i18n("Open With"));
350 for(it = offers.begin(); it != offers.end(); ++it) {
351 // The offer list from the KTrader returns duplicate
352 // application entries. Although this seems to be a configuration
353 // problem outside the scope of Dolphin, duplicated entries just
354 // will be skipped here.
355 const QString appName((*it)->name());
356 if (!containsEntry(openWithMenu, appName)) {
357 const KIcon icon((*it)->icon());
358 QAction* action = openWithMenu->addAction(icon, appName);
359 openWithVector.append(*it);
360 openWithActions << action;
361 }
362 }
363
364 openWithMenu->addSeparator();
365 QAction* action = openWithMenu->addAction(i18n("&Other..."));
366
367 openWithActions << action;
368 popup->addMenu(openWithMenu);
369 }
370 else {
371 // No applications are registered, hence just offer
372 // a "Open With..." item instead of a sub menu containing
373 // only one entry.
374 QAction* action = popup->addAction(i18n("Open With..."));
375 openWithActions << action;
376 }
377 }
378 else {
379 // At least one of the selected items has a different MIME type. In this case
380 // just show a disabled "Open With..." entry.
381 QAction* action = popup->addAction(i18n("Open With..."));
382 action->setEnabled(false);
383 }
384
385 return openWithActions;
386 }
387
388 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
389 QVector<KDEDesktopMimeType::Service>& actionsVector)
390 {
391 // Parts of the following code have been taken
392 // from the class KonqOperations located in
393 // libqonq/konq_operations.h of Konqueror.
394 // (Copyright (C) 2000 David Faure <faure@kde.org>)
395
396 KMenu* actionsMenu = new KMenu(i18n("Actions"));
397
398 QList<QAction*> serviceActions;
399
400 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
401
402 KMenu* menu = 0;
403 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
404 QDir dir(*dirIt);
405 QStringList filters;
406 filters << "*.desktop";
407 dir.setNameFilters(filters);
408 QStringList entries = dir.entryList(QDir::Files);
409
410 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
411 KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry" );
412 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
413 //const QStringList types = cfg.readListEntry("ServiceTypes");
414 QStringList types;
415 types = cfg.readEntry("ServiceTypes", types);
416 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
417 // check whether the mime type is equal or whether the
418 // mimegroup (e. g. image/*) is supported
419
420 bool insert = false;
421 if ((*it) == "all/allfiles") {
422 // The service type is valid for all files, but not for directories.
423 // Check whether the selected items only consist of files...
424 QListIterator<KFileItem*> mimeIt(m_selectedItems);
425 insert = true;
426 while (insert && mimeIt.hasNext()) {
427 KFileItem* item = mimeIt.next();
428 insert = !item->isDir();
429 }
430 }
431
432 if (!insert) {
433 // Check whether the MIME types of all selected files match
434 // to the mimetype of the service action. As soon as one MIME
435 // type does not match, no service menu is shown at all.
436 QListIterator<KFileItem*> mimeIt(m_selectedItems);
437 insert = true;
438 while (insert && mimeIt.hasNext()) {
439 KFileItem* item = mimeIt.next();
440 const QString mimeType(item->mimetype());
441 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
442
443 insert = (*it == mimeType) ||
444 ((*it).right(1) == "*") &&
445 ((*it).left((*it).indexOf('/')) == mimeGroup);
446 }
447 }
448
449 if (insert) {
450 menu = actionsMenu;
451
452 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
453 if (!submenuName.isEmpty()) {
454 menu = new KMenu(submenuName);
455 actionsMenu->addMenu(menu);
456 }
457
458 Q3ValueList<KDEDesktopMimeType::Service> userServices =
459 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
460
461 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
462 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
463 KDEDesktopMimeType::Service service = (*serviceIt);
464 if (!service.m_strIcon.isEmpty()) {
465 QAction* action = menu->addAction(SmallIcon(service.m_strIcon),
466 service.m_strName);
467 serviceActions << action;
468 }
469 else {
470 QAction *action = menu->addAction(service.m_strName);
471 serviceActions << action;
472 }
473 actionsVector.append(service);
474 }
475 }
476 }
477 }
478 }
479 }
480
481 const int itemsCount = actionsMenu->actions().count();
482 if (itemsCount == 0) {
483 // no actions are available at all, hence show the "Actions"
484 // submenu disabled
485 actionsMenu->setEnabled(false);
486 }
487
488 if (itemsCount == 1) {
489 // Exactly one item is available. Instead of showing a sub menu with
490 // only one item, show the item directly in the root menu.
491 if (menu == actionsMenu) {
492 // The item is an action, hence show the action in the root menu.
493 const QList<QAction*> actions = actionsMenu->actions();
494 Q_ASSERT(actions.count() == 1);
495
496 const QString text = actions[0]->text();
497 const QIcon icon = actions[0]->icon();
498 if (icon.isNull()) {
499 QAction* action = popup->addAction(text);
500 serviceActions.clear();
501 serviceActions << action;
502 }
503 else {
504 QAction* action = popup->addAction(icon, text);
505 serviceActions.clear();
506 serviceActions << action;
507 }
508 }
509 else {
510 // The item is a sub menu, hence show the sub menu in the root menu.
511 popup->addMenu(menu);
512 }
513 actionsMenu->deleteLater();
514 actionsMenu = 0;
515 }
516 else {
517 popup->addMenu(actionsMenu);
518 }
519
520 return serviceActions;
521 }
522
523 bool DolphinContextMenu::containsEntry(const KMenu* menu,
524 const QString& entryName) const
525 {
526 Q_ASSERT(menu != 0);
527
528 const QList<QAction*> list = menu->actions();
529 const uint count = list.count();
530 for (uint i = 0; i < count; ++i) {
531 const QAction* action = list.at(i);
532 if (action->text() == entryName) {
533 return true;
534 }
535 }
536
537 return false;
538 }