client-group-chat-room.cpp 15.4 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
	csp.addCustomContactParameter("text");
63

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

72
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
73 74
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->notifyReceived(body);
75 76
}

77 78
// =============================================================================

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

90 91 92
int ClientGroupChatRoom::getCapabilities () const {
	return static_cast<int>(Capabilities::Conference);
}
93

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

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

108 109
	list<Address> addressesList = d->cleanAddressesList(addresses);
	if (addressesList.empty())
110
		return;
111

112 113 114 115
	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;
	}
116

117
	Content content;
118
	content.setBody(getResourceLists(addressesList));
119 120
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
121

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

133
bool ClientGroupChatRoom::canHandleParticipants () const {
134
	return RemoteConference::canHandleParticipants();
135 136
}

137 138 139 140
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

141
const Address &ClientGroupChatRoom::getConferenceAddress () const {
142
	return RemoteConference::getConferenceAddress();
143 144
}

145 146 147 148
shared_ptr<Participant> ClientGroupChatRoom::getMe () const {
	return RemoteConference::getMe();
}

149
int ClientGroupChatRoom::getNbParticipants () const {
150
	return RemoteConference::getNbParticipants();
151 152 153
}

list<shared_ptr<Participant>> ClientGroupChatRoom::getParticipants () const {
154
	return RemoteConference::getParticipants();
155 156
}

157 158 159 160
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

161 162
void ClientGroupChatRoom::join () {
	L_D();
163 164 165
	L_D_T(RemoteConference, dConference);

	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
166
	if (!session && ((d->state == ChatRoom::State::Instantiated) || (d->state == ChatRoom::State::Terminated))) {
167 168 169 170 171 172
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
		d->setState(ChatRoom::State::CreationPending);
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
173 174
void ClientGroupChatRoom::leave () {
	L_D();
175
	L_D_T(RemoteConference, dConference);
176

177
	dConference->eventHandler->unsubscribe();
178

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

Ghislain MARY's avatar
Ghislain MARY committed
187 188 189
	d->setState(ChatRoom::State::TerminationPending);
}

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

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

204
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
205
	RemoteConference::removeParticipants(participants);
206
}
207

208 209 210
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
211 212

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

217 218 219
	LinphoneCore *cCore = CoreAccessor::getCore()->getCCore();

	SalReferOp *referOp = new SalReferOp(cCore->sal);
220
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
221
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
222 223 224 225 226 227 228 229
	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();
}

230 231
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
232
	L_D_T(RemoteConference, dConference);
233

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

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

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

253 254
// -----------------------------------------------------------------------------

255
void ClientGroupChatRoom::onChatMessageReceived (const shared_ptr<ChatMessage> &msg) {
256

257 258
}

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

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
268
	L_D();
269 270
	L_D_T(RemoteConference, dConference);
	dConference->eventHandler->resetLastNotify();
271
	d->setState(ChatRoom::State::Terminated);
272 273
}

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

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

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

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

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

	if (isFullState)
		return;

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

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

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

311 312
	L_D_T(RemoteConference, dConference);

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

320 321
	dConference->participants.remove(participant);

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

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

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

343 344 345 346 347
	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);

348 349 350
	if (isFullState)
		return;

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

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

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

365 366 367
	if (isFullState)
		return;

368 369 370
	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);
371
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
372

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

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

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
393 394 395
	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);
396
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
397 398

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

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

405
	const Address &addr = event->getParticipantAddress();
406
	shared_ptr<Participant> participant;
407
	if (isMe(addr))
408
		participant = getMe();
409 410 411 412 413 414
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
415
	participant->getPrivate()->removeDevice(event->getGruuAddress());
Benjamin REIS's avatar
Benjamin REIS committed
416 417 418
	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);
419
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
420 421

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

425 426
// -----------------------------------------------------------------------------

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

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

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

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

458
LINPHONE_END_NAMESPACE