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

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 52
	shared_ptr<CallSession> session = focus->getPrivate()->createSession(*q, &csp, false, q);
	const Address &myAddress = q->getMe()->getAddress();
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, myAddress, focus->getAddress());
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
	d->setState(ChatRoom::State::Created);
258
	CoreAccessor::getCore()->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
259 260 261
}

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

266
void ClientGroupChatRoom::onParticipantAdded (shared_ptr<ConferenceParticipantEvent> event) {
267 268
	L_D_T(RemoteConference, dConference);

269
	const Address &addr = event->getParticipantAddress();
270 271
	if (isMe(addr))
		return;
272

273 274 275 276 277
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
278

Ronan's avatar
Ronan committed
279
	participant = make_shared<Participant>(addr);
280
	dConference->participants.push_back(participant);
281
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
282 283
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
284
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
285

286
	if (cb)
Ronan's avatar
Ronan committed
287
		cb(cr, L_GET_C_BACK_PTR(event));
288 289
}

290
void ClientGroupChatRoom::onParticipantRemoved (shared_ptr<ConferenceParticipantEvent> event) {
291 292
	L_D_T(RemoteConference, dConference);

293
	const Address &addr = event->getParticipantAddress();
294 295
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
296
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
297 298
		return;
	}
299

300
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
301 302
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(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

	dConference->participants.remove(participant);
309 310
}

311 312
void ClientGroupChatRoom::onParticipantSetAdmin (shared_ptr<ConferenceParticipantEvent> event) {
	const Address &addr = event->getParticipantAddress();
313
	shared_ptr<Participant> participant;
314
	if (isMe(addr))
315
		participant = getMe();
316 317
	else
		participant = findParticipant(addr);
318 319 320 321
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
322

323
	participant->getPrivate()->setAdmin(event->getType() == EventLog::Type::ConferenceParticipantSetAdmin);
324
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
325 326
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
327
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
328

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

333 334
void ClientGroupChatRoom::onSubjectChanged (shared_ptr<ConferenceSubjectEvent> event) {
	RemoteConference::setSubject(event->getSubject());
335 336 337
	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);
338
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
339

340
	if (cb)
Ronan's avatar
Ronan committed
341
		cb(cr, L_GET_C_BACK_PTR(event));
342 343
}

344 345
void ClientGroupChatRoom::onParticipantDeviceAdded (shared_ptr<ConferenceParticipantDeviceEvent> event) {
	const Address &addr = event->getParticipantAddress();
346
	shared_ptr<Participant> participant;
347
	if (isMe(addr))
348
		participant = getMe();
349 350 351 352 353 354
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
355
	participant->getPrivate()->addDevice(event->getGruuAddress());
Benjamin REIS's avatar
Benjamin REIS committed
356 357 358
	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);
359
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
360 361

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

365 366
void ClientGroupChatRoom::onParticipantDeviceRemoved (shared_ptr<ConferenceParticipantDeviceEvent> event) {
	const Address &addr = event->getParticipantAddress();
367
	shared_ptr<Participant> participant;
368
	if (isMe(addr))
369
		participant = getMe();
370 371 372 373 374 375
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
376
	participant->getPrivate()->removeDevice(event->getGruuAddress());
Benjamin REIS's avatar
Benjamin REIS committed
377 378 379
	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);
380
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
381 382

	if (cb)
Ronan's avatar
Ronan committed
383
		cb(cr, L_GET_C_BACK_PTR(event));
384 385
}

386 387
// -----------------------------------------------------------------------------

388
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
389
	L_D_T(RemoteConference, dConference);
390

391
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
392 393
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
394 395
}

396 397 398 399 400
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
401
	L_D();
402 403
	L_D_T(RemoteConference, dConference);

404
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
405
		if (d->state == ChatRoom::State::CreationPending) {
406
			Address addr(session->getRemoteContactAddress()->asStringUriOnly());
Ghislain MARY's avatar
Ghislain MARY committed
407 408
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
409
				dConference->eventHandler->subscribe(getConferenceAddress());
410
		} else if (d->state == ChatRoom::State::TerminationPending)
411
			dConference->focus->getPrivate()->getSession()->terminate();
412
	} else if (state == LinphoneCallReleased && d->state == ChatRoom::State::TerminationPending) {
413
		onConferenceTerminated(getConferenceAddress());
414 415 416
	} else if (state == LinphoneCallError && d->state == ChatRoom::State::CreationPending) {
		d->setState(ChatRoom::State::CreationFailed);
	}
417 418
}

419
LINPHONE_END_NAMESPACE