summaryrefslogtreecommitdiff
path: root/sowatchui/providersmodel.cc
blob: 68ce21b14961f1bbf3ceca1283db7dc8f1ad61e3 (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
#include <QtDebug>

#include "providersmodel.h"

using namespace sowatch;

ProvidersModel::ProvidersModel(QObject *parent) :
    QAbstractListModel(parent),
    _config(0)
{
	QHash<int, QByteArray> roles = roleNames();
	roles[Qt::DisplayRole] = QByteArray("title");
	roles[NameRole] = QByteArray("name");
	roles[EnabledRole] = QByteArray("enabled");
	setRoleNames(roles);
}

QString ProvidersModel::configKey() const
{
	if (_config) {
		return _config->key();
	} else {
		return QString();
	}
}

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

int ProvidersModel::rowCount(const QModelIndex &parent) const
{
	return _all_list.count();
}

QVariant ProvidersModel::data(const QModelIndex &index, int role) const
{
	switch (role) {
	case Qt::DisplayRole:
		return QVariant::fromValue(_info_list[index.row()].name);
	case NameRole:
		return QVariant::fromValue(_all_list[index.row()]);
	case EnabledRole:
		return QVariant::fromValue(_enabled.contains(_all_list[index.row()]));
	}
	return QVariant();
}

bool ProvidersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	switch (role) {
	case EnabledRole:
		setProviderEnabled(_all_list[index.row()], value.toBool());
		return true;
	}
	return false;
}

void ProvidersModel::setProviderEnabled(const QString &id, bool enabled)
{
	if (enabled) {
		_enabled.insert(id);
	} else {
		_enabled.remove(id);
	}
	_config->set(QVariant::fromValue(QStringList(_enabled.toList())));
}

void ProvidersModel::reload()
{
	Registry *registry = Registry::registry();
	beginResetModel();
	_all_list.clear();
	_info_list.clear();
	_enabled.clear();

	_all_list = registry->allNotificationProviders();
	_info_list.reserve(_all_list.size());
	foreach (const QString& s, _all_list) {
		NotificationPluginInterface *plugin = registry->getNotificationPlugin(s);
		if (plugin) {
			_info_list.append(plugin->describeProvider(s));
		} else {
			NotificationPluginInterface::NotificationProviderInfo info;
			info.name = s;
			_info_list.append(info);
		}
	}

	_enabled = _config->value().toStringList().toSet();
	endResetModel();
}