]>
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"
27 #include <kactioncollection.h>
28 #include <kfileplacesmodel.h>
29 #include <kdesktopfile.h>
31 #include <kiconloader.h>
32 #include <kio/netaccess.h>
34 #include <kmessagebox.h>
35 #include <kmimetypetrader.h>
37 #include <konqmimedata.h>
38 #include <konq_operations.h>
40 #include <kpropertiesdialog.h>
42 #include <kstandardaction.h>
43 #include <kstandarddirs.h>
45 #include <QtGui/QApplication>
46 #include <QtGui/QClipboard>
47 #include <QtCore/QDir>
48 #include <Qt3Support/Q3ValueList>
50 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
52 const KUrl
& baseUrl
) :
58 // The context menu either accesses the URLs of the selected items
59 // or the items itself. To increase the performance both lists are cached.
60 DolphinView
* view
= m_mainWindow
->activeView();
61 m_selectedUrls
= view
->selectedUrls();
62 m_selectedItems
= view
->selectedItems();
65 DolphinContextMenu::~DolphinContextMenu()
68 void DolphinContextMenu::open()
70 // get the context information
71 if (m_baseUrl
.protocol() == "trash") {
72 m_context
|= TrashContext
;
75 if (m_fileInfo
!= 0) {
76 m_context
|= ItemContext
;
77 // TODO: handle other use cases like devices + desktop files
80 // open the corresponding popup for the context
81 if (m_context
& TrashContext
) {
82 if (m_context
& ItemContext
) {
83 openTrashItemContextMenu();
85 openTrashContextMenu();
87 } else if (m_context
& ItemContext
) {
88 openItemContextMenu();
90 Q_ASSERT(m_context
== NoContext
);
91 openViewportContextMenu();
96 void DolphinContextMenu::openTrashContextMenu()
98 Q_ASSERT(m_context
& TrashContext
);
100 KMenu
* popup
= new KMenu(m_mainWindow
);
102 QAction
* emptyTrashAction
= new QAction(KIcon("emptytrash"), i18n("Empty Trash"), popup
);
103 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
104 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
105 popup
->addAction(emptyTrashAction
);
107 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
108 popup
->addAction(propertiesAction
);
110 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
111 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
112 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
115 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
116 ) == KMessageBox::Continue
;
118 KonqOperations::emptyTrash(m_mainWindow
);
122 popup
->deleteLater();
125 void DolphinContextMenu::openTrashItemContextMenu()
127 Q_ASSERT(m_context
& TrashContext
);
128 Q_ASSERT(m_context
& ItemContext
);
130 KMenu
* popup
= new KMenu(m_mainWindow
);
132 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
133 popup
->addAction(restoreAction
);
135 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
136 popup
->addAction(deleteAction
);
138 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
139 popup
->addAction(propertiesAction
);
141 if (popup
->exec(QCursor::pos()) == restoreAction
) {
142 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
145 popup
->deleteLater();
148 void DolphinContextMenu::openItemContextMenu()
150 Q_ASSERT(m_fileInfo
!= 0);
152 KMenu
* popup
= new KMenu(m_mainWindow
);
153 insertDefaultItemActions(popup
);
155 popup
->addSeparator();
157 // insert 'Bookmark This Folder' entry if exactly one item is selected
158 QAction
* bookmarkAction
= 0;
159 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
160 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
163 // Insert 'Open With...' sub menu
164 QVector
<KService::Ptr
> openWithVector
;
165 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
167 // Insert 'Actions' sub menu
168 QVector
<KDesktopFileActions::Service
> actionsVector
;
169 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
170 popup
->addSeparator();
172 // insert 'Properties...' entry
173 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
174 popup
->addAction(propertiesAction
);
176 QAction
* activatedAction
= popup
->exec(QCursor::pos());
178 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
179 const KUrl
selectedUrl(m_fileInfo
->url());
180 if (selectedUrl
.isValid()) {
181 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
184 } else if (serviceActions
.contains(activatedAction
)) {
185 // one of the 'Actions' items has been selected
186 int id
= serviceActions
.indexOf(activatedAction
);
187 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
188 } else if (openWithActions
.contains(activatedAction
)) {
189 // one of the 'Open With' items has been selected
190 if (openWithActions
.last() == activatedAction
) {
191 // the item 'Other...' has been selected
192 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
194 int id
= openWithActions
.indexOf(activatedAction
);
195 KService::Ptr servicePtr
= openWithVector
[id
];
196 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
200 openWithVector
.clear();
201 actionsVector
.clear();
202 popup
->deleteLater();
205 void DolphinContextMenu::openViewportContextMenu()
207 Q_ASSERT(m_fileInfo
== 0);
208 KMenu
* popup
= new KMenu(m_mainWindow
);
210 // setup 'Create New' menu
211 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
212 newMenu
->slotCheckUpToDate();
213 newMenu
->setPopupFiles(m_baseUrl
);
214 popup
->addMenu(newMenu
->menu());
215 popup
->addSeparator();
217 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
218 popup
->addAction(pasteAction
);
220 // setup 'View Mode' menu
221 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
223 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
224 viewModeMenu
->addAction(iconsMode
);
226 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
227 viewModeMenu
->addAction(detailsMode
);
229 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
230 viewModeMenu
->addAction(columnsMode
);
232 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
233 viewModeMenu
->addAction(previewsMode
);
235 popup
->addMenu(viewModeMenu
);
236 popup
->addSeparator();
238 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
239 popup
->addSeparator();
241 QAction
* propertiesAction
= popup
->addAction(i18n("Properties"));
243 QAction
* activatedAction
= popup
->exec(QCursor::pos());
244 if (activatedAction
== propertiesAction
) {
245 const KUrl
& url
= m_mainWindow
->activeView()->url();
246 KPropertiesDialog
dialog(url
);
248 } else if (activatedAction
== bookmarkAction
) {
249 const KUrl
& url
= m_mainWindow
->activeView()->url();
251 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
255 popup
->deleteLater();
258 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
260 Q_ASSERT(popup
!= 0);
261 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
263 // insert 'Cut', 'Copy' and 'Paste'
264 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
265 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
266 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
268 popup
->addAction(cutAction
);
269 popup
->addAction(copyAction
);
270 popup
->addAction(pasteAction
);
271 popup
->addSeparator();
274 QAction
* renameAction
= collection
->action("rename");
275 popup
->addAction(renameAction
);
277 // insert 'Move to Trash' and (optionally) 'Delete'
278 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
279 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
280 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
281 const KUrl
& url
= m_mainWindow
->activeView()->url();
282 if (url
.isLocalFile()) {
283 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
284 popup
->addAction(moveToTrashAction
);
286 showDeleteCommand
= true;
289 if (showDeleteCommand
) {
290 QAction
* deleteAction
= collection
->action("delete");
291 popup
->addAction(deleteAction
);
295 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
296 QVector
<KService::Ptr
>& openWithVector
)
298 // Parts of the following code have been taken
299 // from the class KonqOperations located in
300 // libqonq/konq_operations.h of Konqueror.
301 // (Copyright (C) 2000 David Faure <faure@kde.org>)
303 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
304 // are listed which are registered to open the item. As last entry "Other..." will be
305 // attached which allows to select a custom application. If no applications are registered
306 // no sub menu is created at all, only "Open With..." will be offered.
307 bool insertOpenWithItems
= true;
308 const QString
contextMimeType(m_fileInfo
->mimetype());
310 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
311 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
312 KFileItem
* item
= mimeIt
.next();
313 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
316 QList
<QAction
*> openWithActions
;
317 if (insertOpenWithItems
) {
318 // fill the 'Open with' sub menu with application types
319 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
320 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
322 "Type == 'Application'");
323 if (offers
.count() > 0) {
324 KService::List::Iterator it
;
325 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
326 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
327 // The offer list from the KTrader returns duplicate
328 // application entries. Although this seems to be a configuration
329 // problem outside the scope of Dolphin, duplicated entries just
330 // will be skipped here.
331 const QString
appName((*it
)->name());
332 if (!containsEntry(openWithMenu
, appName
)) {
333 const KIcon
icon((*it
)->icon());
334 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
335 openWithVector
.append(*it
);
336 openWithActions
<< action
;
340 openWithMenu
->addSeparator();
341 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
343 openWithActions
<< action
;
344 popup
->addMenu(openWithMenu
);
346 // No applications are registered, hence just offer
347 // a "Open With..." item instead of a sub menu containing
349 QAction
* action
= popup
->addAction(i18n("Open With..."));
350 openWithActions
<< action
;
353 // At least one of the selected items has a different MIME type. In this case
354 // just show a disabled "Open With..." entry.
355 QAction
* action
= popup
->addAction(i18n("Open With..."));
356 action
->setEnabled(false);
359 return openWithActions
;
362 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
363 QVector
<KDesktopFileActions::Service
>& actionsVector
)
365 // Parts of the following code have been taken
366 // from the class KonqOperations located in
367 // libqonq/konq_operations.h of Konqueror.
368 // (Copyright (C) 2000 David Faure <faure@kde.org>)
370 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
372 QList
<QAction
*> serviceActions
;
374 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
377 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
380 filters
<< "*.desktop";
381 dir
.setNameFilters(filters
);
382 QStringList entries
= dir
.entryList(QDir::Files
);
384 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
385 KConfigGroup
cfg(KSharedConfig::openConfig(*dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry");
386 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
387 //const QStringList types = cfg.readListEntry("ServiceTypes");
389 types
= cfg
.readEntry("ServiceTypes", types
);
390 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
391 // check whether the mime type is equal or whether the
392 // mimegroup (e. g. image/*) is supported
395 if ((*it
) == "all/allfiles") {
396 // The service type is valid for all files, but not for directories.
397 // Check whether the selected items only consist of files...
398 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
400 while (insert
&& mimeIt
.hasNext()) {
401 KFileItem
* item
= mimeIt
.next();
402 insert
= !item
->isDir();
407 // Check whether the MIME types of all selected files match
408 // to the mimetype of the service action. As soon as one MIME
409 // type does not match, no service menu is shown at all.
410 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
412 while (insert
&& mimeIt
.hasNext()) {
413 KFileItem
* item
= mimeIt
.next();
414 const QString
mimeType(item
->mimetype());
415 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
417 insert
= (*it
== mimeType
) ||
418 ((*it
).right(1) == "*") &&
419 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
426 const QString submenuName
= cfg
.readEntry("X-KDE-Submenu");
427 if (!submenuName
.isEmpty()) {
428 menu
= new KMenu(submenuName
);
429 actionsMenu
->addMenu(menu
);
432 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
433 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
435 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
436 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
437 KDesktopFileActions::Service service
= (*serviceIt
);
438 if (!service
.m_strIcon
.isEmpty()) {
439 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
441 serviceActions
<< action
;
443 QAction
*action
= menu
->addAction(service
.m_strName
);
444 serviceActions
<< action
;
446 actionsVector
.append(service
);
454 const int itemsCount
= actionsMenu
->actions().count();
455 if (itemsCount
== 0) {
456 // no actions are available at all, hence show the "Actions"
458 actionsMenu
->setEnabled(false);
461 if (itemsCount
== 1) {
462 // Exactly one item is available. Instead of showing a sub menu with
463 // only one item, show the item directly in the root menu.
464 if (menu
== actionsMenu
) {
465 // The item is an action, hence show the action in the root menu.
466 const QList
<QAction
*> actions
= actionsMenu
->actions();
467 Q_ASSERT(actions
.count() == 1);
469 const QString text
= actions
[0]->text();
470 const QIcon icon
= actions
[0]->icon();
472 QAction
* action
= popup
->addAction(text
);
473 serviceActions
.clear();
474 serviceActions
<< action
;
476 QAction
* action
= popup
->addAction(icon
, text
);
477 serviceActions
.clear();
478 serviceActions
<< action
;
481 // The item is a sub menu, hence show the sub menu in the root menu.
482 popup
->addMenu(menu
);
484 actionsMenu
->deleteLater();
487 popup
->addMenu(actionsMenu
);
490 return serviceActions
;
493 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
494 const QString
& entryName
) const
498 const QList
<QAction
*> list
= menu
->actions();
499 const uint count
= list
.count();
500 for (uint i
= 0; i
< count
; ++i
) {
501 const QAction
* action
= list
.at(i
);
502 if (action
->text() == entryName
) {
510 #include "dolphincontextmenu.moc"