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/handlers/remote-conference-event-handler.h"
26
#include "conference/participant-p.h"
27 28
#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() == IdentityAddress(*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;
}

Ghislain MARY's avatar
Ghislain MARY committed
72
void ClientGroupChatRoomPrivate::notifyReceived (const 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,
Ghislain MARY's avatar
Ghislain MARY committed
81
	const std::string &factoryUri,
82
	const IdentityAddress &me,
83
	const std::string &subject
84
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, ChatRoomId(IdentityAddress(), me)),
85
RemoteConference(core, me, nullptr) {
86
	L_D_T(RemoteConference, dConference);
Ghislain MARY's avatar
Ghislain MARY committed
87
	dConference->focus = make_shared<Participant>(Address(factoryUri));
88
	RemoteConference::setSubject(subject);
89 90
}

91 92 93 94
shared_ptr<Core> ClientGroupChatRoom::getCore () const {
	return ChatRoom::getCore();
}

95 96 97 98 99 100 101 102 103 104
ClientGroupChatRoom::CapabilitiesMask ClientGroupChatRoom::getCapabilities () const {
	return static_cast<CapabilitiesMask>(Capabilities::Conference);
}

bool ClientGroupChatRoom::canHandleParticipants () const {
	return RemoteConference::canHandleParticipants();
}

const Address &ClientGroupChatRoom::getConferenceAddress () const {
	return RemoteConference::getConferenceAddress();
105
}
106

107 108 109 110
void ClientGroupChatRoom::addParticipant (const Address &addr, const CallSessionParams *params, bool hasMedia) {
	list<Address> addresses;
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
111 112
}

113 114 115 116 117
void ClientGroupChatRoom::addParticipants (
	const list<Address> &addresses,
	const CallSessionParams *params,
	bool hasMedia
) {
118
	L_D();
119
	L_D_T(RemoteConference, dConference);
120

121 122
	list<Address> addressesList = d->cleanAddressesList(addresses);
	if (addressesList.empty())
123
		return;
124

125 126 127 128
	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;
	}
129

130
	Content content;
131
	content.setBody(getResourceLists(addressesList));
132 133
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
134

135
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
136
	if (session)
137
		session->update(nullptr, getSubject(), &content);
138 139
	else {
		session = d->createSession();
140 141
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
142
			d->setState(ChatRoom::State::CreationPending);
143
	}
144 145
}

146
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
147
	LinphoneCore *cCore = getCore()->getCCore();
148 149

	SalReferOp *referOp = new SalReferOp(cCore->sal);
150
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
151
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
152 153 154 155 156 157
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
158 159
}

160
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
161
	RemoteConference::removeParticipants(participants);
162
}
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

shared_ptr<Participant> ClientGroupChatRoom::getMe () const {
	return RemoteConference::getMe();
}

int ClientGroupChatRoom::getNbParticipants () const {
	return RemoteConference::getNbParticipants();
}

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

180 181 182
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
183 184

	if (!getMe()->isAdmin()) {
185 186 187
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
188

189
	LinphoneCore *cCore = getCore()->getCCore();
190 191

	SalReferOp *referOp = new SalReferOp(cCore->sal);
192
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
193
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
194 195 196 197 198 199 200 201
	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();
}

202 203 204 205
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

206 207
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
208
	L_D_T(RemoteConference, dConference);
209

210 211 212 213
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
214 215

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

220
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
221 222 223 224 225 226
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
227 228
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
void ClientGroupChatRoom::join () {
	L_D();
	L_D_T(RemoteConference, dConference);

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

void ClientGroupChatRoom::leave () {
	L_D();
	L_D_T(RemoteConference, dConference);

	dConference->eventHandler->unsubscribe();
246

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

255
	d->setState(ChatRoom::State::TerminationPending);
256 257
}

258 259 260 261
// -----------------------------------------------------------------------------

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

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

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

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

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

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

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

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

	if (isFullState)
		return;

302
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
303 304
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
305
	getCore()->getPrivate()->mainDb->addEvent(event);
306

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

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

314 315
	L_D_T(RemoteConference, dConference);

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

323 324
	dConference->participants.remove(participant);

325
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
326 327
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
328
	getCore()->getPrivate()->mainDb->addEvent(event);
329

330
	if (cb)
Ronan's avatar
Ronan committed
331
		cb(cr, L_GET_C_BACK_PTR(event));
332 333
}

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

346 347 348 349 350
	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);

351 352 353
	if (isFullState)
		return;

354
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
355 356
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
357
	getCore()->getPrivate()->mainDb->addEvent(event);
358

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

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

368 369 370
	if (isFullState)
		return;

371 372 373
	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);
374
	getCore()->getPrivate()->mainDb->addEvent(event);
375

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

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

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
396 397 398
	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);
399
	getCore()->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
400 401

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

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

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

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

428 429
// -----------------------------------------------------------------------------

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

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

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

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

461
LINPHONE_END_NAMESPACE