From b1ccb04d7dd2fb20def829be084474ed329d4988 Mon Sep 17 00:00:00 2001
From: "Javier S. Pedro" <maemo@javispedro.com>
Date: Thu, 9 Aug 2012 21:02:14 +0200
Subject: change config of enabled/disabled

---
 sowatchui/qml/NewWatchSheet.qml | 13 ++++++++
 sowatchui/sowatchui.pro         |  9 ++++++
 sowatchui/watchesmodel.cpp      | 71 ++++++++++++-----------------------------
 sowatchui/watchesmodel.h        |  8 ++---
 sowatchui/watchscannermodel.cpp |  3 ++
 5 files changed, 48 insertions(+), 56 deletions(-)

(limited to 'sowatchui')

diff --git a/sowatchui/qml/NewWatchSheet.qml b/sowatchui/qml/NewWatchSheet.qml
index 1028bd5..12eecf2 100644
--- a/sowatchui/qml/NewWatchSheet.qml
+++ b/sowatchui/qml/NewWatchSheet.qml
@@ -28,6 +28,19 @@ Sheet {
 
 		model: watchScanner
 
+		header: Column {
+			width: parent.width
+
+			Label {
+				text: qsTr("Pick a watch")
+				font: UiConstants.HeaderFont
+			}
+			GroupHeader {
+				width: parent.width
+				text: qsTr("Nearby watches")
+			}
+		}
+
 		delegate: ListDelegate {
 			onClicked: {
 				watches.addFoundWatch(object);
diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro
index 93a2438..9caa163 100644
--- a/sowatchui/sowatchui.pro
+++ b/sowatchui/sowatchui.pro
@@ -7,6 +7,15 @@ qml_folder.source = qml
 qml_folder.target = .
 DEPLOYMENTFOLDERS = qml_folder
 
+# Install icon files also to resources directory
+res_files.files += sowatch64.png sowatch80.png
+!isEmpty(MEEGO_VERSION_MAJOR)|maemo5 {
+	res_files.path = /opt/sowatch/share/metawatch
+} else {
+	res_files.path = /usr/share/sowatch/metawatch
+}
+INSTALLS += res_files
+
 # Additional import path used to resolve QML modules in Creator's code model
 QML_IMPORT_PATH =
 simulator {
diff --git a/sowatchui/watchesmodel.cpp b/sowatchui/watchesmodel.cpp
index c2bcd1a..0930715 100644
--- a/sowatchui/watchesmodel.cpp
+++ b/sowatchui/watchesmodel.cpp
@@ -7,21 +7,19 @@ using namespace sowatch;
 WatchesModel::WatchesModel(QObject *parent) :
     QAbstractListModel(parent),
     _config(new GConfKey("/apps/sowatch", this)),
-    _active_watches(_config->getSubkey("active-watches", this)),
+    _watches_list(_config->getSubkey("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[EnabledRole] = QByteArray("enabled");
 	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(_watches_list, SIGNAL(changed()),
+	        SLOT(handleWatchesListChanged()));
 	connect(_daemon, SIGNAL(WatchStatusChanged(QString,QString)),
 	        this, SLOT(handleWatchStatusChanged(QString,QString)));
 
@@ -47,18 +45,20 @@ QVariant WatchesModel::data(const QModelIndex &index, int role) const
 	case Qt::DisplayRole:
 		return config->value("name");
 	case Qt::StatusTipRole:
-		if (isWatchIdActive(id)) {
+		if (config->value("enable").toBool()) {
 			QString status = _status[id];
 			if (status == "connected") {
 				return QVariant(tr("Connected"));
 			} else if (status == "enabled") {
-				return QVariant(tr("Searching..."));
+				return QVariant(tr("Disconnected, Searching..."));
 			} else {
-				return QVariant(tr("Enabled"));
+				return QVariant(tr("Disconnected"));
 			}
 		} else {
 			return QVariant(tr("Disabled"));
 		}
+	case EnabledRole:
+		return config->value("enable");
 	case ConfigKeyRole:
 		return QVariant::fromValue(key);
 	case ConfigQmlUrlRole:
@@ -106,23 +106,26 @@ void WatchesModel::addFoundWatch(const QVariantMap &info)
 		qDebug() << "Would add" << providerId;
 	}
 
-	// Now add to active watches
-	QStringList active = _active_watches->value().toStringList();
+	// Now add to the watches list
+	QStringList active = _watches_list->value().toStringList();
 	active << name;
-	_active_watches->set(active);
+	_watches_list->set(active);
+
+	// Now enable
+	newkey->set("enable", true);
 }
 
 void WatchesModel::reload()
 {
-	QStringList dirs = _config->dirs();
+	QStringList names = _watches_list->value().toStringList();
 
 	beginResetModel();
 	foreach (ConfigKey* conf, _list) {
-		conf->deleteLater();
+		delete conf;
 	}
 	_status.clear();
 	_list.clear();
-	foreach (const QString& s, dirs) {
+	foreach (const QString& s, names) {
 		_list.append(_config->getSubkey(s, this));
 		QDBusReply<QString> reply = _daemon->GetWatchStatus(s);
 		if (reply.isValid()) {
@@ -134,37 +137,9 @@ void WatchesModel::reload()
 	qDebug() << "Found" << _list.count() << "configured watches";
 }
 
-void WatchesModel::handleConfigChanged()
-{
-	qDebug() << "Key changed";
-}
-
-void WatchesModel::handleSubkeyChanged(const QString &subkey)
+void WatchesModel::handleWatchesListChanged()
 {
-	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();
-		}
-	}
+	reload();
 }
 
 void WatchesModel::handleWatchStatusChanged(const QString &watch, const QString &status)
@@ -186,9 +161,3 @@ int WatchesModel::findRowByWatchId(const QString &id)
 	}
 	return -1;
 }
-
-bool WatchesModel::isWatchIdActive(const QString &id) const
-{
-	QStringList active = _active_watches->value().toStringList();
-	return active.contains(id);
-}
diff --git a/sowatchui/watchesmodel.h b/sowatchui/watchesmodel.h
index 396fc8d..6e9b65e 100644
--- a/sowatchui/watchesmodel.h
+++ b/sowatchui/watchesmodel.h
@@ -15,7 +15,7 @@ public:
 	~WatchesModel();
 
 	enum DataRoles {
-		ObjectRole = Qt::UserRole,
+		EnabledRole = Qt::UserRole,
 		ConfigKeyRole,
 		ConfigQmlUrlRole
 	};
@@ -29,17 +29,15 @@ public slots:
 
 private slots:
 	void reload();
-	void handleConfigChanged();
-	void handleSubkeyChanged(const QString& subkey);
+	void handleWatchesListChanged();
 	void handleWatchStatusChanged(const QString& watch, const QString& status);
 
 private:
 	int findRowByWatchId(const QString& id);
-	bool isWatchIdActive(const QString& id) const;
 
 private:
 	sowatch::ConfigKey *_config;
-	sowatch::ConfigKey *_active_watches;
+	sowatch::ConfigKey *_watches_list;
 	DaemonProxy *_daemon;
 	QList<sowatch::ConfigKey*> _list;
 	QMap<QString, QString> _status;
diff --git a/sowatchui/watchscannermodel.cpp b/sowatchui/watchscannermodel.cpp
index 8dd8d1e..f2c76f7 100644
--- a/sowatchui/watchscannermodel.cpp
+++ b/sowatchui/watchscannermodel.cpp
@@ -8,6 +8,7 @@ WatchScannerModel::WatchScannerModel(QObject *parent) :
 {
 	QHash<int, QByteArray> roles = roleNames();
 	roles[Qt::DisplayRole] = QByteArray("title");
+	roles[Qt::DecorationRole] = QByteArray("iconSource");
 	roles[Qt::StatusTipRole] = QByteArray("subtitle");
 	roles[ObjectRole] = QByteArray("object");
 	setRoleNames(roles);
@@ -58,6 +59,8 @@ QVariant WatchScannerModel::data(const QModelIndex &index, int role) const
 	switch (role) {
 	case Qt::DisplayRole:
 		return info["name"];
+	case Qt::DecorationRole:
+		return QVariant(SOWATCH_RESOURCES_DIR "/sowatch64.png");
 	case Qt::StatusTipRole:
 		return info["address"];
 	case ObjectRole:
-- 
cgit v1.2.3