]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphincontextmenu.h"
23 #include "dolphinmainwindow.h"
24 #include "dolphinsettings.h"
25 #include "dolphinview.h"
26 #include "editbookmarkdialog.h"
28 #include <kactioncollection.h>
29 #include <kbookmarkmanager.h>
30 #include <kbookmark.h>
31 #include <kdesktopfile.h>
33 #include <kiconloader.h>
34 #include <kio/netaccess.h>
36 #include <kmessagebox.h>
37 #include <kmimetypetrader.h>
39 #include <konq_operations.h>
41 #include <kpropertiesdialog.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
48 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
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();
65 else if (fileInfo
!= 0) {
66 m_selectedUrls
.append(fileInfo
->url());
67 m_selectedItems
.append(fileInfo
);
71 DolphinContextMenu::~DolphinContextMenu()
75 void DolphinContextMenu::open()
77 // get the context information
78 if (m_baseUrl
.protocol() == "trash") {
79 m_context
|= TrashContext
;
82 if (m_fileInfo
!= 0) {
83 m_context
|= ItemContext
;
84 // TODO: handle other use cases like devices + desktop files
87 // open the corresponding popup for the context
88 if (m_context
& TrashContext
) {
89 if (m_context
& ItemContext
) {
90 openTrashItemContextMenu();
93 openTrashContextMenu();
96 else if (m_context
& ItemContext
) {
97 openItemContextMenu();
100 Q_ASSERT(m_context
== NoContext
);
101 openViewportContextMenu();
105 void DolphinContextMenu::cut()
110 void DolphinContextMenu::copy()
115 void DolphinContextMenu::paste()
120 void DolphinContextMenu::rename()
125 void DolphinContextMenu::moveToTrash()
130 void DolphinContextMenu::deleteItem()
135 void DolphinContextMenu::showProperties()
137 new KPropertiesDialog(m_fileInfo
->url());
140 void DolphinContextMenu::openTrashContextMenu()
142 Q_ASSERT(m_context
& TrashContext
);
144 KMenu
* popup
= new KMenu(m_mainWindow
);
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
);
151 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
152 popup
->addAction(propertiesAction
);
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
,
159 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
160 ) == KMessageBox::Continue
;
162 KonqOperations::emptyTrash(m_mainWindow
);
166 popup
->deleteLater();
169 void DolphinContextMenu::openTrashItemContextMenu()
171 Q_ASSERT(m_context
& TrashContext
);
172 Q_ASSERT(m_context
& ItemContext
);
174 KMenu
* popup
= new KMenu(m_mainWindow
);
176 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
177 popup
->addAction(restoreAction
);
179 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
180 popup
->addAction(deleteAction
);
182 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
183 popup
->addAction(propertiesAction
);
185 if (popup
->exec(QCursor::pos()) == restoreAction
) {
186 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
189 popup
->deleteLater();
192 void DolphinContextMenu::openItemContextMenu()
194 Q_ASSERT(m_fileInfo
!= 0);
196 KMenu
* popup
= new KMenu(m_mainWindow
);
197 insertDefaultItemActions(popup
);
199 popup
->addSeparator();
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"));
207 // Insert 'Open With...' sub menu
208 QVector
<KService::Ptr
> openWithVector
;
209 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
211 // Insert 'Actions' sub menu
212 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
213 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
214 popup
->addSeparator();
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()));
223 propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
225 popup
->addAction(propertiesAction
);
227 QAction
* activatedAction
= popup
->exec(QCursor::pos());
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(),
235 if (!bookmark
.isNull()) {
236 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
237 KBookmarkGroup root
= manager
->root();
238 root
.addBookmark(manager
, bookmark
);
239 manager
->emitChanged(root
);
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
]);
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
);
254 int id
= openWithActions
.indexOf(activatedAction
);
255 KService::Ptr servicePtr
= openWithVector
[id
];
256 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
260 openWithVector
.clear();
261 actionsVector
.clear();
262 popup
->deleteLater();
265 void DolphinContextMenu::openViewportContextMenu()
267 Q_ASSERT(m_fileInfo
== 0);
268 KMenu
* popup
= new KMenu(m_mainWindow
);
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();
277 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
278 popup
->addAction(pasteAction
);
280 // setup 'View Mode' menu
281 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
283 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
284 viewModeMenu
->addAction(iconsMode
);
286 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
287 viewModeMenu
->addAction(detailsMode
);
289 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
290 viewModeMenu
->addAction(previewsMode
);
292 popup
->addMenu(viewModeMenu
);
293 popup
->addSeparator();
295 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
296 popup
->addSeparator();
298 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
300 QAction
* activatedAction
= popup
->exec(QCursor::pos());
301 if (activatedAction
== propertiesAction
) {
302 new KPropertiesDialog(m_mainWindow
->activeView()->url());
304 else if (activatedAction
== bookmarkAction
) {
305 const KUrl
& url
= m_mainWindow
->activeView()->url();
306 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as 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
);
318 popup
->deleteLater();
321 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
323 Q_ASSERT(popup
!= 0);
324 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
325 const bool insertSidebarActions
= (m_viewType
== SidebarView
);
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()));
335 copyAction
= new QAction(KIcon("edit-copy"), i18n("Copy"), this);
336 connect(copyAction
, SIGNAL(triggered()), this, SLOT(copy()));
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()));
344 cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
345 copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
346 pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
349 popup
->addAction(cutAction
);
350 popup
->addAction(copyAction
);
351 popup
->addAction(pasteAction
);
352 popup
->addSeparator();
355 QAction
* renameAction
= 0;
356 if (insertSidebarActions
) {
357 renameAction
= new QAction(i18n("Rename"), this);
358 connect(renameAction
, SIGNAL(triggered()), this, SLOT(paste()));
361 collection
->action("rename");
363 popup
->addAction(renameAction
);
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()));
377 collection
->action("move_to_trash");
379 popup
->addAction(moveToTrashAction
);
382 showDeleteCommand
= true;
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()));
392 collection
->action("delete");
394 popup
->addAction(deleteAction
);
398 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
399 QVector
<KService::Ptr
>& openWithVector
)
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>)
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());
413 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
414 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
415 KFileItem
* item
= mimeIt
.next();
416 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
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(),
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
;
443 openWithMenu
->addSeparator();
444 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
446 openWithActions
<< action
;
447 popup
->addMenu(openWithMenu
);
450 // No applications are registered, hence just offer
451 // a "Open With..." item instead of a sub menu containing
453 QAction
* action
= popup
->addAction(i18n("Open With..."));
454 openWithActions
<< action
;
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);
464 return openWithActions
;
467 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
468 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
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>)
475 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
477 QList
<QAction
*> serviceActions
;
479 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
482 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
485 filters
<< "*.desktop";
486 dir
.setNameFilters(filters
);
487 QStringList entries
= dir
.entryList(QDir::Files
);
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");
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
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
);
505 while (insert
&& mimeIt
.hasNext()) {
506 KFileItem
* item
= mimeIt
.next();
507 insert
= !item
->isDir();
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
);
517 while (insert
&& mimeIt
.hasNext()) {
518 KFileItem
* item
= mimeIt
.next();
519 const QString
mimeType(item
->mimetype());
520 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
522 insert
= (*it
== mimeType
) ||
523 ((*it
).right(1) == "*") &&
524 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
531 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
532 if (!submenuName
.isEmpty()) {
533 menu
= new KMenu(submenuName
);
534 actionsMenu
->addMenu(menu
);
537 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
538 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
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
),
546 serviceActions
<< action
;
549 QAction
*action
= menu
->addAction(service
.m_strName
);
550 serviceActions
<< action
;
552 actionsVector
.append(service
);
560 const int itemsCount
= actionsMenu
->actions().count();
561 if (itemsCount
== 0) {
562 // no actions are available at all, hence show the "Actions"
564 actionsMenu
->setEnabled(false);
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);
575 const QString text
= actions
[0]->text();
576 const QIcon icon
= actions
[0]->icon();
578 QAction
* action
= popup
->addAction(text
);
579 serviceActions
.clear();
580 serviceActions
<< action
;
583 QAction
* action
= popup
->addAction(icon
, text
);
584 serviceActions
.clear();
585 serviceActions
<< action
;
589 // The item is a sub menu, hence show the sub menu in the root menu.
590 popup
->addMenu(menu
);
592 actionsMenu
->deleteLater();
596 popup
->addMenu(actionsMenu
);
599 return serviceActions
;
602 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
603 const QString
& entryName
) const
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
) {
619 #include "dolphincontextmenu.moc"