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

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

34 35 36 37
using namespace std;

LINPHONE_BEGIN_NAMESPACE

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

40 41 42 43
// -----------------------------------------------------------------------------

shared_ptr<CallSession> ClientGroupChatRoomPrivate::createSession () {
	L_Q();
44

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

	shared_ptr<Participant> focus = static_cast<RemoteConference *>(q)->getPrivate()->focus;
	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());
52
	session->initiateOutgoing();
53 54

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

60 61
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
	L_Q();
62
	static_cast<RemoteConference *>(q)->getPrivate()->eventHandler->notifyReceived(body);
63 64
}

65 66
// =============================================================================

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196 197
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	L_D();
198

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

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

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

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

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

243 244 245
// -----------------------------------------------------------------------------

void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
246
	L_D();
247 248
	L_D_T(RemoteConference, dConference);
	dConference->conferenceAddress = addr;
249
	d->peerAddress = addr;
250
	d->setState(ChatRoom::State::Created);
251
	_linphone_core_add_group_chat_room(d->core, addr, L_GET_C_BACK_PTR(this));
252 253 254
}

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

void ClientGroupChatRoom::onParticipantAdded (const Address &addr) {
260 261
	L_D_T(RemoteConference, dConference);

262 263
	if (isMe(addr))
		return;
264

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

271
	participant = ObjectFactory::create<Participant>(addr);
272
	dConference->participants.push_back(participant);
273
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
274 275 276
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
	if (cb)
277
		cb(cr, L_GET_C_BACK_PTR(participant));
278 279 280
}

void ClientGroupChatRoom::onParticipantRemoved (const Address &addr) {
281 282
	L_D_T(RemoteConference, dConference);

283 284
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
285
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
286 287
		return;
	}
288

289
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
290 291
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
292

293
	if (cb)
294
		cb(cr, L_GET_C_BACK_PTR(participant));
295 296

	dConference->participants.remove(participant);
297 298 299
}

void ClientGroupChatRoom::onParticipantSetAdmin (const Address &addr, bool isAdmin) {
300
	shared_ptr<Participant> participant;
301
	if (isMe(addr))
302
		participant = getMe();
303 304
	else
		participant = findParticipant(addr);
305 306 307 308
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
309

310
	participant->getPrivate()->setAdmin(isAdmin);
311
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
312 313
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
314

315
	if (cb)
316
		cb(cr, L_GET_C_BACK_PTR(participant), isAdmin);
317 318
}

319
void ClientGroupChatRoom::onSubjectChanged (const std::string &subject) {
320
	RemoteConference::setSubject(subject);
321 322 323
	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);
324

325 326 327 328
	if (cb)
		cb(cr, subject.c_str());
}

329
void ClientGroupChatRoom::onParticipantDeviceAdded (const Address &addr, const Address &gruu) {
330
	shared_ptr<Participant> participant;
331
	if (isMe(addr))
332
		participant = getMe();
333 334 335 336 337 338 339 340 341 342
	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);
}

void ClientGroupChatRoom::onParticipantDeviceRemoved (const Address &addr, const Address &gruu) {
343
	shared_ptr<Participant> participant;
344
	if (isMe(addr))
345
		participant = getMe();
346 347 348 349 350 351 352 353 354
	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);
}

355 356
// -----------------------------------------------------------------------------

357
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
358
	L_D_T(RemoteConference, dConference);
359

360
	ParticipantPrivate *participantPrivate = dConference->focus->getPrivate();
361 362
	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
363 364
}

365 366 367 368 369
void ClientGroupChatRoom::onCallSessionStateChanged (
	const std::shared_ptr<const CallSession> &session,
	LinphoneCallState state,
	const string &message
) {
Ghislain MARY's avatar
Ghislain MARY committed
370
	L_D();
371 372
	L_D_T(RemoteConference, dConference);

373
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
374 375 376 377 378
		if (d->state == ChatRoom::State::CreationPending) {
			Address addr(session->getRemoteContact());
			addr.clean();
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
379
				dConference->eventHandler->subscribe(getConferenceAddress());
380
		} else if (d->state == ChatRoom::State::TerminationPending)
381 382
			dConference->focus->getPrivate()->getSession()->terminate();
	} else if (state == LinphoneCallReleased && d->state == ChatRoom::State::TerminationPending)
383
		onConferenceTerminated(getConferenceAddress());
384 385
}

386
LINPHONE_END_NAMESPACE