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