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