client-group-chat-room.cpp 15.3 KB
Newer Older
1 2
/*
 * client-group-chat-room.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20 21
#include "linphone/utils/utils.h"

22
#include "address/address-p.h"
23
#include "c-wrapper/c-wrapper.h"
24
#include "client-group-chat-room-p.h"
25
#include "conference/participant-p.h"
26 27 28
#include "conference/remote-conference-event-handler.h"
#include "conference/remote-conference-p.h"
#include "conference/session/call-session-p.h"
29
#include "core/core-p.h"
30
#include "event-log/events.h"
31
#include "logger/logger.h"
32
#include "sal/refer-op.h"
33

Ronan's avatar
Ronan committed
34
// =============================================================================
35

36 37 38 39
using namespace std;

LINPHONE_BEGIN_NAMESPACE

40 41
// -----------------------------------------------------------------------------

42 43 44 45 46 47
list<Address> ClientGroupChatRoomPrivate::cleanAddressesList (const list<Address> &addresses) const {
	L_Q();
	list<Address> cleanedList(addresses);
	cleanedList.sort();
	cleanedList.unique();
	for (auto it = cleanedList.begin(); it != cleanedList.end();) {
48
		if (q->findParticipant(*it) || (q->getMe()->getAddress() == SimpleAddress(*it)))
49 50 51 52 53 54 55
			it = cleanedList.erase(it);
		else
			it++;
	}
	return cleanedList;
}

56 57
shared_ptr<CallSession> ClientGroupChatRoomPrivate::createSession () {
	L_Q();
58
	L_Q_T(RemoteConference, qConference);
59

60 61
	CallSessionParams csp;
	csp.addCustomHeader("Require", "recipient-list-invite");
62

63
	shared_ptr<Participant> focus = qConference->getPrivate()->focus;
64 65
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, q);
	const Address &myAddress = q->getMe()->getAddress();
66
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myAddress, focus->getContactAddress());
67
	session->initiateOutgoing();
68 69

	Address addr = myAddress;
70 71 72 73 74
	addr.setParam("text");
	session->getPrivate()->getOp()->set_contact_address(addr.getPrivate()->getInternalAddress());
	return session;
}

75
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
76 77
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->notifyReceived(body);
78 79
}

80 81
// =============================================================================

82
ClientGroupChatRoom::ClientGroupChatRoom (
83
	const std::shared_ptr<Core> &core,
84
	const Address &me,
85 86 87
	const std::string &uri,
	const std::string &subject
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, me), RemoteConference(core->getCCore(), me, nullptr) {
88
	L_D_T(RemoteConference, dConference);
Ronan's avatar
Ronan committed
89
	dConference->focus = make_shared<Participant>(Address(uri));
90
	RemoteConference::setSubject(subject);
91 92
}

93 94 95
int ClientGroupChatRoom::getCapabilities () const {
	return static_cast<int>(Capabilities::Conference);
}
96

97 98 99 100
void ClientGroupChatRoom::addParticipant (const Address &addr, const CallSessionParams *params, bool hasMedia) {
	list<Address> addresses;
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
101 102
}

103 104 105 106 107
void ClientGroupChatRoom::addParticipants (
	const list<Address> &addresses,
	const CallSessionParams *params,
	bool hasMedia
) {
108
	L_D();
109
	L_D_T(RemoteConference, dConference);
110

111 112
	list<Address> addressesList = d->cleanAddressesList(addresses);
	if (addressesList.empty())
113
		return;
114

115 116 117 118
	if ((d->state != ChatRoom::State::Instantiated) && (d->state != ChatRoom::State::Created)) {
		lError() << "Cannot add participants to the ClientGroupChatRoom in a state other than Instantiated or Created";
		return;
	}
119

120
	Content content;
121
	content.setBody(getResourceLists(addressesList));
122 123
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
124

125
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
126
	if (session)
127
		session->update(nullptr, getSubject(), &content);
128 129
	else {
		session = d->createSession();
130 131
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
132
			d->setState(ChatRoom::State::CreationPending);
133
	}
134 135
}

136
bool ClientGroupChatRoom::canHandleParticipants () const {
137
	return RemoteConference::canHandleParticipants();
138 139
}

140 141 142 143
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

144
const Address &ClientGroupChatRoom::getConferenceAddress () const {
145
	return RemoteConference::getConferenceAddress();
146 147
}

148 149 150 151
shared_ptr<Participant> ClientGroupChatRoom::getMe () const {
	return RemoteConference::getMe();
}

152
int ClientGroupChatRoom::getNbParticipants () const {
153
	return RemoteConference::getNbParticipants();
154 155 156
}

list<shared_ptr<Participant>> ClientGroupChatRoom::getParticipants () const {
157
	return RemoteConference::getParticipants();
158 159
}

160 161 162 163
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

164 165
void ClientGroupChatRoom::join () {
	L_D();
166 167 168 169
	L_D_T(RemoteConference, dConference);

	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
	if (!session && d->state == ChatRoom::State::Instantiated) {
170 171 172 173 174 175
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
		d->setState(ChatRoom::State::CreationPending);
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
176 177
void ClientGroupChatRoom::leave () {
	L_D();
178
	L_D_T(RemoteConference, dConference);
179

180
	dConference->eventHandler->unsubscribe();
181

182
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
Ghislain MARY's avatar
Ghislain MARY committed
183 184 185 186 187 188
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
189

Ghislain MARY's avatar
Ghislain MARY committed
190 191 192
	d->setState(ChatRoom::State::TerminationPending);
}

193
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
194 195 196
	LinphoneCore *cCore = CoreAccessor::getCore()->getCCore();

	SalReferOp *referOp = new SalReferOp(cCore->sal);
197
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
198
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
199 200 201 202 203 204
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
205 206
}

207
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
208
	RemoteConference::removeParticipants(participants);
209
}
210

211 212 213
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
214 215

	if (!getMe()->isAdmin()) {
216 217 218
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
219

220 221 222
	LinphoneCore *cCore = CoreAccessor::getCore()->getCCore();

	SalReferOp *referOp = new SalReferOp(cCore->sal);
223
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
224
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
225 226 227 228 229 230 231 232
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setParam("admin", Utils::toString(isAdmin));
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
}

233 234
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
235
	L_D_T(RemoteConference, dConference);
236

237 238 239 240
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
241 242

	if (!getMe()->isAdmin()) {
243 244 245
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
246

247
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
248 249 250 251 252 253
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
254 255
}

256 257
// -----------------------------------------------------------------------------

258
void ClientGroupChatRoom::onChatMessageReceived (const shared_ptr<ChatMessage> &msg) {
259

260 261
}

262
void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
263
	L_D();
264 265
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
266
	d->peerAddress = addr;
267
	CoreAccessor::getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
268 269 270
}

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
271
	L_D();
272
	d->setState(ChatRoom::State::Terminated);
273 274
}

275 276 277 278 279 280
void ClientGroupChatRoom::onFirstNotifyReceived (const Address &addr) {
	L_D();
	d->setState(ChatRoom::State::Created);
	CoreAccessor::getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
}

281
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
282 283
	L_D_T(RemoteConference, dConference);

284
	const Address &addr = event->getParticipantAddress();
285 286
	if (isMe(addr))
		return;
287

288 289 290 291 292
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
293

Ronan's avatar
Ronan committed
294
	participant = make_shared<Participant>(addr);
295
	dConference->participants.push_back(participant);
296 297 298 299

	if (isFullState)
		return;

300
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
301 302
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
303
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
304

305
	if (cb)
Ronan's avatar
Ronan committed
306
		cb(cr, L_GET_C_BACK_PTR(event));
307 308
}

309
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
310
	(void)isFullState;
311

312 313
	L_D_T(RemoteConference, dConference);

314
	const Address &addr = event->getParticipantAddress();
315 316
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
317
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
318 319
		return;
	}
320

321
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
322 323
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
324
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
325

326
	if (cb)
Ronan's avatar
Ronan committed
327
		cb(cr, L_GET_C_BACK_PTR(event));
328 329

	dConference->participants.remove(participant);
330 331
}

332
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
333
	const Address &addr = event->getParticipantAddress();
334
	shared_ptr<Participant> participant;
335
	if (isMe(addr))
336
		participant = getMe();
337 338
	else
		participant = findParticipant(addr);
339 340 341 342
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
343

344 345 346 347 348
	bool isAdmin = event->getType() == EventLog::Type::ConferenceParticipantSetAdmin;
	if (participant->isAdmin() == isAdmin)
		return; // No change in the local admin status, do not notify
	participant->getPrivate()->setAdmin(isAdmin);

349 350 351
	if (isFullState)
		return;

352
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
353 354
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
355
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
356

357
	if (cb)
Ronan's avatar
Ronan committed
358
		cb(cr, L_GET_C_BACK_PTR(event));
359 360
}

361
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
362 363
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
364
	RemoteConference::setSubject(event->getSubject());
365

366 367 368
	if (isFullState)
		return;

369 370 371
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsSubjectChangedCb cb = linphone_chat_room_cbs_get_subject_changed(cbs);
372
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
373

374
	if (cb)
Ronan's avatar
Ronan committed
375
		cb(cr, L_GET_C_BACK_PTR(event));
376 377
}

378
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
379
	const Address &addr = event->getParticipantAddress();
380
	shared_ptr<Participant> participant;
381
	if (isMe(addr))
382
		participant = getMe();
383 384 385 386 387 388
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
389
	participant->getPrivate()->addDevice(event->getGruuAddress());
390 391 392 393

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
394 395 396
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantDeviceAddedCb cb = linphone_chat_room_cbs_get_participant_device_added(cbs);
397
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
398 399

	if (cb)
Ronan's avatar
Ronan committed
400
		cb(cr, L_GET_C_BACK_PTR(event));
401 402
}

403
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
404
	(void)isFullState;
405

406
	const Address &addr = event->getParticipantAddress();
407
	shared_ptr<Participant> participant;
408
	if (isMe(addr))
409
		participant = getMe();
410 411 412 413 414 415
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
416
	participant->getPrivate()->removeDevice(event->getGruuAddress());
Benjamin REIS's avatar
Benjamin REIS committed
417 418 419
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantDeviceRemovedCb cb = linphone_chat_room_cbs_get_participant_device_removed(cbs);
420
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
421 422

	if (cb)
Ronan's avatar
Ronan committed
423
		cb(cr, L_GET_C_BACK_PTR(event));
424 425
}

426 427
// -----------------------------------------------------------------------------

428
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
429
	L_D_T(RemoteConference, dConference);
430

431
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
432 433
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
434 435
}

436 437 438 439 440
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
441
	L_D();
442 443
	L_D_T(RemoteConference, dConference);

444
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
445
		if (d->state == ChatRoom::State::CreationPending) {
446
			Address addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
447 448
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
449
				dConference->eventHandler->subscribe(getConferenceAddress());
450
		} else if (d->state == ChatRoom::State::TerminationPending)
451
			dConference->focus->getPrivate()->getSession()->terminate();
452
	} else if (state == LinphoneCallReleased && d->state == ChatRoom::State::TerminationPending) {
453
		onConferenceTerminated(getConferenceAddress());
454 455 456
	} else if (state == LinphoneCallError && d->state == ChatRoom::State::CreationPending) {
		d->setState(ChatRoom::State::CreationFailed);
	}
457 458
}

459
LINPHONE_END_NAMESPACE