summaryrefslogtreecommitdiff
path: root/src/controller.cpp
blob: 745f92186d864d328faeb528f94059482b0277ad (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <QtCore/QDebug>
#include <QtCore/QDateTime>
#include <QtGui/QPainter>

#include <sailfishapp.h>

#include "controller.h"

static const QLatin1String setting_address("address");
static const QLatin1String setting_transport("transport");
static const QLatin1String setting_cur_page("cur-page");
static const QLatin1String setting_24h_mode("24h-mode");
static const QLatin1String setting_ddmm_mode("ddmm-mode");
static const QLatin1String setting_seconds("show-seconds");
static const QLatin1String setting_separation_lines("show-separation-lines");
static const QLatin1String setting_auto_backlight("auto-backlight");

Controller::Controller(const QString &settingsPrefix, QQuickView *view, QObject *parent) :
	QObject(parent),
	_settings(new MDConfGroup(this)),
	_metawatch(0),
	_reconnect(new ReconnectTimer(this)),
	_monitor(NotificationMonitor::instance()),
	_widgets(new WidgetInfoModel(settingsPrefix, this)),
	_curMode(MetaWatch::WatchModeIdle), _curPage(0),
	_batteryCharge(0), _batteryCharging(false)
{
	_settings->setPath(settingsPrefix);
	_curPage = _settings->value(setting_cur_page).toInt();

	connect(_settings, &MDConfGroup::valueChanged, this, &Controller::handleSettingChanged);
	connect(_monitor, &NotificationMonitor::incomingNotification, this, &Controller::handleIncomingNotification);
	connect(_widgets, &QAbstractItemModel::dataChanged, this, &Controller::handleWidgetChanged);

	connectToDevice();
}

Controller::~Controller()
{
}

bool Controller::isWatchConnected() const
{
	return _metawatch && _metawatch->isDeviceConnected();
}

MetaWatch::WatchMode Controller::mode() const
{
	return _curMode;
}

int Controller::page() const
{
	return _curPage;
}

void Controller::setPage(int page)
{
	if (page != _curPage) {
		_curPage = page;
		if (isWatchConnected()) {
			_metawatch->updateLcdDisplayPage(_curPage);
		}
		emit pageChanged();
	}
}

int Controller::batteryCharge() const
{
	return _batteryCharge;
}

bool Controller::batteryCharging() const
{
	return _batteryCharging;
}

void Controller::connectToDevice()
{
	const QString address = _settings->value(setting_address).toString();
	const QString transport_name = _settings->value(setting_transport).toString();

	MetaWatch::TransportType transport;

	if (transport_name.compare("bluetooth", Qt::CaseInsensitive) == 0) {
		qDebug() << "Using Bluetooth (RFCOMM)";
		transport = MetaWatch::TransportBluetooth;
	} else {
		// Default to BLE
		qDebug() << "Using Bluetooth Low Energy (GATT)";
		transport = MetaWatch::TransportBluetoothLowEnergy;
	}

	if (_metawatch) {
		if (address == _address) {
			// Connecting to the same address and there's already a watch object created
			return;
		}

		_reconnect->stop();
		disconnect(_metawatch, 0, this, 0);
		disconnect(_metawatch, 0, _reconnect, 0);
		delete _metawatch;
		_metawatch = 0;
	}

	_address = address;

	if (_address.isEmpty()) {
		qDebug() << "Empty address, will do nothing until address is set in config";
		return;
	}

	_metawatch = new MetaWatch(_address, transport, this);
	connect(_metawatch, &MetaWatch::connected, _reconnect, &ReconnectTimer::stop);
	connect(_metawatch, &MetaWatch::connected,  this, &Controller::handleMetaWatchConnected);
	connect(_metawatch, &MetaWatch::disconnected, _reconnect, &ReconnectTimer::scheduleNextAttempt);
	connect(_metawatch, &MetaWatch::modeChange, this, &Controller::handleMetaWatchModeChange);
	connect(_metawatch, &MetaWatch::batteryStatus, this, &Controller::handleMetaWatchBatteryStatus);
	connect(_reconnect, &ReconnectTimer::tryReconnect, _metawatch, &MetaWatch::connectDevice);

	qDebug() << "Scheduling initial connection attempt to" << _address;

	_reconnect->scheduleNextAttempt();
}

void Controller::updateProperties()
{
	MetaWatch::WatchProperties props;

	if (!isWatchConnected()) return;

	if (_settings->value(setting_24h_mode).toBool()) {
		props |= MetaWatch::WatchPropertyHourFormat24h;
	}

	if (_settings->value(setting_ddmm_mode).toBool()) {
		props |= MetaWatch::WatchPropertyDateFormatDDMM;
	}

	if (_settings->value(setting_seconds).toBool()) {
		props |= MetaWatch::WatchPropertyShowSeconds;
	}

	if (_settings->value(setting_separation_lines).toBool()) {
		props |= MetaWatch::WatchPropertyShowSeparationLines;
	}

	if (_settings->value(setting_auto_backlight).toBool()) {
		props |= MetaWatch::WatchPropertyAutoBacklight;
	}

	_metawatch->configure(props);
}

void Controller::handleSettingChanged(const QString &key)
{
	qDebug() << "Setting" << key << "changed";
	if (key == setting_address) {
		connectToDevice();
	} else if (key == setting_cur_page) {
		int page = _settings->value(setting_cur_page).toInt();
		if (_curPage != page) {
			setPage(page);
		}
	} else if (key == setting_24h_mode || key == setting_ddmm_mode || key == setting_seconds
			   || key == setting_separation_lines || key == setting_auto_backlight) {
		updateProperties();
	}
}

void Controller::handleMetaWatchConnected()
{
	qDebug() << "MetaWatch connected, synchronizing date time";

	_metawatch->updateDeviceType();
	_metawatch->updateBatteryStatus();
	_metawatch->setDateTime(QDateTime::currentDateTime());
	_metawatch->updateWidgetList(_widgets->toList());
	_metawatch->updateLcdDisplayPage(_curPage);

	_curMode = MetaWatch::WatchModeIdle;

	updateProperties();
}

void Controller::handleMetaWatchModeChange(MetaWatch::WatchMode mode, int page)
{
	switch (mode) {
	case MetaWatch::WatchModeIdle:
		if (_curMode != mode) {
			_curMode = mode;
			emit modeChanged();
		}
		if (page != _curPage) {
			_curPage = page;
			_settings->setValue(setting_cur_page, QVariant::fromValue(_curPage));
			emit pageChanged();
		}

		break;
	case MetaWatch::WatchModeNotification:
		if (_curMode != mode) {
			_curMode = mode;
			emit modeChanged();
		}
		// Watch will often go back to the same page once it returns to idle mode,
		// so let's not clobber _curPage.
		break;
	default:
		qWarning() << "Unhandled metawatch mode:" << mode;
		break;
	}
}

void Controller::handleMetaWatchBatteryStatus(bool charging, int charge)
{
	if (_batteryCharging != charging) {
		_batteryCharging = charging;
		emit batteryChargingChanged();
	}
	if (_batteryCharge != charge) {
		_batteryCharge = charge;
		emit batteryChargeChanged();
	}
}

void Controller::handleIncomingNotification(const QString &sender, const QIcon &icon, const QString &summary, int count, const QString &body, const QDateTime &dateTime)
{
	if (!isWatchConnected()) {
		return; // Ignoring notification if metawatch is not connected.
	}

	QImage image(96, 96, QImage::Format_MonoLSB);
	QPainter p(&image);

	QFont small("MetaWatch Small caps 8pt");
	small.setPixelSize(8);
	QFont large("MetaWatch Large 16pt");
	large.setPixelSize(16);

	// Render notification background
	p.drawImage(0, 0, QImage(SailfishApp::pathTo("qml/watch/notification.png").toLocalFile()));

	// Render the associated icon on the bottom left corner
	icon.paint(&p, 0, 96 - 24, 28, 24, Qt::AlignLeft | Qt::AlignBottom);

	QString title = summary;
	bool title_drawn_fully = false;
	if (count > 1) {
		title = QString("%1 %2").arg(count).arg(summary);
	}

	// Render title area
	p.setFont(large);
	if (!title.isEmpty()) {
		QRect area(2, 2, 96 - 18, 18);
		QRect r;
		p.drawText(area,
				   Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine,
				   title, &r);
		if (area.contains(r)) {
			title_drawn_fully = true;
		}
	}

	const int x = 2, max_x = 96;
	const int max_y = 96 - 24;
	int y = 26;

	// Now render the contents
	// If the title did not fit in the title area, also draw it fully here.
	p.setFont(small);
	if (!title_drawn_fully && !title.isEmpty()) {
		QRect r;
		p.drawText(QRect(x, y, max_x - x, max_y - y),
				   Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
				   title, &r);
		y = r.bottom() + 1;
	}
	if (!body.isEmpty() && y < max_y) {
		QRect r;
		p.drawText(QRect(x, y, max_x - x, max_y - y),
				   Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap,
				   body, &r);
		y = r.bottom() + 1;
	}

	image.invertPixels();
	_metawatch->clearModeImage(MetaWatch::WatchModeNotification);
	_metawatch->sendModeImage(MetaWatch::WatchModeNotification, image);
	_metawatch->updateLcdDisplayMode(MetaWatch::WatchModeNotification);
	_metawatch->setVibrateMode(true, 500, 500, 2);
}

void Controller::handleWidgetChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
	if (!isWatchConnected()) return;

	if (roles.indexOf(WidgetInfoModel::UrlRole) >= 0) {
		// Can't send partial updates to watch, so entire list must be
		// resubmitted everytime something important changes
		_metawatch->updateWidgetList(_widgets->toList());

		// If the changed widget is on the current page, watch needs to refresh.
		if (_widgets->data(topLeft, WidgetInfoModel::PageRole).toInt() == _curPage) {
			_metawatch->updateLcdDisplayPage(_curPage);
		}
	}
}