summaryrefslogtreecommitdiff
path: root/sowatchui/watchesmodel.cpp
blob: c45a229f0de6ec5bec2802895c148a9598d8578e (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <QtDebug>

#include "watchesmodel.h"

using namespace sowatch;

WatchesModel::WatchesModel(QObject *parent) :
    QAbstractListModel(parent),
    _config(new GConfKey("/apps/sowatch", this)),
    _active_watches(_config->getSubkey("active-watches", this)),
    _daemon(new DaemonProxy("com.javispedro.sowatchd", "/com/javispedro/sowatch/daemon", QDBusConnection::sessionBus()))
{
	QHash<int, QByteArray> roles = roleNames();
	roles[Qt::DisplayRole] = QByteArray("title");
	roles[Qt::StatusTipRole] = QByteArray("subtitle");
	roles[ObjectRole] = QByteArray("object");
	roles[ConfigKeyRole] = QByteArray("configKey");
	roles[ConfigQmlUrlRole] = QByteArray("configQmlUrl");
	setRoleNames(roles);

	connect(_config, SIGNAL(changed()),
	        this, SLOT(handleConfigChanged()));
	connect(_config, SIGNAL(subkeyChanged(QString)),
	        this, SLOT(handleSubkeyChanged(QString)));
	connect(_daemon, SIGNAL(WatchStatusChanged(QString,QString)),
	        this, SLOT(handleWatchStatusChanged(QString,QString)));

	reload();
}

WatchesModel::~WatchesModel()
{
}

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

QVariant WatchesModel::data(const QModelIndex &index, int role) const
{
	qDebug() << "Asked for data" << index.row() << index.column() << role;
	ConfigKey *config = _list[index.row()];
	QString key = config->key();
	QString id = key.mid(key.lastIndexOf('/') + 1);
	switch (role) {
	case Qt::DisplayRole:
		return config->value("name");
	case Qt::StatusTipRole:
		if (isWatchIdActive(id)) {
			QString status = _status[id];
			if (status == "connected") {
				return QVariant(tr("Connected"));
			} else if (status == "enabled") {
				return QVariant(tr("Searching..."));
			} else {
				return QVariant(tr("Enabled"));
			}
		} else {
			return QVariant(tr("Disabled"));
		}
	case ConfigKeyRole:
		return QVariant::fromValue(key);
	case ConfigQmlUrlRole:
		if (config->isSet("driver")) {
			QString driver = config->value("driver").toString();
			WatchPluginInterface *plugin = Registry::registry()->getWatchPlugin(driver);
			if (plugin) {
				return QVariant::fromValue(plugin->getConfigQmlUrl(driver));
			}
		}
		return QVariant::fromValue(QUrl());
	}
	return QVariant();
}

bool WatchesModel::removeRows(int row, int count, const QModelIndex &parent)
{
	return false; // TODO
}

void WatchesModel::addFoundWatch(const QVariantMap &info)
{
	QStringList existing = _config->dirs();
	QString base = "watch%1";
	QString name = base.arg("");
	int num = 1;

	while (existing.contains(name)) {
		num++;
		name = base.arg(num);
	}

	ConfigKey* newkey = _config->getSubkey(name);
	foreach (const QString& key, info.keys()) {
		newkey->set(key, info[key]);
	}

	// Now add to active watches
	QStringList active = _active_watches->value().toStringList();
	active << name;
	_active_watches->set(active);
}

void WatchesModel::reload()
{
	QStringList dirs = _config->dirs();

	beginResetModel();
	foreach (ConfigKey* conf, _list) {
		conf->deleteLater();
	}
	_status.clear();
	_list.clear();
	foreach (const QString& s, dirs) {
		_list.append(_config->getSubkey(s, this));
		QDBusReply<QString> reply = _daemon->GetWatchStatus(s);
		if (reply.isValid()) {
			_status[s] = reply.value();
		}
	}
	endResetModel();

	qDebug() << "Found" << _list.count() << "configured watches";
}

void WatchesModel::handleConfigChanged()
{
	qDebug() << "Key changed";
}

void WatchesModel::handleSubkeyChanged(const QString &subkey)
{
	QRegExp nameexp("^([^/]+)/name");
	if (nameexp.exactMatch(subkey)) {
		qDebug() << "Name key changed:" << subkey;
		QString id = nameexp.cap(1);
		int i = findRowByWatchId(id);
		if (i != -1) {
			if (_config->value(subkey).isNull()) {
				beginRemoveRows(QModelIndex(), i, i);
				_list[i]->deleteLater();
				_list.removeAt(i);
				qDebug() << "Removing" << i;
				endRemoveRows();
			} else {
				emit dataChanged(createIndex(i, 0), createIndex(i, 0));
				qDebug() << "Changing" << i;
			}
		} else {
			i = _list.size();
			qDebug() << "Inserting" << i;
			beginInsertRows(QModelIndex(), i, i);
			_list.append(_config->getSubkey(id, this));
			endInsertRows();
		}
	}
}

void WatchesModel::handleWatchStatusChanged(const QString &watch, const QString &status)
{
	_status[watch] = status;
	int i = findRowByWatchId(watch);
	if (i != -1) {
		emit dataChanged(createIndex(i, 0), createIndex(i, 0));
	}
}

int WatchesModel::findRowByWatchId(const QString &id)
{
	QString pattern(_config->key() + "/" + id);
	for (int i = 0; i < _list.size(); i++) {
		if (_list[i]->key().endsWith("/" + id)) {
			return i;
		}
	}
	return -1;
}

bool WatchesModel::isWatchIdActive(const QString &id) const
{
	QStringList active = _active_watches->value().toStringList();
	return active.contains(id);
}