@@ -19,18 +19,13 @@
1919
2020#include "indicatorsmodel.h"
2121#include "indicatorsmanager.h"
22−#include "indicatorclientinterface.h"
22+#include "indicator.h"
23+#include "paths.h"
2324
2425#include <QQmlContext>
2526#include <QQmlEngine>
2627#include <QDebug>
2728
28−
29−static bool comparePlugins(const IndicatorClientInterface::Ptr obj1, const IndicatorClientInterface::Ptr obj2)
30−{
31− return obj1->priority() < obj2->priority();
32−}
33−
3429/*!
3530 \qmltype IndicatorsModel
3631 \inherits QAbstractListModel
@@ -128,37 +123,40 @@ void IndicatorsModel::unload()
128123/*! \internal */
129124void IndicatorsModel::onIndicatorLoaded(const QString& indicator)
130125{
131− IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
126+ Indicator::Ptr plugin = m_manager->indicator(indicator);
132127 if (!plugin)
133128 return;
134129
135− QList<IndicatorClientInterface::Ptr>::iterator i = qLowerBound(m_plugins.begin(), m_plugins.end(), plugin, comparePlugins);
136− int insert_pos = qMin(i - m_plugins.begin(), m_plugins.count());
137−
138− beginInsertRows(QModelIndex(), insert_pos, insert_pos);
130+ if (m_plugins.indexOf(plugin) >= 0)
131+ return;
139132
140− QObject* obj = dynamic_cast<QObject*>(plugin.get());
141− if (obj)
142− {
143− QObject::connect(obj, SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
144− QObject::connect(obj, SIGNAL(titleChanged(const QString&)), this, SLOT(onTitleChanged()));
145− QObject::connect(obj, SIGNAL(labelChanged(const QString&)), this, SLOT(onLabelChanged()));
133+ // find the insert position
134+ int pos = 0;
135+ while (pos < count()) {
136+ // keep going while the existing priority is less.
137+ if (indicatorData(plugin, Priority).toInt() < data(index(pos), Priority).toInt())
138+ break;
139+ pos++;
146140 }
147141
148− m_plugins.insert(i, plugin);
142+ QObject::connect(plugin.get(), SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
143+ QObject::connect(plugin.get(), SIGNAL(indicatorPropertiesChanged(const QVariant&)), this, SLOT(onIndicatorPropertiesChanged()));
144+
145+ beginInsertRows(QModelIndex(), pos, pos);
149146
147+ m_plugins.insert(pos, plugin);
150148 endInsertRows();
151149}
152150
153151/*! \internal */
154152void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator)
155153{
156− IndicatorClientInterface::Ptr plugin = m_manager->indicator(indicator);
154+ Indicator::Ptr plugin = m_manager->indicator(indicator);
157155 if (!plugin)
158156 return;
159157
160158 int i = 0;
161− QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
159+ QMutableListIterator<Indicator::Ptr> iter(m_plugins);
162160 while(iter.hasNext())
163161 {
164162 if (plugin == iter.next())
@@ -179,26 +177,20 @@ void IndicatorsModel::onIdentifierChanged()
179177}
180178
181179/*! \internal */
182−void IndicatorsModel::onTitleChanged()
180+void IndicatorsModel::onIndicatorPropertiesChanged()
183181{
184− notifyDataChanged(QObject::sender(), Title);
185−}
186−
187−/*! \internal */
188−void IndicatorsModel::onLabelChanged()
189−{
190− notifyDataChanged(QObject::sender(), Label);
182+ notifyDataChanged(QObject::sender(), IndicatorProperties);
191183}
192184
193185/*! \internal */
194186void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
195187{
196− IndicatorClientInterface* plugin = dynamic_cast<IndicatorClientInterface*>(sender);
188+ Indicator* plugin = qobject_cast<Indicator*>(sender);
197189 if (!plugin)
198190 return;
199191
200192 int index = 0;
201− QMutableListIterator<IndicatorClientInterface::Ptr> iter(m_plugins);
193+ QMutableListIterator<Indicator::Ptr> iter(m_plugins);
202194 while(iter.hasNext())
203195 {
204196 if (iter.next().get() == plugin)
@@ -215,14 +207,15 @@ void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
215207QHash<int, QByteArray> IndicatorsModel::roleNames() const
216208{
217209 static QHash<int, QByteArray> roles;
218− if (roles.isEmpty()) {
210+ if (roles.isEmpty())
211+ {
219212 roles[Identifier] = "identifier";
213+ roles[Priority] = "priority";
220214 roles[Title] = "title";
221− roles[Label] = "label";
222215 roles[Description] = "description";
223− roles[IconUrl] = "iconUrl";
224− roles[PageUrl] = "pageUrl";
225− roles[InitialProperties] = "initialProperties";
216+ roles[IconQml] = "iconQml";
217+ roles[PageQml] = "pageQml";
218+ roles[IndicatorProperties] = "indicatorProperties";
226219 roles[IsValid] = "isValid";
227220 }
228221 return roles;
@@ -234,63 +227,69 @@ int IndicatorsModel::columnCount(const QModelIndex &) const
234227 return 1;
235228}
236229
230+/*! \internal */
231+QVariant IndicatorsModel::defaultData(Indicator::Ptr plugin, int role)
232+{
233+ switch (role)
234+ {
235+ case Priority:
236+ return 0;
237+ case Title:
238+ return plugin ? plugin->identifier() : "Unknown";
239+ case Description:
240+ return "";
241+ case IconQml:
242+ return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorIcon.qml";
243+ case PageQml:
244+ return shellAppDirectory()+"/Panel/Indicators/DefaultIndicatorPage.qml";
245+ }
246+ return QVariant();
247+}
248+
237249/*! \internal */
238250QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
239251{
240252 if (!index.isValid() || index.row() >= m_plugins.size())
241253 return QVariant();
242254
243− QVariant attribute;
244−
245− IndicatorClientInterface::Ptr plugin = m_plugins[index.row()];
255+ Indicator::Ptr plugin = m_plugins[index.row()];
246256
247− switch (role) {
248− case Identifier:
249− if (plugin) {
250− attribute = QVariant(plugin->identifier());
251− }
252− break;
253− case Title:
254− if (plugin) {
255− attribute = QVariant(plugin->title());
256− }
257− break;
258− case Label:
259− if (plugin) {
260− attribute = QVariant(plugin->label());
261− }
262− break;
263− case Description:
257+ switch (role)
264258 {
265− if (plugin) {
266− attribute = QVariant(plugin->description());
267− }
268− break;
259+ case Identifier:
260+ if (plugin)
261+ {
262+ return QVariant(plugin->identifier());
263+ }
264+ break;
265+ case IndicatorProperties:
266+ if (plugin)
267+ {
268+ return QVariant(plugin->indicatorProperties());
269+ }
270+ break;
271+ case IsValid:
272+ return (plugin.get() ? true : false);
273+ case Priority:
274+ case Title:
275+ case Description:
276+ case IconQml:
277+ case PageQml:
278+ return indicatorData(plugin, role);
279+ default:
280+ break;
269281 }
270− case IconUrl:
271− if (plugin) {
272− return plugin->iconComponentSource().toString();
273− } else {
274− attribute = QVariant("No plugin");
275− }
276− break;
277− case PageUrl:
278− if (plugin) {
279− return plugin->pageComponentSource();
280− }
281− break;
282− case InitialProperties:
283− if (plugin) {
284− attribute = plugin->initialProperties();
285− }
286− break;
287− case IsValid:
288− attribute = QVariant(plugin != 0);
289− break;
290− default:
291− break;
282+ return QVariant();
283+}
284+
285+QVariant IndicatorsModel::indicatorData(const Indicator::Ptr& plugin, int role) const
286+{
287+ if (plugin && m_parsed_indicator_data.contains(plugin->identifier()))
288+ {
289+ QVariantMap data = m_parsed_indicator_data[plugin->identifier()];
290+ return data.value(roleNames()[role], QVariant());
292291 }
293− return attribute;
292+ return defaultData(plugin, role);
294293}
295294
296295/*! \internal */
@@ -304,3 +303,24 @@ int IndicatorsModel::rowCount(const QModelIndex&) const
304303{
305304 return m_plugins.count();
306305}
306+
307+void IndicatorsModel::setIndicatorData(const QVariant& data)
308+{
309+ m_indicator_data = data;
310+
311+ m_parsed_indicator_data.clear();
312+ QMap<QString, QVariant> map = data.toMap();
313+ QMapIterator<QString, QVariant> iter(map);
314+ while(iter.hasNext())
315+ {
316+ iter.next();
317+ m_parsed_indicator_data[iter.key()] = iter.value().toMap();
318+ }
319+
320+ Q_EMIT indicatorDataChanged(m_indicator_data);
321+}
322+
323+QVariant IndicatorsModel::indicatorData() const
324+{
325+ return m_indicator_data;
326+}