summaryrefslogtreecommitdiff
path: root/libsowatch/watchserver.cpp
blob: 8d00088d6113d4844066d5e6946af60bc0ecf6ed (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
#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()));
}

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

void WatchServer::addProvider(NotificationProvider *provider)
{
	provider->setParent(this);

	connect(provider, SIGNAL(notification(Notification)), SLOT(notificationEmitted(Notification)));
	connect(provider, SIGNAL(unreadCountChanged(Notification::Type)), SLOT(unreadCountUpdated(Notification::Type)));

	_providers.append(provider);
}

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

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::watchConnected()
{
	if (_currentWatchlet) {
		_currentWatchlet->activate();
	}
}

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

void WatchServer::notificationEmitted(const Notification &notification)
{
	// TODO app loses button focus...
	_watch->showNotification(notification);
}

void WatchServer::unreadCountUpdated(Notification::Type type)
{
	uint count = 0;
	foreach(NotificationProvider* provider, _providers)
	{
		count += provider->getCount(type);
	}
	_watch->updateNotificationCount(type, count);
}