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 "plasmaappletitemmodel_p.h"
24 PlasmaAppletItem::PlasmaAppletItem(PlasmaAppletItemModel
*model
,
25 const QMap
<QString
, QVariant
>& info
,
27 QMap
<QString
, QVariant
> *extraAttrs
)
28 : QObject(model
), m_model(model
)
30 QMap
<QString
, QVariant
> attrs(info
);
32 attrs
.insert("favorite", flags
& Favorite
? true : false);
33 attrs
.insert("used", flags
& Used
? true : false);
34 //attrs.insert("recommended", flags & Recommended ? true : false);
36 attrs
.unite(* extraAttrs
);
38 setText(info
["name"].toString() + " - "+ info
["category"].toString());
40 setIcon(qvariant_cast
<QIcon
>(info
["icon"]));
43 QString
PlasmaAppletItem::name() const
45 return data().toMap()["name"].toString();
48 QString
PlasmaAppletItem::pluginName() const
50 return data().toMap()["pluginName"].toString();
53 QString
PlasmaAppletItem::description() const
55 return data().toMap()["description"].toString();
58 int PlasmaAppletItem::running() const
60 return data().toMap()["runningCount"].toInt();
63 bool PlasmaAppletItem::used() const
65 return data().toMap()["used"].toBool();
68 void PlasmaAppletItem::setFavorite(bool favorite
)
70 QMap
<QString
, QVariant
> attrs
= data().toMap();
71 attrs
.insert("favorite", favorite
? true : false);
72 setData(QVariant(attrs
));
74 QString pluginName
= attrs
["pluginName"].toString();
75 m_model
->setFavorite(pluginName
, favorite
);
78 void PlasmaAppletItem::setRunning(int count
)
80 QMap
<QString
, QVariant
> attrs
= data().toMap();
81 attrs
.insert("running", count
> 0); //bool for the filter
82 attrs
.insert("runningCount", count
);
83 setData(QVariant(attrs
));
86 void PlasmaAppletItem::setUsed(bool used
)
88 QMap
<QString
, QVariant
> attrs
= data().toMap();
89 attrs
.insert("used", used
);
90 setData(QVariant(attrs
));
93 bool PlasmaAppletItem::passesFiltering(
94 const KCategorizedItemsViewModels::Filter
& filter
) const
96 return data().toMap()[filter
.first
] == filter
.second
;
99 QVariantList
PlasmaAppletItem::arguments() const
101 return qvariant_cast
<QVariantList
>(data().toMap()["arguments"]);
104 PlasmaAppletItemModel::PlasmaAppletItemModel(KConfigGroup configGroup
, QObject
* parent
) :
105 KCategorizedItemsViewModels::DefaultItemModel(parent
),
106 m_configGroup(configGroup
)
108 m_used
= m_configGroup
.readEntry("used").split(',');
109 m_favorites
= m_configGroup
.readEntry("favorites").split(',');
110 connect(KSycoca::self(), SIGNAL(databaseChanged()), this, SLOT(populateModel()));
113 void PlasmaAppletItemModel::populateModel()
116 //kDebug() << "populating model, our application is" << m_application;
118 // Recommended emblems and filters
119 QRegExp
rx("recommended[.]([0-9A-Za-z]+)[.]plugins");
120 QMapIterator
<QString
, QString
> i(m_configGroup
.entryMap());
121 QMap
< QString
, QMap
< QString
, QVariant
> > extraPluginAttrs
;
122 while (i
.hasNext()) {
124 if (!rx
.exactMatch(i
.key())) {
127 QString id
= rx
.cap(1);
129 foreach (const QString
&plugin
, i
.value().split(',')) {
130 extraPluginAttrs
[plugin
]["recommended." + id
] = true;
134 //TODO: get recommended, favorite, used, etc out of listAppletInfo()
135 //kDebug() << "number of applets is"
136 // << Plasma::Applet::listAppletInfo(QString(), m_application).count();
137 foreach (const KPluginInfo
&info
, Plasma::Applet::listAppletInfo(QString(), m_application
)) {
138 //kDebug() << info.pluginName() << "NoDisplay" << info.property("NoDisplay").toBool();
139 if (info
.property("NoDisplay").toBool() || info
.category() == i18n("Containments")) {
140 // we don't want to show the hidden category
143 //kDebug() << info.pluginName() << " is the name of the plugin\n";
145 QMap
<QString
, QVariant
> attrs
;
146 attrs
.insert("name", info
.name());
147 attrs
.insert("pluginName", info
.pluginName());
148 attrs
.insert("description", info
.comment());
149 attrs
.insert("category", info
.category());
151 static_cast<QIcon
>(KIcon(info
.icon().isEmpty() ?
152 "application-x-plasma" : info
.icon())));
154 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()])));
158 void PlasmaAppletItemModel::setRunningApplets(const QHash
<QString
, int> &apps
)
160 //foreach item, find that string and set the count
161 for (int r
=0; r
<rowCount(); ++r
) {
162 QStandardItem
*i
= item(r
);
163 PlasmaAppletItem
*p
= dynamic_cast<PlasmaAppletItem
*>(i
);
166 const bool running
= apps
.value(p
->name());
167 const bool used
= m_used
.contains(p
->pluginName());
169 p
->setRunning(running
);
170 //mark just used applets that arent't running
171 p
->setUsed(!running
&& used
);
173 if (running
&& !used
) {
174 m_used
.append(p
->pluginName());
179 m_configGroup
.writeEntry("used", m_used
.join(","));
182 void PlasmaAppletItemModel::setRunningApplets(const QString
&name
, int count
)
184 for (int r
=0; r
<rowCount(); ++r
) {
185 QStandardItem
*i
= item(r
);
186 PlasmaAppletItem
*p
= dynamic_cast<PlasmaAppletItem
*>(i
);
187 if (p
&& p
->name() == name
) {
188 const bool used
= m_used
.contains(p
->pluginName());
190 p
->setRunning(count
);
191 p
->setUsed(used
&& count
== 0);
193 if (count
> 0 && !used
) {
194 m_used
.append(p
->pluginName());
199 m_configGroup
.writeEntry("used", m_used
.join(","));
202 QStringList
PlasmaAppletItemModel::mimeTypes() const
205 types
<< QLatin1String("text/x-plasmoidservicename");
209 QMimeData
*PlasmaAppletItemModel::mimeData(const QModelIndexList
&indexes
) const
211 kDebug() << "GETTING MIME DATA\n";
212 if (indexes
.count() <= 0) {
216 QStringList types
= mimeTypes();
218 if (types
.isEmpty()) {
222 QMimeData
*data
= new QMimeData();
224 QString format
= types
.at(0);
226 QByteArray appletNames
;
228 foreach (const QModelIndex
&index
, indexes
) {
229 if (index
.row() == lastRow
) {
233 lastRow
= index
.row();
234 PlasmaAppletItem
*selectedItem
= (PlasmaAppletItem
*) itemFromIndex(index
);
235 appletNames
+= '\n' + selectedItem
->pluginName().toUtf8();
236 //kDebug() << selectedItem->pluginName() << index.column() << index.row();
239 data
->setData(format
, appletNames
);
243 void PlasmaAppletItemModel::setFavorite(const QString
&plugin
, bool favorite
)
246 if (!m_favorites
.contains(plugin
)) {
247 m_favorites
.append(plugin
);
249 } else if (m_favorites
.contains(plugin
)) {
250 m_favorites
.removeAll(plugin
);
253 m_configGroup
.writeEntry("favorites", m_favorites
.join(","));
254 m_configGroup
.sync();
257 void PlasmaAppletItemModel::setApplication(const QString
&app
)
263 QString
&PlasmaAppletItemModel::Application()
265 return m_application
;
268 #include <plasmaappletitemmodel_p.moc>