summaryrefslogtreecommitdiff
path: root/saltoqd/voicecallmanager.cpp
blob: d2c5c52e9c839de303f1008d59e3a34a38502e74 (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
#include <QtDBus/QDBusConnection>
#include "voicecallmanager.h"
#include "voicecallmanager_interface.h"
#include "profiled_interface.h"

static ComNokiaProfiledInterface *profiled = NULL;
static OrgNemomobileVoicecallVoiceCallManagerInterface *vcm = NULL;

VoiceCallManager::VoiceCallManager(ToqManager *toq) :
	QObject(toq), _toq(toq)
{
	if (!profiled) {
		qDBusRegisterMetaType<ProfileValue>();
		qDBusRegisterMetaType< QList<ProfileValue> >();

		profiled = new ComNokiaProfiledInterface("com.nokia.profiled",
												 "/com/nokia/profiled",
												 QDBusConnection::sessionBus());
	}
	if (!vcm) {
		vcm = new OrgNemomobileVoicecallVoiceCallManagerInterface("org.nemomobile.voicecall",
																  "/",
																  QDBusConnection::sessionBus());
	}

	connect(profiled, &ComNokiaProfiledInterface::profile_changed,
			this, &VoiceCallManager::handleProfileChanged);

	_toq->setEndpointListener(ToqConnection::VoiceCallEndpoint, this);
}

void VoiceCallManager::handleMessage(const ToqConnection::Message &msg)
{
	Q_ASSERT(msg.destination == ToqConnection::VoiceCallEndpoint);
	switch (msg.type) {
	case 15:
		handleGetPhoneStatusMessage(msg);
		break;
	default:
		qWarning() << "Unknown message" << msg.type;
		break;
	}
}

void VoiceCallManager::setSilentMode(bool silent)
{
	setProfile(silent ? "silent" : "ambience");
}

void VoiceCallManager::handleGetPhoneStatusMessage(const ToqConnection::Message &msg)
{
	_toq->sendReply(msg, 0x400F, buildPhoneStatus());
}

QString VoiceCallManager::getCurrentProfile()
{
	QDBusReply<QString> resp = profiled->get_profile();

	if (resp.isValid()) {
		return resp.value();
	} else {
		qWarning() << resp.error().message();
		return QString();
	}
}

void VoiceCallManager::setProfile(const QString &name)
{
	QDBusReply<bool> resp = profiled->set_profile(name);

	if (!resp.isValid()) {
		qWarning() << resp.error().message();
	}
}

void VoiceCallManager::sendPhoneStatusMessage()
{
	_toq->sendMessage(ToqConnection::VoiceCallEndpoint, ToqConnection::VoiceCallEndpoint + 1,
					  0x8000, buildPhoneStatus());
}

QJsonObject VoiceCallManager::buildPhoneStatus()
{
	QJsonObject obj;
	obj.insert("service", int(1));
	obj.insert("call_status", int(0));
	obj.insert("call_setup_status", int(0));
	obj.insert("silence_mode", getCurrentProfile() == "silent" ? 1 : 0);
	return obj;
}

void VoiceCallManager::handleProfileChanged(bool changed, bool active, const QString &profile)
{
	Q_UNUSED(changed);
	Q_UNUSED(active);
	qDebug() << "Profile changed to" << profile;
	sendPhoneStatusMessage();
}

QDBusArgument &operator<<(QDBusArgument &argument, const ProfileValue &value)
{
	argument.beginStructure();
	argument << value.key << value.val << value.type;
	argument.endStructure();
	return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, ProfileValue &value)
{
	argument.beginStructure();
	argument >> value.key >> value.val >> value.type;
	argument.endStructure();
	return argument;
}