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