]>
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()
70 void DolphinContextMenu::open()
72 // get the context information
73 if (m_baseUrl
.protocol() == "trash") {
74 m_context
|= TrashContext
;
77 if (m_fileInfo
!= 0) {
78 m_context
|= ItemContext
;
79 // TODO: handle other use cases like devices + desktop files
82 // open the corresponding popup for the context
83 if (m_context
& TrashContext
) {
84 if (m_context
& ItemContext
) {
85 openTrashItemContextMenu();
87 openTrashContextMenu();
89 } else if (m_context
& ItemContext
) {
90 openItemContextMenu();
92 Q_ASSERT(m_context
== NoContext
);
93 openViewportContextMenu();
98 void DolphinContextMenu::openTrashContextMenu()
100 Q_ASSERT(m_context
& TrashContext
);
102 KMenu
* popup
= new KMenu(m_mainWindow
);
104 QAction
* emptyTrashAction
= new QAction(KIcon("emptytrash"), i18nc("@action:inmenu", "Empty Trash"), popup
);
105 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
106 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
107 popup
->addAction(emptyTrashAction
);
109 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
110 popup
->addAction(propertiesAction
);
112 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
113 const QString
text(i18nc("@info", "Do you really want to empty the Trash? All items will get deleted."));
114 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
117 KGuiItem(i18nc("@action:button", "Empty Trash"),
119 ) == KMessageBox::Continue
;
121 KonqOperations::emptyTrash(m_mainWindow
);
125 popup
->deleteLater();
128 void DolphinContextMenu::openTrashItemContextMenu()
130 Q_ASSERT(m_context
& TrashContext
);
131 Q_ASSERT(m_context
& ItemContext
);
133 KMenu
* popup
= new KMenu(m_mainWindow
);
135 QAction
* restoreAction
= new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow
);
136 popup
->addAction(restoreAction
);
138 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
139 popup
->addAction(deleteAction
);
141 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
142 popup
->addAction(propertiesAction
);
144 if (popup
->exec(QCursor::pos()) == restoreAction
) {
145 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
148 popup
->deleteLater();
151 void DolphinContextMenu::openItemContextMenu()
153 Q_ASSERT(m_fileInfo
!= 0);
155 KMenu
* popup
= new KMenu(m_mainWindow
);
156 insertDefaultItemActions(popup
);
158 popup
->addSeparator();
160 // insert 'Bookmark This Folder' entry if exactly one item is selected
161 QAction
* bookmarkAction
= 0;
162 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
163 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"),
164 i18nc("@action:inmenu", "Bookmark Folder..."));
167 // Insert 'Open With...' sub menu
168 QVector
<KService::Ptr
> openWithVector
;
169 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
171 // Insert 'Actions' sub menu
172 QVector
<KDesktopFileActions::Service
> actionsVector
;
173 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
174 popup
->addSeparator();
176 // insert 'Properties...' entry
177 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
178 popup
->addAction(propertiesAction
);
180 QAction
* activatedAction
= popup
->exec(QCursor::pos());
182 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
183 const KUrl
selectedUrl(m_fileInfo
->url());
184 if (selectedUrl
.isValid()) {
185 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
188 } else if (serviceActions
.contains(activatedAction
)) {
189 // one of the 'Actions' items has been selected
190 int id
= serviceActions
.indexOf(activatedAction
);
191 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
192 } else if (openWithActions
.contains(activatedAction
)) {
193 // one of the 'Open With' items has been selected
194 if (openWithActions
.last() == activatedAction
) {
195 // the item 'Other...' has been selected
196 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
198 int id
= openWithActions
.indexOf(activatedAction
);
199 KService::Ptr servicePtr
= openWithVector
[id
];
200 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
204 openWithVector
.clear();
205 actionsVector
.clear();
206 popup
->deleteLater();
209 void DolphinContextMenu::openViewportContextMenu()
211 Q_ASSERT(m_fileInfo
== 0);
212 KMenu
* popup
= new KMenu(m_mainWindow
);
214 // setup 'Create New' menu
215 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
216 newMenu
->slotCheckUpToDate();
217 newMenu
->setPopupFiles(m_baseUrl
);
218 popup
->addMenu(newMenu
->menu());
219 popup
->addSeparator();
221 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
222 popup
->addAction(pasteAction
);
224 // setup 'View Mode' menu
225 KMenu
* viewModeMenu
= new KMenu(i18nc("@title:menu", "View Mode"));
227 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
228 viewModeMenu
->addAction(iconsMode
);
230 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
231 viewModeMenu
->addAction(detailsMode
);
233 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
234 viewModeMenu
->addAction(columnsMode
);
236 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
237 viewModeMenu
->addAction(previewsMode
);
239 popup
->addMenu(viewModeMenu
);
241 popup
->addSeparator();
243 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"),
244 i18nc("@action:inmenu", "Bookmark This Folder..."));
245 popup
->addSeparator();
247 QAction
* propertiesAction
= popup
->addAction(i18nc("@action:inmenu", "Properties"));
249 QAction
* action
= popup
->exec(QCursor::pos());
250 if (action
== propertiesAction
) {
251 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
252 KPropertiesDialog
dialog(url
);
254 } else if (action
== bookmarkAction
) {
255 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
257 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
261 popup
->deleteLater();
264 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
266 Q_ASSERT(popup
!= 0);
267 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
269 // insert 'Cut', 'Copy' and 'Paste'
270 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
271 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
272 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
274 popup
->addAction(cutAction
);
275 popup
->addAction(copyAction
);
276 popup
->addAction(pasteAction
);
277 popup
->addSeparator();
280 QAction
* renameAction
= collection
->action("rename");
281 popup
->addAction(renameAction
);
283 // insert 'Move to Trash' and (optionally) 'Delete'
284 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
285 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
286 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
287 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
288 if (url
.isLocalFile()) {
289 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
290 popup
->addAction(moveToTrashAction
);
292 showDeleteCommand
= true;
295 if (showDeleteCommand
) {
296 QAction
* deleteAction
= collection
->action("delete");
297 popup
->addAction(deleteAction
);
301 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
302 QVector
<KService::Ptr
>& openWithVector
)
304 // Parts of the following code have been taken
305 // from the class KonqOperations located in
306 // libqonq/konq_operations.h of Konqueror.
307 // (Copyright (C) 2000 David Faure <faure@kde.org>)
309 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
310 // are listed which are registered to open the item. As last entry "Other..." will be
311 // attached which allows to select a custom application. If no applications are registered
312 // no sub menu is created at all, only "Open With..." will be offered.
313 bool insertOpenWithItems
= true;
314 const QString
contextMimeType(m_fileInfo
->mimetype());
316 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
317 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
318 KFileItem
* item
= mimeIt
.next();
319 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
322 QList
<QAction
*> openWithActions
;
323 if (insertOpenWithItems
) {
324 // fill the 'Open with' sub menu with application types
325 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
326 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
328 "Type == 'Application'");
329 if (offers
.count() > 0) {
330 KService::List::Iterator it
;
331 KMenu
* openWithMenu
= new KMenu(i18nc("@title:menu", "Open With"));
332 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
333 // The offer list from the KTrader returns duplicate
334 // application entries. Although this seems to be a configuration
335 // problem outside the scope of Dolphin, duplicated entries just
336 // will be skipped here.
337 const QString
appName((*it
)->name());
338 if (!containsEntry(openWithMenu
, appName
)) {
339 const KIcon
icon((*it
)->icon());
340 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
341 openWithVector
.append(*it
);
342 openWithActions
<< action
;
346 openWithMenu
->addSeparator();
347 QAction
* action
= openWithMenu
->addAction(i18nc("@action:inmenu Open With", "&Other..."));
349 openWithActions
<< action
;
350 popup
->addMenu(openWithMenu
);
352 // No applications are registered, hence just offer
353 // a "Open With..." item instead of a sub menu containing
355 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
356 openWithActions
<< action
;
359 // At least one of the selected items has a different MIME type. In this case
360 // just show a disabled "Open With..." entry.
361 QAction
* action
= popup
->addAction(i18nc("@title:menu", "Open With..."));
362 action
->setEnabled(false);
365 return openWithActions
;
368 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
369 QVector
<KDesktopFileActions::Service
>& actionsVector
)
371 // Parts of the following code have been taken
372 // from the class KonqOperations located in
373 // libqonq/konq_operations.h of Konqueror.
374 // (Copyright (C) 2000 David Faure <faure@kde.org>)
376 KMenu
* actionsMenu
= new KMenu(i18nc("@title:menu", "Actions"));
378 QList
<QAction
*> serviceActions
;
380 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
383 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
386 filters
<< "*.desktop";
387 dir
.setNameFilters(filters
);
388 QStringList entries
= dir
.entryList(QDir::Files
);
390 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
391 KConfigGroup
cfg(KSharedConfig::openConfig(*dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry");
392 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
393 //const QStringList types = cfg.readListEntry("ServiceTypes");
395 types
= cfg
.readEntry("ServiceTypes", types
);
396 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
397 // check whether the mime type is equal or whether the
398 // mimegroup (e. g. image/*) is supported
401 if ((*it
) == "all/allfiles") {
402 // The service type is valid for all files, but not for directories.
403 // Check whether the selected items only consist of files...
404 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
406 while (insert
&& mimeIt
.hasNext()) {
407 KFileItem
* item
= mimeIt
.next();
408 insert
= !item
->isDir();
413 // Check whether the MIME types of all selected files match
414 // to the mimetype of the service action. As soon as one MIME
415 // type does not match, no service menu is shown at all.
416 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
418 while (insert
&& mimeIt
.hasNext()) {
419 KFileItem
* item
= mimeIt
.next();
420 const QString
mimeType(item
->mimetype());
421 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
423 insert
= (*it
== mimeType
) ||
424 ((*it
).right(1) == "*") &&
425 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
432 const QString submenuName
= cfg
.readEntry("X-KDE-Submenu");
433 if (!submenuName
.isEmpty()) {
434 menu
= new KMenu(submenuName
);
435 actionsMenu
->addMenu(menu
);
438 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
439 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
441 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
442 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
443 KDesktopFileActions::Service service
= (*serviceIt
);
444 if (!service
.m_strIcon
.isEmpty()) {
445 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
447 serviceActions
<< action
;
449 QAction
*action
= menu
->addAction(service
.m_strName
);
450 serviceActions
<< action
;
452 actionsVector
.append(service
);
460 const int itemsCount
= actionsMenu
->actions().count();
461 if (itemsCount
== 0) {
462 // no actions are available at all, hence show the "Actions"
464 actionsMenu
->setEnabled(false);
467 if (itemsCount
== 1) {
468 // Exactly one item is available. Instead of showing a sub menu with
469 // only one item, show the item directly in the root menu.
470 if (menu
== actionsMenu
) {
471 // The item is an action, hence show the action in the root menu.
472 const QList
<QAction
*> actions
= actionsMenu
->actions();
473 Q_ASSERT(actions
.count() == 1);
475 const QString text
= actions
[0]->text();
476 const QIcon icon
= actions
[0]->icon();
478 QAction
* action
= popup
->addAction(text
);
479 serviceActions
.clear();
480 serviceActions
<< action
;
482 QAction
* action
= popup
->addAction(icon
, text
);
483 serviceActions
.clear();
484 serviceActions
<< action
;
487 // The item is a sub menu, hence show the sub menu in the root menu.
488 popup
->addMenu(menu
);
490 actionsMenu
->deleteLater();
493 popup
->addMenu(actionsMenu
);
496 return serviceActions
;
499 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
500 const QString
& entryName
) const
504 const QList
<QAction
*> list
= menu
->actions();
505 const uint count
= list
.count();
506 for (uint i
= 0; i
< count
; ++i
) {
507 const QAction
* action
= list
.at(i
);
508 if (action
->text() == entryName
) {
516 #include "dolphincontextmenu.moc"