client-group-chat-room.cpp 13.9 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 "event-log/events.h"
30
#include "logger/logger.h"
31
#include "sal/refer-op.h"
32

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

35 36 37 38
using namespace std;

LINPHONE_BEGIN_NAMESPACE

Ronan's avatar
Ronan committed
39
ClientGroupChatRoomPrivate::ClientGroupChatRoomPrivate (LinphoneCore *core) : ChatRoomPrivate(core) {}
40

41 42 43 44
// -----------------------------------------------------------------------------

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

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

50
	shared_ptr<Participant> focus = qConference->getPrivate()->focus;
51 52 53
	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());
54
	session->initiateOutgoing();
55 56

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

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

67 68
// =============================================================================

69 70 71 72 73 74 75 76
ClientGroupChatRoom::ClientGroupChatRoom (
	LinphoneCore *core,
	const Address &me,
	const string &uri,
	const string &subject
) : ChatRoom(*new ClientGroupChatRoomPrivate(core)), RemoteConference(core, me, nullptr) {
	L_D_T(RemoteConference, dConference);
	dConference->focus = ObjectFactory::create<Participant>(Address(uri));
77
	RemoteConference::setSubject(subject);
78 79
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

179
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
180 181
	L_D();
	SalReferOp *referOp = new SalReferOp(d->core->sal);
182
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
183 184 185 186 187 188 189 190 191
	linphone_configure_op(d->core, referOp, lAddr, nullptr, false);
	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
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	L_D();
200

201 202
	if (isAdmin == participant->isAdmin())
		return;
203 204

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

209
	SalReferOp *referOp = new SalReferOp(d->core->sal);
210
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
211 212 213 214 215 216 217 218 219 220 221
	linphone_configure_op(d->core, referOp, lAddr, nullptr, false);
	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::onConferenceCreated (const Address &addr) {
248
	L_D();
249 250
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
251
	d->peerAddress = addr;
252
	d->setState(ChatRoom::State::Created);
253
	_linphone_core_add_group_chat_room(d->core, addr, L_GET_C_BACK_PTR(this));
254 255 256
}

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
257
	L_D();
258
	d->setState(ChatRoom::State::Terminated);
259 260
}

261
void ClientGroupChatRoom::onParticipantAdded (time_t tm, const Address &addr) {
262 263
	L_D_T(RemoteConference, dConference);

264 265
	if (isMe(addr))
		return;
266

267 268 269 270 271
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
272

273
	participant = ObjectFactory::create<Participant>(addr);
274
	dConference->participants.push_back(participant);
275
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
276 277
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
278 279 280 281 282 283 284 285
	ConferenceParticipantEvent event(
		EventLog::Type::ConferenceParticipantAdded,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);

286
	if (cb)
287
		cb(cr, L_GET_C_BACK_PTR(participant));
288 289
}

290
void ClientGroupChatRoom::onParticipantRemoved (time_t tm, const Address &addr) {
291 292
	L_D_T(RemoteConference, dConference);

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

299
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
300 301
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
302 303 304 305 306 307 308
	ConferenceParticipantEvent event(
		EventLog::Type::ConferenceParticipantRemoved,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);
309

310
	if (cb)
311
		cb(cr, L_GET_C_BACK_PTR(participant));
312 313

	dConference->participants.remove(participant);
314 315
}

316
void ClientGroupChatRoom::onParticipantSetAdmin (time_t tm, const Address &addr, bool isAdmin) {
317
	L_D_T(RemoteConference, dConference);
318
	shared_ptr<Participant> participant;
319
	if (isMe(addr))
320
		participant = getMe();
321 322
	else
		participant = findParticipant(addr);
323 324 325 326
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
327

328
	participant->getPrivate()->setAdmin(isAdmin);
329
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
330 331
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
332 333 334 335 336 337 338
	ConferenceParticipantEvent event(
		isAdmin ? EventLog::Type::ConferenceParticipantSetAdmin : EventLog::Type::ConferenceParticipantUnsetAdmin,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);
339

340
	if (cb)
341
		cb(cr, L_GET_C_BACK_PTR(participant), isAdmin);
342 343
}

344
void ClientGroupChatRoom::onSubjectChanged (time_t tm, const std::string &subject) {
345
	L_D_T(RemoteConference, dConference);
346
	RemoteConference::setSubject(subject);
347 348 349
	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);
350 351 352 353 354 355
	ConferenceSubjectEvent event(
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		subject
	);
356

357 358 359 360
	if (cb)
		cb(cr, subject.c_str());
}

361
void ClientGroupChatRoom::onParticipantDeviceAdded (time_t tm, const Address &addr, const Address &gruu) {
362
	L_D_T(RemoteConference, dConference);
363
	shared_ptr<Participant> participant;
364
	if (isMe(addr))
365
		participant = getMe();
366 367 368 369 370 371 372
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " added a device but is not in the list of participants!";
		return;
	}
	participant->getPrivate()->addDevice(gruu);
373 374 375 376 377 378 379 380
	ConferenceParticipantDeviceEvent event(
		EventLog::Type::ConferenceParticipantDeviceAdded,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr,
		gruu
	);
381 382
}

383
void ClientGroupChatRoom::onParticipantDeviceRemoved (time_t tm, const Address &addr, const Address &gruu) {
384
	L_D_T(RemoteConference, dConference);
385
	shared_ptr<Participant> participant;
386
	if (isMe(addr))
387
		participant = getMe();
388 389 390 391 392 393 394
	else
		participant = findParticipant(addr);
	if (!participant) {
		lWarning() << "Participant " << participant << " removed a device but is not in the list of participants!";
		return;
	}
	participant->getPrivate()->removeDevice(gruu);
395 396 397 398 399 400 401 402
	ConferenceParticipantDeviceEvent event(
		EventLog::Type::ConferenceParticipantDeviceRemoved,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr,
		gruu
	);
403 404
}

405 406
// -----------------------------------------------------------------------------

407
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
408
	L_D_T(RemoteConference, dConference);
409

410
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
411 412
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
413 414
}

415 416 417 418 419
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
420
	L_D();
421 422
	L_D_T(RemoteConference, dConference);

423
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
424 425 426 427 428
		if (d->state == ChatRoom::State::CreationPending) {
			Address addr(session->getRemoteContact());
			addr.clean();
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
429
				dConference->eventHandler->subscribe(getConferenceAddress());
430
		} else if (d->state == ChatRoom::State::TerminationPending)
431 432
			dConference->focus->getPrivate()->getSession()->terminate();
	} else if (state == LinphoneCallReleased && d->state == ChatRoom::State::TerminationPending)
433
		onConferenceTerminated(getConferenceAddress());
434 435
}

436
LINPHONE_END_NAMESPACE