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>
27 #include <KConfigGroup>
29 #include <KPageWidgetItem>
30 #include <KPushButton>
31 #include <KServiceTypeTrader>
32 #include <KStandardAction>
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"
48 class AppletBrowserWidgetPrivate
51 AppletBrowserWidgetPrivate(AppletBrowserWidget
*w
)
56 configGroup(&config
, "Applet Browser"),
57 itemModel(configGroup
, w
),
64 void initRunningApplets();
65 void containmentDestroyed();
68 * Tracks a new running applet
70 void appletAdded(Plasma::Applet
*applet
);
73 * A running applet is no more
75 void appletRemoved(Plasma::Applet
*applet
);
77 AppletBrowserWidget
*q
;
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
;
86 KConfigGroup configGroup
;
88 PlasmaAppletItemModel itemModel
;
89 KCategorizedItemsViewModels::DefaultFilterModel filterModel
;
92 void AppletBrowserWidgetPrivate::initFilters()
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()) {
104 if (!rx
.exactMatch(i
.key())) {
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
),
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),
125 filterModel
.addFilter(i18n("My Favorite Widgets"),
126 KCategorizedItemsViewModels::Filter("favorite", true),
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))
150 AppletBrowserWidget::~AppletBrowserWidget()
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
);
164 appletList
->addEmblem(i18n("Widgets I Have Used Before"), KIcon("view-history"),
165 KCategorizedItemsViewModels::Filter("used", true));
168 appletList
->setFilterModel(&filterModel
);
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
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
190 kDebug() << "can't happen";
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
;
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()
256 void AppletBrowserWidget::addApplet()
258 if (!d
->containment
) {
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();
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
);
287 if (runningApplets
.contains(name
)) {
288 count
= runningApplets
[name
] - 1;
291 runningApplets
.remove(name
);
293 runningApplets
[name
] = count
;
297 itemModel
.setRunningApplets(name
, count
);
300 void AppletBrowserWidget::destroyApplets(const QString
&name
)
302 if (!d
->containment
) {
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
311 kDebug() << "can't happen";
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);
326 d
->runningApplets
.remove(name
);
327 d
->itemModel
.setRunningApplets(name
, 0);
330 void AppletBrowserWidget::infoAboutApplet(const QString
&name
)
332 if (!d
->containment
) {
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);
358 void AppletBrowserWidget::downloadWidgets(const QString
&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",
368 if (offers
.isEmpty()) {
369 kDebug() << "could not find requested PackageStructure plugin" << type
;
371 KService::Ptr service
= offers
.first();
373 installer
= service
->createInstance
<Plasma::PackageStructure
>(topLevelWidget(),
374 QVariantList(), &error
);
377 connect(installer
, SIGNAL(newWidgetBrowserFinished()),
378 installer
, SLOT(deleteLater()));
380 kDebug() << "found, but could not load requested PackageStructure plugin" << type
381 << "; reported error was" << error
;
387 installer
->createNewWidgetBrowser(this);
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);
404 class AppletBrowserPrivate
407 void init(AppletBrowser
*browser
);
408 void populateWidgetsMenu();
411 AppletBrowserWidget
*widget
;
415 void AppletBrowserPrivate::populateWidgetsMenu()
417 if (!widgetsMenu
->actions().isEmpty()) {
418 // already populated.
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
)
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
)
490 AppletBrowser::~AppletBrowser()
492 KConfigGroup
cg(KGlobal::config(), "PlasmaAppletBrowserDialog");
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"