]>
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 <konq_operations.h>
41 #include <kpropertiesdialog.h>
43 #include <kstandardaction.h>
44 #include <kstandarddirs.h>
48 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow
* parent
,
58 if (viewType
== ItemsView
) {
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
->activeView();
62 m_selectedUrls
= view
->selectedUrls();
63 m_selectedItems
= view
->selectedItems();
65 else if (fileInfo
!= 0) {
66 m_selectedUrls
.append(fileInfo
->url());
67 m_selectedItems
.append(fileInfo
);
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();
101 DolphinContextMenu::~DolphinContextMenu()
105 void DolphinContextMenu::openTrashContextMenu()
107 Q_ASSERT(m_context
& TrashContext
);
109 KMenu
* popup
= new KMenu(m_mainWindow
);
111 QAction
* emptyTrashAction
= new QAction(KIcon("user-trash"), i18n("Emtpy Trash"), popup
);
112 KConfig
trashConfig("trashrc", KConfig::OnlyLocal
);
113 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
114 popup
->addAction(emptyTrashAction
);
116 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
117 popup
->addAction(propertiesAction
);
119 if (popup
->exec(QCursor::pos()) == emptyTrashAction
) {
120 const QString
text(i18n("Do you really want to empty the Trash? All items will get deleted."));
121 const bool del
= KMessageBox::warningContinueCancel(m_mainWindow
,
124 KGuiItem(i18n("Empty Trash"), KIcon("user-trash"))
125 ) == KMessageBox::Continue
;
127 KonqOperations::emptyTrash(m_mainWindow
);
131 popup
->deleteLater();
134 void DolphinContextMenu::openTrashItemContextMenu()
136 Q_ASSERT(m_context
& TrashContext
);
137 Q_ASSERT(m_context
& ItemContext
);
139 KMenu
* popup
= new KMenu(m_mainWindow
);
141 QAction
* restoreAction
= new QAction(i18n("Restore"), m_mainWindow
);
142 popup
->addAction(restoreAction
);
144 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
145 popup
->addAction(deleteAction
);
147 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
148 popup
->addAction(propertiesAction
);
150 if (popup
->exec(QCursor::pos()) == restoreAction
) {
151 KonqOperations::restoreTrashedItems(m_selectedUrls
, m_mainWindow
);
154 popup
->deleteLater();
157 void DolphinContextMenu::openItemContextMenu()
159 Q_ASSERT(m_fileInfo
!= 0);
161 KMenu
* popup
= new KMenu(m_mainWindow
);
162 if (m_viewType
== ItemsView
) {
163 insertDefaultItemActions(popup
);
166 popup
->addSeparator();
168 // insert 'Bookmark this folder' entry if exactly one item is selected
169 QAction
* bookmarkAction
= 0;
170 if (m_fileInfo
->isDir() && (m_selectedUrls
.count() == 1)) {
171 bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark folder"));
174 // Insert 'Open With...' sub menu
175 QVector
<KService::Ptr
> openWithVector
;
176 const QList
<QAction
*> openWithActions
= insertOpenWithItems(popup
, openWithVector
);
178 // Insert 'Actions' sub menu
179 QVector
<KDEDesktopMimeType::Service
> actionsVector
;
180 const QList
<QAction
*> serviceActions
= insertActionItems(popup
, actionsVector
);
181 popup
->addSeparator();
183 if (m_viewType
== ItemsView
) {
184 // insert 'Properties...' entry
185 QAction
* propertiesAction
= m_mainWindow
->actionCollection()->action("properties");
186 popup
->addAction(propertiesAction
);
189 QAction
* activatedAction
= popup
->exec(QCursor::pos());
191 if ((bookmarkAction
!= 0) && (activatedAction
== bookmarkAction
)) {
192 const KUrl
selectedUrl(m_fileInfo
->url());
193 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
194 selectedUrl
.fileName(),
197 if (!bookmark
.isNull()) {
198 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
199 KBookmarkGroup root
= manager
->root();
200 root
.addBookmark(manager
, bookmark
);
201 manager
->emitChanged(root
);
204 else if (serviceActions
.contains(activatedAction
)) {
205 // one of the 'Actions' items has been selected
206 int id
= serviceActions
.indexOf(activatedAction
);
207 KDEDesktopMimeType::executeService(m_selectedUrls
, actionsVector
[id
]);
209 else if (openWithActions
.contains(activatedAction
)) {
210 // one of the 'Open With' items has been selected
211 if (openWithActions
.last() == activatedAction
) {
212 // the item 'Other...' has been selected
213 KRun::displayOpenWithDialog(m_selectedUrls
, m_mainWindow
);
216 int id
= openWithActions
.indexOf(activatedAction
);
217 KService::Ptr servicePtr
= openWithVector
[id
];
218 KRun::run(*servicePtr
, m_selectedUrls
, m_mainWindow
);
222 openWithVector
.clear();
223 actionsVector
.clear();
224 popup
->deleteLater();
227 void DolphinContextMenu::openViewportContextMenu()
229 Q_ASSERT(m_fileInfo
== 0);
230 KMenu
* popup
= new KMenu(m_mainWindow
);
232 // setup 'Create New' menu
233 KNewMenu
* newMenu
= m_mainWindow
->newMenu();
234 newMenu
->slotCheckUpToDate();
235 newMenu
->setPopupFiles(m_baseUrl
);
236 popup
->addMenu(newMenu
->menu());
237 popup
->addSeparator();
239 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
240 popup
->addAction(pasteAction
);
242 // setup 'View Mode' menu
243 KMenu
* viewModeMenu
= new KMenu(i18n("View Mode"));
245 QAction
* iconsMode
= m_mainWindow
->actionCollection()->action("icons");
246 viewModeMenu
->addAction(iconsMode
);
248 QAction
* detailsMode
= m_mainWindow
->actionCollection()->action("details");
249 viewModeMenu
->addAction(detailsMode
);
251 QAction
* previewsMode
= m_mainWindow
->actionCollection()->action("previews");
252 viewModeMenu
->addAction(previewsMode
);
254 popup
->addMenu(viewModeMenu
);
255 popup
->addSeparator();
257 QAction
* bookmarkAction
= popup
->addAction(KIcon("bookmark-folder"), i18n("Bookmark this folder"));
258 popup
->addSeparator();
260 QAction
* propertiesAction
= popup
->addAction(i18n("Properties..."));
262 QAction
* activatedAction
= popup
->exec(QCursor::pos());
263 if (activatedAction
== propertiesAction
) {
264 new KPropertiesDialog(m_mainWindow
->activeView()->url());
266 else if (activatedAction
== bookmarkAction
) {
267 const KUrl
& url
= m_mainWindow
->activeView()->url();
268 KBookmark bookmark
= EditBookmarkDialog::getBookmark(i18n("Add folder as bookmark"),
272 if (!bookmark
.isNull()) {
273 KBookmarkManager
* manager
= DolphinSettings::instance().bookmarkManager();
274 KBookmarkGroup root
= manager
->root();
275 root
.addBookmark(manager
, bookmark
);
276 manager
->emitChanged(root
);
280 popup
->deleteLater();
283 void DolphinContextMenu::insertDefaultItemActions(KMenu
* popup
)
285 Q_ASSERT(popup
!= 0);
287 // insert 'Cut', 'Copy' and 'Paste'
288 QAction
* cutAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Cut
));
289 QAction
* copyAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Copy
));
290 QAction
* pasteAction
= m_mainWindow
->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste
));
291 popup
->addAction(cutAction
);
292 popup
->addAction(copyAction
);
293 popup
->addAction(pasteAction
);
294 popup
->addSeparator();
297 QAction
* renameAction
= m_mainWindow
->actionCollection()->action("rename");
298 popup
->addAction(renameAction
);
300 // insert 'Move to Trash' and (optionally) 'Delete'
301 const KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
302 const KConfigGroup
kdeConfig(globalConfig
, "KDE");
303 bool showDeleteCommand
= kdeConfig
.readEntry("ShowDeleteCommand", false);
304 const KUrl
& url
= m_mainWindow
->activeView()->url();
305 if (url
.isLocalFile()) {
306 QAction
* moveToTrashAction
= m_mainWindow
->actionCollection()->action("move_to_trash");
307 popup
->addAction(moveToTrashAction
);
310 showDeleteCommand
= true;
313 if (showDeleteCommand
) {
314 QAction
* deleteAction
= m_mainWindow
->actionCollection()->action("delete");
315 popup
->addAction(deleteAction
);
319 QList
<QAction
*> DolphinContextMenu::insertOpenWithItems(KMenu
* popup
,
320 QVector
<KService::Ptr
>& openWithVector
)
322 // Parts of the following code have been taken
323 // from the class KonqOperations located in
324 // libqonq/konq_operations.h of Konqueror.
325 // (Copyright (C) 2000 David Faure <faure@kde.org>)
327 // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications
328 // are listed which are registered to open the item. As last entry "Other..." will be
329 // attached which allows to select a custom application. If no applications are registered
330 // no sub menu is created at all, only "Open With..." will be offered.
331 bool insertOpenWithItems
= true;
332 const QString
contextMimeType(m_fileInfo
->mimetype());
334 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
335 while (insertOpenWithItems
&& mimeIt
.hasNext()) {
336 KFileItem
* item
= mimeIt
.next();
337 insertOpenWithItems
= (contextMimeType
== item
->mimetype());
340 QList
<QAction
*> openWithActions
;
341 if (insertOpenWithItems
) {
342 // fill the 'Open with' sub menu with application types
343 const KMimeType::Ptr mimePtr
= KMimeType::findByUrl(m_fileInfo
->url());
344 KService::List offers
= KMimeTypeTrader::self()->query(mimePtr
->name(),
346 "Type == 'Application'");
347 if (offers
.count() > 0) {
348 KService::List::Iterator it
;
349 KMenu
* openWithMenu
= new KMenu(i18n("Open With"));
350 for(it
= offers
.begin(); it
!= offers
.end(); ++it
) {
351 // The offer list from the KTrader returns duplicate
352 // application entries. Although this seems to be a configuration
353 // problem outside the scope of Dolphin, duplicated entries just
354 // will be skipped here.
355 const QString
appName((*it
)->name());
356 if (!containsEntry(openWithMenu
, appName
)) {
357 const KIcon
icon((*it
)->icon());
358 QAction
* action
= openWithMenu
->addAction(icon
, appName
);
359 openWithVector
.append(*it
);
360 openWithActions
<< action
;
364 openWithMenu
->addSeparator();
365 QAction
* action
= openWithMenu
->addAction(i18n("&Other..."));
367 openWithActions
<< action
;
368 popup
->addMenu(openWithMenu
);
371 // No applications are registered, hence just offer
372 // a "Open With..." item instead of a sub menu containing
374 QAction
* action
= popup
->addAction(i18n("Open With..."));
375 openWithActions
<< action
;
379 // At least one of the selected items has a different MIME type. In this case
380 // just show a disabled "Open With..." entry.
381 QAction
* action
= popup
->addAction(i18n("Open With..."));
382 action
->setEnabled(false);
385 return openWithActions
;
388 QList
<QAction
*> DolphinContextMenu::insertActionItems(KMenu
* popup
,
389 QVector
<KDEDesktopMimeType::Service
>& actionsVector
)
391 // Parts of the following code have been taken
392 // from the class KonqOperations located in
393 // libqonq/konq_operations.h of Konqueror.
394 // (Copyright (C) 2000 David Faure <faure@kde.org>)
396 KMenu
* actionsMenu
= new KMenu(i18n("Actions"));
398 QList
<QAction
*> serviceActions
;
400 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
403 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
406 filters
<< "*.desktop";
407 dir
.setNameFilters(filters
);
408 QStringList entries
= dir
.entryList(QDir::Files
);
410 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
411 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
412 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
413 //const QStringList types = cfg.readListEntry("ServiceTypes");
415 types
= cfg
.readEntry("ServiceTypes", types
);
416 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
417 // check whether the mime type is equal or whether the
418 // mimegroup (e. g. image/*) is supported
421 if ((*it
) == "all/allfiles") {
422 // The service type is valid for all files, but not for directories.
423 // Check whether the selected items only consist of files...
424 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
426 while (insert
&& mimeIt
.hasNext()) {
427 KFileItem
* item
= mimeIt
.next();
428 insert
= !item
->isDir();
433 // Check whether the MIME types of all selected files match
434 // to the mimetype of the service action. As soon as one MIME
435 // type does not match, no service menu is shown at all.
436 QListIterator
<KFileItem
*> mimeIt(m_selectedItems
);
438 while (insert
&& mimeIt
.hasNext()) {
439 KFileItem
* item
= mimeIt
.next();
440 const QString
mimeType(item
->mimetype());
441 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
443 insert
= (*it
== mimeType
) ||
444 ((*it
).right(1) == "*") &&
445 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
452 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
453 if (!submenuName
.isEmpty()) {
454 menu
= new KMenu(submenuName
);
455 actionsMenu
->addMenu(menu
);
458 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
459 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
461 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
462 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
463 KDEDesktopMimeType::Service service
= (*serviceIt
);
464 if (!service
.m_strIcon
.isEmpty()) {
465 QAction
* action
= menu
->addAction(SmallIcon(service
.m_strIcon
),
467 serviceActions
<< action
;
470 QAction
*action
= menu
->addAction(service
.m_strName
);
471 serviceActions
<< action
;
473 actionsVector
.append(service
);
481 const int itemsCount
= actionsMenu
->actions().count();
482 if (itemsCount
== 0) {
483 // no actions are available at all, hence show the "Actions"
485 actionsMenu
->setEnabled(false);
488 if (itemsCount
== 1) {
489 // Exactly one item is available. Instead of showing a sub menu with
490 // only one item, show the item directly in the root menu.
491 if (menu
== actionsMenu
) {
492 // The item is an action, hence show the action in the root menu.
493 const QList
<QAction
*> actions
= actionsMenu
->actions();
494 Q_ASSERT(actions
.count() == 1);
496 const QString text
= actions
[0]->text();
497 const QIcon icon
= actions
[0]->icon();
499 QAction
* action
= popup
->addAction(text
);
500 serviceActions
.clear();
501 serviceActions
<< action
;
504 QAction
* action
= popup
->addAction(icon
, text
);
505 serviceActions
.clear();
506 serviceActions
<< action
;
510 // The item is a sub menu, hence show the sub menu in the root menu.
511 popup
->addMenu(menu
);
513 actionsMenu
->deleteLater();
517 popup
->addMenu(actionsMenu
);
520 return serviceActions
;
523 bool DolphinContextMenu::containsEntry(const KMenu
* menu
,
524 const QString
& entryName
) const
528 const QList
<QAction
*> list
= menu
->actions();
529 const uint count
= list
.count();
530 for (uint i
= 0; i
< count
; ++i
) {
531 const QAction
* action
= list
.at(i
);
532 if (action
->text() == entryName
) {