summaryrefslogtreecommitdiff
path: root/libsowatch/watchserver.cpp
blob: d95d4fb217449a8105b816eb2d023e1e735028c1 (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
#include <QtCore/QDebug>

#include "notificationprovider.h"
#include "watch.h"
#include "watchlet.h"
#include "watchserver.h"


using namespace sowatch;

WatchServer::WatchServer(Watch* watch, QObject* parent) :
	QObject(parent), _watch(watch), _currentWatchlet(0)
{
	connect(_watch, SIGNAL(connected()), SLOT(watchConnected()));
	connect(_watch, SIGNAL(disconnected()), SLOT(watchDisconnected()));
	connect(_watch, SIGNAL(idling()), SLOT(watchIdling()));
}

Watch* WatchServer::watch()
{
	return _watch;
}

void WatchServer::addProvider(NotificationProvider *provider)
{
	provider->setParent(this);
	connect(provider, SIGNAL(incomingNotification(Notification*)), SLOT(notificationReceived(Notification*)));
	// And that's it, really.
}

void WatchServer::runWatchlet(const QString& id)
{
	if (_currentWatchlet) {
		closeWatchlet();
	}
	_currentWatchlet = _watchlets[id];
	if (_watch->isConnected()) {
		reactivateCurrentWatchlet();
	}
}

void WatchServer::closeWatchlet()
{
	Q_ASSERT(_currentWatchlet != 0);
	if (_watch->isConnected()) {
		_currentWatchlet->deactivate();
	}
	_currentWatchlet = 0;
}

void WatchServer::registerWatchlet(Watchlet *watchlet)
{
	Q_ASSERT(watchlet->_server == this);
	_watchlets[watchlet->id()] = watchlet;
}

void WatchServer::reactivateCurrentWatchlet()
{
	Q_ASSERT(_currentWatchlet != 0);
	_watch->displayApplication();
	_currentWatchlet->activate();
}

void WatchServer::nextNotification()
{
	if (!_watch->isConnected()) return;
	if (!_pendingNotifications.empty()) {
		Notification *n = _pendingNotifications.head();
		_watch->displayNotification(n);
	} else if (_currentWatchlet) {
		reactivateCurrentWatchlet();
	} else {
		_watch->displayIdleScreen();
	}
}

uint WatchServer::getNotificationCount(Notification::Type type)
{
	uint count = 0;
	foreach (Notification* n, _notifications[type]) {
		count += n->count();
	}
	return count;
}

void WatchServer::watchConnected()
{
	if (!_pendingNotifications.isEmpty()) {
		nextNotification();
	} else if (_currentWatchlet) {
		reactivateCurrentWatchlet();
	}
}

void WatchServer::watchDisconnected()
{
	if (_currentWatchlet) {
		_currentWatchlet->deactivate();
	}
	_pendingNotifications.clear();
}

void WatchServer::watchIdling()
{
	qDebug() << "Watch idling";
	if (!_pendingNotifications.empty()) {
		_pendingNotifications.dequeue();
		nextNotification();
	}
}

void WatchServer::notificationReceived(Notification *notification)
{
	const Notification::Type type = notification->type();
	_notifications[type].append(notification);

	connect(notification, SIGNAL(changed()), SLOT(notificationChanged()));
	connect(notification, SIGNAL(cleared()), SLOT(notificationCleared()));

	qDebug() << "notification received" << notification->title() << notification->count();

	_watch->updateNotificationCount(type, getNotificationCount(type));
	if (_pendingNotifications.isEmpty()) {
		_pendingNotifications.enqueue(notification);
		nextNotification();
	} else if (type == Notification::CallNotification) {
		// Oops, priority!!!!
		_pendingNotifications.prepend(notification);
		nextNotification();
	} else {
		_pendingNotifications.enqueue(notification);
	}
}

void WatchServer::notificationChanged()
{
	QObject *obj = sender();
	if (obj) {
		Notification* n = static_cast<Notification*>(obj);
		const Notification::Type type = n->type();

		qDebug() << "notification changed" << n->title() << n->count();

		_watch->updateNotificationCount(type, getNotificationCount(type));
		if (!_pendingNotifications.isEmpty() && _pendingNotifications.head() == n) {
			nextNotification();
		}
	}
}

void WatchServer::notificationCleared()
{
	QObject *obj = sender();
	if (obj) {
		Notification* n = static_cast<Notification*>(obj);
		const Notification::Type type = n->type();
		_notifications[type].removeOne(n);

		qDebug() << "notification deleted" << n->title() << n->count();

		_watch->updateNotificationCount(type, getNotificationCount(type));
		if (!_pendingNotifications.isEmpty() && _pendingNotifications.head() == n) {
			_pendingNotifications.removeAll(n);
			nextNotification();
		} else {
			_pendingNotifications.removeAll(n);
		}
	}
}