summaryrefslogtreecommitdiff
path: root/saltoqd/voicecallmanager.cpp
blob: 79cfcf69ae34288e2c94eac950f677a04764fd80 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <QtDBus/QDBusConnection>
#include "contactsmanager.h"
#include "voicecallmanager.h"
#include "voicecallmanager_interface.h"
#include "voicecall_interface.h"
#include "profiled_interface.h"

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

enum VoiceCallStatus {
	STATUS_NULL,
	STATUS_ACTIVE,
	STATUS_HELD,
	STATUS_DIALING,
	STATUS_ALERTING,
	STATUS_INCOMING,
	STATUS_WAITING,
	STATUS_DISCONNECTED
};

VoiceCallManager::VoiceCallManager(ContactsManager *contacts, ToqManager *toq) :
	QObject(toq), _toq(toq), _contacts(contacts),
	_activeCall(0)
{
	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);
	connect(vcm, &OrgNemomobileVoicecallVoiceCallManagerInterface::activeVoiceCallChanged,
			this, &VoiceCallManager::handleActiveVoiceCallChanged);

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

void VoiceCallManager::handleMessage(const ToqConnection::Message &msg)
{
	Q_ASSERT(msg.destination == ToqConnection::VoiceCallEndpoint);
	switch (msg.type) {
	case 0: // Call number?
		if (vcm) {
			QStringList providers = vcm->providers();
			if (providers.isEmpty()) {
				sendReply(msg, 3, "No service");
				return;
			}
			QString provider = providers.first().split(":").at(0);
			QString number = msg.toJson().object().value("dial_number").toString();
			if (vcm->dial(provider, number)) {
				sendReply(msg, 0, "Succesfully dialed");
			} else {
				sendReply(msg, 1, "Wrong number");
			}
		}
		break;
	case 1: // Redial?
		// TODO
		sendReply(msg, 1, "Could not redial");
		break;
	case 4: // Cancel call
		if (_activeCall && _activeCall->hangup()) {
			sendReply(msg, 0, "Succesfully ended");
		} else {
			sendReply(msg, 1, "Could not cancel call");
		}
		break;
	case 5: // Answer call
		if (_activeCall && _activeCall->answer()) {
			sendReply(msg, 0, "Succesfully answered");
		} else {
			sendReply(msg, 1, "Could not answer call");
		}
		break;
	case 6: // Reject call
		if (_activeCall && _activeCall->hangup()) {
			sendReply(msg, 0, "Succesfully rejected");
		} else {
			sendReply(msg, 1, "Could not reject call");
		}
		break;
	case 7: // End call
		if (_activeCall && _activeCall->hangup()) {
			sendReply(msg, 0, "Succesfully ended");
		} else {
			sendReply(msg, 1, "Could not end call");
		}
		break;
	case 9: // Speaker volume up
	case 10: // Speaker volume down
		// TODO
		sendReply(msg, 1, "TODO");
		break;
	case 11: // Mic mute
		vcm->setIsMicrophoneMuted(true);
		sendReply(msg, 0, "Succesfully muted");
		break;
	case 12: // Mic unmute
		vcm->setIsMicrophoneMuted(false);
		sendReply(msg, 0, "Succesfully unmuted");
		break;
	case 15: // Phone status
		_toq->sendReply(msg, 0x400F, buildPhoneStatus());
		break;
	default:
		qWarning() << "Unknown message" << msg.type;
		break;
	}
}

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

void VoiceCallManager::sendReply(const ToqConnection::Message &msg, int status, const QString &message)
{
	QJsonObject obj;
	obj.insert("result", status);
	obj.insert("description", message);
	_toq->sendReply(msg, 0x4000 | msg.type, obj);
}

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();
	}
}

QJsonObject VoiceCallManager::buildPhoneStatus()
{
	QJsonObject obj;
	obj.insert("service", int(1)); // TODO
	int call_status = 0;
	int call_setup_status = 0;
	int call_held = 0;
	if (_activeCall) {
		switch (_activeCall->status()) {
		case STATUS_ACTIVE:
			call_status = 1;
			break;
		case STATUS_HELD:
			call_status = 1;
			call_held = 1;
			break;
		case STATUS_DIALING:
		case STATUS_ALERTING:
			call_status = 1;
			break;
		case STATUS_INCOMING:
			call_status = 0;
			call_setup_status = 1;
			break;
		case STATUS_WAITING:
			call_status = 1;
			call_setup_status = 0;
			break;
		case STATUS_NULL:
		case STATUS_DISCONNECTED:
		default:
			call_status = 0;
			call_setup_status = 0;
			break;
		}

		QString phoneNumber = _activeCall->lineId();
		auto callerId = _contacts->findNameTypeForPhoneNumber(phoneNumber);
		obj.insert("caller_id", phoneNumber);
		obj.insert("caller_name", callerId.name);
		obj.insert("caller_phone_type", callerId.type);
		obj.insert("privileged", callerId.favorite);
		QDateTime callTime = _activeCall->startedAt();
		if (callTime.isValid()) {
			obj.insert("call_time", qint64(_activeCall->startedAt().toTime_t()));
		}
	}

	obj.insert("call_status", call_status);
	obj.insert("call_setup_status", call_setup_status);
	obj.insert("call_held", call_held);
	obj.insert("silence_mode", getCurrentProfile() == "silent" ? 1 : 0);
	obj.insert("mic_mute", vcm->isMicrophoneMuted() ? 1 : 0);

	return obj;
}

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

void VoiceCallManager::sendPhoneRingMessage()
{
	QJsonObject obj;

	if (_activeCall) {
		QString phoneNumber = _activeCall->lineId();
		auto callerId = _contacts->findNameTypeForPhoneNumber(phoneNumber);
		obj.insert("caller_id_present", phoneNumber.isEmpty() ? 0 : 1);
		obj.insert("caller_id", phoneNumber);
		obj.insert("caller_name", callerId.name);
		obj.insert("caller_phone_type", callerId.type);
		obj.insert("privileged", callerId.favorite);
	}

	_toq->sendMessage(ToqConnection::VoiceCallEndpoint, ToqConnection::VoiceCallEndpoint + 1,
					  0x8001, obj);
}

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

void VoiceCallManager::handleActiveVoiceCallChanged()
{
	if (_activeCall) {
		delete _activeCall;
	}
	QString id = vcm->activeVoiceCall();
	if (!id.isEmpty()) {
		_activeCall = new OrgNemomobileVoicecallVoiceCallInterface("org.nemomobile.voicecall",
																   QString("/calls/%1").arg(id),
																   vcm->connection(),
																   this);
		connect(_activeCall, &OrgNemomobileVoicecallVoiceCallInterface::statusChanged,
				this, &VoiceCallManager::handleActiveVoiceCallStatusChanged);
	} else {
		_activeCall = 0;
	}
	sendPhoneStatusMessage();
}

void VoiceCallManager::handleActiveVoiceCallStatusChanged()
{
	int status = _activeCall->status();
	qDebug() << "Status changed" << status << _activeCall->statusText();
	if (status == STATUS_INCOMING) {
		sendPhoneRingMessage();
	}
	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;
}