summaryrefslogtreecommitdiff
path: root/sowatchui/configuredwatchletsmodel.cpp
blob: 0bea11e0fd42f7e5947b822e22c4937aa3422287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "configuredwatchletsmodel.h"

using namespace sowatch;

static const QString watchletsSubKey("/watchlets");

ConfiguredWatchletsModel::ConfiguredWatchletsModel(QObject *parent) :
    QAbstractListModel(parent),
    _config(0),
    _unadded(false)
{
	QHash<int, QByteArray> roles = roleNames();
	roles[Qt::DisplayRole] = QByteArray("title");
	roles[Qt::DecorationRole] = QByteArray("iconSource");
	roles[NameRole] = QByteArray("name");
	roles[ConfigQmlUrlRole] = QByteArray("configQmlUrl");
	setRoleNames(roles);
}

QString ConfiguredWatchletsModel::configKey() const
{
	if (_config) {
		QString key = _config->key();
		return key.left(key.length() - watchletsSubKey.length());
	} else {
		return QString();
	}
}

void ConfiguredWatchletsModel::setConfigKey(const QString &configKey)
{
	QString oldConfigKey = this->configKey();
	if (_config) {
		delete _config;
		_config = 0;
	}
	if (!configKey.isEmpty()) {
		_config = new GConfKey(configKey + watchletsSubKey, this);
		connect(_config, SIGNAL(changed()), SLOT(handleConfigChanged()));
	}
	if (this->configKey() != oldConfigKey) {
		reload();
		emit configKeyChanged();
	}
}

bool ConfiguredWatchletsModel::displayUnadded() const
{
	return _unadded;
}

void ConfiguredWatchletsModel::setDisplayUnadded(bool displayUnadded)
{
	qDebug() << "Set dunadded" << displayUnadded;
	_unadded = displayUnadded;
	if (_config) reload();
	emit displayUnaddedChanged();
}

int ConfiguredWatchletsModel::rowCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	return _list.count();
}

QVariant ConfiguredWatchletsModel::data(const QModelIndex &index, int role) const
{
	const QString id = _list[index.row()];
	switch (role) {
	case Qt::DisplayRole:
		return QVariant::fromValue(_info[id].name);
	case Qt::DecorationRole:
		return QVariant::fromValue(_info[id].phoneIcon);
	case NameRole:
		return QVariant::fromValue(id);
	case ConfigQmlUrlRole:
		return QVariant::fromValue(_info[id].configQmlUrl);
	}
	return QVariant();
}

void ConfiguredWatchletsModel::addWatchlet(const QString &name)
{
	if (!_config) return;
	QStringList enabled = _config->value().toStringList();
	if (_enabled.contains(name)) return;
	enabled << name;
	_config->set(enabled);
}

void ConfiguredWatchletsModel::removeWatchlet(const QString &name)
{
	if (!_config) return;
	QStringList enabled = _config->value().toStringList();
	enabled.removeAll(name);
	_config->set(enabled);
}

void ConfiguredWatchletsModel::moveWatchletUp(const QString &name)
{
	if (!_config) return;
	QStringList enabled = _config->value().toStringList();
	int index = enabled.indexOf(name);
	qDebug() << "move up" << enabled << index << enabled[index];
	if (index > 0 && index < enabled.size()) {
		enabled.swap(index - 1, index);
	}
	_config->set(enabled);
}

void ConfiguredWatchletsModel::moveWatchletDown(const QString &name)
{
	if (!_config) return;
	QStringList enabled = _config->value().toStringList();
	int index = enabled.indexOf(name);
	qDebug() << "move down" << enabled << index << enabled[index];
	if (index >= 0 && index < enabled.size() - 1) {
		enabled.swap(index, index + 1);
	}
	_config->set(enabled);
}

void ConfiguredWatchletsModel::reload()
{
	Registry *registry = Registry::registry();
	Q_ASSERT(_config);
	beginResetModel();
	_list.clear();
	_info.clear();
	_enabled.clear();

	qDebug() << "Reloading watchlets";

	QString watchModel = _config->value("driver").toString();

	QStringList all = registry->allWatchlets();
	foreach (const QString& s, all) {
		WatchletPluginInterface *plugin = registry->getWatchletPlugin(s);
		if (plugin) {
			_info[s] = plugin->describeWatchlet(s, watchModel);
		} else {
			WatchletPluginInterface::WatchletInfo info;
			info.name = s;
			_info[s] = info;
		}
	}

	QStringList enabled = _config->value().toStringList();
	_enabled = enabled.toSet();

	if (_unadded) {
		qDebug() << "Listing unadded watchlets from" << all;
		foreach (const QString& s, all) {
			if (_info[s].visible && !_enabled.contains(s)) {
				_list.append(s);
			}
		}
		_list.sort();
	} else {
		qDebug() << "Listing added watchlets from" << enabled;
		_list = enabled;
	}
	endResetModel();
}

void ConfiguredWatchletsModel::handleConfigChanged()
{
	// TODO
	reload();
}