client-group-chat-room.cpp 12.6 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
	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 69 70 71 72 73 74 75
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));
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
	L_D();
	SalReferOp *referOp = new SalReferOp(d->core->sal);
181
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
182 183 184 185 186 187 188 189 190
	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();
191 192
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

330
void ClientGroupChatRoom::onParticipantDeviceAdded (const Address &addr, const Address &gruu) {
331
	shared_ptr<Participant> participant;
332
	if (isMe(addr))
333
		participant = getMe();
334 335 336 337 338 339 340 341 342 343
	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) {
344
	shared_ptr<Participant> participant;
345
	if (isMe(addr))
346
		participant = getMe();
347 348 349 350 351 352 353 354 355
	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);
}

356 357
// -----------------------------------------------------------------------------

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

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

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

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

387
LINPHONE_END_NAMESPACE