not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / common / appletbrowser.cpp
blobbf905cd4cb816c4b8a115c11c613561d2517806c
1 /*
2 * Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library/Lesser General Public License
6 * version 2, or (at your option) any later version, as published by the
7 * Free Software Foundation
9 * This program 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
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library/Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "appletbrowser.h"
22 #include <QVBoxLayout>
23 #include <QLabel>
25 #include <KAction>
26 #include <KConfig>
27 #include <KConfigGroup>
28 #include <KMenu>
29 #include <KPageWidgetItem>
30 #include <KPushButton>
31 #include <KServiceTypeTrader>
32 #include <KStandardAction>
33 #include <KAboutData>
34 #include <KAboutApplicationDialog>
35 #include <KComponentData>
36 #include <KPluginLoader>
38 #include <Plasma/Applet>
39 #include <Plasma/Corona>
40 #include <Plasma/Containment>
41 #include "kcategorizeditemsview_p.h"
42 #include "plasmaappletitemmodel_p.h"
43 #include "openwidgetassistant_p.h"
45 namespace Plasma
48 class AppletBrowserWidgetPrivate
50 public:
51 AppletBrowserWidgetPrivate(AppletBrowserWidget *w)
52 : q(w),
53 containment(0),
54 appletList(0),
55 config("plasmarc"),
56 configGroup(&config, "Applet Browser"),
57 itemModel(configGroup, w),
58 filterModel(w)
62 void initFilters();
63 void init();
64 void initRunningApplets();
65 void containmentDestroyed();
67 /**
68 * Tracks a new running applet
70 void appletAdded(Plasma::Applet *applet);
72 /**
73 * A running applet is no more
75 void appletRemoved(Plasma::Applet *applet);
77 AppletBrowserWidget *q;
78 QString application;
79 Plasma::Containment *containment;
80 KCategorizedItemsView *appletList;
81 QHash<QString, int> runningApplets; // applet name => count
82 //extra hash so we can look up the names of deleted applets
83 QHash<Plasma::Applet *,QString> appletNames;
85 KConfig config;
86 KConfigGroup configGroup;
88 PlasmaAppletItemModel itemModel;
89 KCategorizedItemsViewModels::DefaultFilterModel filterModel;
92 void AppletBrowserWidgetPrivate::initFilters()
94 filterModel.clear();
96 filterModel.addFilter(i18n("All Widgets"),
97 KCategorizedItemsViewModels::Filter(), KIcon("plasma"));
99 // Recommended emblems and filters
100 QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]caption");
101 QMapIterator<QString, QString> i(configGroup.entryMap());
102 while (i.hasNext()) {
103 i.next();
104 if (!rx.exactMatch(i.key())) {
105 continue;
107 //kDebug() << "These are the key/vals in rc file " << rx.cap(1) << "\n";
109 QString id = rx.cap(1);
110 QString caption = configGroup.readEntry("recommended." + id + ".caption");
111 QString icon = configGroup.readEntry("recommended." + id + ".icon");
112 QString plugins = configGroup.readEntry("recommended." + id + ".plugins");
114 appletList->addEmblem(i18nc("%1 is the entity (person, staff, organization...) which recommends the widget",
115 "Recommended by %1", caption),
116 KIcon(icon),
117 KCategorizedItemsViewModels::Filter("recommended." + id, true));
118 filterModel.addFilter(i18nc("%1 is the entity (person, staff, organization...) which recommends the widget",
119 "Recommended by %1", caption),
120 KCategorizedItemsViewModels::Filter("recommended." + id, true),
121 KIcon(icon));
124 // Filters: Special
125 filterModel.addFilter(i18n("My Favorite Widgets"),
126 KCategorizedItemsViewModels::Filter("favorite", true),
127 KIcon("bookmarks"));
128 filterModel.addFilter(i18n("Widgets I Have Used Before"),
129 KCategorizedItemsViewModels::Filter("used", true),
130 KIcon("view-history"));
131 filterModel.addFilter(i18n("Currently Running Widgets"),
132 KCategorizedItemsViewModels::Filter("running", true),
133 KIcon("view-history"));
135 filterModel.addSeparator(i18n("Categories:"));
137 foreach (const QString &category, Plasma::Applet::listCategories(application)) {
138 filterModel.addFilter(category,
139 KCategorizedItemsViewModels::Filter("category", category));
143 AppletBrowserWidget::AppletBrowserWidget(QWidget * parent, Qt::WindowFlags f)
144 : QWidget(parent, f),
145 d(new AppletBrowserWidgetPrivate(this))
147 d->init();
150 AppletBrowserWidget::~AppletBrowserWidget()
152 delete d;
155 void AppletBrowserWidgetPrivate::init()
157 QVBoxLayout *layout = new QVBoxLayout(q);
159 appletList = new KCategorizedItemsView(q);
160 QObject::connect(appletList, SIGNAL(doubleClicked(const QModelIndex &)), q, SLOT(addApplet()));
161 layout->addWidget(appletList);
163 // Other Emblems
164 appletList->addEmblem(i18n("Widgets I Have Used Before"), KIcon("view-history"),
165 KCategorizedItemsViewModels::Filter("used", true));
167 initFilters();
168 appletList->setFilterModel(&filterModel);
170 // Other models
171 appletList->setItemModel(&itemModel);
172 initRunningApplets();
174 q->setLayout(layout);
177 void AppletBrowserWidgetPrivate::initRunningApplets()
179 //get applets from corona, count them, send results to model
180 if (!containment) {
181 return;
184 //kDebug() << runningApplets.count();
185 Plasma::Corona *c = containment->corona();
187 //we've tried our best to get a corona
188 //we don't want just one containment, we want them all
189 if (!c) {
190 kDebug() << "can't happen";
191 return;
194 appletNames.clear();
195 runningApplets.clear();
196 QList<Containment*> containments = c->containments();
197 foreach (Containment *containment, containments) {
198 QObject::connect(containment, SIGNAL(appletAdded(Plasma::Applet*,QPointF)), q, SLOT(appletAdded(Plasma::Applet*)));
199 QObject::connect(containment, SIGNAL(appletRemoved(Plasma::Applet*)), q, SLOT(appletRemoved(Plasma::Applet*)));
201 foreach (Applet *applet, containment->applets()) {
202 runningApplets[applet->name()]++;
206 //kDebug() << runningApplets;
207 itemModel.setRunningApplets(runningApplets);
210 void AppletBrowserWidget::setApplication(const QString &app)
212 d->application = app;
213 d->initFilters();
214 d->itemModel.setApplication(app);
216 //FIXME: AFAIK this shouldn't be necessary ... but here it is. need to find out what in that
217 // maze of models and views is screwing up
218 d->appletList->setItemModel(&d->itemModel);
220 //kDebug() << d->runningApplets;
221 d->itemModel.setRunningApplets(d->runningApplets);
224 QString AppletBrowserWidget::application()
226 return d->application;
229 void AppletBrowserWidget::setContainment(Plasma::Containment *containment)
231 if (d->containment != containment) {
232 if (d->containment) {
233 d->containment->disconnect(this);
236 d->containment = containment;
238 if (d->containment) {
239 connect(d->containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
242 d->initRunningApplets();
246 Containment *AppletBrowserWidget::containment() const
248 return d->containment;
251 void AppletBrowserWidgetPrivate::containmentDestroyed()
253 containment = 0;
256 void AppletBrowserWidget::addApplet()
258 if (!d->containment) {
259 return;
262 foreach (AbstractItem *item, d->appletList->selectedItems()) {
263 PlasmaAppletItem *selectedItem = (PlasmaAppletItem *) item;
264 //kDebug() << "Adding applet " << selectedItem->name() << "to containment";
265 d->containment->addApplet(selectedItem->pluginName(), selectedItem->arguments());
269 void AppletBrowserWidgetPrivate::appletAdded(Plasma::Applet *applet)
271 QString name = applet->name();
272 //kDebug() << name;
274 runningApplets[name]++;
275 appletNames.insert(applet, name);
276 itemModel.setRunningApplets(name, runningApplets[name]);
279 void AppletBrowserWidgetPrivate::appletRemoved(Plasma::Applet *applet)
281 //kDebug() << (QObject*)applet;
282 Plasma::Applet *a = (Plasma::Applet *)applet; //don't care if it's valid, just need the address
284 QString name = appletNames.take(a);
286 int count = 0;
287 if (runningApplets.contains(name)) {
288 count = runningApplets[name] - 1;
290 if (count < 1) {
291 runningApplets.remove(name);
292 } else {
293 runningApplets[name] = count;
297 itemModel.setRunningApplets(name, count);
300 void AppletBrowserWidget::destroyApplets(const QString &name)
302 if (!d->containment) {
303 return;
306 Plasma::Corona *c = d->containment->corona();
308 //we've tried our best to get a corona
309 //we don't want just one containment, we want them all
310 if (!c) {
311 kDebug() << "can't happen";
312 return;
315 foreach (Containment *containment, c->containments()) {
316 QList<Applet*> applets = containment->applets();
317 foreach (Applet *applet, applets) {
318 if (applet->name() == name) {
319 d->appletNames.remove(applet);
320 applet->disconnect(this);
321 applet->destroy();
326 d->runningApplets.remove(name);
327 d->itemModel.setRunningApplets(name, 0);
330 void AppletBrowserWidget::infoAboutApplet(const QString &name)
332 if (!d->containment) {
333 return;
336 KPluginInfo::List applets = Plasma::Applet::listAppletInfo();
337 foreach (const KPluginInfo &info, applets) {
338 if (info.name() == name) {
339 KAboutData aboutData = KAboutData(info.name().toUtf8(),
340 info.name().toUtf8(),
341 ki18n(info.name().toUtf8()),
342 info.version().toUtf8(), ki18n(info.comment().toUtf8()),
343 info.fullLicense().key(), ki18n(QByteArray()), ki18n(QByteArray()), info.website().toLatin1(),
344 info.email().toLatin1());
346 aboutData.setProgramIconName(info.icon());
348 aboutData.addAuthor(ki18n(info.author().toUtf8()), ki18n(QByteArray()), info.email().toLatin1());
350 //TODO should recycle this dialog if it is called up twice
351 KAboutApplicationDialog *aboutDialog = new KAboutApplicationDialog(&aboutData, this);
352 aboutDialog->show();
353 break;
358 void AppletBrowserWidget::downloadWidgets(const QString &type)
360 //kDebug() << type;
361 PackageStructure *installer = 0;
363 if (!type.isEmpty()) {
364 QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(type);
365 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure",
366 constraint);
368 if (offers.isEmpty()) {
369 kDebug() << "could not find requested PackageStructure plugin" << type;
370 } else {
371 KService::Ptr service = offers.first();
372 QString error;
373 installer = service->createInstance<Plasma::PackageStructure>(topLevelWidget(),
374 QVariantList(), &error);
376 if (installer) {
377 connect(installer, SIGNAL(newWidgetBrowserFinished()),
378 installer, SLOT(deleteLater()));
379 } else {
380 kDebug() << "found, but could not load requested PackageStructure plugin" << type
381 << "; reported error was" << error;
386 if (installer) {
387 installer->createNewWidgetBrowser(this);
388 } else {
389 // we don't need to delete the default Applet::packageStructure as that
390 // belongs to the applet
391 Applet::packageStructure()->createNewWidgetBrowser(this);
395 void AppletBrowserWidget::openWidgetFile()
397 // TODO: if we already have one of these showing and the user clicks to
398 // add it again, show the same window?
399 OpenWidgetAssistant *assistant = new OpenWidgetAssistant(topLevelWidget());
400 assistant->setAttribute(Qt::WA_DeleteOnClose, true);
401 assistant->show();
404 class AppletBrowserPrivate
406 public:
407 void init(AppletBrowser *browser);
408 void populateWidgetsMenu();
410 AppletBrowser *q;
411 AppletBrowserWidget *widget;
412 QMenu *widgetsMenu;
415 void AppletBrowserPrivate::populateWidgetsMenu()
417 if (!widgetsMenu->actions().isEmpty()) {
418 // already populated.
419 return;
422 QSignalMapper *mapper = new QSignalMapper(q);
423 QObject::connect(mapper, SIGNAL(mapped(QString)), widget, SLOT(downloadWidgets(QString)));
425 QAction *action = new QAction(KIcon("applications-internet"),
426 i18n("Download New Plasma Widgets"), q);
427 QObject::connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
428 mapper->setMapping(action, QString());
429 widgetsMenu->addAction(action);
431 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure");
432 foreach (const KService::Ptr service, offers) {
433 //kDebug() << service->property("X-Plasma-ProvidesWidgetBrowser");
434 if (service->property("X-Plasma-ProvidesWidgetBrowser").toBool()) {
435 QAction *action = new QAction(KIcon("applications-internet"),
436 i18nc("%1 is a type of widgets, as defined by "
437 "e.g. some plasma-packagestructure-*.desktop files",
438 "Download New %1", service->name()), q);
439 QObject::connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
440 mapper->setMapping(action, service->property("X-KDE-PluginInfo-Name").toString());
441 widgetsMenu->addAction(action);
445 widgetsMenu->addSeparator();
447 action = new QAction(KIcon("package-x-generic"),
448 i18n("Install Widget From Local File..."), q);
449 QObject::connect(action, SIGNAL(triggered(bool)), widget, SLOT(openWidgetFile()));
450 widgetsMenu->addAction(action);
453 void AppletBrowserPrivate::init(AppletBrowser *browser)
455 q = browser;
456 widget = new AppletBrowserWidget(q);
458 q->setMainWidget(widget);
459 q->setWindowTitle(i18n("Widgets"));
461 q->setButtons(KDialog::Apply | KDialog::Close | KDialog::User1);
462 q->setButtonText(KDialog::Apply, i18n("Add Widget"));
463 q->setButtonText(KDialog::User1, i18n("Install New Widgets"));
465 widgetsMenu = new KMenu(i18n("Get New Widgets"), q);
466 QObject::connect(widgetsMenu, SIGNAL(aboutToShow()), q, SLOT(populateWidgetsMenu()));
467 q->button(KDialog::User1)->setMenu(widgetsMenu);
469 q->setButtonToolTip(KDialog::Close, i18n("Close the dialog"));
470 q->setButtonWhatsThis(KDialog::Close, i18n("<qt>When clicking <b>Close</b>, this dialog will be closed with no further action taken.</qt>"));
471 q->setButtonToolTip(KDialog::Apply, i18n("Add selected widgets"));
472 q->setButtonWhatsThis(KDialog::Apply, i18n("<qt>When clicking <b>Add Widget</b>, the selected widgets will be added to your desktop.</qt>"));
473 q->setButtonToolTip(KDialog::User1, i18n("Install new widgets"));
474 q->setButtonWhatsThis(KDialog::User1, i18n("<qt>Selecting <b>Get New Widgets</b> will show a window that allows you to download new widgets directly from the Internet, while Install From File allows you to add new widgets from files you have on disk.</qt>"));
476 QObject::connect(q, SIGNAL(applyClicked()), widget, SLOT(addApplet()));
478 q->setInitialSize(QSize(400, 600));
479 KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
480 q->restoreDialogSize(cg);
483 AppletBrowser::AppletBrowser(QWidget * parent, Qt::WindowFlags f)
484 : KDialog(parent, f),
485 d(new AppletBrowserPrivate)
487 d->init(this);
490 AppletBrowser::~AppletBrowser()
492 KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
493 saveDialogSize(cg);
496 void AppletBrowser::setApplication(const QString &app)
498 d->widget->setApplication(app);
501 QString AppletBrowser::application()
503 return d->widget->application();
506 void AppletBrowser::setContainment(Plasma::Containment *containment)
508 d->widget->setContainment(containment);
511 Containment *AppletBrowser::containment() const
513 return d->widget->containment();
516 } // namespace Plasma
518 #include "appletbrowser.moc"