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

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

42 43 44 45
// -----------------------------------------------------------------------------

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

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

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

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

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

68 69
// =============================================================================

70 71 72 73 74 75 76 77
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));
78
	RemoteConference::setSubject(subject);
79 80
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

180
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
181 182
	L_D();
	SalReferOp *referOp = new SalReferOp(d->core->sal);
183
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
184 185 186 187 188 189 190 191 192
	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();
193 194
}

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

199 200
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	L_D();
201

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

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

210
	SalReferOp *referOp = new SalReferOp(d->core->sal);
211
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
212 213 214 215 216 217 218 219 220 221 222
	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();
}

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

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

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

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

246 247 248
// -----------------------------------------------------------------------------

void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
249
	L_D();
250 251
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
252
	d->peerAddress = addr;
253
	d->setState(ChatRoom::State::Created);
254
	d->core->cppCore->getPrivate()->insertChatRoomWithDb(getSharedFromThis());
255 256 257
}

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

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

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

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

274
	participant = ObjectFactory::create<Participant>(addr);
275
	dConference->participants.push_back(participant);
276
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
277 278
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
Ronan's avatar
Ronan committed
279
	shared_ptr<ConferenceParticipantEvent> event = make_shared<ConferenceParticipantEvent>(
280 281 282 283 284 285
		EventLog::Type::ConferenceParticipantAdded,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);
286
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
287

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

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

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

301
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
302 303
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
Ronan's avatar
Ronan committed
304
	shared_ptr<ConferenceParticipantEvent> event = make_shared<ConferenceParticipantEvent>(
305 306 307 308 309 310
		EventLog::Type::ConferenceParticipantRemoved,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);
311
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
312

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

	dConference->participants.remove(participant);
317 318
}

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

331
	participant->getPrivate()->setAdmin(isAdmin);
332
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
333 334
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
Ronan's avatar
Ronan committed
335
	shared_ptr<ConferenceParticipantEvent> event = make_shared<ConferenceParticipantEvent>(
336 337 338 339 340 341
		isAdmin ? EventLog::Type::ConferenceParticipantSetAdmin : EventLog::Type::ConferenceParticipantUnsetAdmin,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr
	);
342
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
343

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

348
void ClientGroupChatRoom::onSubjectChanged (time_t tm, const std::string &subject) {
349
	L_D_T(RemoteConference, dConference);
350
	RemoteConference::setSubject(subject);
351 352 353
	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);
Ronan's avatar
Ronan committed
354
	shared_ptr<ConferenceSubjectEvent> event = make_shared<ConferenceSubjectEvent>(
355 356 357 358 359
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		subject
	);
360
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
361

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

366
void ClientGroupChatRoom::onParticipantDeviceAdded (time_t tm, const Address &addr, const Address &gruu) {
367
	L_D_T(RemoteConference, dConference);
368
	shared_ptr<Participant> participant;
369
	if (isMe(addr))
370
		participant = getMe();
371 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;
	}
	participant->getPrivate()->addDevice(gruu);
Benjamin REIS's avatar
Benjamin REIS committed
378 379 380
	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);
Ronan's avatar
Ronan committed
381
	shared_ptr<ConferenceParticipantDeviceEvent> event = make_shared<ConferenceParticipantDeviceEvent>(
382 383 384 385 386 387 388
		EventLog::Type::ConferenceParticipantDeviceAdded,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr,
		gruu
	);
389
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
390 391

	if (cb)
Ronan's avatar
Ronan committed
392
		cb(cr, L_GET_C_BACK_PTR(event));
393 394
}

395
void ClientGroupChatRoom::onParticipantDeviceRemoved (time_t tm, const Address &addr, const Address &gruu) {
396
	L_D_T(RemoteConference, dConference);
397
	shared_ptr<Participant> participant;
398
	if (isMe(addr))
399
		participant = getMe();
400 401 402 403 404 405 406
	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);
Benjamin REIS's avatar
Benjamin REIS committed
407 408 409
	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);
Ronan's avatar
Ronan committed
410
	shared_ptr<ConferenceParticipantDeviceEvent> event = make_shared<ConferenceParticipantDeviceEvent>(
411 412 413 414 415 416 417
		EventLog::Type::ConferenceParticipantDeviceRemoved,
		tm,
		dConference->conferenceAddress,
		dConference->eventHandler->getLastNotify(),
		addr,
		gruu
	);
418
	Conference::getCore()->cppCore->getPrivate()->mainDb->addEvent(event);
Benjamin REIS's avatar
Benjamin REIS committed
419 420

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

424 425
// -----------------------------------------------------------------------------

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

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

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

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

454
LINPHONE_END_NAMESPACE