From b9082fda48bb026fc4e6148efeba9aabf608373a Mon Sep 17 00:00:00 2001
From: "Javier S. Pedro" <maemo@javispedro.com>
Date: Fri, 10 Aug 2012 14:02:07 +0200
Subject: NotificationProviders UI

---
 ckitcallnotification/ckitcallplugin.cpp         |   8 ++
 ckitcallnotification/ckitcallplugin.h           |   1 +
 harmaccuweather/harmaccuplugin.cpp              |   8 ++
 harmaccuweather/harmaccuplugin.h                |   1 +
 libsowatch/notificationplugininterface.h        |   7 ++
 meegohandsetnotification/meegohandsetplugin.cpp |  10 ++-
 meegohandsetnotification/meegohandsetplugin.h   |   1 +
 sowatch.pro                                     |   3 +
 sowatchui/main.cpp                              |   2 +
 sowatchui/providersmodel.cc                     | 105 ++++++++++++++++++++++++
 sowatchui/providersmodel.h                      |  44 ++++++++++
 sowatchui/qml/WatchPage.qml                     |  29 ++++---
 sowatchui/sowatchui.pro                         |   6 +-
 13 files changed, 207 insertions(+), 18 deletions(-)
 create mode 100644 sowatchui/providersmodel.cc
 create mode 100644 sowatchui/providersmodel.h

diff --git a/ckitcallnotification/ckitcallplugin.cpp b/ckitcallnotification/ckitcallplugin.cpp
index b63c1b6..cda8f58 100644
--- a/ckitcallnotification/ckitcallplugin.cpp
+++ b/ckitcallnotification/ckitcallplugin.cpp
@@ -19,6 +19,14 @@ QStringList CKitCallPlugin::providers()
 	return providers;
 }
 
+NotificationPluginInterface::NotificationProviderInfo CKitCallPlugin::describeProvider(const QString &driver)
+{
+	Q_UNUSED(driver);
+	NotificationProviderInfo info;
+	info.name = "MeeGo Incoming calls";
+	return info;
+}
+
 NotificationProvider* CKitCallPlugin::getProvider(const QString& driver, ConfigKey *settings, QObject *parent)
 {
 	Q_UNUSED(driver);
diff --git a/ckitcallnotification/ckitcallplugin.h b/ckitcallnotification/ckitcallplugin.h
index b55f178..6484115 100644
--- a/ckitcallnotification/ckitcallplugin.h
+++ b/ckitcallnotification/ckitcallplugin.h
@@ -16,6 +16,7 @@ public:
 	~CKitCallPlugin();
 
 	QStringList providers();
+	NotificationProviderInfo describeProvider(const QString &driver);
 	NotificationProvider* getProvider(const QString& driver, ConfigKey *settings, QObject *parent = 0);
 };
 
diff --git a/harmaccuweather/harmaccuplugin.cpp b/harmaccuweather/harmaccuplugin.cpp
index 7414187..ff7b6cf 100644
--- a/harmaccuweather/harmaccuplugin.cpp
+++ b/harmaccuweather/harmaccuplugin.cpp
@@ -19,6 +19,14 @@ QStringList HarmAccuPlugin::providers()
 	return providers;
 }
 
+NotificationPluginInterface::NotificationProviderInfo HarmAccuPlugin::describeProvider(const QString &driver)
+{
+	NotificationProviderInfo info;
+	if (driver != "harmaccu") return info;
+	info.name = "Accuweather";
+	return info;
+}
+
 NotificationProvider* HarmAccuPlugin::getProvider(const QString& id, ConfigKey *settings, QObject *parent)
 {
 	Q_UNUSED(settings);
diff --git a/harmaccuweather/harmaccuplugin.h b/harmaccuweather/harmaccuplugin.h
index d27c796..535b4a2 100644
--- a/harmaccuweather/harmaccuplugin.h
+++ b/harmaccuweather/harmaccuplugin.h
@@ -16,6 +16,7 @@ public:
 	~HarmAccuPlugin();
 
 	QStringList providers();
+	NotificationProviderInfo describeProvider(const QString &driver);
 	NotificationProvider* getProvider(const QString& driver, ConfigKey *settings, QObject *parent = 0);
 };
 
diff --git a/libsowatch/notificationplugininterface.h b/libsowatch/notificationplugininterface.h
index d753afc..fa75ddd 100644
--- a/libsowatch/notificationplugininterface.h
+++ b/libsowatch/notificationplugininterface.h
@@ -4,6 +4,7 @@
 #include <QtPlugin>
 #include <QtCore/QSettings>
 #include <QtCore/QStringList>
+#include <QtGui/QIcon>
 #include "sowatch_global.h"
 
 namespace sowatch
@@ -18,7 +19,13 @@ class SOWATCH_EXPORT NotificationPluginInterface
 public:
 	virtual ~NotificationPluginInterface();
 
+	struct NotificationProviderInfo {
+		QString name;
+		QIcon icon;
+	};
+
 	virtual QStringList providers() = 0;
+	virtual NotificationProviderInfo describeProvider(const QString& driver) = 0;
 	virtual NotificationProvider* getProvider(const QString& driver, ConfigKey *settings, QObject *parent = 0) = 0;
 };
 
diff --git a/meegohandsetnotification/meegohandsetplugin.cpp b/meegohandsetnotification/meegohandsetplugin.cpp
index 0f50575..de0f925 100644
--- a/meegohandsetnotification/meegohandsetplugin.cpp
+++ b/meegohandsetnotification/meegohandsetplugin.cpp
@@ -18,10 +18,18 @@ QStringList MeegoHandsetPlugin::providers()
 	return providers;
 }
 
+NotificationPluginInterface::NotificationProviderInfo MeegoHandsetPlugin::describeProvider(const QString &driver)
+{
+	NotificationProviderInfo info;
+	if (driver != "meegohandset") return info;
+	info.name = "MeeGo Notifications";
+	return info;
+}
+
 NotificationProvider* MeegoHandsetPlugin::getProvider(const QString& driver, ConfigKey* settings, QObject *parent)
 {
-	Q_UNUSED(driver);
 	Q_UNUSED(settings);
+	if (driver != "meegohandset") return 0;
 	return new MeegoHandsetNotificationProvider(parent);
 }
 
diff --git a/meegohandsetnotification/meegohandsetplugin.h b/meegohandsetnotification/meegohandsetplugin.h
index 4d826dc..d6f285f 100644
--- a/meegohandsetnotification/meegohandsetplugin.h
+++ b/meegohandsetnotification/meegohandsetplugin.h
@@ -16,6 +16,7 @@ public:
 	~MeegoHandsetPlugin();
 
 	QStringList providers();
+	NotificationProviderInfo describeProvider(const QString& driver);
 	NotificationProvider* getProvider(const QString& driver, ConfigKey *settings, QObject *parent = 0);
 };
 
diff --git a/sowatch.pro b/sowatch.pro
index 2f4ed37..63b541d 100644
--- a/sowatch.pro
+++ b/sowatch.pro
@@ -29,6 +29,9 @@ contains(MEEGO_EDITION,harmattan) {
 	ckitcallnotification.depends = libsowatch
 	harmaccuweather.depends = libsowatch
     qmafwwatchlet.depends = libsowatch
+} else:simulator {
+	SUBDIRS += harmaccuweather
+	harmaccuweather.depends = libsowatch
 }
 
 OTHER_FILES += \
diff --git a/sowatchui/main.cpp b/sowatchui/main.cpp
index af2a0c0..5e116ac 100644
--- a/sowatchui/main.cpp
+++ b/sowatchui/main.cpp
@@ -6,6 +6,7 @@
 
 #include "watchesmodel.h"
 #include "watchscannermodel.h"
+#include "providersmodel.h"
 
 static sowatch::Registry *registry;
 static WatchesModel *watches;
@@ -24,6 +25,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
 
 	qmlRegisterType<sowatch::ConfigKey>();
 	qmlRegisterType<sowatch::GConfKey>("com.javispedro.sowatch", 1, 0, "GConfKey");
+	qmlRegisterType<ProvidersModel>("com.javispedro.sowatch", 1, 0, "ProvidersModel");
 
 	viewer->rootContext()->setContextProperty("watches", watches);
 	viewer->rootContext()->setContextProperty("watchScanner", watchScanner);
diff --git a/sowatchui/providersmodel.cc b/sowatchui/providersmodel.cc
new file mode 100644
index 0000000..68ce21b
--- /dev/null
+++ b/sowatchui/providersmodel.cc
@@ -0,0 +1,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();
+}
diff --git a/sowatchui/providersmodel.h b/sowatchui/providersmodel.h
new file mode 100644
index 0000000..9cf6ec6
--- /dev/null
+++ b/sowatchui/providersmodel.h
@@ -0,0 +1,44 @@
+#ifndef PROVIDERSMODEL_H
+#define PROVIDERSMODEL_H
+
+#include <QtCore/QAbstractListModel>
+
+#include <sowatch.h>
+
+class ProvidersModel : public QAbstractListModel
+{
+	Q_OBJECT
+	Q_PROPERTY(QString configKey READ configKey WRITE setConfigKey NOTIFY configKeyChanged)
+
+public:
+	explicit ProvidersModel(QObject *parent = 0);
+
+	enum DataRoles {
+		NameRole = Qt::UserRole,
+		EnabledRole
+	};
+
+	QString configKey() const;
+	void setConfigKey(const QString& configKey);
+
+	int rowCount(const QModelIndex &parent) const;
+	QVariant data(const QModelIndex &index, int role) const;
+	bool setData(const QModelIndex &index, const QVariant &value, int role);
+
+public slots:
+	void setProviderEnabled(const QString& id, bool enabled);
+
+signals:
+	void configKeyChanged();
+	
+private slots:
+	void reload();
+
+private:
+	sowatch::ConfigKey *_config;
+	QStringList _all_list;
+	QList<sowatch::NotificationPluginInterface::NotificationProviderInfo> _info_list;
+	QSet<QString> _enabled;
+};
+
+#endif // PROVIDERSMODEL_H
diff --git a/sowatchui/qml/WatchPage.qml b/sowatchui/qml/WatchPage.qml
index 59bb5c4..21c4543 100644
--- a/sowatchui/qml/WatchPage.qml
+++ b/sowatchui/qml/WatchPage.qml
@@ -73,38 +73,37 @@ Page {
 
 			GroupHeader {
 				width: parent.width
-				text: "Watchlets"
+				text: "Notification sources"
 				visible: configQmlLoader.status === Loader.Ready
 			}
 
 			ListView {
-				id: watchletsListView
+				id: providersListView
 				interactive: false
 				width: parent.width
 				height: UiConstants.ListItemHeightDefault * count
-				model: ListModel {
-					ListElement {
-						title: "Test"
-					}
+				model: ProvidersModel {
+					id: providersModel
+					configKey: watchPage.configKey + "/providers"
 				}
 				delegate: ListDelegate {
-
+					CheckBox {
+						anchors.verticalCenter: parent.verticalCenter
+						anchors.right: parent.right
+						checked: model.enabled
+						onCheckedChanged: providersModel.setProviderEnabled(model.name, checked);
+					}
 				}
 			}
 
-			Button {
-				anchors.horizontalCenter: parent.horizontalCenter
-				text: qsTr("Add watchlet")
-			}
-
 			GroupHeader {
 				width: parent.width
-				text: "Notification sources"
+				text: "Watchlets"
 				visible: configQmlLoader.status === Loader.Ready
 			}
 
 			ListView {
-				id: providersListView
+				id: watchletsListView
 				interactive: false
 				width: parent.width
 				height: UiConstants.ListItemHeightDefault * count
@@ -120,7 +119,7 @@ Page {
 
 			Button {
 				anchors.horizontalCenter: parent.horizontalCenter
-				text: qsTr("Add notification source")
+				text: qsTr("Add new watchlet")
 			}
 		}
 	}
diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro
index c0589fc..fa965d3 100644
--- a/sowatchui/sowatchui.pro
+++ b/sowatchui/sowatchui.pro
@@ -37,11 +37,13 @@ DEPENDPATH += $$PWD/../libsowatch
 # Source files
 SOURCES += main.cpp \
     watchesmodel.cpp daemonproxy.cpp \
-    watchscannermodel.cpp
+    watchscannermodel.cpp \
+    providersmodel.cc
 
 HEADERS += \
     watchesmodel.h daemonproxy.h \
-    watchscannermodel.h
+    watchscannermodel.h \
+    providersmodel.h
 
 OTHER_FILES += qml/main.qml \
 	qml/MainPage.qml \
-- 
cgit v1.2.3