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