client-group-chat-room.cpp 11.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 "client-group-chat-room-p.h"
24
#include "c-wrapper/c-wrapper.h"
25
#include "conference/session/call-session-p.h"
26
#include "conference/participant-p.h"
27
#include "logger/logger.h"
28
#include "sal/refer-op.h"
29

Ronan's avatar
Ronan committed
30
// =============================================================================
31

32 33 34 35
using namespace std;

LINPHONE_BEGIN_NAMESPACE

Ronan's avatar
Ronan committed
36
ClientGroupChatRoomPrivate::ClientGroupChatRoomPrivate (LinphoneCore *core) : ChatRoomPrivate(core) {}
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// -----------------------------------------------------------------------------

shared_ptr<CallSession> ClientGroupChatRoomPrivate::createSession () {
	L_Q();
	CallSessionParams csp;
	csp.addCustomHeader("Require", "recipient-list-invite");
	shared_ptr<CallSession> session = q->focus->getPrivate()->createSession(*q, &csp, false, q);
	session->configure(LinphoneCallOutgoing, nullptr, nullptr, q->me->getAddress(), q->focus->getAddress());
	session->initiateOutgoing();
	Address addr = q->me->getAddress();
	addr.setParam("text");
	session->getPrivate()->getOp()->set_contact_address(addr.getPrivate()->getInternalAddress());
	return session;
}

53 54 55 56 57
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
	L_Q();
	q->eventHandler->notifyReceived(body);
}

58 59
// =============================================================================

60
ClientGroupChatRoom::ClientGroupChatRoom (LinphoneCore *core, const Address &me, const string &uri, const string &subject)
61
	: ChatRoom(*new ClientGroupChatRoomPrivate(core)), RemoteConference(core, me, nullptr) {
62
	focus = ObjectFactory::create<Participant>(Address(uri));
63
	this->subject = subject;
64 65
}

66 67 68
int ClientGroupChatRoom::getCapabilities () const {
	return static_cast<int>(Capabilities::Conference);
}
69

70 71 72 73
void ClientGroupChatRoom::addParticipant (const Address &addr, const CallSessionParams *params, bool hasMedia) {
	list<Address> addresses;
	addresses.push_back(addr);
	addParticipants(addresses, params, hasMedia);
74 75
}

76
void ClientGroupChatRoom::addParticipants (const list<Address> &addresses, const CallSessionParams *params, bool hasMedia) {
77
	L_D();
78 79
	if (addresses.empty())
		return;
80 81 82 83
	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;
	}
84 85 86
	list<Address> sortedAddresses(addresses);
	sortedAddresses.sort();
	sortedAddresses.unique();
87 88 89 90 91 92 93 94 95
	Content content;
	content.setBody(getResourceLists(sortedAddresses));
	content.setContentType("application/resource-lists+xml");
	content.setContentDisposition("recipient-list");
	shared_ptr<CallSession> session = focus->getPrivate()->getSession();
	if (session)
		session->update(nullptr, subject, &content);
	else {
		session = d->createSession();
96
		session->startInvite(nullptr, subject, &content);
97 98 99
		if (d->state == ChatRoom::State::Instantiated) {
			d->setState(ChatRoom::State::CreationPending);
		}
100
	}
101 102
}

103
bool ClientGroupChatRoom::canHandleParticipants () const {
104
	return RemoteConference::canHandleParticipants();
105 106
}

107 108 109 110
shared_ptr<Participant> ClientGroupChatRoom::findParticipant (const Address &addr) const {
	return RemoteConference::findParticipant(addr);
}

111 112
const Address *ClientGroupChatRoom::getConferenceAddress () const {
	return RemoteConference::getConferenceAddress();
113 114 115
}

int ClientGroupChatRoom::getNbParticipants () const {
116
	return RemoteConference::getNbParticipants();
117 118 119
}

list<shared_ptr<Participant>> ClientGroupChatRoom::getParticipants () const {
120
	return RemoteConference::getParticipants();
121 122
}

123 124 125 126
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

127 128 129 130 131 132 133 134 135 136
void ClientGroupChatRoom::join () {
	L_D();
	shared_ptr<CallSession> session = focus->getPrivate()->getSession();
	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
137 138 139 140 141 142 143 144 145 146 147 148 149
void ClientGroupChatRoom::leave () {
	L_D();
	eventHandler->unsubscribe();
	shared_ptr<CallSession> session = focus->getPrivate()->getSession();
	if (session)
		session->terminate();
	else {
		session = d->createSession();
		session->startInvite(nullptr, "", nullptr);
	}
	d->setState(ChatRoom::State::TerminationPending);
}

150
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
151 152 153 154 155 156 157 158 159 160 161 162
	L_D();
	SalReferOp *referOp = new SalReferOp(d->core->sal);
	LinphoneAddress *lAddr = linphone_address_new(conferenceAddress.asString().c_str());
	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();
163 164
}

165
void ClientGroupChatRoom::removeParticipants (const list<shared_ptr<Participant>> &participants) {
166
	RemoteConference::removeParticipants(participants);
167
}
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
void ClientGroupChatRoom::setParticipantAdminStatus (shared_ptr<Participant> &participant, bool isAdmin) {
	L_D();
	if (isAdmin == participant->isAdmin())
		return;
	if (!me->isAdmin()) {
		lError() << "Cannot change the participant admin status because I am not admin";
		return;
	}
	SalReferOp *referOp = new SalReferOp(d->core->sal);
	LinphoneAddress *lAddr = linphone_address_new(conferenceAddress.asString().c_str());
	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();
}

190 191 192 193 194 195 196 197 198 199 200
void ClientGroupChatRoom::setSubject (const string &subject) {
	L_D();
	if (d->state != ChatRoom::State::Created) {
		lError() << "Cannot change the ClientGroupChatRoom subject in a state other than Created";
		return;
	}
	if (!me->isAdmin()) {
		lError() << "Cannot change the ClientGroupChatRoom subject because I am not admin";
		return;
	}
	shared_ptr<CallSession> session = focus->getPrivate()->getSession();
201 202 203 204 205 206
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
207 208
}

209 210 211
// -----------------------------------------------------------------------------

void ClientGroupChatRoom::onConferenceCreated (const Address &addr) {
212
	L_D();
213
	conferenceAddress = addr;
214
	d->setState(ChatRoom::State::Created);
215
	_linphone_core_add_group_chat_room(d->core, addr, L_GET_C_BACK_PTR(this));
216 217 218
}

void ClientGroupChatRoom::onConferenceTerminated (const Address &addr) {
219
	L_D();
220
	d->setState(ChatRoom::State::Terminated);
221 222 223
}

void ClientGroupChatRoom::onParticipantAdded (const Address &addr) {
224 225
	if (isMe(addr))
		return;
226 227 228 229 230
	shared_ptr<Participant> participant = findParticipant(addr);
	if (participant) {
		lWarning() << "Participant " << participant << " added but already in the list of participants!";
		return;
	}
231
	participant = ObjectFactory::create<Participant>(addr);
232
	participants.push_back(participant);
233
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
234 235 236
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
	if (cb)
237
		cb(cr, L_GET_C_BACK_PTR(participant));
238 239 240 241 242
}

void ClientGroupChatRoom::onParticipantRemoved (const Address &addr) {
	shared_ptr<Participant> participant = findParticipant(addr);
	if (!participant) {
243
		lWarning() << "Participant " << addr.asString() << " removed but not in the list of participants!";
244 245
		return;
	}
246
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
247 248 249
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
	if (cb)
250
		cb(cr, L_GET_C_BACK_PTR(participant));
251 252 253 254
	participants.remove(participant);
}

void ClientGroupChatRoom::onParticipantSetAdmin (const Address &addr, bool isAdmin) {
255
	shared_ptr<Participant> participant;
256 257 258 259
	if (isMe(addr))
		participant = me;
	else
		participant = findParticipant(addr);
260 261 262 263
	if (!participant) {
		lWarning() << "Participant " << participant << " admin status has been changed but is not in the list of participants!";
		return;
	}
264
	participant->getPrivate()->setAdmin(isAdmin);
265
	LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
266 267 268
	LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
	LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
	if (cb)
269
		cb(cr, L_GET_C_BACK_PTR(participant), isAdmin);
270 271
}

272 273 274 275 276 277 278 279 280
void ClientGroupChatRoom::onSubjectChanged (const std::string &subject) {
	this->subject = subject;
	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());
}

281
void ClientGroupChatRoom::onParticipantDeviceAdded (const Address &addr, const Address &gruu) {
282
	shared_ptr<Participant> participant;
283 284 285 286 287 288 289 290 291 292 293 294
	if (isMe(addr))
		participant = me;
	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) {
295
	shared_ptr<Participant> participant;
296 297 298 299 300 301 302 303 304 305 306
	if (isMe(addr))
		participant = me;
	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);
}

307 308
// -----------------------------------------------------------------------------

309
void ClientGroupChatRoom::onCallSessionSetReleased (const std::shared_ptr<const CallSession> &session) {
310 311 312 313
	if (session == focus->getPrivate()->getSession())
		focus->getPrivate()->removeSession();
}

314
void ClientGroupChatRoom::onCallSessionStateChanged (const std::shared_ptr<const CallSession> &session, LinphoneCallState state, const string &message) {
Ghislain MARY's avatar
Ghislain MARY committed
315
	L_D();
316
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
317 318 319 320 321 322 323 324 325 326
		if (d->state == ChatRoom::State::CreationPending) {
			Address addr(session->getRemoteContact());
			addr.clean();
			onConferenceCreated(addr);
			if (session->getRemoteContactAddress()->hasParam("isfocus"))
				eventHandler->subscribe(conferenceAddress);
		} else if (d->state == ChatRoom::State::TerminationPending) {
			focus->getPrivate()->getSession()->terminate();
		}
	} else {
327
		if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending))
Ghislain MARY's avatar
Ghislain MARY committed
328
			onConferenceTerminated(conferenceAddress);
329 330 331
	}
}

332
LINPHONE_END_NAMESPACE