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