2
Copyright 2007 Robert Knight <robertknight@gmail.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
21
#include "ui/contextmenufactory.h"
24
#include <QAbstractItemView>
31
#include <KLocalizedString>
33
#include <kbookmarkmanager.h>
36
//#include <konq_popupmenu.h>
37
#include <kactioncollection.h>
38
#include <kfileitem.h>
39
#include <kparts/browserextension.h>
42
#include "core/favoritesmodel.h"
43
#include "core/models.h"
45
using namespace Kickoff;
47
class ContextMenuFactory::Private
50
QAction *advancedActionsMenu(const QString& url) const
53
KActionCollection actionCollection((QObject*)0);
55
QString mimeType = KMimeType::findByUrl(kUrl,0,false,true)->name();
56
items << KFileItem(url,mimeType,KFileItem::Unknown);
57
KParts::BrowserExtension::PopupFlags browserFlags = KParts::BrowserExtension::DefaultPopupItems;
58
if (items.first().isLocalFile()) {
59
browserFlags |= KParts::BrowserExtension::ShowProperties;
61
KParts::BrowserExtension::ActionGroupMap actionGroupMap;
64
KonqPopupMenu *menu = new KonqPopupMenu(items, kUrl,actionCollection,
66
0, KBookmarkManager::userBookmarksManager(), actionGroupMap);
68
if (!menu->isEmpty()) {
69
QAction *action = menu->menuAction();
70
action->setText(i18n("Advanced"));
71
action->setIcon(KIcon("edit-add"));
80
QMap<QAbstractItemView*,QList<QAction*> > viewActions;
83
ContextMenuFactory::ContextMenuFactory(QObject *parent)
89
ContextMenuFactory::~ContextMenuFactory()
94
void ContextMenuFactory::showContextMenu(QAbstractItemView *view,const QPoint& pos)
98
const QModelIndex index = view->indexAt(pos);
99
const QString url = index.data(UrlRole).value<QString>();
105
bool isFavorite = FavoritesModel::isFavorite(url);
107
QList<QAction*> actions;
109
// add to / remove from favorites
110
QAction *favoriteAction = new QAction(this);
112
favoriteAction->setText(i18n("Remove from Favorites"));
113
favoriteAction->setIcon(KIcon("list-remove"));
115
favoriteAction->setText(i18n("Add to Favorites"));
116
favoriteAction->setIcon(KIcon("bookmark-new"));
119
actions << favoriteAction;
122
QAction *addToDesktopAction = new QAction(this);
123
addToDesktopAction->setText(i18n("Add to Desktop"));
124
addToDesktopAction->setEnabled(false);
125
actions << addToDesktopAction;
128
QAction *addToPanelAction = new QAction(this);
129
addToPanelAction->setText(i18n("Add to Panel"));
130
addToPanelAction->setEnabled(false);
131
actions << addToPanelAction;
133
// advanced item actions
134
QAction *advancedSeparator = new QAction(this);
135
advancedSeparator->setSeparator(true);
136
actions << advancedSeparator;
138
QAction *advanced = d->advancedActionsMenu(url);
143
// add view specific actions
144
QAction *viewSeparator = new QAction(this);
145
viewSeparator->setSeparator(true);
146
actions << viewSeparator;
147
actions << viewActions(view);
150
QAction *result = QMenu::exec(actions,view->mapToGlobal(pos));
152
if (result == favoriteAction) {
154
FavoritesModel::remove(url);
156
FavoritesModel::add(url);
160
delete favoriteAction;
161
delete addToDesktopAction;
162
delete addToPanelAction;
163
delete viewSeparator;
165
void ContextMenuFactory::setViewActions(QAbstractItemView *view,const QList<QAction*>& actions)
167
if (actions.isEmpty()) {
168
d->viewActions.remove(view);
170
d->viewActions.insert(view,actions);
173
QList<QAction*> ContextMenuFactory::viewActions(QAbstractItemView *view) const
175
return d->viewActions[view];
178
#include "contextmenufactory.moc"