• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

appletbrowser.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library/Lesser General Public License
00006  *   version 2, or (at your option) any later version, as published by the
00007  *   Free Software Foundation
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library/Lesser General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "plasma/appletbrowser.h"
00021 
00022 #include <QVBoxLayout>
00023 #include <QLabel>
00024 
00025 #include <KAction>
00026 #include <KConfig>
00027 #include <KConfigGroup>
00028 #include <KMenu>
00029 #include <KPageWidgetItem>
00030 #include <KPushButton>
00031 #include <KServiceTypeTrader>
00032 #include <KStandardAction>
00033 
00034 #include <knewstuff2/engine.h>
00035 
00036 #include "plasma/applet.h"
00037 #include "plasma/corona.h"
00038 #include "plasma/containment.h"
00039 #include "plasma/appletbrowser/kcategorizeditemsview_p.h"
00040 #include "plasma/appletbrowser/plasmaappletitemmodel_p.h"
00041 #include "plasma/appletbrowser/openwidgetassistant_p.h"
00042 
00043 namespace Plasma
00044 {
00045 
00046 class AppletBrowserWidgetPrivate
00047 {
00048 public:
00049     AppletBrowserWidgetPrivate(AppletBrowserWidget* w)
00050         : q(w),
00051           containment(0),
00052           appletList(0),
00053           config("plasmarc"),
00054           configGroup(&config, "Applet Browser"),
00055           itemModel(configGroup, w),
00056           filterModel(w)
00057     {
00058     }
00059 
00060     void initFilters();
00061     void init();
00062     void initRunningApplets();
00063 
00067     void appletAdded(Plasma::Applet* applet);
00068 
00072     void appletRemoved(Plasma::Applet* applet);
00073 
00074     AppletBrowserWidget *q;
00075     QString application;
00076     Plasma::Containment *containment;
00077     KCategorizedItemsView *appletList;
00078     QHash<QString, int> runningApplets; // applet name => count
00079     //extra hash so we can look up the names of deleted applets
00080     QHash<Plasma::Applet*, QString> appletNames;
00081 
00082     KConfig config;
00083     KConfigGroup configGroup;
00084 
00085     PlasmaAppletItemModel itemModel;
00086     KCategorizedItemsViewModels::DefaultFilterModel filterModel;
00087 };
00088 
00089 void AppletBrowserWidgetPrivate::initFilters()
00090 {
00091     filterModel.clear();
00092 
00093     filterModel.addFilter(i18n("All Widgets"),
00094                           KCategorizedItemsViewModels::Filter(), new KIcon("plasma"));
00095 
00096     // Recommended emblems and filters
00097     QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]caption");
00098     QMapIterator<QString, QString> i(configGroup.entryMap());
00099     while (i.hasNext()) {
00100         i.next();
00101         if (!rx.exactMatch(i.key())) {
00102             continue;
00103         }
00104         //kDebug() << "These are the key/vals in rc file " << rx.cap(1) << "\n";
00105 
00106         QString id = rx.cap(1);
00107         QString caption = configGroup.readEntry("recommended." + id + ".caption");
00108         QString icon    = configGroup.readEntry("recommended." + id + ".icon");
00109         QString plugins = configGroup.readEntry("recommended." + id + ".plugins");
00110 
00111         appletList->addEmblem(i18n("Recommended by %1", caption), new KIcon(icon),
00112                               KCategorizedItemsViewModels::Filter("recommended." + id, true));
00113         filterModel.addFilter(i18n("Recommended by %1", caption),
00114                               KCategorizedItemsViewModels::Filter("recommended." + id, true), new KIcon(icon));
00115     }
00116 
00117     // Filters: Special
00118     filterModel.addFilter(i18n("My Favorite Widgets"),
00119                           KCategorizedItemsViewModels::Filter("favorite", true),
00120                           new KIcon("bookmarks"));
00121     filterModel.addFilter(i18n("Widgets I Have Used Before"),
00122                           KCategorizedItemsViewModels::Filter("used", true),
00123                           new KIcon("view-history"));
00124     filterModel.addFilter(i18n("Currently Running Widgets"),
00125                           KCategorizedItemsViewModels::Filter("running", true),
00126                           new KIcon("view-history"));
00127 
00128     filterModel.addSeparator(i18n("Categories:"));
00129 
00130     foreach (const QString& category, Plasma::Applet::listCategories(application)) {
00131         filterModel.addFilter(category,
00132                               KCategorizedItemsViewModels::Filter("category", category));
00133     }
00134 }
00135 
00136 AppletBrowserWidget::AppletBrowserWidget(QWidget * parent, Qt::WindowFlags f)
00137     : QWidget(parent, f),
00138     d(new AppletBrowserWidgetPrivate(this))
00139 {
00140     d->init();
00141 }
00142 
00143 AppletBrowserWidget::~AppletBrowserWidget()
00144 {
00145     delete d;
00146 }
00147 
00148 void AppletBrowserWidgetPrivate::init()
00149 {
00150     QVBoxLayout *layout = new QVBoxLayout(q);
00151 
00152     appletList = new KCategorizedItemsView(q);
00153     QObject::connect(appletList, SIGNAL(doubleClicked(const QModelIndex &)), q, SLOT(addApplet()));
00154     layout->addWidget(appletList);
00155 
00156     // Other Emblems
00157     appletList->addEmblem(i18n("Widgets I Have Used Before"), new KIcon("view-history"),
00158                           KCategorizedItemsViewModels::Filter("used", true));
00159 
00160     initFilters();
00161     appletList->setFilterModel(&filterModel);
00162 
00163     // Other models
00164     appletList->setItemModel(&itemModel);
00165     initRunningApplets();
00166 
00167     q->setLayout(layout);
00168 }
00169 
00170 void AppletBrowserWidgetPrivate::initRunningApplets()
00171 {
00172 //get applets from corona, count them, send results to model
00173     if (!containment) {
00174         return;
00175     }
00176 
00177     //kDebug() << runningApplets.count();
00178     Plasma::Corona *c = containment->corona();
00179 
00180     //we've tried our best to get a corona
00181     //we don't want just one containment, we want them all
00182     if (!c) {
00183         kDebug() << "can't happen";
00184         return;
00185     }
00186 
00187     appletNames.clear();
00188     runningApplets.clear();
00189     QList<Containment*> containments = c->containments();
00190     foreach (Containment *containment, containments) {
00191         QObject::connect(containment, SIGNAL(appletAdded(Plasma::Applet*,QPointF)), q, SLOT(appletAdded(Plasma::Applet*)));
00192         QObject::connect(containment, SIGNAL(appletRemoved(Plasma::Applet*)), q, SLOT(appletRemoved(Plasma::Applet*)));
00193 
00194         foreach (Applet *applet, containment->applets()) {
00195             runningApplets[applet->name()]++;
00196         }
00197     }
00198 
00199     //kDebug() << runningApplets;
00200     itemModel.setRunningApplets(runningApplets);
00201 }
00202 
00203 void AppletBrowserWidget::setApplication(const QString& app)
00204 {
00205     d->application = app;
00206     d->initFilters();
00207     d->itemModel.setApplication(app);
00208 
00209     //FIXME: AFAIK this shouldn't be necessary ... but here it is. need to find out what in that
00210     //       maze of models and views is screwing up
00211     d->appletList->setItemModel(&d->itemModel);
00212 
00213     //kDebug() << d->runningApplets;
00214     d->itemModel.setRunningApplets(d->runningApplets);
00215 }
00216 
00217 QString AppletBrowserWidget::application()
00218 {
00219     return d->application;
00220 }
00221 
00222 void AppletBrowserWidget::setContainment(Plasma::Containment *containment)
00223 {
00224     if (d->containment != containment) {
00225         d->containment = containment;
00226         d->initRunningApplets();
00227     }
00228 }
00229 
00230 Containment *AppletBrowserWidget::containment() const
00231 {
00232     return d->containment;
00233 }
00234 
00235 void AppletBrowserWidget::addApplet()
00236 {
00237     if (!d->containment) {
00238         return;
00239     }
00240 
00241     foreach (AbstractItem *item, d->appletList->selectedItems()) {
00242         PlasmaAppletItem *selectedItem = (PlasmaAppletItem *) item;
00243         //kDebug() << "Adding applet " << selectedItem->name() << "to containment";
00244         d->containment->addApplet(selectedItem->pluginName(), selectedItem->arguments());
00245     }
00246 }
00247 
00248 void AppletBrowserWidgetPrivate::appletAdded(Plasma::Applet* applet)
00249 {
00250     QString name = applet->name();
00251     //kDebug() << name;
00252 
00253     runningApplets[name]++;
00254     appletNames.insert(applet, name);
00255     itemModel.setRunningApplets(name, runningApplets[name]);
00256 }
00257 
00258 void AppletBrowserWidgetPrivate::appletRemoved(Plasma::Applet* applet)
00259 {
00260     //kDebug() << (QObject*)applet;
00261     Plasma::Applet* a = (Plasma::Applet*)applet; //don't care if it's valid, just need the address
00262 
00263     QString name = appletNames.take(a);
00264 
00265     int count = 0;
00266     if (runningApplets.contains(name)) {
00267         count = runningApplets[name] - 1;
00268 
00269         if (count < 1) {
00270             runningApplets.remove(name);
00271         } else {
00272             runningApplets[name] = count;
00273         }
00274     }
00275 
00276     itemModel.setRunningApplets(name, count);
00277 }
00278 
00279 void AppletBrowserWidget::destroyApplets(const QString &name)
00280 {
00281     if (!d->containment) {
00282         return;
00283     }
00284 
00285     Plasma::Corona *c = d->containment->corona();
00286 
00287     //we've tried our best to get a corona
00288     //we don't want just one containment, we want them all
00289     if (!c) {
00290         kDebug() << "can't happen";
00291         return;
00292     }
00293 
00294     foreach (Containment *containment, c->containments()) {
00295         QList<Applet*> applets = containment->applets();
00296         foreach (Applet *applet, applets) {
00297             if (applet->name() == name) {
00298                 d->appletNames.remove(applet);
00299                 applet->disconnect(this);
00300                 applet->destroy();
00301             }
00302         }
00303     }
00304 
00305     d->runningApplets.remove(name);
00306     d->itemModel.setRunningApplets(name, 0);
00307 }
00308 
00309 void AppletBrowserWidget::downloadWidgets()
00310 {
00311     KNS::Engine engine(0);
00312     if (engine.init("plasmoids.knsrc")) {
00313         KNS::Entry::List entries = engine.downloadDialogModal(this);
00314     }
00315 }
00316 
00317 void AppletBrowserWidget::openWidgetFile()
00318 {
00319     // TODO: if we already have one of these showing and the user clicks to add it again, show the same window?
00320     OpenWidgetAssistant *assistant = new OpenWidgetAssistant(topLevelWidget());
00321     assistant->setAttribute(Qt::WA_DeleteOnClose, true);
00322     assistant->show();
00323 }
00324 
00325 class AppletBrowserPrivate
00326 {
00327 public:
00328     void init(AppletBrowser*);
00329     AppletBrowserWidget *widget;
00330 };
00331 
00332 AppletBrowser::AppletBrowser(QWidget * parent, Qt::WindowFlags f)
00333     : KDialog(parent, f),
00334       d(new AppletBrowserPrivate)
00335 {
00336     d->init(this);
00337 }
00338 
00339 void AppletBrowserPrivate::init(AppletBrowser *q)
00340 {
00341     widget = new AppletBrowserWidget(q);
00342 
00343     q->setMainWidget(widget);
00344     q->setWindowTitle(i18n("Widgets"));
00345 
00346     q->setButtons(KDialog::Apply | KDialog::Close | KDialog::User1);
00347     q->setButtonText(KDialog::Apply, i18n("Add Widget"));
00348     q->setButtonText(KDialog::User1, i18n("Install New Widgets"));
00349 
00350     KMenu *widgetsMenu = new KMenu(i18n("Get New Widgets"), q);
00351     QAction *action = new QAction(KIcon("applications-internet"),
00352                                   i18n("Download From Internet"), q);
00353     QObject::connect(action, SIGNAL(triggered(bool)), widget, SLOT(downloadWidgets()));
00354     widgetsMenu->addAction(action);
00355 
00356     action = new QAction(KIcon("applications-internet"),
00357                          i18n("Install From File..."), q);
00358     QObject::connect(action, SIGNAL(triggered(bool)), widget, SLOT(openWidgetFile()));
00359     widgetsMenu->addAction(action);
00360     q->button(KDialog::User1)->setMenu(widgetsMenu);
00361 
00362     q->setButtonToolTip(KDialog::Close, i18n("Close the dialog"));
00363     q->setButtonWhatsThis(KDialog::Close, i18n("<qt>When clicking <b>Close</b>, this dialog will be closed with no further action taken.</qt>"));
00364     q->setButtonToolTip(KDialog::Apply, i18n("Add selected widgets"));
00365     q->setButtonWhatsThis(KDialog::Apply, i18n("<qt>When clicking <b>Add Widget</b>, the selected widgets will be added to your desktop.</qt>"));
00366     q->setButtonToolTip(KDialog::User1, i18n("Install new widgets"));
00367     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>"));
00368 
00369     QObject::connect(q, SIGNAL(applyClicked()), widget, SLOT(addApplet()));
00370 
00371     q->setInitialSize(QSize(400, 600));
00372     KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
00373     q->restoreDialogSize(cg);
00374 }
00375 
00376 AppletBrowser::~AppletBrowser()
00377 {
00378     KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
00379     saveDialogSize(cg);
00380 }
00381 
00382 void AppletBrowser::setApplication(const QString& app)
00383 {
00384     d->widget->setApplication( app );
00385 }
00386 
00387 QString AppletBrowser::application()
00388 {
00389     return d->widget->application();
00390 }
00391 
00392 void AppletBrowser::setContainment(Plasma::Containment *containment)
00393 {
00394     d->widget->setContainment(containment);
00395 }
00396 
00397 Containment* AppletBrowser::containment() const
00398 {
00399     return d->widget->containment();
00400 }
00401 
00402 } // namespace Plasma
00403 
00404 #include "appletbrowser.moc"

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal