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