summaryrefslogtreecommitdiff
path: root/saltoqd/toqmanager.cpp
blob: c3199390c0de3af5ee6cda8f674483dc77a71484 (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
#include <QtDBus/QDBusConnection>

#include "toqmanager.h"
#include "toqmanageradaptor.h"

#include "obexconnection.h"

#include "versionmanager.h"
#include "systemmanager.h"
#include "fmsmanager.h"
#include "storagemanager.h"
#include "settingsmanager.h"
#include "musicmanager.h"
#include "contactsmanager.h"
#include "commmanager.h"
#include "agendamanager.h"
#include "voicecallmanager.h"
#include "weathermanager.h"
#include "notificationmanager.h"

static const bool PROTO_DEBUG = true;

ToqManager::ToqManager(Settings *settings, QObject *parent) :
	QObject(parent),
	_settings(settings),
	_conn(new ToqConnection(this)),
	_obex(new ObexConnection(_conn, this)),
	_versionManager(new VersionManager(this)),
	_systemManager(new SystemManager(this)),
	_fmsManager(new FmsManager(_obex, this)),
	_storageManager(new StorageManager(_obex, this)),
	_settingsManager(new SettingsManager(_settings, _fmsManager, this)),
	_musicManager(new MusicManager(this)),
	_contactsManager(new ContactsManager(_storageManager, this)),
	_commManager(new CommManager(_settings, _storageManager, _contactsManager, this)),
	_agendaManager(new AgendaManager(_storageManager, this)),
	_voiceCallManager(new VoiceCallManager(_contactsManager, this)),
	_weatherManager(new WeatherManager(_fmsManager, this)),
	_cardManager(new CardManager(_fmsManager, this)),
	_notificationManager(new NotificationManager(_cardManager, this))
{
	connect(_conn, &ToqConnection::messageReceived,
			this, &ToqManager::handleToqMessage);
	connect(_conn, &ToqConnection::connected,
			this, &ToqManager::connected);
	connect(_conn, &ToqConnection::disconnected,
			this, &ToqManager::disconnected);
	connect(_conn, &ToqConnection::connectedChanged,
			this, &ToqManager::connectedChanged);
	connect(_settings, &Settings::addressChanged,
			this, &ToqManager::handleSettingsAddressChanged);

	_conn->setAddress(QBluetoothAddress(_settings->address()));

	new ToqManagerAdaptor(_fmsManager, this);
	QDBusConnection::sessionBus().registerObject("/com/javispedro/saltoq/ToqManager", this);
}

void ToqManager::setEndpointListener(ToqConnection::Endpoint ep, EndpointHandler *handler)
{
	Q_ASSERT(!_handlers.contains(ep));
	_handlers.insert(ep, handler);
}

void ToqManager::sendMessage(const ToqConnection::Message &msg)
{
	if (PROTO_DEBUG) {
		QString content = QString::fromUtf8(msg.payload);
		qDebug() << "Sending message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.source) << "type" << msg.type << content;
	}
	_conn->sendMessage(msg);
}

void ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint16 transactionId, quint32 type, const QByteArray &payload)
{
	ToqConnection::Message msg(source, destination, transactionId, type, payload);
	sendMessage(msg);
}

quint16 ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint32 type, const QByteArray &payload)
{
	quint16 transactionId = newTransactionId();
	ToqConnection::Message msg(source, destination, transactionId, type, payload);
	sendMessage(msg);
	return transactionId;
}

void ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint16 transactionId, quint32 type, const QJsonObject &payload)
{
	QJsonDocument doc(payload);
	ToqConnection::Message msg(source, destination, transactionId, type, doc);
	sendMessage(msg);
}

quint16 ToqManager::sendMessage(ToqConnection::Endpoint source, ToqConnection::Endpoint destination, quint32 type, const QJsonObject &payload)
{
	QJsonDocument doc(payload);
	quint16 transactionId = newTransactionId();
	ToqConnection::Message msg(source, destination, transactionId, type, doc);
	sendMessage(msg);
	return transactionId;
}

void ToqManager::sendReply(const ToqConnection::Message &msg, quint32 type, const QJsonObject &payload)
{
	ToqConnection::Message reply(msg.destination, msg.source, msg.transactionId, type, QJsonDocument(payload));
	sendMessage(reply);
}

void ToqManager::handleToqMessage(const ToqConnection::Message &msg)
{
	EndpointHandler *handler = _handlers.value(msg.destination, 0);

	if (PROTO_DEBUG) {
		QString content = QString::fromUtf8(msg.payload);
		qDebug() << "Received message to" << ToqConnection::nameOfEndpoint(msg.destination) << "from" << ToqConnection::nameOfEndpoint(msg.destination) << "type" << msg.type << content;
	}

	if (handler) {
		handler->handleMessage(msg);
	} else {
		qWarning() << "No registered handler for endpoint" << ToqConnection::nameOfEndpoint(msg.destination);
	}
}

void ToqManager::handleSettingsAddressChanged()
{
	_conn->setAddress(QBluetoothAddress(_settings->address()));
}