00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "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 #include <KAboutData>
00034 #include <KAboutApplicationDialog>
00035 #include <KComponentData>
00036 #include <KPluginLoader>
00037
00038 #include <Plasma/Applet>
00039 #include <Plasma/Corona>
00040 #include <Plasma/Containment>
00041 #include "kcategorizeditemsview_p.h"
00042 #include "plasmaappletitemmodel_p.h"
00043 #include "openwidgetassistant_p.h"
00044
00045 namespace Plasma
00046 {
00047
00048 class AppletBrowserWidgetPrivate
00049 {
00050 public:
00051 AppletBrowserWidgetPrivate(AppletBrowserWidget *w)
00052 : q(w),
00053 containment(0),
00054 appletList(0),
00055 config("plasmarc"),
00056 configGroup(&config, "Applet Browser"),
00057 itemModel(configGroup, w),
00058 filterModel(w)
00059 {
00060 }
00061
00062 void initFilters();
00063 void init();
00064 void initRunningApplets();
00065 void containmentDestroyed();
00066
00070 void appletAdded(Plasma::Applet *applet);
00071
00075 void appletRemoved(Plasma::Applet *applet);
00076
00077 AppletBrowserWidget *q;
00078 QString application;
00079 Plasma::Containment *containment;
00080 KCategorizedItemsView *appletList;
00081 QHash<QString, int> runningApplets;
00082
00083 QHash<Plasma::Applet *,QString> appletNames;
00084
00085 KConfig config;
00086 KConfigGroup configGroup;
00087
00088 PlasmaAppletItemModel itemModel;
00089 KCategorizedItemsViewModels::DefaultFilterModel filterModel;
00090 };
00091
00092 void AppletBrowserWidgetPrivate::initFilters()
00093 {
00094 filterModel.clear();
00095
00096 filterModel.addFilter(i18n("All Widgets"),
00097 KCategorizedItemsViewModels::Filter(), KIcon("plasma"));
00098
00099
00100 QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]caption");
00101 QMapIterator<QString, QString> i(configGroup.entryMap());
00102 while (i.hasNext()) {
00103 i.next();
00104 if (!rx.exactMatch(i.key())) {
00105 continue;
00106 }
00107
00108
00109 QString id = rx.cap(1);
00110 QString caption = configGroup.readEntry("recommended." + id + ".caption");
00111 QString icon = configGroup.readEntry("recommended." + id + ".icon");
00112 QString plugins = configGroup.readEntry("recommended." + id + ".plugins");
00113
00114 appletList->addEmblem(i18nc("%1 is the entity (person, staff, organization...) which recommends the widget",
00115 "Recommended by %1", caption),
00116 KIcon(icon),
00117 KCategorizedItemsViewModels::Filter("recommended." + id, true));
00118 filterModel.addFilter(i18nc("%1 is the entity (person, staff, organization...) which recommends the widget",
00119 "Recommended by %1", caption),
00120 KCategorizedItemsViewModels::Filter("recommended." + id, true),
00121 KIcon(icon));
00122 }
00123
00124
00125 filterModel.addFilter(i18n("My Favorite Widgets"),
00126 KCategorizedItemsViewModels::Filter("favorite", true),
00127 KIcon("bookmarks"));
00128 filterModel.addFilter(i18n("Widgets I Have Used Before"),
00129 KCategorizedItemsViewModels::Filter("used", true),
00130 KIcon("view-history"));
00131 filterModel.addFilter(i18n("Currently Running Widgets"),
00132 KCategorizedItemsViewModels::Filter("running", true),
00133 KIcon("view-history"));
00134
00135 filterModel.addSeparator(i18n("Categories:"));
00136
00137 foreach (const QString &category, Plasma::Applet::listCategories(application)) {
00138 filterModel.addFilter(category,
00139 KCategorizedItemsViewModels::Filter("category", category));
00140 }
00141 }
00142
00143 AppletBrowserWidget::AppletBrowserWidget(QWidget * parent, Qt::WindowFlags f)
00144 : QWidget(parent, f),
00145 d(new AppletBrowserWidgetPrivate(this))
00146 {
00147 d->init();
00148 }
00149
00150 AppletBrowserWidget::~AppletBrowserWidget()
00151 {
00152 delete d;
00153 }
00154
00155 void AppletBrowserWidgetPrivate::init()
00156 {
00157 QVBoxLayout *layout = new QVBoxLayout(q);
00158
00159 appletList = new KCategorizedItemsView(q);
00160 QObject::connect(appletList, SIGNAL(doubleClicked(const QModelIndex &)), q, SLOT(addApplet()));
00161 layout->addWidget(appletList);
00162
00163
00164 appletList->addEmblem(i18n("Widgets I Have Used Before"), KIcon("view-history"),
00165 KCategorizedItemsViewModels::Filter("used", true));
00166
00167 initFilters();
00168 appletList->setFilterModel(&filterModel);
00169
00170
00171 appletList->setItemModel(&itemModel);
00172 initRunningApplets();
00173
00174 q->setLayout(layout);
00175 }
00176
00177 void AppletBrowserWidgetPrivate::initRunningApplets()
00178 {
00179
00180 if (!containment) {
00181 return;
00182 }
00183
00184
00185 Plasma::Corona *c = containment->corona();
00186
00187
00188
00189 if (!c) {
00190 kDebug() << "can't happen";
00191 return;
00192 }
00193
00194 appletNames.clear();
00195 runningApplets.clear();
00196 QList<Containment*> containments = c->containments();
00197 foreach (Containment *containment, containments) {
00198 QObject::connect(containment, SIGNAL(appletAdded(Plasma::Applet*,QPointF)), q, SLOT(appletAdded(Plasma::Applet*)));
00199 QObject::connect(containment, SIGNAL(appletRemoved(Plasma::Applet*)), q, SLOT(appletRemoved(Plasma::Applet*)));
00200
00201 foreach (Applet *applet, containment->applets()) {
00202 runningApplets[applet->name()]++;
00203 }
00204 }
00205
00206
00207 itemModel.setRunningApplets(runningApplets);
00208 }
00209
00210 void AppletBrowserWidget::setApplication(const QString &app)
00211 {
00212 d->application = app;
00213 d->initFilters();
00214 d->itemModel.setApplication(app);
00215
00216
00217
00218 d->appletList->setItemModel(&d->itemModel);
00219
00220
00221 d->itemModel.setRunningApplets(d->runningApplets);
00222 }
00223
00224 QString AppletBrowserWidget::application()
00225 {
00226 return d->application;
00227 }
00228
00229 void AppletBrowserWidget::setContainment(Plasma::Containment *containment)
00230 {
00231 if (d->containment != containment) {
00232 if (d->containment) {
00233 d->containment->disconnect(this);
00234 }
00235
00236 d->containment = containment;
00237
00238 if (d->containment) {
00239 connect(d->containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
00240 }
00241
00242 d->initRunningApplets();
00243 }
00244 }
00245
00246 Containment *AppletBrowserWidget::containment() const
00247 {
00248 return d->containment;
00249 }
00250
00251 void AppletBrowserWidgetPrivate::containmentDestroyed()
00252 {
00253 containment = 0;
00254 }
00255
00256 void AppletBrowserWidget::addApplet()
00257 {
00258 if (!d->containment) {
00259 return;
00260 }
00261
00262 foreach (AbstractItem *item, d->appletList->selectedItems()) {
00263 PlasmaAppletItem *selectedItem = (PlasmaAppletItem *) item;
00264
00265 d->containment->addApplet(selectedItem->pluginName(), selectedItem->arguments());
00266 }
00267 }
00268
00269 void AppletBrowserWidgetPrivate::appletAdded(Plasma::Applet *applet)
00270 {
00271 QString name = applet->name();
00272
00273
00274 runningApplets[name]++;
00275 appletNames.insert(applet, name);
00276 itemModel.setRunningApplets(name, runningApplets[name]);
00277 }
00278
00279 void AppletBrowserWidgetPrivate::appletRemoved(Plasma::Applet *applet)
00280 {
00281
00282 Plasma::Applet *a = (Plasma::Applet *)applet;
00283
00284 QString name = appletNames.take(a);
00285
00286 int count = 0;
00287 if (runningApplets.contains(name)) {
00288 count = runningApplets[name] - 1;
00289
00290 if (count < 1) {
00291 runningApplets.remove(name);
00292 } else {
00293 runningApplets[name] = count;
00294 }
00295 }
00296
00297 itemModel.setRunningApplets(name, count);
00298 }
00299
00300 void AppletBrowserWidget::destroyApplets(const QString &name)
00301 {
00302 if (!d->containment) {
00303 return;
00304 }
00305
00306 Plasma::Corona *c = d->containment->corona();
00307
00308
00309
00310 if (!c) {
00311 kDebug() << "can't happen";
00312 return;
00313 }
00314
00315 foreach (Containment *containment, c->containments()) {
00316 QList<Applet*> applets = containment->applets();
00317 foreach (Applet *applet, applets) {
00318 if (applet->name() == name) {
00319 d->appletNames.remove(applet);
00320 applet->disconnect(this);
00321 applet->destroy();
00322 }
00323 }
00324 }
00325
00326 d->runningApplets.remove(name);
00327 d->itemModel.setRunningApplets(name, 0);
00328 }
00329
00330 void AppletBrowserWidget::infoAboutApplet(const QString &name)
00331 {
00332 if (!d->containment) {
00333 return;
00334 }
00335
00336 KPluginInfo::List applets = Plasma::Applet::listAppletInfo();
00337 foreach (const KPluginInfo &info, applets) {
00338 if (info.name() == name) {
00339 KAboutData aboutData = KAboutData(info.name().toUtf8(),
00340 info.name().toUtf8(),
00341 ki18n(info.name().toUtf8()),
00342 info.version().toUtf8(), ki18n(info.comment().toUtf8()),
00343 info.fullLicense().key(), ki18n(QByteArray()), ki18n(QByteArray()), info.website().toLatin1(),
00344 info.email().toLatin1());
00345
00346 aboutData.setProgramIconName(info.icon());
00347
00348 aboutData.addAuthor(ki18n(info.author().toUtf8()), ki18n(QByteArray()), info.email().toLatin1());
00349
00350
00351 KAboutApplicationDialog *aboutDialog = new KAboutApplicationDialog(&aboutData, this);
00352 aboutDialog->show();
00353 break;
00354 }
00355 }
00356 }
00357
00358 void AppletBrowserWidget::downloadWidgets(const QString &type)
00359 {
00360
00361 PackageStructure *installer = 0;
00362
00363 if (!type.isEmpty()) {
00364 QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(type);
00365 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure",
00366 constraint);
00367
00368 if (offers.isEmpty()) {
00369 kDebug() << "could not find requested PackageStructure plugin" << type;
00370 } else {
00371 KService::Ptr service = offers.first();
00372 QString error;
00373 installer = service->createInstance<Plasma::PackageStructure>(topLevelWidget(),
00374 QVariantList(), &error);
00375
00376 if (installer) {
00377 connect(installer, SIGNAL(newWidgetBrowserFinished()),
00378 installer, SLOT(deleteLater()));
00379 } else {
00380 kDebug() << "found, but could not load requested PackageStructure plugin" << type
00381 << "; reported error was" << error;
00382 }
00383 }
00384 }
00385
00386 if (installer) {
00387 installer->createNewWidgetBrowser(this);
00388 } else {
00389
00390
00391 Applet::packageStructure()->createNewWidgetBrowser(this);
00392 }
00393 }
00394
00395 void AppletBrowserWidget::openWidgetFile()
00396 {
00397
00398
00399 OpenWidgetAssistant *assistant = new OpenWidgetAssistant(topLevelWidget());
00400 assistant->setAttribute(Qt::WA_DeleteOnClose, true);
00401 assistant->show();
00402 }
00403
00404 class AppletBrowserPrivate
00405 {
00406 public:
00407 void init(AppletBrowser *browser);
00408 void populateWidgetsMenu();
00409
00410 AppletBrowser *q;
00411 AppletBrowserWidget *widget;
00412 QMenu *widgetsMenu;
00413 };
00414
00415 void AppletBrowserPrivate::populateWidgetsMenu()
00416 {
00417 if (!widgetsMenu->actions().isEmpty()) {
00418
00419 return;
00420 }
00421
00422 QSignalMapper *mapper = new QSignalMapper(q);
00423 QObject::connect(mapper, SIGNAL(mapped(QString)), widget, SLOT(downloadWidgets(QString)));
00424
00425 QAction *action = new QAction(KIcon("applications-internet"),
00426 i18n("Download New Plasma Widgets"), q);
00427 QObject::connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
00428 mapper->setMapping(action, QString());
00429 widgetsMenu->addAction(action);
00430
00431 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure");
00432 foreach (const KService::Ptr service, offers) {
00433
00434 if (service->property("X-Plasma-ProvidesWidgetBrowser").toBool()) {
00435 QAction *action = new QAction(KIcon("applications-internet"),
00436 i18nc("%1 is a type of widgets, as defined by "
00437 "e.g. some plasma-packagestructure-*.desktop files",
00438 "Download New %1", service->name()), q);
00439 QObject::connect(action, SIGNAL(triggered(bool)), mapper, SLOT(map()));
00440 mapper->setMapping(action, service->property("X-KDE-PluginInfo-Name").toString());
00441 widgetsMenu->addAction(action);
00442 }
00443 }
00444
00445 widgetsMenu->addSeparator();
00446
00447 action = new QAction(KIcon("package-x-generic"),
00448 i18n("Install Widget From Local File..."), q);
00449 QObject::connect(action, SIGNAL(triggered(bool)), widget, SLOT(openWidgetFile()));
00450 widgetsMenu->addAction(action);
00451 }
00452
00453 void AppletBrowserPrivate::init(AppletBrowser *browser)
00454 {
00455 q = browser;
00456 widget = new AppletBrowserWidget(q);
00457
00458 q->setMainWidget(widget);
00459 q->setWindowTitle(i18n("Widgets"));
00460
00461 q->setButtons(KDialog::Apply | KDialog::Close | KDialog::User1);
00462 q->setButtonText(KDialog::Apply, i18n("Add Widget"));
00463 q->setButtonText(KDialog::User1, i18n("Install New Widgets"));
00464
00465 widgetsMenu = new KMenu(i18n("Get New Widgets"), q);
00466 QObject::connect(widgetsMenu, SIGNAL(aboutToShow()), q, SLOT(populateWidgetsMenu()));
00467 q->button(KDialog::User1)->setMenu(widgetsMenu);
00468
00469 q->setButtonToolTip(KDialog::Close, i18n("Close the dialog"));
00470 q->setButtonWhatsThis(KDialog::Close, i18n("<qt>When clicking <b>Close</b>, this dialog will be closed with no further action taken.</qt>"));
00471 q->setButtonToolTip(KDialog::Apply, i18n("Add selected widgets"));
00472 q->setButtonWhatsThis(KDialog::Apply, i18n("<qt>When clicking <b>Add Widget</b>, the selected widgets will be added to your desktop.</qt>"));
00473 q->setButtonToolTip(KDialog::User1, i18n("Install new widgets"));
00474 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>"));
00475
00476 QObject::connect(q, SIGNAL(applyClicked()), widget, SLOT(addApplet()));
00477
00478 q->setInitialSize(QSize(400, 600));
00479 KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
00480 q->restoreDialogSize(cg);
00481 }
00482
00483 AppletBrowser::AppletBrowser(QWidget * parent, Qt::WindowFlags f)
00484 : KDialog(parent, f),
00485 d(new AppletBrowserPrivate)
00486 {
00487 d->init(this);
00488 }
00489
00490 AppletBrowser::~AppletBrowser()
00491 {
00492 KConfigGroup cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
00493 saveDialogSize(cg);
00494 }
00495
00496 void AppletBrowser::setApplication(const QString &app)
00497 {
00498 d->widget->setApplication(app);
00499 }
00500
00501 QString AppletBrowser::application()
00502 {
00503 return d->widget->application();
00504 }
00505
00506 void AppletBrowser::setContainment(Plasma::Containment *containment)
00507 {
00508 d->widget->setContainment(containment);
00509 }
00510
00511 Containment *AppletBrowser::containment() const
00512 {
00513 return d->widget->containment();
00514 }
00515
00516 }
00517
00518 #include "appletbrowser.moc"