]>
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"), i18n("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(i18n("Do you really want to empty the Trash? All items will get deleted."));
114 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
117 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
118 ) == KMessageBox::Continue
;
120 KonqOperations::emptyTrash(m_mainWindow
);
124 popup
->deleteLater();
127 void DolphinContextMenu::openTrashItemContextMenu()
129 Q_ASSERT(m_context
& TrashContext
);
130 Q_ASSERT(m_context
& ItemContext
);
132 KMenu
* popup
= new KMenu(m_mainWindow
);
134 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
135 popup
->addAction(restoreAction
);
137 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
138 popup
->addAction(deleteAction
);
140 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
141 popup
->addAction(propertiesAction
);
143 if (popup
->exec(QCursor::pos()) == restoreAction
) {
144 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
147 popup
->deleteLater();
150 void DolphinContextMenu::openItemContextMenu()
152 Q_ASSERT(m_fileInfo
!= 0);
154 KMenu
* popup
= new KMenu(m_mainWindow
);
155 insertDefaultItemActions(popup
);
157 popup
->addSeparator();
159 // insert 'Bookmark This Folder' entry if exactly one item is selected
160 QAction
* bookmarkAction
= 0;
161 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
162 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
165 // Insert 'Open With...' sub menu
166 QVector
<KService::Ptr
> openWithVector
;
167 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
169 // Insert 'Actions' sub menu
170 QVector
<KDesktopFileActions::Service
> actionsVector
;
171 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
172 popup
->addSeparator();
174 // insert 'Properties...' entry
175 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
176 popup
->addAction(propertiesAction
);
178 QAction
* activatedAction
= popup
->exec(QCursor::pos());
180 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
181 const KUrl
selectedUrl(m_fileInfo
->url());
182 if (selectedUrl
.isValid()) {
183 DolphinSettings::instance().placesModel()->addPlace(selectedUrl
.fileName(),
186 } else if (serviceActions
.contains(activatedAction
)) {
187 // one of the 'Actions' items has been selected
188 int id
= serviceActions
.indexOf(activatedAction
);
189 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
190 } else if (openWithActions
.contains(activatedAction
)) {
191 // one of the 'Open With' items has been selected
192 if (openWithActions
.last() == activatedAction
) {
193 // the item 'Other...' has been selected
194 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
196 int id
= openWithActions
.indexOf(activatedAction
);
197 KService::Ptr servicePtr
= openWithVector
[id
];
198 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
202 openWithVector
.clear();
203 actionsVector
.clear();
204 popup
->deleteLater();
207 void DolphinContextMenu::openViewportContextMenu()
209 Q_ASSERT(m_fileInfo
== 0);
210 KMenu
* popup
= new KMenu(m_mainWindow
);
212 // setup 'Create New' menu
213 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
214 newMenu
->slotCheckUpToDate();
215 newMenu
->setPopupFiles(m_baseUrl
);
216 popup
->addMenu(newMenu
->menu());
217 popup
->addSeparator();
219 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
220 popup
->addAction(pasteAction
);
222 // setup 'View Mode' menu
223 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
225 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
226 viewModeMenu
->addAction(iconsMode
);
228 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
229 viewModeMenu
->addAction(detailsMode
);
231 QAction
* columnsMode
= m_mainWindow
->actionCollection()->action("columns");
232 viewModeMenu
->addAction(columnsMode
);
234 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
235 viewModeMenu
->addAction(previewsMode
);
237 popup
->addMenu(viewModeMenu
);
239 QAction
* toggleViewsAction
= 0;
240 if (m_mainWindow
->isSplit()) {
241 toggleViewsAction
= popup
->addAction(i18n("Toggle Views"));
244 popup
->addSeparator();
246 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
247 popup
->addSeparator();
249 QAction
* propertiesAction
= popup
->addAction(i18n("Properties"));
251 QAction
* action
= popup
->exec(QCursor::pos());
252 if (action
== propertiesAction
) {
253 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
254 KPropertiesDialog
dialog(url
);
256 } else if (action
== bookmarkAction
) {
257 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
259 DolphinSettings::instance().placesModel()->addPlace(url
.fileName(), url
);
261 } else if ((toggleViewsAction
!= 0) && (action
== toggleViewsAction
)) {
262 m_mainWindow
->toggleViews();
265 popup
->deleteLater();
268 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
270 Q_ASSERT(popup
!= 0);
271 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
273 // insert 'Cut', 'Copy' and 'Paste'
274 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
275 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
276 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
278 popup
->addAction(cutAction
);
279 popup
->addAction(copyAction
);
280 popup
->addAction(pasteAction
);
281 popup
->addSeparator();
284 QAction
* renameAction
= collection
->action("rename");
285 popup
->addAction(renameAction
);
287 // insert 'Move to Trash' and (optionally) 'Delete'
288 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
289 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
290 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
291 const KUrl
& url
= m_mainWindow
->activeViewContainer()->url();
292 if (url
.isLocalFile()) {
293 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
294 popup
->addAction(moveToTrashAction
);
296 showDeleteCommand
= true;
299 if (showDeleteCommand
) {
300 QAction
* deleteAction
= collection
->action("delete");
301 popup
->addAction(deleteAction
);
305 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
306 QVector
<KService::Ptr
>& openWithVector
)
308 // Parts of the following code have been taken
309 // from the class KonqOperations located in
310 // libqonq/konq_operations.h of Konqueror.
311 // (Copyright (C) 2000 David Faure <faure@kde.org>)
313 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
314 // are listed which are registered to open the item. As last entry "Other..." will be
315 // attached which allows to select a custom application. If no applications are registered
316 // no sub menu is created at all, only "Open With..." will be offered.
317 bool insertOpenWithItems
= true;
318 const QString
contextMimeType(m_fileInfo
->mimetype());
320 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
321 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
322 KFileItem
* item
= mimeIt
.next();
323 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
326 QList
<QAction
*> openWithActions
;
327 if (insertOpenWithItems
) {
328 // fill the 'Open with' sub menu with application types
329 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
330 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
332 "Type == 'Application'");
333 if (offers
.count() > 0) {
334 KService::List::Iterator it
;
335 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
336 for (it
= offers
.begin(); it
!= offers
.end(); ++it
) {
337 // The offer list from the KTrader returns duplicate
338 // application entries. Although this seems to be a configuration
339 // problem outside the scope of Dolphin, duplicated entries just
340 // will be skipped here.
341 const QString
appName((*it
)->name());
342 if (!containsEntry(openWithMenu
, appName
)) {
343 const KIcon
icon((*it
)->icon());
344 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
345 openWithVector
.append(*it
);
346 openWithActions
<< action
;
350 openWithMenu
->addSeparator();
351 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
353 openWithActions
<< action
;
354 popup
->addMenu(openWithMenu
);
356 // No applications are registered, hence just offer
357 // a "Open With..." item instead of a sub menu containing
359 QAction
* action
= popup
->addAction(i18n("Open With..."));
360 openWithActions
<< action
;
363 // At least one of the selected items has a different MIME type. In this case
364 // just show a disabled "Open With..." entry.
365 QAction
* action
= popup
->addAction(i18n("Open With..."));
366 action
->setEnabled(false);
369 return openWithActions
;
372 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
373 QVector
<KDesktopFileActions::Service
>& actionsVector
)
375 // Parts of the following code have been taken
376 // from the class KonqOperations located in
377 // libqonq/konq_operations.h of Konqueror.
378 // (Copyright (C) 2000 David Faure <faure@kde.org>)
380 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
382 QList
<QAction
*> serviceActions
;
384 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
387 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
390 filters
<< "*.desktop";
391 dir
.setNameFilters(filters
);
392 QStringList entries
= dir
.entryList(QDir::Files
);
394 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
395 KConfigGroup
cfg(KSharedConfig::openConfig(*dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry");
396 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
397 //const QStringList types = cfg.readListEntry("ServiceTypes");
399 types
= cfg
.readEntry("ServiceTypes", types
);
400 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
401 // check whether the mime type is equal or whether the
402 // mimegroup (e. g. image/*) is supported
405 if ((*it
) == "all/allfiles") {
406 // The service type is valid for all files, but not for directories.
407 // Check whether the selected items only consist of files...
408 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
410 while (insert
&& mimeIt
.hasNext()) {
411 KFileItem
* item
= mimeIt
.next();
412 insert
= !item
->isDir();
417 // Check whether the MIME types of all selected files match
418 // to the mimetype of the service action. As soon as one MIME
419 // type does not match, no service menu is shown at all.
420 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
422 while (insert
&& mimeIt
.hasNext()) {
423 KFileItem
* item
= mimeIt
.next();
424 const QString
mimeType(item
->mimetype());
425 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
427 insert
= (*it
== mimeType
) ||
428 ((*it
).right(1) == "*") &&
429 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
436 const QString submenuName
= cfg
.readEntry("X-KDE-Submenu");
437 if (!submenuName
.isEmpty()) {
438 menu
= new KMenu(submenuName
);
439 actionsMenu
->addMenu(menu
);
442 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
443 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
445 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
446 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
447 KDesktopFileActions::Service service
= (*serviceIt
);
448 if (!service
.m_strIcon
.isEmpty()) {
449 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
451 serviceActions
<< action
;
453 QAction
*action
= menu
->addAction(service
.m_strName
);
454 serviceActions
<< action
;
456 actionsVector
.append(service
);
464 const int itemsCount
= actionsMenu
->actions().count();
465 if (itemsCount
== 0) {
466 // no actions are available at all, hence show the "Actions"
468 actionsMenu
->setEnabled(false);
471 if (itemsCount
== 1) {
472 // Exactly one item is available. Instead of showing a sub menu with
473 // only one item, show the item directly in the root menu.
474 if (menu
== actionsMenu
) {
475 // The item is an action, hence show the action in the root menu.
476 const QList
<QAction
*> actions
= actionsMenu
->actions();
477 Q_ASSERT(actions
.count() == 1);
479 const QString text
= actions
[0]->text();
480 const QIcon icon
= actions
[0]->icon();
482 QAction
* action
= popup
->addAction(text
);
483 serviceActions
.clear();
484 serviceActions
<< action
;
486 QAction
* action
= popup
->addAction(icon
, text
);
487 serviceActions
.clear();
488 serviceActions
<< action
;
491 // The item is a sub menu, hence show the sub menu in the root menu.
492 popup
->addMenu(menu
);
494 actionsMenu
->deleteLater();
497 popup
->addMenu(actionsMenu
);
500 return serviceActions
;
503 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
504 const QString
& entryName
) const
508 const QList
<QAction
*> list
= menu
->actions();
509 const uint count
= list
.count();
510 for (uint i
= 0; i
< count
; ++i
) {
511 const QAction
* action
= list
.at(i
);
512 if (action
->text() == entryName
) {
520 #include "dolphincontextmenu.moc"