summaryrefslogtreecommitdiff
path: root/libsowatch/declarativewatchwrapper.cpp
blob: 122545f2214c3fe5afee12aed4a6b2c3c536e6b6 (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
#include <QtCore/QDebug>
#include "watchserver.h"
#include "watch.h"
#include "notification.h"
#include "declarativewatchwrapper.h"

using namespace sowatch;

DeclarativeWatchWrapper::DeclarativeWatchWrapper(WatchServer* server, Watch* watch, QObject* parent) :
	QObject(parent), _server(server), _watch(watch),
	_active(false)
{

}

QString DeclarativeWatchWrapper::model() const
{
	return _watch->model();
}

bool DeclarativeWatchWrapper::active() const
{
	return _active;
}

QList<QObject*> DeclarativeWatchWrapper::notifications() const
{
	// TODO: Figure out a better way for this; QAbstractListModel, etc.
	QList<Notification*> nl = _server->liveNotifications();
	QList<QObject*> ol;
	foreach (Notification* n, nl) {
		QObject * o = n;
		ol.append(o);
	}
	qDebug() << "notifications to declarative: " << ol;
	return ol;
}

void DeclarativeWatchWrapper::useButton(int button)
{
	if (!_usedButtons.contains(button)) {
		_usedButtons.insert(button);
		if (_active) {
			_watch->grabButton(button);
		}
	}
}

void DeclarativeWatchWrapper::vibrate(int msecs)
{
	if (_active) {
		_watch->vibrate(msecs);
	}
}

void DeclarativeWatchWrapper::activate()
{
	if (!_active) {
		_active = true;

		// Grab all of the buttons used by this watchlet
		foreach (int button, _usedButtons) {
			_watch->grabButton(button);
		}

		// Forward the button signals
		connect(_watch, SIGNAL(buttonPressed(int)), this, SIGNAL(buttonPressed(int)));
		connect(_watch, SIGNAL(buttonReleased(int)), this, SIGNAL(buttonReleased(int)));

		// Emit the active signal
		emit activeChanged();

		// Since a notification currently causes the active watchlet to be deactivated,
		// we can assume the notifications list only changes when we are deactivated.
		emit notificationsChanged();
	}
}

void DeclarativeWatchWrapper::deactivate()
{
	if (_active) {
		_active = false;

		// Stop forwarding button presses
		disconnect(_watch, 0, this, 0);

		// Ungrab all the buttons used by this watchlet
		foreach (int button, _usedButtons) {
			_watch->ungrabButton(button);
		}

		// Emit the deactivated signal
		emit activeChanged();
	}
}