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/params/call-session-params-p.h"
26
#include "conference/session/call-session-p.h"
27
#include "conference/participant-p.h"
28
#include "content/content.h"
29
#include "hacks/hacks.h"
30
#include "logger/logger.h"
31
#include "sal/refer-op.h"
32

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

35 36 37 38
using namespace std;

LINPHONE_BEGIN_NAMESPACE

39 40
ClientGroupChatRoomPrivate::ClientGroupChatRoomPrivate (LinphoneCore *core)
	: ChatRoomPrivate(core) {}
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// -----------------------------------------------------------------------------

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;
}

57 58 59 60 61
void ClientGroupChatRoomPrivate::notifyReceived (string body) {
	L_Q();
	q->eventHandler->notifyReceived(body);
}

62 63
// =============================================================================

64
ClientGroupChatRoom::ClientGroupChatRoom (LinphoneCore *core, const Address &me, const string &uri, const string &subject)
65
	: ChatRoom(*new ClientGroupChatRoomPrivate(core)), RemoteConference(core, me, nullptr) {
66
	focus = ObjectFactory::create<Participant>(Address(uri));
67
	this->subject = subject;
68 69 70 71
}

// -----------------------------------------------------------------------------

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

78
void ClientGroupChatRoom::addParticipants (const list<Address> &addresses, const CallSessionParams *params, bool hasMedia) {
79
	L_D();
80 81
	if (addresses.empty())
		return;
82 83 84 85
	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;
	}
86 87 88
	list<Address> sortedAddresses(addresses);
	sortedAddresses.sort();
	sortedAddresses.unique();
89 90 91 92 93 94 95 96 97
	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();
98
		session->startInvite(nullptr, subject, &content);
99 100 101
		if (d->state == ChatRoom::State::Instantiated) {
			d->setState(ChatRoom::State::CreationPending);
		}
102
	}
103 104
}

105
bool ClientGroupChatRoom::canHandleParticipants () const {
106
	return RemoteConference::canHandleParticipants();
107 108
}

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

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

int ClientGroupChatRoom::getNbParticipants () const {
118
	return RemoteConference::getNbParticipants();
119 120 121
}

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

125 126 127 128
const string &ClientGroupChatRoom::getSubject () const {
	return RemoteConference::getSubject();
}

129 130 131 132 133 134 135 136 137 138
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
139 140 141 142 143 144 145 146 147 148 149 150 151
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);
}

152
void ClientGroupChatRoom::removeParticipant (const shared_ptr<const Participant> &participant) {
153 154 155 156 157 158 159 160 161 162 163 164
	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();
165 166
}

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

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
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();
}

192 193 194 195 196 197 198 199 200 201 202
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();
203 204 205 206 207 208
	if (session)
		session->update(nullptr, subject);
	else {
		session = d->createSession();
		session->startInvite(nullptr, subject, nullptr);
	}
209 210
}

211 212 213
// -----------------------------------------------------------------------------

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

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

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

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

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

274 275 276 277 278 279 280 281 282
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());
}

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

309 310
// -----------------------------------------------------------------------------

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

316
void ClientGroupChatRoom::onCallSessionStateChanged (const std::shared_ptr<const CallSession> &session, LinphoneCallState state, const string &message) {
Ghislain MARY's avatar
Ghislain MARY committed
317
	L_D();
318
	if (state == LinphoneCallConnected) {
Ghislain MARY's avatar
Ghislain MARY committed
319 320 321 322 323 324 325 326 327 328
		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 {
329
		if ((state == LinphoneCallReleased) && (d->state == ChatRoom::State::TerminationPending))
Ghislain MARY's avatar
Ghislain MARY committed
330
			onConferenceTerminated(conferenceAddress);
331 332 333
	}
}

334
LINPHONE_END_NAMESPACE