c-chat-room.cpp 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * c-chat-room.cpp
 * Copyright (C) 2017  Belledonne Communications SARL
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19
// TODO: Remove me later.
20
#include "linphone/chat.h"
Ronan's avatar
Ronan committed
21

22
#include "linphone/wrapper_utils.h"
23
#include "linphone/api/c-chat-room.h"
24

25
#include "c-wrapper/c-wrapper.h"
26 27 28 29
#include "chat/basic-chat-room.h"
#include "chat/client-group-chat-room.h"
#include "chat/real-time-text-chat-room-p.h"

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

Ronan's avatar
Ronan committed
32
using namespace std;
33

Ronan's avatar
Ronan committed
34 35
static void _linphone_chat_room_constructor (LinphoneChatRoom *cr);
static void _linphone_chat_room_destructor (LinphoneChatRoom *cr);
36

37
L_DECLARE_C_OBJECT_IMPL_WITH_XTORS(
38
	ChatRoom,
39
	_linphone_chat_room_constructor, _linphone_chat_room_destructor,
40
	LinphoneChatRoomCbs *cbs;
41 42 43
	LinphoneAddress *peerAddressCache;
)

Ronan's avatar
Ronan committed
44
static void _linphone_chat_room_constructor (LinphoneChatRoom *cr) {
45 46
	cr->cbs = linphone_chat_room_cbs_new();
}
47

Ronan's avatar
Ronan committed
48
static void _linphone_chat_room_destructor (LinphoneChatRoom *cr) {
49 50
	linphone_chat_room_cbs_unref(cr->cbs);
	cr->cbs = nullptr;
51 52 53 54 55 56
	if (cr->peerAddressCache) {
		linphone_address_unref(cr->peerAddressCache);
		cr->peerAddressCache = nullptr;
	}
}

Ronan's avatar
Ronan committed
57 58 59
// =============================================================================
// Public functions.
// =============================================================================
60

Ronan's avatar
Ronan committed
61
void linphone_chat_room_release (LinphoneChatRoom *cr) {
62
	L_GET_PRIVATE_FROM_C_OBJECT(cr)->release();
63 64
}

Ronan's avatar
Ronan committed
65
void linphone_chat_room_remove_transient_message (LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
66
	L_GET_PRIVATE_FROM_C_OBJECT(cr)->removeTransientMessage(msg);
67 68
}

Ronan's avatar
Ronan committed
69
void linphone_chat_room_send_message (LinphoneChatRoom *cr, const char *msg) {
70
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->sendMessage(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->createMessage(msg));
71 72
}

Ronan's avatar
Ronan committed
73
bool_t linphone_chat_room_is_remote_composing (const LinphoneChatRoom *cr) {
74
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->isRemoteComposing();
75 76
}

Ronan's avatar
Ronan committed
77
LinphoneCore *linphone_chat_room_get_lc (const LinphoneChatRoom *cr) {
78 79 80
	return linphone_chat_room_get_core(cr);
}

Ronan's avatar
Ronan committed
81
LinphoneCore *linphone_chat_room_get_core (const LinphoneChatRoom *cr) {
82
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getCore();
83 84
}

Ronan's avatar
Ronan committed
85
const LinphoneAddress *linphone_chat_room_get_peer_address (LinphoneChatRoom *cr) {
86 87 88
	if (cr->peerAddressCache) {
		linphone_address_unref(cr->peerAddressCache);
	}
89
	cr->peerAddressCache = linphone_address_new(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getPeerAddress().asString().c_str());
90 91 92
	return cr->peerAddressCache;
}

Ronan's avatar
Ronan committed
93
LinphoneChatMessage *linphone_chat_room_create_message (LinphoneChatRoom *cr, const char *message) {
94
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->createMessage(message ? message : "");
95 96
}

Ronan's avatar
Ronan committed
97 98 99 100 101 102 103 104 105
LinphoneChatMessage *linphone_chat_room_create_message_2 (
	LinphoneChatRoom *cr,
	const char *message,
	const char *external_body_url,
	LinphoneChatMessageState state,
	time_t time,
	bool_t is_read,
	bool_t is_incoming
) {
106 107
	LinphoneChatMessage *msg = linphone_chat_room_create_message(cr, message);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
108 109 110
	linphone_chat_message_set_external_body_url(msg, external_body_url ? ms_strdup(external_body_url) : NULL);
	linphone_chat_message_set_time(msg, time);
	linphone_chat_message_set_is_secured(msg, FALSE);
111 112
	linphone_chat_message_set_state(msg, state);
	if (is_incoming) {
113 114 115
		linphone_chat_message_set_incoming(msg);
		linphone_chat_message_set_from_address(msg, linphone_chat_room_get_peer_address(cr));
		linphone_chat_message_set_to_address(msg, linphone_address_new(linphone_core_get_identity(lc)));
116
	} else {
117 118 119
		linphone_chat_message_set_outgoing(msg);
		linphone_chat_message_set_to_address(msg, linphone_chat_room_get_peer_address(cr));
		linphone_chat_message_set_from_address(msg, linphone_address_new(linphone_core_get_identity(lc)));
120 121 122 123
	}
	return msg;
}

Ronan's avatar
Ronan committed
124 125 126 127 128 129
void linphone_chat_room_send_message2 (
	LinphoneChatRoom *cr,
	LinphoneChatMessage *msg,
	LinphoneChatMessageStateChangedCb status_cb,
	void *ud
) {
130 131
	linphone_chat_message_set_message_state_changed_cb(msg, status_cb);
	linphone_chat_message_set_message_state_changed_cb_user_data(msg, ud);
132
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->sendMessage(msg);
133 134
}

Ronan's avatar
Ronan committed
135
void linphone_chat_room_send_chat_message_2 (LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
136
	linphone_chat_message_ref(msg);
137
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->sendMessage(msg);
138 139
}

Ronan's avatar
Ronan committed
140
void linphone_chat_room_send_chat_message (LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
141
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->sendMessage(msg);
142 143
}

Ronan's avatar
Ronan committed
144
uint32_t linphone_chat_room_get_char (const LinphoneChatRoom *cr) {
145
	if (linphone_core_realtime_text_enabled(linphone_chat_room_get_core(cr)))
146
		return static_cast<const LinphonePrivate::RealTimeTextChatRoom *>(L_GET_CPP_PTR_FROM_C_OBJECT(cr).get())->getChar();
147 148 149
	return 0;
}

Ronan's avatar
Ronan committed
150
void linphone_chat_room_compose (LinphoneChatRoom *cr) {
151
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->compose();
152 153
}

Ronan's avatar
Ronan committed
154
LinphoneCall *linphone_chat_room_get_call (const LinphoneChatRoom *cr) {
155
	if (linphone_core_realtime_text_enabled(linphone_chat_room_get_core(cr)))
156
		return static_cast<const LinphonePrivate::RealTimeTextChatRoom *>(L_GET_CPP_PTR_FROM_C_OBJECT(cr).get())->getCall();
157 158 159
	return nullptr;
}

Ronan's avatar
Ronan committed
160
void linphone_chat_room_set_call (LinphoneChatRoom *cr, LinphoneCall *call) {
161
	if (linphone_core_realtime_text_enabled(linphone_chat_room_get_core(cr)))
162
		static_cast<LinphonePrivate::RealTimeTextChatRoomPrivate *>(L_GET_PRIVATE_FROM_C_OBJECT(cr))->setCall(call);
163 164
}

Ronan's avatar
Ronan committed
165
bctbx_list_t *linphone_chat_room_get_transient_messages (const LinphoneChatRoom *cr) {
166
	return L_GET_C_LIST_FROM_CPP_LIST(L_GET_PRIVATE_FROM_C_OBJECT(cr)->getTransientMessages());
167 168
}

Ronan's avatar
Ronan committed
169
void linphone_chat_room_mark_as_read (LinphoneChatRoom *cr) {
170
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->markAsRead();
171 172
}

Ronan's avatar
Ronan committed
173
int linphone_chat_room_get_unread_messages_count (LinphoneChatRoom *cr) {
174
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getUnreadMessagesCount();
175 176
}

Ronan's avatar
Ronan committed
177
int linphone_chat_room_get_history_size (LinphoneChatRoom *cr) {
178
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistorySize();
179 180
}

Ronan's avatar
Ronan committed
181
void linphone_chat_room_delete_message (LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
182
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->deleteMessage(msg);
183 184
}

Ronan's avatar
Ronan committed
185
void linphone_chat_room_delete_history (LinphoneChatRoom *cr) {
186
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->deleteHistory();
187 188
}

Ronan's avatar
Ronan committed
189
bctbx_list_t *linphone_chat_room_get_history_range (LinphoneChatRoom *cr, int startm, int endm) {
190
	return L_GET_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistoryRange(startm, endm));
191 192
}

Ronan's avatar
Ronan committed
193
bctbx_list_t *linphone_chat_room_get_history (LinphoneChatRoom *cr, int nb_message) {
194
	return L_GET_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistory(nb_message));
195 196
}

Ronan's avatar
Ronan committed
197
LinphoneChatMessage *linphone_chat_room_find_message (LinphoneChatRoom *cr, const char *message_id) {
198
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->findMessage(message_id);
199 200
}

Ronan's avatar
Ronan committed
201
LinphoneChatRoomCbs *linphone_chat_room_get_callbacks (const LinphoneChatRoom *cr) {
202 203 204
	return cr->cbs;
}

205
LinphoneChatRoomState linphone_chat_room_get_state (const LinphoneChatRoom *cr) {
206
	return (LinphoneChatRoomState)L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getState();
207 208
}

Ronan's avatar
Ronan committed
209
LinphoneParticipant *linphone_chat_room_add_participant (LinphoneChatRoom *cr, const LinphoneAddress *addr) {
210
	return L_GET_C_BACK_PTR(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->addParticipant(
211 212
		*L_GET_CPP_PTR_FROM_C_OBJECT(addr), nullptr, false)
	);
213 214 215
}

void linphone_chat_room_add_participants (LinphoneChatRoom *cr, const bctbx_list_t *addresses) {
216
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->addParticipants(L_GET_RESOLVED_CPP_LIST_FROM_C_LIST(addresses, Address), nullptr, false);
217 218 219
}

bool_t linphone_chat_room_can_handle_participants (const LinphoneChatRoom *cr) {
220
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->canHandleParticipants();
221 222
}

Ronan's avatar
Ronan committed
223
const char *linphone_chat_room_get_id (const LinphoneChatRoom *cr) {
224
	string id = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getId();
225 226 227 228
	return id.empty() ? nullptr : id.c_str();
}

int linphone_chat_room_get_nb_participants (const LinphoneChatRoom *cr) {
229
	return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getNbParticipants();
230 231
}

Ronan's avatar
Ronan committed
232
bctbx_list_t *linphone_chat_room_get_participants (const LinphoneChatRoom *cr) {
233
	return L_GET_RESOLVED_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getParticipants());
234 235 236
}

void linphone_chat_room_remove_participant (LinphoneChatRoom *cr, LinphoneParticipant *participant) {
237
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->removeParticipant(L_GET_CPP_PTR_FROM_C_OBJECT(participant));
238 239 240
}

void linphone_chat_room_remove_participants (LinphoneChatRoom *cr, const bctbx_list_t *participants) {
241
	L_GET_CPP_PTR_FROM_C_OBJECT(cr)->removeParticipants(L_GET_RESOLVED_CPP_LIST_FROM_C_LIST(participants, Participant));
242 243
}

Ronan's avatar
Ronan committed
244 245 246
// =============================================================================
// Reference and user data handling functions.
// =============================================================================
247

Ronan's avatar
Ronan committed
248
LinphoneChatRoom *linphone_chat_room_ref (LinphoneChatRoom *cr) {
249 250 251 252
	belle_sip_object_ref(cr);
	return cr;
}

Ronan's avatar
Ronan committed
253
void linphone_chat_room_unref (LinphoneChatRoom *cr) {
254 255 256
	belle_sip_object_unref(cr);
}

Ronan's avatar
Ronan committed
257
void *linphone_chat_room_get_user_data (const LinphoneChatRoom *cr) {
258
	return L_GET_USER_DATA_FROM_C_OBJECT(cr);
259 260
}

Ronan's avatar
Ronan committed
261
void linphone_chat_room_set_user_data (LinphoneChatRoom *cr, void *ud) {
262
	L_SET_USER_DATA_FROM_C_OBJECT(cr, ud);
263 264
}

Ronan's avatar
Ronan committed
265 266 267
// =============================================================================
// Constructor and destructor functions.
// =============================================================================
268

Ronan's avatar
Ronan committed
269
LinphoneChatRoom *linphone_chat_room_new (LinphoneCore *core, const LinphoneAddress *addr) {
Ronan's avatar
Ronan committed
270
	LinphoneChatRoom *cr = _linphone_ChatRoom_init();
271
	if (linphone_core_realtime_text_enabled(core))
272
		L_SET_CPP_PTR_FROM_C_OBJECT(cr, std::make_shared<LinphonePrivate::RealTimeTextChatRoom>(core, *L_GET_CPP_PTR_FROM_C_OBJECT(addr)));
273
	else
274
		L_SET_CPP_PTR_FROM_C_OBJECT(cr, std::make_shared<LinphonePrivate::BasicChatRoom>(core, *L_GET_CPP_PTR_FROM_C_OBJECT(addr)));
275
	linphone_core_notify_chat_room_instantiated(core, cr);
276 277
	L_GET_PRIVATE_FROM_C_OBJECT(cr)->setState(LinphonePrivate::ChatRoom::State::Instantiated);
	L_GET_PRIVATE_FROM_C_OBJECT(cr)->setState(LinphonePrivate::ChatRoom::State::Created);
278 279 280
	return cr;
}

281
LinphoneChatRoom *linphone_client_group_chat_room_new (LinphoneCore *core) {
282
	const char *factoryUri = linphone_core_get_chat_conference_factory_uri(core);
283 284 285
	if (!factoryUri)
		return nullptr;
	LinphoneAddress *factoryAddr = linphone_address_new(factoryUri);
286
	LinphoneProxyConfig *proxy = linphone_core_lookup_known_proxy(core, factoryAddr);
287 288 289
	linphone_address_unref(factoryAddr);
	std::string from;
	if (proxy)
290
		from = L_GET_CPP_PTR_FROM_C_OBJECT(linphone_proxy_config_get_identity_address(proxy))->asString();
291
	if (from.empty())
292
		from = linphone_core_get_primary_contact(core);
293
	LinphonePrivate::Address me(from);
Ronan's avatar
Ronan committed
294
	LinphoneChatRoom *cr = _linphone_ChatRoom_init();
295
	L_SET_CPP_PTR_FROM_C_OBJECT(cr, make_shared<LinphonePrivate::ClientGroupChatRoom>(core, me));
296
	linphone_core_notify_chat_room_instantiated(core, cr);
297
	L_GET_PRIVATE_FROM_C_OBJECT(cr)->setState(LinphonePrivate::ChatRoom::State::Instantiated);
298 299 300 301
	return cr;
}

/* DEPRECATED */
Ronan's avatar
Ronan committed
302
void linphone_chat_room_destroy (LinphoneChatRoom *cr) {
303 304
	linphone_chat_room_unref(cr);
}