]>
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 <konqmimedata.h>
40 #include <konq_operations.h>
42 #include <kpropertiesdialog.h>
44 #include <kstandardaction.h>
45 #include <kstandarddirs.h>
47 #include <QApplication>
50 #include <Q3ValueList>
52 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
54 const KUrl
& baseUrl
) :
60 // The context menu either accesses the URLs of the selected items
61 // or the items itself. To increase the performance both lists are cached.
62 DolphinView
* view
= m_mainWindow
->activeView();
63 m_selectedUrls
= view
->selectedUrls();
64 m_selectedItems
= view
->selectedItems();
67 DolphinContextMenu::~DolphinContextMenu()
71 void DolphinContextMenu::open()
73 // get the context information
74 if (m_baseUrl
.protocol() == "trash") {
75 m_context
|= TrashContext
;
78 if (m_fileInfo
!= 0) {
79 m_context
|= ItemContext
;
80 // TODO: handle other use cases like devices + desktop files
83 // open the corresponding popup for the context
84 if (m_context
& TrashContext
) {
85 if (m_context
& ItemContext
) {
86 openTrashItemContextMenu();
89 openTrashContextMenu();
92 else if (m_context
& ItemContext
) {
93 openItemContextMenu();
96 Q_ASSERT(m_context
== NoContext
);
97 openViewportContextMenu();
102 void DolphinContextMenu::openTrashContextMenu()
104 Q_ASSERT(m_context
& TrashContext
);
106 KMenu
* popup
= new KMenu(m_mainWindow
);
108 QAction
* emptyTrashAction
= new QAction(KIcon("emptytrash"), i18n("Emtpy Trash"), popup
);
109 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
110 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
111 popup
->addAction(emptyTrashAction
);
113 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
114 popup
->addAction(propertiesAction
);
116 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
117 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
118 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
121 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
122 ) == KMessageBox::Continue
;
124 KonqOperations::emptyTrash(m_mainWindow
);
128 popup
->deleteLater();
131 void DolphinContextMenu::openTrashItemContextMenu()
133 Q_ASSERT(m_context
& TrashContext
);
134 Q_ASSERT(m_context
& ItemContext
);
136 KMenu
* popup
= new KMenu(m_mainWindow
);
138 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
139 popup
->addAction(restoreAction
);
141 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
142 popup
->addAction(deleteAction
);
144 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
145 popup
->addAction(propertiesAction
);
147 if (popup
->exec(QCursor::pos()) == restoreAction
) {
148 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
151 popup
->deleteLater();
154 void DolphinContextMenu::openItemContextMenu()
156 Q_ASSERT(m_fileInfo
!= 0);
158 KMenu
* popup
= new KMenu(m_mainWindow
);
159 insertDefaultItemActions(popup
);
161 popup
->addSeparator();
163 // insert 'Bookmark This Folder' entry if exactly one item is selected
164 QAction
* bookmarkAction
= 0;
165 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
166 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder..."));
169 // Insert 'Open With...' sub menu
170 QVector
<KService::Ptr
> openWithVector
;
171 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
173 // Insert 'Actions' sub menu
174 QVector
<KDesktopFileActions::Service
> actionsVector
;
175 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
176 popup
->addSeparator();
178 // insert 'Properties...' entry
179 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
180 popup
->addAction(propertiesAction
);
182 QAction
* activatedAction
= popup
->exec(QCursor::pos());
184 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
185 const KUrl
selectedUrl(m_fileInfo
->url());
186 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Folder as Bookmark"),
187 selectedUrl
.fileName(),
190 if (!bookmark
.isNull()) {
191 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
192 KBookmarkGroup root
= manager
->root();
193 root
.addBookmark(manager
, bookmark
);
194 manager
->emitChanged(root
);
197 else if (serviceActions
.contains(activatedAction
)) {
198 // one of the 'Actions' items has been selected
199 int id
= serviceActions
.indexOf(activatedAction
);
200 KDesktopFileActions::executeService(m_selectedUrls
, actionsVector
[id
]);
202 else if (openWithActions
.contains(activatedAction
)) {
203 // one of the 'Open With' items has been selected
204 if (openWithActions
.last() == activatedAction
) {
205 // the item 'Other...' has been selected
206 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
209 int id
= openWithActions
.indexOf(activatedAction
);
210 KService::Ptr servicePtr
= openWithVector
[id
];
211 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
215 openWithVector
.clear();
216 actionsVector
.clear();
217 popup
->deleteLater();
220 void DolphinContextMenu::openViewportContextMenu()
222 Q_ASSERT(m_fileInfo
== 0);
223 KMenu
* popup
= new KMenu(m_mainWindow
);
225 // setup 'Create New' menu
226 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
227 newMenu
->slotCheckUpToDate();
228 newMenu
->setPopupFiles(m_baseUrl
);
229 popup
->addMenu(newMenu
->menu());
230 popup
->addSeparator();
232 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
233 popup
->addAction(pasteAction
);
235 // setup 'View Mode' menu
236 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
238 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
239 viewModeMenu
->addAction(iconsMode
);
241 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
242 viewModeMenu
->addAction(detailsMode
);
244 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
245 viewModeMenu
->addAction(previewsMode
);
247 popup
->addMenu(viewModeMenu
);
248 popup
->addSeparator();
250 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder..."));
251 popup
->addSeparator();
253 QAction
* propertiesAction
= popup
->addAction(i18n("Properties"));
255 QAction
* activatedAction
= popup
->exec(QCursor::pos());
256 if (activatedAction
== propertiesAction
) {
257 new KPropertiesDialog(m_mainWindow
->activeView()->url());
259 else if (activatedAction
== bookmarkAction
) {
260 const KUrl
& url
= m_mainWindow
->activeView()->url();
261 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add Folder as Bookmark"),
265 if (!bookmark
.isNull()) {
266 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
267 KBookmarkGroup root
= manager
->root();
268 root
.addBookmark(manager
, bookmark
);
269 manager
->emitChanged(root
);
273 popup
->deleteLater();
276 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
278 Q_ASSERT(popup
!= 0);
279 const KActionCollection
* collection
= m_mainWindow
->actionCollection();
281 // insert 'Cut', 'Copy' and 'Paste'
282 QAction
* cutAction
= collection
->action(KStandardAction::stdName(KStandardAction::Cut
));
283 QAction
* copyAction
= collection
->action(KStandardAction::stdName(KStandardAction::Copy
));
284 QAction
* pasteAction
= collection
->action(KStandardAction::stdName(KStandardAction::Paste
));
286 popup
->addAction(cutAction
);
287 popup
->addAction(copyAction
);
288 popup
->addAction(pasteAction
);
289 popup
->addSeparator();
292 QAction
* renameAction
= collection
->action("rename");
293 popup
->addAction(renameAction
);
295 // insert 'Move to Trash' and (optionally) 'Delete'
296 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
297 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
298 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
299 const KUrl
& url
= m_mainWindow
->activeView()->url();
300 if (url
.isLocalFile()) {
301 QAction
* moveToTrashAction
= collection
->action("move_to_trash");
302 popup
->addAction(moveToTrashAction
);
305 showDeleteCommand
= true;
308 if (showDeleteCommand
) {
309 QAction
* deleteAction
= collection
->action("delete");
310 popup
->addAction(deleteAction
);
314 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
315 QVector
<KService::Ptr
>& openWithVector
)
317 // Parts of the following code have been taken
318 // from the class KonqOperations located in
319 // libqonq/konq_operations.h of Konqueror.
320 // (Copyright (C) 2000 David Faure <faure@kde.org>)
322 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
323 // are listed which are registered to open the item. As last entry "Other..." will be
324 // attached which allows to select a custom application. If no applications are registered
325 // no sub menu is created at all, only "Open With..." will be offered.
326 bool insertOpenWithItems
= true;
327 const QString
contextMimeType(m_fileInfo
->mimetype());
329 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
330 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
331 KFileItem
* item
= mimeIt
.next();
332 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
335 QList
<QAction
*> openWithActions
;
336 if (insertOpenWithItems
) {
337 // fill the 'Open with' sub menu with application types
338 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
339 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
341 "Type == 'Application'");
342 if (offers
.count() > 0) {
343 KService::List::Iterator it
;
344 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
345 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
346 // The offer list from the KTrader returns duplicate
347 // application entries. Although this seems to be a configuration
348 // problem outside the scope of Dolphin, duplicated entries just
349 // will be skipped here.
350 const QString
appName((*it
)->name());
351 if (!containsEntry(openWithMenu
, appName
)) {
352 const KIcon
icon((*it
)->icon());
353 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
354 openWithVector
.append(*it
);
355 openWithActions
<< action
;
359 openWithMenu
->addSeparator();
360 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
362 openWithActions
<< action
;
363 popup
->addMenu(openWithMenu
);
366 // No applications are registered, hence just offer
367 // a "Open With..." item instead of a sub menu containing
369 QAction
* action
= popup
->addAction(i18n("Open With..."));
370 openWithActions
<< action
;
374 // At least one of the selected items has a different MIME type. In this case
375 // just show a disabled "Open With..." entry.
376 QAction
* action
= popup
->addAction(i18n("Open With..."));
377 action
->setEnabled(false);
380 return openWithActions
;
383 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
384 QVector
<KDesktopFileActions::Service
>& actionsVector
)
386 // Parts of the following code have been taken
387 // from the class KonqOperations located in
388 // libqonq/konq_operations.h of Konqueror.
389 // (Copyright (C) 2000 David Faure <faure@kde.org>)
391 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
393 QList
<QAction
*> serviceActions
;
395 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
398 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
401 filters
<< "*.desktop";
402 dir
.setNameFilters(filters
);
403 QStringList entries
= dir
.entryList(QDir::Files
);
405 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
406 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
407 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
408 //const QStringList types = cfg.readListEntry("ServiceTypes");
410 types
= cfg
.readEntry("ServiceTypes", types
);
411 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
412 // check whether the mime type is equal or whether the
413 // mimegroup (e. g. image/*) is supported
416 if ((*it
) == "all/allfiles") {
417 // The service type is valid for all files, but not for directories.
418 // Check whether the selected items only consist of files...
419 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
421 while (insert
&& mimeIt
.hasNext()) {
422 KFileItem
* item
= mimeIt
.next();
423 insert
= !item
->isDir();
428 // Check whether the MIME types of all selected files match
429 // to the mimetype of the service action. As soon as one MIME
430 // type does not match, no service menu is shown at all.
431 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
433 while (insert
&& mimeIt
.hasNext()) {
434 KFileItem
* item
= mimeIt
.next();
435 const QString
mimeType(item
->mimetype());
436 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
438 insert
= (*it
== mimeType
) ||
439 ((*it
).right(1) == "*") &&
440 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
447 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
448 if (!submenuName
.isEmpty()) {
449 menu
= new KMenu(submenuName
);
450 actionsMenu
->addMenu(menu
);
453 Q3ValueList
<KDesktopFileActions::Service
> userServices
=
454 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
456 Q3ValueList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
457 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
458 KDesktopFileActions::Service service
= (*serviceIt
);
459 if (!service
.m_strIcon
.isEmpty()) {
460 QAction
* action
= menu
->addAction(KIcon(service
.m_strIcon
),
462 serviceActions
<< action
;
465 QAction
*action
= menu
->addAction(service
.m_strName
);
466 serviceActions
<< action
;
468 actionsVector
.append(service
);
476 const int itemsCount
= actionsMenu
->actions().count();
477 if (itemsCount
== 0) {
478 // no actions are available at all, hence show the "Actions"
480 actionsMenu
->setEnabled(false);
483 if (itemsCount
== 1) {
484 // Exactly one item is available. Instead of showing a sub menu with
485 // only one item, show the item directly in the root menu.
486 if (menu
== actionsMenu
) {
487 // The item is an action, hence show the action in the root menu.
488 const QList
<QAction
*> actions
= actionsMenu
->actions();
489 Q_ASSERT(actions
.count() == 1);
491 const QString text
= actions
[0]->text();
492 const QIcon icon
= actions
[0]->icon();
494 QAction
* action
= popup
->addAction(text
);
495 serviceActions
.clear();
496 serviceActions
<< action
;
499 QAction
* action
= popup
->addAction(icon
, text
);
500 serviceActions
.clear();
501 serviceActions
<< action
;
505 // The item is a sub menu, hence show the sub menu in the root menu.
506 popup
->addMenu(menu
);
508 actionsMenu
->deleteLater();
512 popup
->addMenu(actionsMenu
);
515 return serviceActions
;
518 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
519 const QString
& entryName
) const
523 const QList
<QAction
*> list
= menu
->actions();
524 const uint count
= list
.count();
525 for (uint i
= 0; i
< count
; ++i
) {
526 const QAction
* action
= list
.at(i
);
527 if (action
->text() == entryName
) {
535 #include "dolphincontextmenu.moc"