]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Allow to cut, copy, paste, ... the currently selected item from a sidebar by the...
[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 = m_mainWindow->activeView()->url();
370 if (url.isLocalFile()) {
371 QAction* moveToTrashAction = 0;
372 if (insertSidebarActions) {
373 moveToTrashAction = new QAction(KIcon("edit-trash"), i18n("Move To Trash"), this);
374 connect(moveToTrashAction, SIGNAL(triggered()), this, SLOT(moveToTrash()));
375 }
376 else {
377 collection->action("move_to_trash");
378 }
379 popup->addAction(moveToTrashAction);
380 }
381 else {
382 showDeleteCommand = true;
383 }
384
385 if (showDeleteCommand) {
386 QAction* deleteAction = 0;
387 if (insertSidebarActions) {
388 deleteAction = new QAction(KIcon("edit-delete"), i18n("Delete"), this);
389 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
390 }
391 else {
392 collection->action("delete");
393 }
394 popup->addAction(deleteAction);
395 }
396 }
397
398 QList<QAction*> DolphinContextMenu::insertOpenWithItems(KMenu* popup,
399 QVector<KService::Ptr>& openWithVector)
400 {
401 // Parts of the following code have been taken
402 // from the class KonqOperations located in
403 // libqonq/konq_operations.h of Konqueror.
404 // (Copyright (C) 2000 David Faure <faure@kde.org>)
405
406 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
407 // are listed which are registered to open the item. As last entry "Other..." will be
408 // attached which allows to select a custom application. If no applications are registered
409 // no sub menu is created at all, only "Open With..." will be offered.
410 bool insertOpenWithItems = true;
411 const QString contextMimeType(m_fileInfo->mimetype());
412
413 QListIterator<KFileItem*> mimeIt(m_selectedItems);
414 while (insertOpenWithItems && mimeIt.hasNext()) {
415 KFileItem* item = mimeIt.next();
416 insertOpenWithItems = (contextMimeType == item->mimetype());
417 }
418
419 QList<QAction*> openWithActions;
420 if (insertOpenWithItems) {
421 // fill the 'Open with' sub menu with application types
422 const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url());
423 KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(),
424 "Application",
425 "Type == 'Application'");
426 if (offers.count() > 0) {
427 KService::List::Iterator it;
428 KMenu* openWithMenu = new KMenu(i18n("Open With"));
429 for(it = offers.begin(); it != offers.end(); ++it) {
430 // The offer list from the KTrader returns duplicate
431 // application entries. Although this seems to be a configuration
432 // problem outside the scope of Dolphin, duplicated entries just
433 // will be skipped here.
434 const QString appName((*it)->name());
435 if (!containsEntry(openWithMenu, appName)) {
436 const KIcon icon((*it)->icon());
437 QAction* action = openWithMenu->addAction(icon, appName);
438 openWithVector.append(*it);
439 openWithActions << action;
440 }
441 }
442
443 openWithMenu->addSeparator();
444 QAction* action = openWithMenu->addAction(i18n("&Other..."));
445
446 openWithActions << action;
447 popup->addMenu(openWithMenu);
448 }
449 else {
450 // No applications are registered, hence just offer
451 // a "Open With..." item instead of a sub menu containing
452 // only one entry.
453 QAction* action = popup->addAction(i18n("Open With..."));
454 openWithActions << action;
455 }
456 }
457 else {
458 // At least one of the selected items has a different MIME type. In this case
459 // just show a disabled "Open With..." entry.
460 QAction* action = popup->addAction(i18n("Open With..."));
461 action->setEnabled(false);
462 }
463
464 return openWithActions;
465 }
466
467 QList<QAction*> DolphinContextMenu::insertActionItems(KMenu* popup,
468 QVector<KDEDesktopMimeType::Service>& actionsVector)
469 {
470 // Parts of the following code have been taken
471 // from the class KonqOperations located in
472 // libqonq/konq_operations.h of Konqueror.
473 // (Copyright (C) 2000 David Faure <faure@kde.org>)
474
475 KMenu* actionsMenu = new KMenu(i18n("Actions"));
476
477 QList<QAction*> serviceActions;
478
479 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
480
481 KMenu* menu = 0;
482 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
483 QDir dir(*dirIt);
484 QStringList filters;
485 filters << "*.desktop";
486 dir.setNameFilters(filters);
487 QStringList entries = dir.entryList(QDir::Files);
488
489 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
490 KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry" );
491 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
492 //const QStringList types = cfg.readListEntry("ServiceTypes");
493 QStringList types;
494 types = cfg.readEntry("ServiceTypes", types);
495 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
496 // check whether the mime type is equal or whether the
497 // mimegroup (e. g. image/*) is supported
498
499 bool insert = false;
500 if ((*it) == "all/allfiles") {
501 // The service type is valid for all files, but not for directories.
502 // Check whether the selected items only consist of files...
503 QListIterator<KFileItem*> mimeIt(m_selectedItems);
504 insert = true;
505 while (insert && mimeIt.hasNext()) {
506 KFileItem* item = mimeIt.next();
507 insert = !item->isDir();
508 }
509 }
510
511 if (!insert) {
512 // Check whether the MIME types of all selected files match
513 // to the mimetype of the service action. As soon as one MIME
514 // type does not match, no service menu is shown at all.
515 QListIterator<KFileItem*> mimeIt(m_selectedItems);
516 insert = true;
517 while (insert && mimeIt.hasNext()) {
518 KFileItem* item = mimeIt.next();
519 const QString mimeType(item->mimetype());
520 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
521
522 insert = (*it == mimeType) ||
523 ((*it).right(1) == "*") &&
524 ((*it).left((*it).indexOf('/')) == mimeGroup);
525 }
526 }
527
528 if (insert) {
529 menu = actionsMenu;
530
531 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
532 if (!submenuName.isEmpty()) {
533 menu = new KMenu(submenuName);
534 actionsMenu->addMenu(menu);
535 }
536
537 Q3ValueList<KDEDesktopMimeType::Service> userServices =
538 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
539
540 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
541 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
542 KDEDesktopMimeType::Service service = (*serviceIt);
543 if (!service.m_strIcon.isEmpty()) {
544 QAction* action = menu->addAction(SmallIcon(service.m_strIcon),
545 service.m_strName);
546 serviceActions << action;
547 }
548 else {
549 QAction *action = menu->addAction(service.m_strName);
550 serviceActions << action;
551 }
552 actionsVector.append(service);
553 }
554 }
555 }
556 }
557 }
558 }
559
560 const int itemsCount = actionsMenu->actions().count();
561 if (itemsCount == 0) {
562 // no actions are available at all, hence show the "Actions"
563 // submenu disabled
564 actionsMenu->setEnabled(false);
565 }
566
567 if (itemsCount == 1) {
568 // Exactly one item is available. Instead of showing a sub menu with
569 // only one item, show the item directly in the root menu.
570 if (menu == actionsMenu) {
571 // The item is an action, hence show the action in the root menu.
572 const QList<QAction*> actions = actionsMenu->actions();
573 Q_ASSERT(actions.count() == 1);
574
575 const QString text = actions[0]->text();
576 const QIcon icon = actions[0]->icon();
577 if (icon.isNull()) {
578 QAction* action = popup->addAction(text);
579 serviceActions.clear();
580 serviceActions << action;
581 }
582 else {
583 QAction* action = popup->addAction(icon, text);
584 serviceActions.clear();
585 serviceActions << action;
586 }
587 }
588 else {
589 // The item is a sub menu, hence show the sub menu in the root menu.
590 popup->addMenu(menu);
591 }
592 actionsMenu->deleteLater();
593 actionsMenu = 0;
594 }
595 else {
596 popup->addMenu(actionsMenu);
597 }
598
599 return serviceActions;
600 }
601
602 bool DolphinContextMenu::containsEntry(const KMenu* menu,
603 const QString& entryName) const
604 {
605 Q_ASSERT(menu != 0);
606
607 const QList<QAction*> list = menu->actions();
608 const uint count = list.count();
609 for (uint i = 0; i < count; ++i) {
610 const QAction* action = list.at(i);
611 if (action->text() == entryName) {
612 return true;
613 }
614 }
615
616 return false;
617 }
618
619 #include "dolphincontextmenu.moc"