Applets
contextmenufactory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "ui/contextmenufactory.h"
00023
00024
00025 #include <QAbstractItemView>
00026 #include <QMap>
00027
00028
00029 #include <KIcon>
00030 #include <KLocalizedString>
00031 #include <KMenu>
00032 #include <KActionCollection>
00033 #include <KFileItem>
00034 #include <KParts/BrowserExtension>
00035 #include <KBookmarkManager>
00036 #include <Solid/Device>
00037 #include <Solid/StorageAccess>
00038 #include <Solid/OpticalDrive>
00039 #include <Solid/OpticalDisc>
00040 #include <KUrl>
00041
00042
00043 #include <Plasma/Containment>
00044 #include <Plasma/Corona>
00045
00046
00047 #include "core/favoritesmodel.h"
00048 #include "core/models.h"
00049
00050 using namespace Kickoff;
00051
00052 class ContextMenuFactory::Private
00053 {
00054 public:
00055 Private()
00056 : applet(0) {
00057 }
00058
00059 QAction *advancedActionsMenu(const QString& url) const {
00060 KUrl kUrl(url);
00061 KActionCollection actionCollection((QObject*)0);
00062 KFileItemList items;
00063 QString mimeType = KMimeType::findByUrl(kUrl, 0, false, true)->name();
00064 items << KFileItem(url, mimeType, KFileItem::Unknown);
00065 KParts::BrowserExtension::PopupFlags browserFlags = KParts::BrowserExtension::DefaultPopupItems;
00066 if (items.first().isLocalFile()) {
00067 browserFlags |= KParts::BrowserExtension::ShowProperties;
00068 }
00069 KParts::BrowserExtension::ActionGroupMap actionGroupMap;
00070 return 0;
00071
00072 #if 0
00073 KonqPopupMenu *menu = new KonqPopupMenu(items, kUrl, actionCollection,
00074 0, 0, browserFlags,
00075 0, KBookmarkManager::userBookmarksManager(), actionGroupMap);
00076
00077 if (!menu->isEmpty()) {
00078 QAction *action = menu->menuAction();
00079 action->setText(i18n("Advanced"));
00080 action->setIcon(KIcon("list-add"));
00081 return action;
00082 } else {
00083 delete menu;
00084 return 0;
00085 }
00086 #endif
00087 }
00088
00089 QMap<QAbstractItemView*, QList<QAction*> > viewActions;
00090 Plasma::Applet *applet;
00091 };
00092
00093 ContextMenuFactory::ContextMenuFactory(QObject *parent)
00094 : QObject(parent)
00095 , d(new Private)
00096 {
00097 }
00098
00099 ContextMenuFactory::~ContextMenuFactory()
00100 {
00101 delete d;
00102 }
00103
00104 void ContextMenuFactory::showContextMenu(QAbstractItemView *view, const QPoint &pos)
00105 {
00106 Q_ASSERT(view);
00107
00108 const QModelIndex index = view->indexAt(pos);
00109 const QString url = index.data(UrlRole).value<QString>();
00110
00111 if (url.isEmpty()) {
00112 return;
00113 }
00114
00115 bool isFavorite = FavoritesModel::isFavorite(url);
00116
00117 QList<QAction*> actions;
00118
00119
00120 QAction *favoriteAction = new QAction(this);
00121 if (isFavorite) {
00122 favoriteAction->setText(i18n("Remove From Favorites"));
00123 favoriteAction->setIcon(KIcon("list-remove"));
00124 actions << favoriteAction;
00125
00126 } else if (KUrl(url).protocol() != "leave") {
00127 favoriteAction->setText(i18n("Add to Favorites"));
00128 favoriteAction->setIcon(KIcon("bookmark-new"));
00129 actions << favoriteAction;
00130 }
00131
00132
00133
00134 QAction *addToDesktopAction = new QAction(this);
00135
00136
00137 QAction *addToPanelAction = new QAction(this);
00138
00139
00140
00141
00142
00143 KUrl kurl(url);
00144 if ((d->applet) && (kurl.scheme() != "leave")) {
00145 Plasma::Containment *containment = d->applet->containment();
00146
00147 if (containment && containment->corona()) {
00148 Plasma::Containment *desktop = containment->corona()->containmentForScreen(containment->screen());
00149
00150 if (desktop && desktop->immutability() == Plasma::Mutable) {
00151 addToDesktopAction->setText(i18n("Add to Desktop"));
00152 actions << addToDesktopAction;
00153 }
00154 }
00155
00156 if (containment && containment->immutability() == Plasma::Mutable &&
00157 containment->containmentType() == Plasma::Containment::PanelContainment) {
00158 addToPanelAction->setText(i18n("Add to Panel"));
00159 actions << addToPanelAction;
00160 }
00161 }
00162
00163
00164 QAction *advancedSeparator = new QAction(this);
00165 advancedSeparator->setSeparator(true);
00166 actions << advancedSeparator;
00167
00168 QAction *advanced = d->advancedActionsMenu(url);
00169 if (advanced) {
00170 actions << advanced;
00171 }
00172
00173
00174 QString udi = index.data(DeviceUdiRole).toString();
00175 Solid::Device device(udi);
00176 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00177 QAction *ejectAction = 0;
00178 if (device.isValid() && access) {
00179 ejectAction = new QAction(this);
00180 if (device.is<Solid::OpticalDisc>()) {
00181 ejectAction->setText(i18n("Eject"));
00182 } else {
00183 ejectAction->setText(i18n("Safely Remove"));
00184 }
00185 actions << ejectAction;
00186 }
00187
00188
00189 if (actions.count() < 2) {
00190 return;
00191 }
00192
00193
00194 QAction *viewSeparator = new QAction(this);
00195 viewSeparator->setSeparator(true);
00196 actions << viewSeparator;
00197 actions << viewActions(view);
00198
00199
00200 KMenu menu;
00201 menu.addTitle(index.data(Qt::DisplayRole).value<QString>());
00202 foreach(QAction* action, actions) {
00203 menu.addAction(action);
00204 }
00205
00206 QAction *result = menu.exec(QCursor::pos());
00207
00208 if (favoriteAction && result == favoriteAction) {
00209 if (isFavorite) {
00210 FavoritesModel::remove(url);
00211 } else {
00212 FavoritesModel::add(url);
00213 }
00214 } else if (ejectAction && result == ejectAction) {
00215 if (device.is<Solid::OpticalDisc>()) {
00216 Solid::OpticalDrive *drive = device.parent().as<Solid::OpticalDrive>();
00217 drive->eject();
00218 } else {
00219 access->teardown();
00220 }
00221 } else if (addToDesktopAction && result == addToDesktopAction) {
00222 if (d->applet) {
00223 Plasma::Containment *containment = d->applet->containment();
00224 if (containment) {
00225 Plasma::Corona *corona = containment->corona();
00226 if (corona) {
00227 Plasma::Containment *desktop = corona->containmentForScreen(containment->screen());
00228 if (desktop) {
00229 QVariantList args;
00230 args << url;
00231 desktop->addApplet("icon", args);
00232 }
00233 }
00234 }
00235 }
00236 } else if (addToPanelAction && result == addToPanelAction) {
00237 if (d->applet) {
00238
00239 Plasma::Containment *panel = d->applet->containment();
00240 if (panel) {
00241 QVariantList args;
00242 args << url;
00243
00244
00245 QRectF rect(panel->geometry().width() / 2, 0, 150, panel->boundingRect().height());
00246 panel->addApplet("icon", args, rect);
00247 }
00248 }
00249 }
00250
00251 delete favoriteAction;
00252 delete addToDesktopAction;
00253 delete addToPanelAction;
00254 delete viewSeparator;
00255 delete ejectAction;
00256 }
00257 void ContextMenuFactory::setViewActions(QAbstractItemView *view, const QList<QAction*>& actions)
00258 {
00259 if (actions.isEmpty()) {
00260 d->viewActions.remove(view);
00261 } else {
00262 d->viewActions.insert(view, actions);
00263 }
00264 }
00265 QList<QAction*> ContextMenuFactory::viewActions(QAbstractItemView *view) const
00266 {
00267 return d->viewActions[view];
00268 }
00269
00270 void ContextMenuFactory::setApplet(Plasma::Applet *applet)
00271 {
00272 d->applet = applet;
00273 }
00274
00275 #include "contextmenufactory.moc"