• Skip to content
  • Skip to link menu
KDE 4.0 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 <KAction>
00023 #include <KStandardAction>
00024 #include <KConfig>
00025 #include <KConfigGroup>
00026 
00027 
00028 #include "plasma/corona.h"
00029 #include "plasma/containment.h"
00030 #include "plasma/applet.h"
00031 #include "plasma/appletbrowser/plasmaappletitemmodel_p.h"
00032 #include "plasma/appletbrowser/kcategorizeditemsview_p.h"
00033 
00034 namespace Plasma
00035 {
00036 
00037 class AppletBrowserWidget::Private
00038 {
00039 public:
00040     Private(Corona* co, Containment* cont, AppletBrowserWidget* w)
00041         : containment(cont),
00042           appletList(0),
00043           config("plasmarc"),
00044           configGroup(&config, "Applet Browser"),
00045           itemModel(configGroup, w),
00046           filterModel(w)
00047     {
00048     }
00049 
00050     void initFilters();
00051     //update the itemModel on our running applets
00052     void updateRunningApplets();
00053 
00054     QString application;
00055     Plasma::Containment *containment;
00056     KCategorizedItemsView *appletList;
00057     QMultiHash<QString,Plasma::Applet*> runningApplets;
00058     //extra hash so we can look up the names of deleted applets
00059     QHash<Plasma::Applet*,QString> appletNames;
00060 
00061     KConfig config;
00062     KConfigGroup configGroup;
00063 
00064     PlasmaAppletItemModel itemModel;
00065     KCategorizedItemsViewModels::DefaultFilterModel filterModel;
00066 };
00067 
00068 void AppletBrowserWidget::Private::initFilters()
00069 {
00070     filterModel.clear();
00071 
00072     filterModel.addFilter(i18n("All Widgets"),
00073                           KCategorizedItemsViewModels::Filter(), new KIcon("plasma"));
00074 
00075     // Recommended emblems and filters
00076     QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]caption");
00077     QMapIterator<QString, QString> i(configGroup.entryMap());
00078     while (i.hasNext()) {
00079         i.next();
00080         if (!rx.exactMatch(i.key())) {
00081             continue;
00082         }
00083         //kDebug() << "These are the key/vals in rc file " << rx.cap(1) << "\n";
00084 
00085         QString id = rx.cap(1);
00086         QString caption = configGroup.readEntry("recommended." + id + ".caption");
00087         QString icon    = configGroup.readEntry("recommended." + id + ".icon");
00088         QString plugins = configGroup.readEntry("recommended." + id + ".plugins");
00089 
00090         appletList->addEmblem(i18n("Recommended by %1", caption), new KIcon(icon),
00091                               KCategorizedItemsViewModels::Filter("recommended." + id, true));
00092         filterModel.addFilter(i18n("Recommended by %1", caption),
00093                               KCategorizedItemsViewModels::Filter("recommended." + id, true), new KIcon(icon));
00094     }
00095 
00096     // Filters: Special
00097     filterModel.addFilter(i18n("My Favorite Widgets"),
00098                           KCategorizedItemsViewModels::Filter("favorite", true),
00099                           new KIcon("bookmarks"));
00100     filterModel.addFilter(i18n("Widgets I Have Used Before"),
00101                           KCategorizedItemsViewModels::Filter("used", true),
00102                           new KIcon("view-history"));
00103     filterModel.addFilter(i18n("Currently Running Widgets"),
00104                           KCategorizedItemsViewModels::Filter("running", true),
00105                           new KIcon("view-history"));
00106 
00107     filterModel.addSeparator(i18n("Categories:"));
00108 
00109     foreach (const QString& category, Plasma::Applet::knownCategories(application)) {
00110         filterModel.addFilter(category,
00111                               KCategorizedItemsViewModels::Filter("category", category));
00112     }
00113 }
00114 
00115 void AppletBrowserWidget::Private::updateRunningApplets()
00116 {
00117     QHash<QString,int> appCount;
00118     foreach (QString key, runningApplets.uniqueKeys()) {
00119         appCount[key]=runningApplets.count(key);
00120     }
00121     kDebug() << appCount;
00122     itemModel.setRunningApplets(appCount);
00123 }
00124 
00125 /*
00126 AppletBrowserWidget::AppletBrowserWidget(Plasma::Corona * corona, bool showButtons, QWidget * parent, Qt::WindowFlags f)
00127     : QWidget(parent, f),
00128     d(new Private(corona, 0, this)),
00129     m_showButtons( showButtons )
00130 {
00131     init();
00132 }
00133 */
00134 AppletBrowserWidget::AppletBrowserWidget(Plasma::Containment * containment, bool showButtons, QWidget * parent, Qt::WindowFlags f)
00135     : QWidget(parent, f),
00136     d(new Private(0, containment, this)),
00137     m_showButtons( showButtons )
00138 {
00139     init();
00140 }
00141 
00142 AppletBrowserWidget::~AppletBrowserWidget()
00143 {
00144     delete d;
00145 }
00146 
00147 void AppletBrowserWidget::init()
00148 {
00149     QVBoxLayout *layout = new QVBoxLayout(this);
00150 
00151     d->appletList = new KCategorizedItemsView(this);
00152     connect(d->appletList, SIGNAL(activated(const QModelIndex &)), this, SLOT(addApplet()));
00153     layout->addWidget( d->appletList );
00154 
00155     if( m_showButtons ) {
00156         QHBoxLayout *buttonLayout = new QHBoxLayout();
00157         buttonLayout->setSpacing( KDialog::spacingHint() );
00158         buttonLayout->setMargin( KDialog::marginHint() );
00159 
00160         QPushButton *addButton = new QPushButton(i18n("Add Widget"), this );
00161         connect(addButton, SIGNAL(clicked()), this, SLOT(addApplet()));
00162         buttonLayout->addWidget( addButton );
00163 
00164 /*        QPushButton *newButton = new QPushButton(i18n("Get New Widgets"), this ); //TODO: not overly happy with this text
00165         newButton->setEnabled( false ); //TODO: enable when GHNS integration is implemented
00166         connect(newButton, SIGNAL(clicked()), this, SLOT(downloadApplets()));
00167         buttonLayout->addWidget( newButton );*/
00168 
00169         layout->addItem( buttonLayout );
00170     }
00171 
00172     // Other Emblems
00173     d->appletList->addEmblem(i18n("Widgets I Have Used Before"), new KIcon("view-history"),
00174                                 KCategorizedItemsViewModels::Filter("used", true));
00175 
00176     d->initFilters();
00177     d->appletList->setFilterModel(&d->filterModel);
00178 
00179     // Other models
00180     d->appletList->setItemModel(&d->itemModel);
00181     initRunningApplets();
00182 }
00183 
00184 void AppletBrowserWidget::initRunningApplets()
00185 {
00186 //get applets from corona, count them, send results to model
00187     kDebug() << d->runningApplets.count();
00188     QHash<QString,int> appCount;
00189     Plasma::Corona *c = d->containment->corona();
00190 
00191     //we've tried our best to get a corona
00192     //we don't want just one containment, we want them all
00193     if (!c) {
00194         kDebug() << "can't happen";
00195         return;
00196     }
00197 
00198     QList<Containment*> containments = c->containments();
00199     foreach (Containment * containment,containments) {
00200         connect(containment, SIGNAL(appletAdded(Plasma::Applet*)), this, SLOT(appletAdded(Plasma::Applet*)));
00201         //TODO track containments too?
00202         QList<Applet*>applets=containment->applets();
00203         foreach (Applet *applet,applets) {
00204             d->runningApplets.insert(applet->name(), applet);
00205             d->appletNames.insert(applet, applet->name());
00206             connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletDestroyed(QObject*)));
00207             appCount[applet->name()]++;
00208         }
00209     }
00210     kDebug() << appCount;
00211     d->itemModel.setRunningApplets(appCount);
00212 }
00213 
00214 void AppletBrowserWidget::setApplication(const QString& app)
00215 {
00216 
00217     d->application = app;
00218     d->initFilters();
00219     d->itemModel.setApplication(app);
00220 
00221     //FIXME: AFAIK this shouldn't be necessary ... but here it is. need to find out what in that
00222     //       maze of models and views is screwing up
00223     d->appletList->setItemModel(&d->itemModel);
00224 
00225     d->updateRunningApplets();
00226 }
00227 
00228 QString AppletBrowserWidget::application()
00229 {
00230     return d->application;
00231 }
00232 
00233 void AppletBrowserWidget::setContainment(Plasma::Containment *containment)
00234 {
00235     d->containment = containment;
00236 }
00237 
00238 Containment *AppletBrowserWidget::containment() const
00239 {
00240     return d->containment;
00241 }
00242 
00243 void AppletBrowserWidget::addApplet()
00244 {
00245     kDebug() << "Button ADD clicked";
00246     if (!d->containment) {
00247         return;
00248     }
00249 
00250     foreach (AbstractItem *item, d->appletList->selectedItems()) {
00251         PlasmaAppletItem *selectedItem = (PlasmaAppletItem *) item;
00252         kDebug() << "Adding applet " << selectedItem->name() << "to containment";
00253         d->containment->addApplet(selectedItem->pluginName(), selectedItem->arguments());
00254     }
00255 }
00256 
00257 void AppletBrowserWidget::appletAdded(Plasma::Applet* applet)
00258 {
00259     QString name = applet->name();
00260     kDebug() << name;
00261     d->runningApplets.insert(name, applet);
00262     d->appletNames.insert(applet, name);
00263     connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletDestroyed(QObject*)));
00264     d->itemModel.setRunningApplets(name, d->runningApplets.count(name));
00265 }
00266 
00267 void AppletBrowserWidget::appletDestroyed(QObject* applet)
00268 {
00269     kDebug() << applet;
00270     Plasma::Applet* a = (Plasma::Applet*)applet; //don't care if it's valid, just need the address
00271     QString name = d->appletNames.take(a);
00272     //if !name, was the applet not found or was the name actually ""?
00273     d->runningApplets.remove(name, a);
00274     d->itemModel.setRunningApplets(name, d->runningApplets.count(name));
00275 }
00276 
00277 void AppletBrowserWidget::destroyApplets(QString name)
00278 {
00279     foreach (Plasma::Applet* app, d->runningApplets.values(name)) {
00280         //FIXME I have a hard time believing this is safe without QPointer
00281         app->disconnect(this); //don't need to be told it's being destroyed
00282         app->destroy();
00283         d->appletNames.remove(app);
00284     }
00285     d->runningApplets.remove(name);
00286 }
00287 
00288 void AppletBrowserWidget::downloadApplets()
00289 {
00290     //TODO: implement
00291     kDebug() << "GHNS button clicked";
00292 }
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 /*AppletBrowser::AppletBrowser(Plasma::Corona * corona, QWidget * parent, Qt::WindowFlags f)
00305     : KDialog(parent, f),
00306     m_widget(new AppletBrowserWidget(corona, false, this))
00307 {
00308     winId(); // this is to get us a win id so that the next line doesn't abort on us
00309     setWindowRole("appletbrowser");
00310     init();
00311 }*/
00312 
00313 AppletBrowser::AppletBrowser(Plasma::Containment * containment, QWidget * parent, Qt::WindowFlags f)
00314     : KDialog(parent, f),
00315     m_widget(new AppletBrowserWidget(containment, false, this))
00316 {
00317     init();
00318 }
00319 
00320 void AppletBrowser::init()
00321 {
00322     setMainWidget(m_widget);
00323 
00324     setWindowTitle(i18n("Widgets"));
00325 
00326     setButtons(KDialog::Apply | KDialog::Close /*| KDialog::User1*/);
00327     setButtonText(KDialog::Apply, i18n("Add Widget"));
00328 //    setButtonText(KDialog::User1, i18n("Get New Widgets")); //TODO: not overly happy with this text
00329 //    enableButton(KDialog::User1, false); //TODO: enable when GHNS integration is implemented
00330 
00331     connect(this, SIGNAL(applyClicked()), m_widget, SLOT(addApplet()));
00332 //    connect(this, SIGNAL(user1Clicked()), m_widget, SLOT(downloadApplets()));
00333 
00334     QAction* quit = KStandardAction::quit(qApp, SLOT(quit()), this);
00335     addAction(quit);
00336 }
00337 
00338 AppletBrowser::~AppletBrowser()
00339 {
00340 }
00341 
00342 void AppletBrowser::setApplication(const QString& app)
00343 {
00344     m_widget->setApplication( app );
00345 }
00346 
00347 QString AppletBrowser::application()
00348 {
00349     return m_widget->application();
00350 }
00351 
00352 void AppletBrowser::setContainment(Plasma::Containment *containment)
00353 {
00354     m_widget->setContainment(containment);
00355 }
00356 
00357 Containment* AppletBrowser::containment() const
00358 {
00359     return m_widget->containment();
00360 }
00361 
00362 } // namespace Plasma
00363 
00364 #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
  • 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