2007-09-26 09:44:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-10-12 10:44:02 +02:00
|
|
|
* it under the terms of the GNU Library/Lesser General Public License
|
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
2007-09-26 09:44:06 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
2007-10-12 10:44:02 +02:00
|
|
|
* You should have received a copy of the GNU Library/Lesser General Public
|
2007-09-26 09:44:06 +02:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-09-27 20:51:31 +02:00
|
|
|
#include "plasmaappletitemmodel_p.h"
|
2007-09-26 09:44:06 +02:00
|
|
|
|
2007-10-12 19:30:30 +02:00
|
|
|
PlasmaAppletItem::PlasmaAppletItem(PlasmaAppletItemModel * model, const QMap<QString, QVariant>& info,
|
2007-10-08 23:50:38 +02:00
|
|
|
FilterFlags flags, QMap<QString, QVariant> * extraAttrs) :
|
2007-10-09 11:51:55 +02:00
|
|
|
QObject(model), m_model(model)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2007-10-12 19:30:30 +02:00
|
|
|
QMap<QString, QVariant> attrs(info);
|
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
attrs.insert("favorite", flags & Favorite ? true : false);
|
|
|
|
attrs.insert("used", flags & Used ? true : false);
|
2007-10-08 23:50:38 +02:00
|
|
|
//attrs.insert("recommended", flags & Recommended ? true : false);
|
|
|
|
if (extraAttrs) attrs.unite(* extraAttrs);
|
2007-10-12 19:30:30 +02:00
|
|
|
setText(info["name"].toString() + " - "+ info["category"].toString());
|
2007-09-26 09:44:06 +02:00
|
|
|
setData(attrs);
|
2007-10-12 19:30:30 +02:00
|
|
|
setIcon(qvariant_cast<QIcon>(info["icon"]));
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString PlasmaAppletItem::name() const
|
|
|
|
{
|
|
|
|
return data().toMap()["name"].toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PlasmaAppletItem::pluginName() const
|
|
|
|
{
|
|
|
|
return data().toMap()["pluginName"].toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PlasmaAppletItem::description() const
|
|
|
|
{
|
|
|
|
return data().toMap()["description"].toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmaAppletItem::setFavorite(bool favorite)
|
|
|
|
{
|
|
|
|
QMap<QString, QVariant> attrs = data().toMap();
|
|
|
|
attrs.insert("favorite", favorite ? true : false);
|
|
|
|
setData(QVariant(attrs));
|
2007-10-12 19:30:30 +02:00
|
|
|
|
|
|
|
QString pluginName = attrs["pluginName"].toString();
|
|
|
|
|
|
|
|
if (pluginName == "skapplet" && attrs.contains("arguments")) {
|
|
|
|
// skapplet can be used with all SuperKaramba themes,
|
|
|
|
// so when setting skapplet as favorite it is also
|
|
|
|
// necessary to know which theme is meant
|
|
|
|
QString themePath = qvariant_cast<QVariantList>(attrs["arguments"])[0].toString();
|
|
|
|
|
|
|
|
m_model->setFavorite(pluginName + " - " + themePath, favorite);
|
|
|
|
} else {
|
|
|
|
m_model->setFavorite(pluginName, favorite);
|
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlasmaAppletItem::passesFiltering(
|
|
|
|
const KCategorizedItemsViewModels::Filter & filter) const
|
|
|
|
{
|
|
|
|
return data().toMap()[filter.first] == filter.second;
|
|
|
|
}
|
|
|
|
|
2007-10-12 19:30:30 +02:00
|
|
|
QVariantList PlasmaAppletItem::arguments() const
|
|
|
|
{
|
|
|
|
return qvariant_cast<QVariantList>(data().toMap()["arguments"]);
|
|
|
|
}
|
|
|
|
|
2007-11-06 22:10:11 +01:00
|
|
|
PlasmaAppletItemModel::PlasmaAppletItemModel(KConfigGroup configGroup, QObject * parent) :
|
2007-10-09 11:51:55 +02:00
|
|
|
KCategorizedItemsViewModels::DefaultItemModel(parent),
|
|
|
|
m_configGroup(configGroup)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2007-11-06 22:10:11 +01:00
|
|
|
m_used = m_configGroup.readEntry("used").split(",");
|
|
|
|
m_favorites = m_configGroup.readEntry("favorites").split(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmaAppletItemModel::populateModel()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
//kDebug() << "populating model, our application is" << m_application;
|
2007-10-08 23:50:38 +02:00
|
|
|
|
|
|
|
// Recommended emblems and filters
|
|
|
|
QRegExp rx("recommended[.]([0-9A-Za-z]+)[.]plugins");
|
2007-10-09 11:51:55 +02:00
|
|
|
QMapIterator<QString, QString> i(m_configGroup.entryMap());
|
2007-10-08 23:50:38 +02:00
|
|
|
QMap < QString, QMap < QString, QVariant > > extraPluginAttrs;
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (!rx.exactMatch(i.key())) continue;
|
|
|
|
QString id = rx.cap(1);
|
2007-10-12 19:30:30 +02:00
|
|
|
|
2007-10-08 23:50:38 +02:00
|
|
|
foreach (QString plugin, i.value().split(",")) {
|
|
|
|
extraPluginAttrs[plugin]["recommended." + id] = true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
|
2007-11-06 22:10:11 +01:00
|
|
|
//TODO: get recommended, favorite, used, etc out of knownApplets()
|
|
|
|
//kDebug() << "number of applets is" << Plasma::Applet::knownApplets(QString(), m_application).count();
|
|
|
|
foreach (const KPluginInfo& info, Plasma::Applet::knownApplets(QString(), m_application)) {
|
2007-10-25 03:42:32 +02:00
|
|
|
//kDebug() << info.pluginName() << "NoDisplay" << info.property("NoDisplay").toBool();
|
|
|
|
if (info.property("NoDisplay").toBool()) {
|
2007-10-24 03:04:36 +02:00
|
|
|
// we don't want to show the hidden category
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-25 03:42:32 +02:00
|
|
|
//kDebug() << info.pluginName() << " is the name of the plugin\n";
|
2007-10-12 19:30:30 +02:00
|
|
|
|
|
|
|
if (info.pluginName() == "skapplet") {
|
2007-10-24 03:04:36 +02:00
|
|
|
// If there is the SuperKaramba applet,
|
|
|
|
// add SuperKaramba themes to the
|
|
|
|
// model too
|
2007-10-12 19:30:30 +02:00
|
|
|
loadSuperKarambaThemes(info);
|
2007-10-24 03:04:36 +02:00
|
|
|
} else {
|
|
|
|
QMap<QString, QVariant> attrs;
|
|
|
|
attrs.insert("name", info.name());
|
|
|
|
attrs.insert("pluginName", info.pluginName());
|
|
|
|
attrs.insert("description", info.comment());
|
|
|
|
attrs.insert("category", info.category());
|
|
|
|
attrs.insert("icon", static_cast<QIcon>(KIcon(info.icon().isEmpty()?"application-x-plasma":info.icon())));
|
|
|
|
|
|
|
|
appendRow(new PlasmaAppletItem(this, attrs,
|
|
|
|
((m_favorites.contains(info.pluginName())) ? PlasmaAppletItem::Favorite : PlasmaAppletItem::NoFilter) |
|
|
|
|
((m_used.contains(info.pluginName())) ? PlasmaAppletItem::Used : PlasmaAppletItem::NoFilter)
|
|
|
|
, &(extraPluginAttrs[info.pluginName()])));
|
2007-10-12 19:30:30 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PlasmaAppletItemModel::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
|
|
|
types << QLatin1String("text/x-plasmoidservicename");
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* PlasmaAppletItemModel::mimeData(const QModelIndexList & indexes) const
|
|
|
|
{
|
|
|
|
kDebug() << "GETTING MIME DATA\n";
|
|
|
|
if (indexes.count() <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList types = mimeTypes();
|
|
|
|
|
|
|
|
if (types.isEmpty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData * data = new QMimeData();
|
|
|
|
|
|
|
|
QString format = types.at(0);
|
|
|
|
|
|
|
|
PlasmaAppletItem
|
|
|
|
* selectedItem = (PlasmaAppletItem *) itemFromIndex(indexes[0]);
|
|
|
|
QByteArray appletName(selectedItem->pluginName().toUtf8());
|
|
|
|
|
|
|
|
data->setData(format, appletName);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2007-10-09 11:51:55 +02:00
|
|
|
|
2007-10-12 19:30:30 +02:00
|
|
|
void PlasmaAppletItemModel::setFavorite(QString plugin, bool favorite)
|
|
|
|
{
|
2007-10-09 11:51:55 +02:00
|
|
|
if (favorite) {
|
|
|
|
if (!m_favorites.contains(plugin)) {
|
|
|
|
m_favorites.append(plugin);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_favorites.contains(plugin)) {
|
|
|
|
m_favorites.removeAll(plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_configGroup.writeEntry("favorites", m_favorites.join(","));
|
|
|
|
m_configGroup.sync();
|
|
|
|
|
|
|
|
}
|
2007-10-12 19:30:30 +02:00
|
|
|
|
2007-11-06 03:45:24 +01:00
|
|
|
void PlasmaAppletItemModel::setApplication(const QString& app)
|
|
|
|
{
|
|
|
|
m_application = app;
|
2007-11-06 22:10:11 +01:00
|
|
|
populateModel();
|
2007-11-06 03:45:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString& PlasmaAppletItemModel::Application()
|
|
|
|
{
|
|
|
|
return m_application;
|
|
|
|
}
|
|
|
|
|
2007-10-12 19:30:30 +02:00
|
|
|
/*
|
|
|
|
* Define function type to get the SuperKaramba themes
|
|
|
|
* from skapplet (see skapplet.cpp in kdeutils/superkaramba)
|
|
|
|
*/
|
|
|
|
extern "C" {
|
|
|
|
typedef QList<QMap<QString, QVariant> > (*installedThemes)();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlasmaAppletItemModel::loadSuperKarambaThemes(const KPluginInfo &info)
|
|
|
|
{
|
|
|
|
KService::Ptr service = info.service();
|
|
|
|
QString libName = service->library();
|
|
|
|
|
|
|
|
// Load the Plugin as library to get access
|
|
|
|
// to installedThemes() in skapplet
|
|
|
|
KLibrary *lib = KLibLoader::self()->library(libName);
|
|
|
|
if (lib) {
|
|
|
|
installedThemes loadThemes = 0;
|
|
|
|
|
|
|
|
loadThemes = (installedThemes)lib->resolveFunction("installedThemes");
|
|
|
|
|
|
|
|
if (loadThemes) {
|
|
|
|
// loadThemes() returns the name, description, the icon
|
|
|
|
// and one argument (file path) from the theme
|
|
|
|
QList<QMap<QString, QVariant> > themeMetadata = loadThemes();
|
|
|
|
|
|
|
|
QMap <QString, QVariant> metadata;
|
|
|
|
foreach (metadata, themeMetadata) {
|
|
|
|
metadata.insert("pluginName", "skapplet");
|
|
|
|
metadata.insert("category", "SuperKaramba");
|
|
|
|
|
|
|
|
QString favorite = info.pluginName() + " - " + qvariant_cast<QVariantList>(metadata["arguments"])[0].toString();
|
|
|
|
|
|
|
|
appendRow(new PlasmaAppletItem(this, metadata,
|
|
|
|
((m_favorites.contains(favorite)) ? PlasmaAppletItem::Favorite : PlasmaAppletItem::NoFilter) |
|
|
|
|
((m_used.contains(info.pluginName())) ? PlasmaAppletItem::Used : PlasmaAppletItem::NoFilter)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning() << "Could not load" << libName;
|
|
|
|
}
|
|
|
|
}
|