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();
|
|
|
|
}
|
|
|
|
|
2007-12-10 02:18:57 +01:00
|
|
|
int PlasmaAppletItem::running() const
|
|
|
|
{
|
|
|
|
return data().toMap()["runningCount"].toInt();
|
|
|
|
}
|
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
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();
|
2008-03-03 00:41:20 +01:00
|
|
|
m_model->setFavorite(pluginName, favorite);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2007-12-10 02:18:57 +01:00
|
|
|
void PlasmaAppletItem::setRunning(int count)
|
|
|
|
{
|
|
|
|
QMap<QString, QVariant> attrs = data().toMap();
|
|
|
|
attrs.insert("running", count > 0); //bool for the filter
|
|
|
|
attrs.insert("runningCount", count);
|
|
|
|
setData(QVariant(attrs));
|
|
|
|
}
|
|
|
|
|
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();
|
2008-01-08 02:25:09 +01:00
|
|
|
//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
|
|
|
|
2008-04-26 18:19:00 +02:00
|
|
|
foreach (const QString &plugin, i.value().split(",")) {
|
2007-10-08 23:50:38 +02:00
|
|
|
extraPluginAttrs[plugin]["recommended." + id] = true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
|
2008-04-16 23:15:38 +02:00
|
|
|
//TODO: get recommended, favorite, used, etc out of listAppletInfo()
|
|
|
|
//kDebug() << "number of applets is" << Plasma::Applet::listAppletInfo(QString(), m_application).count();
|
|
|
|
foreach (const KPluginInfo& info, Plasma::Applet::listAppletInfo(QString(), m_application)) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << info.pluginName() << "NoDisplay" << info.property("NoDisplay").toBool();
|
2007-10-25 03:42:32 +02:00
|
|
|
if (info.property("NoDisplay").toBool()) {
|
2007-10-24 03:04:36 +02:00
|
|
|
// we don't want to show the hidden category
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << info.pluginName() << " is the name of the plugin\n";
|
2007-10-12 19:30:30 +02:00
|
|
|
|
2008-03-03 00:41:20 +01:00
|
|
|
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-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
void PlasmaAppletItemModel::setRunningApplets(const QHash<QString, int> &apps)
|
2007-12-10 02:18:57 +01:00
|
|
|
{
|
|
|
|
//foreach item, find that string and set the count
|
|
|
|
for (int r=0; r<rowCount(); ++r) {
|
|
|
|
QStandardItem *i = item(r);
|
2008-03-12 21:39:23 +01:00
|
|
|
PlasmaAppletItem *p = dynamic_cast<PlasmaAppletItem *>(i);
|
2007-12-10 02:18:57 +01:00
|
|
|
if (p) {
|
|
|
|
p->setRunning(apps.value(p->name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
void PlasmaAppletItemModel::setRunningApplets(const QString &name, int count)
|
2007-12-10 02:18:57 +01:00
|
|
|
{
|
|
|
|
for (int r=0; r<rowCount(); ++r) {
|
|
|
|
QStandardItem *i = item(r);
|
2008-03-12 21:39:23 +01:00
|
|
|
PlasmaAppletItem *p = dynamic_cast<PlasmaAppletItem *>(i);
|
2007-12-10 02:18:57 +01:00
|
|
|
if (p && p->name() == name) {
|
|
|
|
p->setRunning(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << "GETTING MIME DATA\n";
|
2007-09-26 09:44:06 +02:00
|
|
|
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
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
void PlasmaAppletItemModel::setFavorite(const QString &plugin, bool favorite)
|
2007-10-12 19:30:30 +02:00
|
|
|
{
|
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;
|
|
|
|
}
|