client-group-chat-room.cpp 15 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
// -----------------------------------------------------------------------------

shared_ptr<CallSession> ClientGroupChatRoomPrivate::createSession () {
	L_Q();
44
	L_Q_T(RemoteConference, qConference);
45

46 47
	CallSessionParams csp;
	csp.addCustomHeader("Require", "recipient-list-invite");
48

49
	shared_ptr<Participant> focus = qConference->getPrivate()->focus;
50 51
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, q);
	const Address &myAddress = q->getMe()->getAddress();
52
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myAddress, focus->getContactAddress());
53
	session->initiateOutgoing();
54 55

	Address addr = myAddress;
56 57 58 59 60
	addr.setParam("text");
	session->getPrivate()->getOp()->set_contact_address(addr.getPrivate()->getInternalAddress());
	return session;
}

61
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
62 63
	L_Q_T(RemoteConference, qConference);
	qConference->getPrivate()->eventHandler->notifyReceived(body);
64 65
}

66 67
// =============================================================================

68
ClientGroupChatRoom::ClientGroupChatRoom (
69
	const std::shared_ptr<Core> &core,
70
	const Address &me,
71 72 73
	const std::string &uri,
	const std::string &subject
) : ChatRoom(*new ClientGroupChatRoomPrivate, core, me), RemoteConference(core->getCCore(), me, nullptr) {
74
	L_D_T(RemoteConference, dConference);
Ronan's avatar
Ronan committed
75
	dConference->focus = make_shared<Participant>(Address(uri));
76
	RemoteConference::setSubject(subject);
77 78
}

79 80 81
int ClientGroupChatRoom::getCapabilities () const {
	return static_cast<int>(Capabilities::Conference);
}
82

83 84 85 86
void ClientGroupChatRoom::addParticipant (const Address &addr, const CallSessionParams *params, bool hasMedia) {
	list<Address> addresses;
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
87 88
}

89 90 91 92 93
void ClientGroupChatRoom::addParticipants (
	const list<Address> &addresses,
	const CallSessionParams *params,
	bool hasMedia
) {
94
	L_D();
95
	L_D_T(RemoteConference, dConference);
96

97 98
	if (addresses.empty())
		return;
99

100 101 102 103
	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;
	}
104

105 106 107
	list<Address> sortedAddresses(addresses);
	sortedAddresses.sort();
	sortedAddresses.unique();
108

109 110 111 112
	Content content;
	content.setBody(getResourceLists(sortedAddresses));
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
113

114
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
115
	if (session)
116
		session->update(nullptr, getSubject(), &content);
117 118
	else {
		session = d->createSession();
119 120
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
121
			d->setState(ChatRoom::State::CreationPending);
122
	}
123 124
}

125
bool ClientGroupChatRoom::canHandleParticipants () const {
126
	return RemoteConference::canHandleParticipants();
127 128
}

129 130 131 132
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

133
const Address &ClientGroupChatRoom::getConferenceAddress () const {
134
	return RemoteConference::getConferenceAddress();
135 136 137
}

int ClientGroupChatRoom::getNbParticipants () const {
138
	return RemoteConference::getNbParticipants();
139 140 141
}

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

145 146 147 148
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

149 150
void ClientGroupChatRoom::join () {
	L_D();
151 152 153 154
	L_D_T(RemoteConference, dConference);

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

Ghislain MARY's avatar
Ghislain MARY committed
161 162
void ClientGroupChatRoom::leave () {
	L_D();
163
	L_D_T(RemoteConference, dConference);
164

165
	dConference->eventHandler->unsubscribe();
166

167
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
Ghislain MARY's avatar
Ghislain MARY committed
168 169 170 171 172 173
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
174

Ghislain MARY's avatar
Ghislain MARY committed
175 176 177
	d->setState(ChatRoom::State::TerminationPending);
}

178
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
179 180 181
	LinphoneCore *cCore = CoreAccessor::getCore()->getCCore();

	SalReferOp *referOp = new SalReferOp(cCore->sal);
182
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
183
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
184 185 186 187 188 189 190 191
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setUriParam("method", "BYE");
	referToAddr.setDomain("");
	referToAddr.setPort(-1);
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
192 193
}

194
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
195
	RemoteConference::removeParticipants(participants);
196
}
197

198 199 200
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	if (isAdmin == participant->isAdmin())
		return;
201 202

	if (!getMe()->isAdmin()) {
203 204 205
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
206

207 208 209
	LinphoneCore *cCore = CoreAccessor::getCore()->getCCore();

	SalReferOp *referOp = new SalReferOp(cCore->sal);
210
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
211
	linphone_configure_op(cCore, referOp, lAddr, nullptr, false);
212 213 214 215 216 217 218 219 220 221
	linphone_address_unref(lAddr);
	Address referToAddr = participant->getAddress();
	referToAddr.setParam("text");
	referToAddr.setParam("admin", Utils::toString(isAdmin));
	referToAddr.setDomain("");
	referToAddr.setPort(-1);
	referOp->send_refer(referToAddr.getPrivate()->getInternalAddress());
	referOp->unref();
}

222 223
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
224
	L_D_T(RemoteConference, dConference);
225

226 227 228 229
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
230 231

	if (!getMe()->isAdmin()) {
232 233 234
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
235

236
	shared_ptr<CallSession> session = dConference->focus->getPrivate()->getSession();
237 238 239 240 241 242
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
243 244
}

245 246
// -----------------------------------------------------------------------------

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

249 250
}

251
void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
252
	L_D();
253 254
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
255
	d->peerAddress = addr;
256
	CoreAccessor::getCore()->getPrivate()->insertChatRoom(getSharedFromThis());
257 258 259
}

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
260
	L_D();
261
	d->setState(ChatRoom::State::Terminated);
262 263
}

264 265 266 267 268 269
void ClientGroupChatRoom::onFirstNotifyReceived (const Address &addr) {
	L_D();
	d->setState(ChatRoom::State::Created);
	CoreAccessor::getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
}

270
void ClientGroupChatRoom::onParticipantAdded (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
271 272
	L_D_T(RemoteConference, dConference);

273
	const Address &addr = event->getParticipantAddress();
274 275
	if (isMe(addr))
		return;
276

277 278 279 280 281
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
282

Ronan's avatar
Ronan committed
283
	participant = make_shared<Participant>(addr);
284
	dConference->participants.push_back(participant);
285 286 287 288

	if (isFullState)
		return;

289
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
290 291
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
292
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
293

294
	if (cb)
Ronan's avatar
Ronan committed
295
		cb(cr, L_GET_C_BACK_PTR(event));
296 297
}

298 299 300
void ClientGroupChatRoom::onParticipantRemoved (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
	(void)isFullState; // unused

301 302
	L_D_T(RemoteConference, dConference);

303
	const Address &addr = event->getParticipantAddress();
304 305
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
306
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
307 308
		return;
	}
309

310
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
311 312
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
313
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
314

315
	if (cb)
Ronan's avatar
Ronan committed
316
		cb(cr, L_GET_C_BACK_PTR(event));
317 318

	dConference->participants.remove(participant);
319 320
}

321
void ClientGroupChatRoom::onParticipantSetAdmin (const shared_ptr<ConferenceParticipantEvent> &event, bool isFullState) {
322
	const Address &addr = event->getParticipantAddress();
323
	shared_ptr<Participant> participant;
324
	if (isMe(addr))
325
		participant = getMe();
326 327
	else
		participant = findParticipant(addr);
328 329 330 331
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
332

333 334 335 336 337
	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);

338 339 340
	if (isFullState)
		return;

341
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
342 343
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
344
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
345

346
	if (cb)
Ronan's avatar
Ronan committed
347
		cb(cr, L_GET_C_BACK_PTR(event));
348 349
}

350
void ClientGroupChatRoom::onSubjectChanged (const shared_ptr<ConferenceSubjectEvent> &event, bool isFullState) {
351 352
	if (getSubject() == event->getSubject())
		return; // No change in the local subject, do not notify
353
	RemoteConference::setSubject(event->getSubject());
354

355 356 357
	if (isFullState)
		return;

358 359 360
	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);
361
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
362

363
	if (cb)
Ronan's avatar
Ronan committed
364
		cb(cr, L_GET_C_BACK_PTR(event));
365 366
}

367
void ClientGroupChatRoom::onParticipantDeviceAdded (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
368
	const Address &addr = event->getParticipantAddress();
369
	shared_ptr<Participant> participant;
370
	if (isMe(addr))
371
		participant = getMe();
372 373 374 375 376 377
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
378
	participant->getPrivate()->addDevice(event->getGruuAddress());
379 380 381 382

	if (isFullState)
		return;

Benjamin REIS's avatar
Benjamin REIS committed
383 384 385
	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);
386
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
387 388

	if (cb)
Ronan's avatar
Ronan committed
389
		cb(cr, L_GET_C_BACK_PTR(event));
390 391
}

392 393 394
void ClientGroupChatRoom::onParticipantDeviceRemoved (const shared_ptr<ConferenceParticipantDeviceEvent> &event, bool isFullState) {
	(void)isFullState; // unused

395
	const Address &addr = event->getParticipantAddress();
396
	shared_ptr<Participant> participant;
397
	if (isMe(addr))
398
		participant = getMe();
399 400 401 402 403 404
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
405
	participant->getPrivate()->removeDevice(event->getGruuAddress());
Benjamin REIS's avatar
Benjamin REIS committed
406 407 408
	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);
409
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
410 411

	if (cb)
Ronan's avatar
Ronan committed
412
		cb(cr, L_GET_C_BACK_PTR(event));
413 414
}

415 416
// -----------------------------------------------------------------------------

417
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
418
	L_D_T(RemoteConference, dConference);
419

420
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
421 422
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
423 424
}

425 426 427 428 429
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
430
	L_D();
431 432
	L_D_T(RemoteConference, dConference);

433
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
434
		if (d->state == ChatRoom::State::CreationPending) {
435
			Address addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
436 437
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
438
				dConference->eventHandler->subscribe(getConferenceAddress());
439
		} else if (d->state == ChatRoom::State::TerminationPending)
440
			dConference->focus->getPrivate()->getSession()->terminate();
441
	} else if (state == LinphoneCallReleased && d->state == ChatRoom::State::TerminationPending) {
442
		onConferenceTerminated(getConferenceAddress());
443 444 445
	} else if (state == LinphoneCallError && d->state == ChatRoom::State::CreationPending) {
		d->setState(ChatRoom::State::CreationFailed);
	}
446 447
}

448
LINPHONE_END_NAMESPACE