vtables.c 14.1 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
Copyright (C) 2010  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 2
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, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Simon Morlat's avatar
Simon Morlat committed
19 20
*/

21 22 23
#include "c-wrapper/c-wrapper.h"
#include "core/core-p.h"

Simon Morlat's avatar
Simon Morlat committed
24
#include "private.h"
25
#include "linphone/wrapper_utils.h"
Simon Morlat's avatar
Simon Morlat committed
26 27 28 29 30 31 32 33 34 35


LinphoneCoreVTable *linphone_core_v_table_new() {
	return ms_new0(LinphoneCoreVTable,1);
}

void linphone_core_v_table_set_user_data(LinphoneCoreVTable *table, void *data) {
	table->user_data = data;
}

36
void* linphone_core_v_table_get_user_data(const LinphoneCoreVTable *table) {
Simon Morlat's avatar
Simon Morlat committed
37 38 39 40 41 42 43 44
	return table->user_data;
}

void linphone_core_v_table_destroy(LinphoneCoreVTable* table) {
	ms_free(table);
}

LinphoneCoreVTable *linphone_core_get_current_vtable(LinphoneCore *lc) {
45 46
	if (lc->current_cbs != NULL) return lc->current_cbs->vtable;
	else return NULL;
Simon Morlat's avatar
Simon Morlat committed
47 48 49
}

static void cleanup_dead_vtable_refs(LinphoneCore *lc){
50
	bctbx_list_t *it,*next_it;
51

52
	if (lc->vtable_notify_recursion > 0) return; /*don't cleanup vtable if we are iterating through a listener list.*/
Simon Morlat's avatar
Simon Morlat committed
53 54 55
	for(it=lc->vtable_refs; it!=NULL; ){
		VTableReference *ref=(VTableReference*)it->data;
		next_it=it->next;
Simon Morlat's avatar
Simon Morlat committed
56
		if (ref->valid == 0){
57
			lc->vtable_refs=bctbx_list_erase_link(lc->vtable_refs, it);
Simon Morlat's avatar
Simon Morlat committed
58
			belle_sip_object_unref(ref->cbs);
Simon Morlat's avatar
Simon Morlat committed
59 60 61 62 63 64
			ms_free(ref);
		}
		it=next_it;
	}
}

65
#define NOTIFY_IF_EXIST(function_name, ...) \
66
	bctbx_list_t* iterator; \
Simon Morlat's avatar
Simon Morlat committed
67
	VTableReference *ref; \
68
	bool_t has_cb = FALSE; \
69 70
	lc->vtable_notify_recursion++;\
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next){\
71 72
		if ((ref=(VTableReference*)iterator->data)->valid && (lc->current_cbs=ref->cbs)->vtable->function_name) {\
			lc->current_cbs->vtable->function_name(__VA_ARGS__);\
73 74
			has_cb = TRUE;\
		}\
75 76 77
	}\
	lc->vtable_notify_recursion--;\
	if (has_cb) ms_message("Linphone core [%p] notified [%s]",lc,#function_name)
Simon Morlat's avatar
Simon Morlat committed
78

79
#define NOTIFY_IF_EXIST_INTERNAL(function_name, internal_val, ...) \
80
	bctbx_list_t* iterator; \
81
	VTableReference *ref; \
82 83
	lc->vtable_notify_recursion++;\
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next){\
84 85
		if ((ref=(VTableReference*)iterator->data)->valid && (lc->current_cbs=ref->cbs)->vtable->function_name && (ref->internal == internal_val)) {\
			lc->current_cbs->vtable->function_name(__VA_ARGS__);\
86
		}\
87 88
	}\
	lc->vtable_notify_recursion--;
89

Simon Morlat's avatar
Simon Morlat committed
90
void linphone_core_notify_global_state_changed(LinphoneCore *lc, LinphoneGlobalState gstate, const char *message) {
91
	NOTIFY_IF_EXIST(global_state_changed,lc,gstate,message);
Simon Morlat's avatar
Simon Morlat committed
92 93 94 95
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message){
96
	NOTIFY_IF_EXIST(call_state_changed, lc,call,cstate,message);
Simon Morlat's avatar
Simon Morlat committed
97 98 99 100
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_encryption_changed(LinphoneCore *lc, LinphoneCall *call, bool_t on, const char *authentication_token) {
101
	NOTIFY_IF_EXIST(call_encryption_changed, lc,call,on,authentication_token);
Simon Morlat's avatar
Simon Morlat committed
102 103 104 105
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_registration_state_changed(LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
106
	L_GET_PRIVATE_FROM_C_OBJECT(lc)->notifyRegistrationStateChanged(cfg, cstate, message);
107
	NOTIFY_IF_EXIST(registration_state_changed, lc,cfg,cstate,message);
Simon Morlat's avatar
Simon Morlat committed
108 109 110
	cleanup_dead_vtable_refs(lc);
}

111 112
void linphone_core_notify_notify_presence_received(LinphoneCore *lc, LinphoneFriend * lf) {
	NOTIFY_IF_EXIST(notify_presence_received, lc, lf);
Simon Morlat's avatar
Simon Morlat committed
113 114 115
	cleanup_dead_vtable_refs(lc);
}

116 117
void linphone_core_notify_notify_presence_received_for_uri_or_tel(LinphoneCore *lc, LinphoneFriend *lf, const char *uri_or_tel, const LinphonePresenceModel *presence_model) {
	NOTIFY_IF_EXIST(notify_presence_received_for_uri_or_tel, lc, lf, uri_or_tel, presence_model);
Simon Morlat's avatar
Simon Morlat committed
118 119 120
	cleanup_dead_vtable_refs(lc);
}

121 122
void linphone_core_notify_new_subscription_requested(LinphoneCore *lc, LinphoneFriend *lf, const char *url) {
	NOTIFY_IF_EXIST(new_subscription_requested, lc, lf, url);
Simon Morlat's avatar
Simon Morlat committed
123 124 125
	cleanup_dead_vtable_refs(lc);
}

126 127 128

void linphone_core_notify_authentication_requested(LinphoneCore *lc, LinphoneAuthInfo *ai, LinphoneAuthMethod method) {
	NOTIFY_IF_EXIST(authentication_requested, lc, ai, method);
129 130 131 132 133
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_log_updated(LinphoneCore *lc, LinphoneCallLog *newcl) {
	NOTIFY_IF_EXIST(call_log_updated, lc, newcl);
Simon Morlat's avatar
Simon Morlat committed
134 135
	cleanup_dead_vtable_refs(lc);
}
136
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
137
#pragma GCC diagnostic push
138
#endif
139 140 141
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
142
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
143
#endif
Simon Morlat's avatar
Simon Morlat committed
144

145 146 147 148 149
void linphone_core_notify_auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
	NOTIFY_IF_EXIST(auth_info_requested, lc, realm, username, domain);
	cleanup_dead_vtable_refs(lc);
}

Simon Morlat's avatar
Simon Morlat committed
150
void linphone_core_notify_text_message_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message){
151
	NOTIFY_IF_EXIST(text_received, lc,room,from,message);
Simon Morlat's avatar
Simon Morlat committed
152 153
	cleanup_dead_vtable_refs(lc);
}
154
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
155
#pragma GCC diagnostic pop
156
#endif
Simon Morlat's avatar
Simon Morlat committed
157 158

void linphone_core_notify_message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *message){
159
	NOTIFY_IF_EXIST(message_received, lc,room,message);
Simon Morlat's avatar
Simon Morlat committed
160 161
	cleanup_dead_vtable_refs(lc);
}
162 163 164 165 166

void linphone_core_notify_message_received_unable_decrypt(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *message){
	NOTIFY_IF_EXIST(message_received_unable_decrypt, lc,room,message);
	cleanup_dead_vtable_refs(lc);
}
167
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
168
#pragma GCC diagnostic push
169
#endif
170 171 172
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
173
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
174
#endif
Simon Morlat's avatar
Simon Morlat committed
175
void linphone_core_notify_file_transfer_recv(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, const char* buff, size_t size) {
176
	NOTIFY_IF_EXIST(file_transfer_recv, lc,message,content,buff,size);
Simon Morlat's avatar
Simon Morlat committed
177 178 179 180
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_file_transfer_send(LinphoneCore *lc, LinphoneChatMessage *message,  const LinphoneContent* content, char* buff, size_t* size) {
181
	NOTIFY_IF_EXIST(file_transfer_send, lc,message,content,buff,size);
Simon Morlat's avatar
Simon Morlat committed
182 183 184 185
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_file_transfer_progress_indication(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t total) {
186
	NOTIFY_IF_EXIST(file_transfer_progress_indication, lc,message,content,offset,total);
Simon Morlat's avatar
Simon Morlat committed
187 188
	cleanup_dead_vtable_refs(lc);
}
189
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
190
#pragma GCC diagnostic pop
191
#endif
Simon Morlat's avatar
Simon Morlat committed
192
void linphone_core_notify_is_composing_received(LinphoneCore *lc, LinphoneChatRoom *room) {
193 194 195 196 197
	LinphoneImNotifPolicy *policy = linphone_core_get_im_notif_policy(lc);
	if (linphone_im_notif_policy_get_recv_is_composing(policy) == TRUE) {
		NOTIFY_IF_EXIST(is_composing_received, lc,room);
		cleanup_dead_vtable_refs(lc);
	}
Simon Morlat's avatar
Simon Morlat committed
198 199 200
}

void linphone_core_notify_dtmf_received(LinphoneCore* lc, LinphoneCall *call, int dtmf) {
201
	NOTIFY_IF_EXIST(dtmf_received, lc,call,dtmf);
Simon Morlat's avatar
Simon Morlat committed
202 203 204 205
	cleanup_dead_vtable_refs(lc);
}

bool_t linphone_core_dtmf_received_has_listener(const LinphoneCore* lc) {
206
	bctbx_list_t* iterator;
Simon Morlat's avatar
Simon Morlat committed
207 208
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next){
		VTableReference *ref=(VTableReference*)iterator->data;
209
		if (ref->valid && ref->cbs->vtable->dtmf_received)
Simon Morlat's avatar
Simon Morlat committed
210 211 212 213 214 215
			return TRUE;
	}
	return FALSE;
}

void linphone_core_notify_refer_received(LinphoneCore *lc, const char *refer_to) {
216
	NOTIFY_IF_EXIST(refer_received, lc,refer_to);
Simon Morlat's avatar
Simon Morlat committed
217 218 219 220
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_buddy_info_updated(LinphoneCore *lc, LinphoneFriend *lf) {
221
	NOTIFY_IF_EXIST(buddy_info_updated, lc,lf);
Simon Morlat's avatar
Simon Morlat committed
222 223 224 225
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_transfer_state_changed(LinphoneCore *lc, LinphoneCall *transfered, LinphoneCallState new_call_state) {
226
	NOTIFY_IF_EXIST(transfer_state_changed, lc,transfered,new_call_state);
Simon Morlat's avatar
Simon Morlat committed
227 228 229 230
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_stats_updated(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallStats *stats) {
231
	NOTIFY_IF_EXIST(call_stats_updated, lc,call,stats);
Simon Morlat's avatar
Simon Morlat committed
232 233 234 235
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_info_received(LinphoneCore *lc, LinphoneCall *call, const LinphoneInfoMessage *msg) {
236
	NOTIFY_IF_EXIST(info_received, lc,call,msg);
Simon Morlat's avatar
Simon Morlat committed
237 238 239 240
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_configuring_status(LinphoneCore *lc, LinphoneConfiguringState status, const char *message) {
241
	NOTIFY_IF_EXIST(configuring_status, lc,status,message);
Simon Morlat's avatar
Simon Morlat committed
242 243 244 245
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_network_reachable(LinphoneCore *lc, bool_t reachable) {
246
	L_GET_PRIVATE_FROM_C_OBJECT(lc)->notifyNetworkReachable(!!lc->sip_network_reachable, lc->media_network_reachable);
247
	NOTIFY_IF_EXIST(network_reachable, lc,reachable);
Simon Morlat's avatar
Simon Morlat committed
248 249 250 251
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *notified_event, const LinphoneContent *body) {
252
	NOTIFY_IF_EXIST_INTERNAL(notify_received, linphone_event_is_internal(lev), lc, lev, notified_event, body);
Simon Morlat's avatar
Simon Morlat committed
253 254 255 256
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_subscription_state_changed(LinphoneCore *lc, LinphoneEvent *lev, LinphoneSubscriptionState state) {
257
	NOTIFY_IF_EXIST_INTERNAL(subscription_state_changed,linphone_event_is_internal(lev), lc,lev,state);
Simon Morlat's avatar
Simon Morlat committed
258 259 260 261
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_publish_state_changed(LinphoneCore *lc, LinphoneEvent *lev, LinphonePublishState state) {
262
	NOTIFY_IF_EXIST_INTERNAL(publish_state_changed, linphone_event_is_internal(lev), lc, lev, state);
Simon Morlat's avatar
Simon Morlat committed
263 264 265 266
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_log_collection_upload_state_changed(LinphoneCore *lc, LinphoneCoreLogCollectionUploadState state, const char *info) {
267
	NOTIFY_IF_EXIST(log_collection_upload_state_changed, lc, state, info);
Simon Morlat's avatar
Simon Morlat committed
268 269 270 271
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_log_collection_upload_progress_indication(LinphoneCore *lc, size_t offset, size_t total) {
272
	NOTIFY_IF_EXIST(log_collection_upload_progress_indication, lc, offset, total);
Simon Morlat's avatar
Simon Morlat committed
273 274 275
	cleanup_dead_vtable_refs(lc);
}

276 277 278 279 280 281 282 283 284 285
void linphone_core_notify_friend_list_created(LinphoneCore *lc, LinphoneFriendList *list) {
	NOTIFY_IF_EXIST(friend_list_created, lc, list);
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_friend_list_removed(LinphoneCore *lc, LinphoneFriendList *list) {
	NOTIFY_IF_EXIST(friend_list_removed, lc, list);
	cleanup_dead_vtable_refs(lc);
}

286 287 288 289 290
void linphone_core_notify_call_created(LinphoneCore *lc, LinphoneCall *call) {
	NOTIFY_IF_EXIST(call_created, lc, call);
	cleanup_dead_vtable_refs(lc);
}

Ghislain MARY's avatar
Ghislain MARY committed
291 292 293 294 295
void linphone_core_notify_version_update_check_result_received(LinphoneCore *lc, LinphoneVersionUpdateCheckResult result, const char *version, const char *url) {
	NOTIFY_IF_EXIST(version_update_check_result_received, lc, result, version, url);
	cleanup_dead_vtable_refs(lc);
}

296 297
void linphone_core_notify_chat_room_state_changed (LinphoneCore *lc, LinphoneChatRoom *cr, LinphoneChatRoomState state) {
	NOTIFY_IF_EXIST(chat_room_state_changed, lc, cr, state);
298 299 300
	cleanup_dead_vtable_refs(lc);
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
void linphone_core_notify_ec_calibration_result(LinphoneCore *lc, LinphoneEcCalibratorStatus status, int delay_ms) {
	NOTIFY_IF_EXIST(ec_calibration_result, lc, status, delay_ms);
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_ec_calibration_audio_init(LinphoneCore *lc) {
	NOTIFY_IF_EXIST(ec_calibration_audio_init, lc);
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_ec_calibration_audio_uninit(LinphoneCore *lc) {
	NOTIFY_IF_EXIST(ec_calibration_audio_uninit, lc);
	cleanup_dead_vtable_refs(lc);
}

316
static VTableReference * v_table_reference_new(LinphoneCoreCbs *cbs, bool_t internal){
Simon Morlat's avatar
Simon Morlat committed
317
	VTableReference *ref=ms_new0(VTableReference,1);
318
	ref->valid=TRUE;
319
	ref->internal = internal;
Ghislain MARY's avatar
Ghislain MARY committed
320
	ref->cbs=linphone_core_cbs_ref(cbs);
Simon Morlat's avatar
Simon Morlat committed
321 322 323 324
	return ref;
}

void v_table_reference_destroy(VTableReference *ref){
Ghislain MARY's avatar
Ghislain MARY committed
325
	linphone_core_cbs_unref(ref->cbs);
Simon Morlat's avatar
Simon Morlat committed
326 327 328
	ms_free(ref);
}

329 330 331
void _linphone_core_add_callbacks(LinphoneCore *lc, LinphoneCoreCbs *vtable, bool_t internal) {
	ms_message("Core callbacks [%p] registered on core [%p]", vtable, lc);
	lc->vtable_refs=bctbx_list_append(lc->vtable_refs,v_table_reference_new(vtable, internal));
Simon Morlat's avatar
Simon Morlat committed
332 333 334
}

void linphone_core_add_listener(LinphoneCore *lc, LinphoneCoreVTable *vtable){
335 336 337
	LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
	_linphone_core_cbs_set_v_table(cbs, vtable, FALSE);
	_linphone_core_add_callbacks(lc, cbs, FALSE);
Ghislain MARY's avatar
Ghislain MARY committed
338
	linphone_core_cbs_unref(cbs);
339 340 341 342
}

void linphone_core_add_callbacks(LinphoneCore *lc, LinphoneCoreCbs *cbs) {
	_linphone_core_add_callbacks(lc, cbs, FALSE);
Simon Morlat's avatar
Simon Morlat committed
343 344 345
}

void linphone_core_remove_listener(LinphoneCore *lc, const LinphoneCoreVTable *vtable) {
346
	bctbx_list_t *it;
347
	ms_message("Vtable [%p] unregistered on core [%p]",vtable,lc);
Simon Morlat's avatar
Simon Morlat committed
348 349
	for(it=lc->vtable_refs; it!=NULL; it=it->next){
		VTableReference *ref=(VTableReference*)it->data;
350 351 352 353 354 355
		if (ref->cbs->vtable==vtable) {
			ref->valid=FALSE;
		}
	}
}

356
bctbx_list_t *linphone_core_get_callbacks_list(const LinphoneCore *lc) {
357
	bctbx_list_t *result = NULL;
358 359 360 361 362 363 364 365
	bctbx_list_t *it;
	for(it=lc->vtable_refs; it!=NULL; it=it->next){
		VTableReference *ref=(VTableReference*)it->data;
		result = bctbx_list_append(result, ref->cbs);
	}
	return result;
}

366 367 368 369 370 371 372 373
void linphone_core_remove_callbacks(LinphoneCore *lc, const LinphoneCoreCbs *cbs) {
	bctbx_list_t *it;
	ms_message("Callbacks [%p] unregistered on core [%p]",cbs,lc);
	for(it=lc->vtable_refs; it!=NULL; it=it->next){
		VTableReference *ref=(VTableReference*)it->data;
		if (ref->cbs==cbs) {
			ref->valid=FALSE;
		}
Simon Morlat's avatar
Simon Morlat committed
374 375
	}
}