2 Copyright 2007 Robert Knight <robertknight@gmail.com>
3 Copyright 2007 Kevin Ottens <ervin@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "ui/contextmenufactory.h"
25 #include <QAbstractItemView>
30 #include <KLocalizedString>
32 #include <KActionCollection>
34 #include <KParts/BrowserExtension>
35 #include <KBookmarkManager>
36 #include <Solid/Device>
37 #include <Solid/StorageAccess>
38 #include <Solid/OpticalDrive>
39 #include <Solid/OpticalDisc>
43 #include <Plasma/Containment>
44 #include <Plasma/Corona>
47 #include "core/favoritesmodel.h"
48 #include "core/models.h"
50 using namespace Kickoff
;
52 class ContextMenuFactory::Private
59 QAction
*advancedActionsMenu(const QString
& url
) const {
61 KActionCollection
actionCollection((QObject
*)0);
63 QString mimeType
= KMimeType::findByUrl(kUrl
, 0, false, true)->name();
64 items
<< KFileItem(url
, mimeType
, KFileItem::Unknown
);
65 KParts::BrowserExtension::PopupFlags browserFlags
= KParts::BrowserExtension::DefaultPopupItems
;
66 if (items
.first().isLocalFile()) {
67 browserFlags
|= KParts::BrowserExtension::ShowProperties
;
69 KParts::BrowserExtension::ActionGroupMap actionGroupMap
;
71 // ### TODO: remove kdebase-apps dependency
73 KonqPopupMenu
*menu
= new KonqPopupMenu(items
, kUrl
, actionCollection
,
75 0, KBookmarkManager::userBookmarksManager(), actionGroupMap
);
77 if (!menu
->isEmpty()) {
78 QAction
*action
= menu
->menuAction();
79 action
->setText(i18n("Advanced"));
80 action
->setIcon(KIcon("list-add"));
89 QMap
<QAbstractItemView
*, QList
<QAction
*> > viewActions
;
90 Plasma::Applet
*applet
;
93 ContextMenuFactory::ContextMenuFactory(QObject
*parent
)
99 ContextMenuFactory::~ContextMenuFactory()
104 void ContextMenuFactory::showContextMenu(QAbstractItemView
*view
, const QPoint
&pos
)
108 const QModelIndex index
= view
->indexAt(pos
);
109 const QString url
= index
.data(UrlRole
).value
<QString
>();
115 bool isFavorite
= FavoritesModel::isFavorite(url
);
117 QList
<QAction
*> actions
;
119 // add to / remove from favorites
120 QAction
*favoriteAction
= new QAction(this);
122 favoriteAction
->setText(i18n("Remove From Favorites"));
123 favoriteAction
->setIcon(KIcon("list-remove"));
124 actions
<< favoriteAction
;
125 //exclude stuff in the leave tab
126 } else if (KUrl(url
).protocol() != "leave") {
127 favoriteAction
->setText(i18n("Add to Favorites"));
128 favoriteAction
->setIcon(KIcon("bookmark-new"));
129 actions
<< favoriteAction
;
134 QAction
*addToDesktopAction
= new QAction(this);
137 QAction
*addToPanelAction
= new QAction(this);
139 //### FIXME : icons in leave-view are not properly based on a .desktop file
140 //so you cant put them in desktop or panel
141 //### TODO : do not forget to remove (kurl.scheme() != "leave") and kurl declaration
142 //when proper action for such case will be provided
144 if ((d
->applet
) && (kurl
.scheme() != "leave")) {
145 Plasma::Containment
*containment
= d
->applet
->containment();
147 if (containment
&& containment
->corona()) {
148 Plasma::Containment
*desktop
= containment
->corona()->containmentForScreen(containment
->screen());
150 if (desktop
&& desktop
->immutability() == Plasma::Mutable
) {
151 addToDesktopAction
->setText(i18n("Add to Desktop"));
152 actions
<< addToDesktopAction
;
156 if (containment
&& containment
->immutability() == Plasma::Mutable
&&
157 containment
->containmentType() == Plasma::Containment::PanelContainment
) {
158 addToPanelAction
->setText(i18n("Add to Panel"));
159 actions
<< addToPanelAction
;
163 // advanced item actions
164 QAction
*advancedSeparator
= new QAction(this);
165 advancedSeparator
->setSeparator(true);
166 actions
<< advancedSeparator
;
168 QAction
*advanced
= d
->advancedActionsMenu(url
);
174 QString udi
= index
.data(DeviceUdiRole
).toString();
175 Solid::Device
device(udi
);
176 Solid::StorageAccess
*access
= device
.as
<Solid::StorageAccess
>();
177 QAction
*ejectAction
= 0;
178 if (device
.isValid() && access
) {
179 ejectAction
= new QAction(this);
180 if (device
.is
<Solid::OpticalDisc
>()) {
181 ejectAction
->setText(i18n("Eject"));
183 ejectAction
->setText(i18n("Safely Remove"));
185 actions
<< ejectAction
;
188 //return if we added just a separator so far
189 if (actions
.count() < 2) {
193 // add view specific actions
194 QAction
*viewSeparator
= new QAction(this);
195 viewSeparator
->setSeparator(true);
196 actions
<< viewSeparator
;
197 actions
<< viewActions(view
);
201 menu
.addTitle(index
.data(Qt::DisplayRole
).value
<QString
>());
202 foreach(QAction
* action
, actions
) {
203 menu
.addAction(action
);
206 QAction
*result
= menu
.exec(QCursor::pos());
208 if (favoriteAction
&& result
== favoriteAction
) {
210 FavoritesModel::remove(url
);
212 FavoritesModel::add(url
);
214 } else if (ejectAction
&& result
== ejectAction
) {
215 if (device
.is
<Solid::OpticalDisc
>()) {
216 Solid::OpticalDrive
*drive
= device
.parent().as
<Solid::OpticalDrive
>();
221 } else if (addToDesktopAction
&& result
== addToDesktopAction
) {
223 Plasma::Containment
*containment
= d
->applet
->containment();
225 Plasma::Corona
*corona
= containment
->corona();
227 Plasma::Containment
*desktop
= corona
->containmentForScreen(containment
->screen());
231 desktop
->addApplet("icon", args
);
236 } else if (addToPanelAction
&& result
== addToPanelAction
) {
238 // we assume that the panel is the same containment where the kickoff is located
239 Plasma::Containment
*panel
= d
->applet
->containment();
244 // move it to the middle of the panel
245 QRectF
rect(panel
->geometry().width() / 2, 0, 150, panel
->boundingRect().height());
246 panel
->addApplet("icon", args
, rect
);
251 delete favoriteAction
;
252 delete addToDesktopAction
;
253 delete addToPanelAction
;
254 delete viewSeparator
;
257 void ContextMenuFactory::setViewActions(QAbstractItemView
*view
, const QList
<QAction
*>& actions
)
259 if (actions
.isEmpty()) {
260 d
->viewActions
.remove(view
);
262 d
->viewActions
.insert(view
, actions
);
265 QList
<QAction
*> ContextMenuFactory::viewActions(QAbstractItemView
*view
) const
267 return d
->viewActions
[view
];
270 void ContextMenuFactory::setApplet(Plasma::Applet
*applet
)
275 #include "contextmenufactory.moc"