]>
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 "dolphinviewcontainer.h"
28 #include <kactioncollection.h>
29 #include <kfileplacesmodel.h>
30 #include <kdesktopfile.h>
32 #include <kiconloader.h>
33 #include <kio/netaccess.h>
35 #include <kmessagebox.h>
36 #include <kmimetypetrader.h>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
41 #include <kpropertiesdialog.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
46 #include <QtGui/QApplication>
47 #include <QtGui/QClipboard>
48 #include <QtCore/QDir>
49 #include <Qt3Support/Q3ValueList>
51 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
53 const KUrl
& baseUrl
) :
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();
66 DolphinContextMenu::~DolphinContextMenu()
69 void DolphinContextMenu::open()
71 // get the context information
72 if (m_baseUrl
.protocol() == "trash") {
73 m_context
|= TrashContext
;
76 if (m_fileInfo
!= 0) {
77 m_context
|= ItemContext
;
78 // TODO: handle other use cases like devices + desktop files
81 // open the corresponding popup for the context
82 if (m_context
& TrashContext
) {
83 if (m_context
& ItemContext
) {
84 openTrashItemContextMenu();
86 openTrashContextMenu();
88 } else if (m_context
& ItemContext
) {
89 openItemContextMenu();
91 Q_ASSERT(m_context
== NoContext
);
92 openViewportContextMenu();
97 void DolphinContextMenu::openTrashContextMenu()
99 Q_ASSERT(m_context
& TrashContext
);
101 KMenu
* popup
= new KMenu(m_mainWindow
);
103 QAction
* emptyTrashAction
= new QAction(KIcon("emptytrash"), i18n("Empty Trash"), popup
);
104 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
105 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
106 popup
->addAction(emptyTrashAction
);
108 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
109 popup
->addAction(propertiesAction
);
111 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
112 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
113 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
116 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
117 ) == KMessageBox::Continue
;
119 KonqOperations::emptyTrash(m_mainWindow
);
123 popup
->deleteLater();
126 void DolphinContextMenu::openTrashItemContextMenu()
128 Q_ASSERT(m_context
& TrashContext
);
129 Q_ASSERT(m_context
& ItemContext
);
131 KMenu
* popup
= new KMenu(m_mainWindow
);
133 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
134 popup
->addAction(restoreAction
);
136 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
137 popup
->addAction(deleteAction
);
139 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
140 popup
->addAction(propertiesAction
);
142 if (popup
->exec(QCursor::pos()) == restoreAction
) {
143 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
146 popup
->deleteLater();
149 void DolphinContextMenu::openItemContextMenu()
151 Q_ASSERT(m_fileInfo
!= 0);
153 KMenu
* popup
= new KMenu(m_mainWindow
);
154 insertDefaultItemActions(popup
);
156 popup
->addSeparator();
158 // insert 'Bookmark This Folder' entry if exactly one item is selected
159 QAction
* bookmarkAction
= 0;
160 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
161 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
164 // Insert 'Open With...' sub menu
165 QVector
<KService::Ptr
> openWithVector
;
166 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
168 // Insert 'Actions' sub menu
169 QVector
<KDesktopFileActions::Service
> actionsVector
;
170 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
171 popup
->addSeparator();
173 // insert 'Properties...' entry
174 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
175 popup
->addAction(propertiesAction
);
177 QAction
* activatedAction
= popup
->exec(QCursor::pos());
179 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
180 const KUrl
selectedUrl(m_fileInfo
->url());
181 if (selectedUrl
.isValid()) {
182 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
185 } else if (serviceActions
.contains(activatedAction
)) {
186 // one of the 'Actions' items has been selected
187 int id
= serviceActions
.indexOf(activatedAction
);
188 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
189 } else if (openWithActions
.contains(activatedAction
)) {
190 // one of the 'Open With' items has been selected
191 if (openWithActions
.last() == activatedAction
) {
192 // the item 'Other...' has been selected
193 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
195 int id
= openWithActions
.indexOf(activatedAction
);
196 KService::Ptr servicePtr
= openWithVector
[id
];
197 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
201 openWithVector
.clear();
202 actionsVector
.clear();
203 popup
->deleteLater();
206 void DolphinContextMenu::openViewportContextMenu()
208 Q_ASSERT(m_fileInfo
== 0);
209 KMenu
* popup
= new KMenu(m_mainWindow
);
211 // setup 'Create New' menu
212 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
213 newMenu
->slotCheckUpToDate();
214 newMenu
->setPopupFiles(m_baseUrl
);
215 popup
->addMenu(newMenu
->menu());
216 popup
->addSeparator();
218 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
219 popup
->addAction(pasteAction
);
221 // setup 'View Mode' menu
222 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
224 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
225 viewModeMenu
->addAction(iconsMode
);
227 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
228 viewModeMenu
->addAction(detailsMode
);
230 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
231 viewModeMenu
->addAction(columnsMode
);
233 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
234 viewModeMenu
->addAction(previewsMode
);
236 popup
->addMenu(viewModeMenu
);
238 QAction
* toggleViewsAction
= 0;
239 if (m_mainWindow
->isSplit()) {
240 toggleViewsAction
= popup
->addAction(i18n("Toggle Views"));
243 popup
->addSeparator();
245 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
246 popup
->addSeparator();
248 QAction
* propertiesAction
= popup
->addAction(i18n("Properties"));
250 QAction
* action
= popup
->exec(QCursor::pos());
251 if (action
== propertiesAction
) {
252 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
253 KPropertiesDialog
dialog(url
);
255 } else if (action
== bookmarkAction
) {
256 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
258 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
260 } else if ((toggleViewsAction
!= 0) && (action
== toggleViewsAction
)) {
261 m_mainWindow
->toggleViews();
264 popup
->deleteLater();
267 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
269 Q_ASSERT(popup
!= 0);
270 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
272 // insert 'Cut', 'Copy' and 'Paste'
273 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
274 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
275 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
277 popup
->addAction(cutAction
);
278 popup
->addAction(copyAction
);
279 popup
->addAction(pasteAction
);
280 popup
->addSeparator();
283 QAction
* renameAction
= collection
->action("rename");
284 popup
->addAction(renameAction
);
286 // insert 'Move to Trash' and (optionally) 'Delete'
287 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
288 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
289 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
290 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
291 if (url
.isLocalFile()) {
292 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
293 popup
->addAction(moveToTrashAction
);
295 showDeleteCommand
= true;
298 if (showDeleteCommand
) {
299 QAction
* deleteAction
= collection
->action("delete");
300 popup
->addAction(deleteAction
);
304 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
305 QVector
<KService::Ptr
>& openWithVector
)
307 // Parts of the following code have been taken
308 // from the class KonqOperations located in
309 // libqonq/konq_operations.h of Konqueror.
310 // (Copyright (C) 2000 David Faure <faure@kde.org>)
312 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
313 // are listed which are registered to open the item. As last entry "Other..." will be
314 // attached which allows to select a custom application. If no applications are registered
315 // no sub menu is created at all, only "Open With..." will be offered.
316 bool insertOpenWithItems
= true;
317 const QString
contextMimeType(m_fileInfo
->mimetype());
319 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
320 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
321 KFileItem
* item
= mimeIt
.next();
322 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
325 QList
<QAction
*> openWithActions
;
326 if (insertOpenWithItems
) {
327 // fill the 'Open with' sub menu with application types
328 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
329 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
331 "Type == 'Application'");
332 if (offers
.count() > 0) {
333 KService::List::Iterator it
;
334 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
335 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
336 // The offer list from the KTrader returns duplicate
337 // application entries. Although this seems to be a configuration
338 // problem outside the scope of Dolphin, duplicated entries just
339 // will be skipped here.
340 const QString
appName((*it
)->name());
341 if (!containsEntry(openWithMenu
, appName
)) {
342 const KIcon
icon((*it
)->icon());
343 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
344 openWithVector
.append(*it
);
345 openWithActions
<< action
;
349 openWithMenu
->addSeparator();
350 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
352 openWithActions
<< action
;
353 popup
->addMenu(openWithMenu
);
355 // No applications are registered, hence just offer
356 // a "Open With..." item instead of a sub menu containing
358 QAction
* action
= popup
->addAction(i18n("Open With..."));
359 openWithActions
<< action
;
362 // At least one of the selected items has a different MIME type. In this case
363 // just show a disabled "Open With..." entry.
364 QAction
* action
= popup
->addAction(i18n("Open With..."));
365 action
->setEnabled(false);
368 return openWithActions
;
371 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
372 QVector
<KDesktopFileActions::Service
>& actionsVector
)
374 // Parts of the following code have been taken
375 // from the class KonqOperations located in
376 // libqonq/konq_operations.h of Konqueror.
377 // (Copyright (C) 2000 David Faure <faure@kde.org>)
379 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
381 QList
<QAction
*> serviceActions
;
383 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
386 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
389 filters
<< "*.desktop";
390 dir
.setNameFilters(filters
);
391 QStringList entries
= dir
.entryList(QDir::Files
);
393 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
394 KConfigGroup
cfg(KSharedConfig::openConfig(*dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry");
395 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
396 //const QStringList types = cfg.readListEntry("ServiceTypes");
398 types
= cfg
.readEntry("ServiceTypes", types
);
399 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
400 // check whether the mime type is equal or whether the
401 // mimegroup (e. g. image/*) is supported
404 if ((*it
) == "all/allfiles") {
405 // The service type is valid for all files, but not for directories.
406 // Check whether the selected items only consist of files...
407 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
409 while (insert
&& mimeIt
.hasNext()) {
410 KFileItem
* item
= mimeIt
.next();
411 insert
= !item
->isDir();
416 // Check whether the MIME types of all selected files match
417 // to the mimetype of the service action. As soon as one MIME
418 // type does not match, no service menu is shown at all.
419 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
421 while (insert
&& mimeIt
.hasNext()) {
422 KFileItem
* item
= mimeIt
.next();
423 const QString
mimeType(item
->mimetype());
424 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
426 insert
= (*it
== mimeType
) ||
427 ((*it
).right(1) == "*") &&
428 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
435 const QString submenuName
= cfg
.readEntry("X-KDE-Submenu");
436 if (!submenuName
.isEmpty()) {
437 menu
= new KMenu(submenuName
);
438 actionsMenu
->addMenu(menu
);
441 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
442 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
444 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
445 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
446 KDesktopFileActions::Service service
= (*serviceIt
);
447 if (!service
.m_strIcon
.isEmpty()) {
448 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
450 serviceActions
<< action
;
452 QAction
*action
= menu
->addAction(service
.m_strName
);
453 serviceActions
<< action
;
455 actionsVector
.append(service
);
463 const int itemsCount
= actionsMenu
->actions().count();
464 if (itemsCount
== 0) {
465 // no actions are available at all, hence show the "Actions"
467 actionsMenu
->setEnabled(false);
470 if (itemsCount
== 1) {
471 // Exactly one item is available. Instead of showing a sub menu with
472 // only one item, show the item directly in the root menu.
473 if (menu
== actionsMenu
) {
474 // The item is an action, hence show the action in the root menu.
475 const QList
<QAction
*> actions
= actionsMenu
->actions();
476 Q_ASSERT(actions
.count() == 1);
478 const QString text
= actions
[0]->text();
479 const QIcon icon
= actions
[0]->icon();
481 QAction
* action
= popup
->addAction(text
);
482 serviceActions
.clear();
483 serviceActions
<< action
;
485 QAction
* action
= popup
->addAction(icon
, text
);
486 serviceActions
.clear();
487 serviceActions
<< action
;
490 // The item is a sub menu, hence show the sub menu in the root menu.
491 popup
->addMenu(menu
);
493 actionsMenu
->deleteLater();
496 popup
->addMenu(actionsMenu
);
499 return serviceActions
;
502 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
503 const QString
& entryName
) const
507 const QList
<QAction
*> list
= menu
->actions();
508 const uint count
= list
.count();
509 for (uint i
= 0; i
< count
; ++i
) {
510 const QAction
* action
= list
.at(i
);
511 if (action
->text() == entryName
) {
519 #include "dolphincontextmenu.moc"