client-group-chat-room.cpp 12.7 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
ClientGroupChatRoom::ClientGroupChatRoom (LinphoneCore *core, const Address &me, const string &uri, const string &subject)
68
	: ChatRoom(*new ClientGroupChatRoomPrivate(core)), RemoteConference(core, me, nullptr) {
69 70
	static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus =
		ObjectFactory::create<Participant>(Address(uri));
71
	RemoteConference::setSubject(subject);
72 73
}

74 75 76
int ClientGroupChatRoom::getCapabilities () const {
	return static_cast<int>(Capabilities::Conference);
}
77

78 79 80 81
void ClientGroupChatRoom::addParticipant (const Address &addr, const CallSessionParams *params, bool hasMedia) {
	list<Address> addresses;
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
82 83
}

84
void ClientGroupChatRoom::addParticipants (const list<Address> &addresses, const CallSessionParams *params, bool hasMedia) {
85
	L_D();
86

87 88
	if (addresses.empty())
		return;
89

90 91 92 93
	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;
	}
94

95 96 97
	list<Address> sortedAddresses(addresses);
	sortedAddresses.sort();
	sortedAddresses.unique();
98

99 100 101 102
	Content content;
	content.setBody(getResourceLists(sortedAddresses));
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
103 104 105

	shared_ptr<CallSession> session =
		static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus->getPrivate()->getSession();
106
	if (session)
107
		session->update(nullptr, getSubject(), &content);
108 109
	else {
		session = d->createSession();
110 111
		session->startInvite(nullptr, getSubject(), &content);
		if (d->state == ChatRoom::State::Instantiated)
112
			d->setState(ChatRoom::State::CreationPending);
113
	}
114 115
}

116
bool ClientGroupChatRoom::canHandleParticipants () const {
117
	return RemoteConference::canHandleParticipants();
118 119
}

120 121 122 123
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

124
const Address &ClientGroupChatRoom::getConferenceAddress () const {
125
	return RemoteConference::getConferenceAddress();
126 127 128
}

int ClientGroupChatRoom::getNbParticipants () const {
129
	return RemoteConference::getNbParticipants();
130 131 132
}

list<shared_ptr<Participant>> ClientGroupChatRoom::getParticipants () const {
133
	return RemoteConference::getParticipants();
134 135
}

136 137 138 139
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

140 141
void ClientGroupChatRoom::join () {
	L_D();
142 143
	shared_ptr<CallSession> session =
		static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus->getPrivate()->getSession();
144 145 146 147 148 149 150
	if (!session && (d->state == ChatRoom::State::Instantiated)) {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
		d->setState(ChatRoom::State::CreationPending);
	}
}

Ghislain MARY's avatar
Ghislain MARY committed
151 152
void ClientGroupChatRoom::leave () {
	L_D();
153 154

	RemoteConferencePrivate *conferencePrivate =
155
		static_cast<RemoteConferencePrivate *>(Conference::mPrivate);
156 157 158
	conferencePrivate->eventHandler->unsubscribe();

	shared_ptr<CallSession> session = conferencePrivate->focus->getPrivate()->getSession();
Ghislain MARY's avatar
Ghislain MARY committed
159 160 161 162 163 164
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
165

Ghislain MARY's avatar
Ghislain MARY committed
166 167 168
	d->setState(ChatRoom::State::TerminationPending);
}

169
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
170 171
	L_D();
	SalReferOp *referOp = new SalReferOp(d->core->sal);
172
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
173 174 175 176 177 178 179 180 181
	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();
182 183
}

184
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
185
	RemoteConference::removeParticipants(participants);
186
}
187

188 189
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	L_D();
190

191 192
	if (isAdmin == participant->isAdmin())
		return;
193 194

	if (!getMe()->isAdmin()) {
195 196 197
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
198

199
	SalReferOp *referOp = new SalReferOp(d->core->sal);
200
	LinphoneAddress *lAddr = linphone_address_new(getConferenceAddress().asString().c_str());
201 202 203 204 205 206 207 208 209 210 211
	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();
}

212 213
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
214

215 216 217 218
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
219 220

	if (!getMe()->isAdmin()) {
221 222 223
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
224 225 226

	shared_ptr<CallSession> session =
		static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus->getPrivate()->getSession();
227 228 229 230 231 232
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
233 234
}

235 236 237
// -----------------------------------------------------------------------------

void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
238
	L_D();
239
	static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->conferenceAddress = addr;
240
	d->setState(ChatRoom::State::Created);
241
	_linphone_core_add_group_chat_room(d->core, addr, L_GET_C_BACK_PTR(this));
242 243 244
}

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
245
	L_D();
246
	d->setState(ChatRoom::State::Terminated);
247 248 249
}

void ClientGroupChatRoom::onParticipantAdded (const Address &addr) {
250 251
	if (isMe(addr))
		return;
252

253 254 255 256 257
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
258

259
	participant = ObjectFactory::create<Participant>(addr);
260
	RemoteConference::mPrivate->participants.push_back(participant);
261
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
262 263 264
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
	if (cb)
265
		cb(cr, L_GET_C_BACK_PTR(participant));
266 267 268 269 270
}

void ClientGroupChatRoom::onParticipantRemoved (const Address &addr) {
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
271
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
272 273
		return;
	}
274
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
275 276 277
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
	if (cb)
278
		cb(cr, L_GET_C_BACK_PTR(participant));
279
	RemoteConference::mPrivate->participants.remove(participant);
280 281 282
}

void ClientGroupChatRoom::onParticipantSetAdmin (const Address &addr, bool isAdmin) {
283
	shared_ptr<Participant> participant;
284
	if (isMe(addr))
285
		participant = getMe();
286 287
	else
		participant = findParticipant(addr);
288 289 290 291
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
292
	participant->getPrivate()->setAdmin(isAdmin);
293
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
294 295 296
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
	if (cb)
297
		cb(cr, L_GET_C_BACK_PTR(participant), isAdmin);
298 299
}

300
void ClientGroupChatRoom::onSubjectChanged (const std::string &subject) {
301
	RemoteConference::setSubject(subject);
302 303 304 305 306 307 308
	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);
	if (cb)
		cb(cr, subject.c_str());
}

309
void ClientGroupChatRoom::onParticipantDeviceAdded (const Address &addr, const Address &gruu) {
310
	shared_ptr<Participant> participant;
311
	if (isMe(addr))
312
		participant = getMe();
313 314 315 316 317 318 319 320 321 322
	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) {
323
	shared_ptr<Participant> participant;
324
	if (isMe(addr))
325
		participant = getMe();
326 327 328 329 330 331 332 333 334
	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);
}

335 336
// -----------------------------------------------------------------------------

337
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
338 339 340 341 342
	ParticipantPrivate *participantPrivate =
		static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus->getPrivate();

	if (session == participantPrivate->getSession())
		participantPrivate->removeSession();
343 344
}

345
void ClientGroupChatRoom::onCallSessionStateChanged (const std::shared_ptr<const CallSession> &session, LinphoneCallState state, const string &message) {
Ghislain MARY's avatar
Ghislain MARY committed
346
	L_D();
347
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
348 349 350 351 352
		if (d->state == ChatRoom::State::CreationPending) {
			Address addr(session->getRemoteContact());
			addr.clean();
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
353 354 355 356 357 358 359
				static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->eventHandler->subscribe(
					getConferenceAddress()
				);
		} else if (d->state == ChatRoom::State::TerminationPending)
			static_cast<RemoteConferencePrivate *>(Conference::mPrivate)->focus->getPrivate()->getSession()->terminate();
	} else if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending))
		onConferenceTerminated(getConferenceAddress());
360 361
}

362
LINPHONE_END_NAMESPACE