From ed40a7f1cbc1da5ae21c58882df241fc0071c2f7 Mon Sep 17 00:00:00 2001
From: "Javier S. Pedro" <dev.git@javispedro.com>
Date: Sat, 6 Sep 2014 03:25:38 +0200
Subject: adding initial UI to select device

---
 src/controller.cpp     |  7 ++++---
 src/reconnecttimer.cpp | 27 +++++++++++++--------------
 src/reconnecttimer.h   |  7 ++-----
 src/salmeta.cpp        | 10 +++++-----
 src/watchviewitem.cpp  | 15 +++++++++++++++
 src/watchviewitem.h    | 20 ++++++++++++++++++++
 6 files changed, 59 insertions(+), 27 deletions(-)
 create mode 100644 src/watchviewitem.cpp
 create mode 100644 src/watchviewitem.h

(limited to 'src')

diff --git a/src/controller.cpp b/src/controller.cpp
index 02c8252..047ad96 100644
--- a/src/controller.cpp
+++ b/src/controller.cpp
@@ -22,10 +22,11 @@ Controller::Controller(const QString &settingsPrefix, QQuickView *view, QObject
 	connect(_settings, &MDConfGroup::valueChanged, this, &Controller::handleSettingChanged);
 	connect(_metawatch, &MetaWatch::connected, _reconnect, &ReconnectTimer::stop);
 	connect(_metawatch, &MetaWatch::connected,  this, &Controller::handleMetaWatchConnected);
-	connect(_metawatch, &MetaWatch::disconnected, _reconnect, &ReconnectTimer::start);
+	connect(_metawatch, &MetaWatch::disconnected, _reconnect, &ReconnectTimer::scheduleNextAttempt);
 	connect(_metawatch, &MetaWatch::batteryStatus, this, &Controller::handleMetaWatchBatteryStatus);
-	connect(_reconnect, &ReconnectTimer::tick, _metawatch, &MetaWatch::connectDevice);
-	_reconnect->start();
+	connect(_reconnect, &ReconnectTimer::tryReconnect, _metawatch, &MetaWatch::connectDevice);
+
+	_reconnect->scheduleNextAttempt();
 
 	reloadPages();
 }
diff --git a/src/reconnecttimer.cpp b/src/reconnecttimer.cpp
index eaf6cbb..a58a13e 100644
--- a/src/reconnecttimer.cpp
+++ b/src/reconnecttimer.cpp
@@ -15,33 +15,33 @@ ReconnectTimer::ReconnectTimer(QObject *parent)
 	: QObject(parent),
 	  _iphb(iphb_open(0)),
 	  _notifier(new QSocketNotifier(iphb_get_fd(_iphb), QSocketNotifier::Read, this)),
-	  _active(false)
+	  _active(false),
+	  _counter(0)
 {
 	connect(_notifier, &QSocketNotifier::activated, this, &ReconnectTimer::handleIphbActivity);
 }
 
 ReconnectTimer::~ReconnectTimer()
 {
-	iphb_close(_iphb);
+	_active = false;
+	_iphb = iphb_close(_iphb);
 }
 
-void ReconnectTimer::start()
+void ReconnectTimer::scheduleNextAttempt()
 {
 	_active = true;
-	_counter = 0;
-	setupWait();
+
+	time_t res = iphb_wait2(_iphb, wait_times[_counter] / 2, wait_times[_counter], 0, 0);
+
+	if (res == -1) {
+		qErrnoWarning("Failed to iphb_wait");
+	}
 }
 
 void ReconnectTimer::stop()
 {
 	_active = false;
 	_counter = 0;
-	iphb_wait(_iphb, 0, 0, 0);
-}
-
-void ReconnectTimer::setupWait()
-{
-	iphb_wait(_iphb, wait_times[_counter] / 2, wait_times[_counter], 0);
 }
 
 void ReconnectTimer::handleIphbActivity()
@@ -55,10 +55,9 @@ void ReconnectTimer::handleIphbActivity()
 		return;
 	}
 
-	emit tick();
-
+	_active = false;
 	if (++_counter > num_wait_times)
 		_counter = num_wait_times;
 
-	setupWait();
+	emit tryReconnect();
 }
diff --git a/src/reconnecttimer.h b/src/reconnecttimer.h
index c8d901f..af4ae98 100644
--- a/src/reconnecttimer.h
+++ b/src/reconnecttimer.h
@@ -13,14 +13,11 @@ public:
 	~ReconnectTimer();
 
 public slots:
-	void start();
+	void scheduleNextAttempt();
 	void stop();
 
 signals:
-	void tick();
-
-private:
-	void setupWait();
+	void tryReconnect();
 
 private slots:
 	void handleIphbActivity();
diff --git a/src/salmeta.cpp b/src/salmeta.cpp
index c4bcb0c..f13cd64 100644
--- a/src/salmeta.cpp
+++ b/src/salmeta.cpp
@@ -2,9 +2,10 @@
 #include <sailfishapp.h>
 
 #include "controller.h"
+#include "watchviewitem.h"
 
 static bool launch_daemon = false;
-static QString settings_key_prefix;
+static QString settings_key_prefix("/apps/salmeta/watch0");
 
 int main(int argc, char *argv[])
 {
@@ -24,16 +25,15 @@ int main(int argc, char *argv[])
 		++it;
 	}
 
-	if (launch_daemon) {
-		if (settings_key_prefix.isEmpty()) {
-			settings_key_prefix = "/apps/salmeta/watch0";
-		}
+	qmlRegisterType<WatchViewItem>("com.javispedro.salmeta", 1, 0, "WatchView");
 
+	if (launch_daemon) {
 		qDebug() << "Starting salmeta (daemon) with settings from" << settings_key_prefix;
 
 		new Controller(settings_key_prefix, SailfishApp::createView());
 	} else {
 		QQuickView *view = SailfishApp::createView();
+		view->rootContext()->setContextProperty("settingsPrefix", settings_key_prefix);
 		view->setSource(SailfishApp::pathTo("qml/salmeta.qml"));
 		view->show();
 	}
diff --git a/src/watchviewitem.cpp b/src/watchviewitem.cpp
new file mode 100644
index 0000000..dbd4e8a
--- /dev/null
+++ b/src/watchviewitem.cpp
@@ -0,0 +1,15 @@
+#include <QtCore/QDebug>
+#include <QtGui/QPainter>
+
+#include "watchviewitem.h"
+
+WatchViewItem::WatchViewItem(QQuickItem *parent) :
+	QQuickPaintedItem(parent)
+{
+	setFillColor(Qt::white);
+}
+
+void WatchViewItem::paint(QPainter *painter)
+{
+
+}
diff --git a/src/watchviewitem.h b/src/watchviewitem.h
new file mode 100644
index 0000000..5219852
--- /dev/null
+++ b/src/watchviewitem.h
@@ -0,0 +1,20 @@
+#ifndef WATCHVIEWITEM_H
+#define WATCHVIEWITEM_H
+
+#include <QtQuick/QQuickPaintedItem>
+
+class WatchViewItem : public QQuickPaintedItem
+{
+	Q_OBJECT
+public:
+	explicit WatchViewItem(QQuickItem *parent = 0);
+
+	void paint(QPainter *painter);
+
+signals:
+
+public slots:
+
+};
+
+#endif // WATCHVIEWITEM_H
-- 
cgit v1.2.3