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