vtables.c 12.3 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 24 25 26 27 28 29 30 31
*/

#include "private.h"


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

32
void* linphone_core_v_table_get_user_data(const LinphoneCoreVTable *table) {
Simon Morlat's avatar
Simon Morlat committed
33 34 35 36 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) {
	return lc->current_vtable;
}

static void cleanup_dead_vtable_refs(LinphoneCore *lc){
45
	bctbx_list_t *it,*next_it;
Simon Morlat's avatar
Simon Morlat committed
46 47 48 49 50
	for(it=lc->vtable_refs; it!=NULL; ){
		VTableReference *ref=(VTableReference*)it->data;
		next_it=it->next;
		if (ref->valid==0){
			ref->valid=0;
51
			lc->vtable_refs=bctbx_list_erase_link(lc->vtable_refs, it);
Simon Morlat's avatar
Simon Morlat committed
52 53 54 55 56 57
			ms_free(ref);
		}
		it=next_it;
	}
}

58
#define NOTIFY_IF_EXIST(function_name, ...) \
59
	bctbx_list_t* iterator; \
Simon Morlat's avatar
Simon Morlat committed
60
	VTableReference *ref; \
61 62 63
	bool_t has_cb = FALSE; \
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next)\
		if ((ref=(VTableReference*)iterator->data)->valid && (lc->current_vtable=ref->vtable)->function_name) {\
64
			lc->current_vtable->function_name(__VA_ARGS__);\
65 66 67
			has_cb = TRUE;\
		}\
	if (has_cb) ms_message("Linphone core [%p] notifying [%s]",lc,#function_name)
Simon Morlat's avatar
Simon Morlat committed
68

69
#define NOTIFY_IF_EXIST_INTERNAL(function_name, internal_val, ...) \
70
	bctbx_list_t* iterator; \
71 72
	VTableReference *ref; \
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next)\
73
		if ((ref=(VTableReference*)iterator->data)->valid && (lc->current_vtable=ref->vtable)->function_name && (ref->internal == internal_val)) {\
74 75 76
			lc->current_vtable->function_name(__VA_ARGS__);\
		}\

Simon Morlat's avatar
Simon Morlat committed
77
void linphone_core_notify_global_state_changed(LinphoneCore *lc, LinphoneGlobalState gstate, const char *message) {
78
	NOTIFY_IF_EXIST(global_state_changed,lc,gstate,message);
Simon Morlat's avatar
Simon Morlat committed
79 80 81 82
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *message){
83
	NOTIFY_IF_EXIST(call_state_changed, lc,call,cstate,message);
Simon Morlat's avatar
Simon Morlat committed
84 85 86 87
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_encryption_changed(LinphoneCore *lc, LinphoneCall *call, bool_t on, const char *authentication_token) {
88
	NOTIFY_IF_EXIST(call_encryption_changed, lc,call,on,authentication_token);
Simon Morlat's avatar
Simon Morlat committed
89 90 91 92
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_registration_state_changed(LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
93
	NOTIFY_IF_EXIST(registration_state_changed, lc,cfg,cstate,message);
Simon Morlat's avatar
Simon Morlat committed
94 95
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
96
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
97
#pragma GCC diagnostic push
jehan's avatar
jehan committed
98
#endif
99 100 101
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
102
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
103
#endif
Simon Morlat's avatar
Simon Morlat committed
104
void linphone_core_notify_show_interface(LinphoneCore *lc){
105
	NOTIFY_IF_EXIST(show, lc);
Simon Morlat's avatar
Simon Morlat committed
106 107 108 109
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_display_status(LinphoneCore *lc, const char *message) {
110
	NOTIFY_IF_EXIST(display_status, lc,message);
Simon Morlat's avatar
Simon Morlat committed
111 112 113 114
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_display_message(LinphoneCore *lc, const char *message){
115
	NOTIFY_IF_EXIST(display_message, lc,message);
Simon Morlat's avatar
Simon Morlat committed
116 117 118 119
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_display_warning(LinphoneCore *lc, const char *message){
120
	NOTIFY_IF_EXIST(display_warning, lc,message);
Simon Morlat's avatar
Simon Morlat committed
121 122 123 124
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_display_url(LinphoneCore *lc, const char *message, const char *url){
125
	NOTIFY_IF_EXIST(display_url, lc,message,url);
Simon Morlat's avatar
Simon Morlat committed
126 127
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
128
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
129
#pragma GCC diagnostic pop
jehan's avatar
jehan committed
130
#endif
131 132
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
133 134 135
	cleanup_dead_vtable_refs(lc);
}

136 137
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
138 139 140
	cleanup_dead_vtable_refs(lc);
}

141 142
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
143 144 145
	cleanup_dead_vtable_refs(lc);
}

146 147 148 149 150 151 152
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);
}

void linphone_core_notify_authentication_requested(LinphoneCore *lc, LinphoneAuthInfo *ai, LinphoneAuthMethod method) {
	NOTIFY_IF_EXIST(authentication_requested, lc, ai, method);
153 154 155 156 157
	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
158 159
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
160
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
161
#pragma GCC diagnostic push
jehan's avatar
jehan committed
162
#endif
163 164 165
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
166
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
167
#endif
Simon Morlat's avatar
Simon Morlat committed
168 169

void linphone_core_notify_text_message_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message){
170
	NOTIFY_IF_EXIST(text_received, lc,room,from,message);
Simon Morlat's avatar
Simon Morlat committed
171 172
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
173
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
174
#pragma GCC diagnostic pop
jehan's avatar
jehan committed
175
#endif
Simon Morlat's avatar
Simon Morlat committed
176 177

void linphone_core_notify_message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *message){
178
	NOTIFY_IF_EXIST(message_received, lc,room,message);
Simon Morlat's avatar
Simon Morlat committed
179 180
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
181
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
182
#pragma GCC diagnostic push
jehan's avatar
jehan committed
183
#endif
184 185 186
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
187
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
188
#endif
Simon Morlat's avatar
Simon Morlat committed
189
void linphone_core_notify_file_transfer_recv(LinphoneCore *lc, LinphoneChatMessage *message, const LinphoneContent* content, const char* buff, size_t size) {
190
	NOTIFY_IF_EXIST(file_transfer_recv, lc,message,content,buff,size);
Simon Morlat's avatar
Simon Morlat committed
191 192 193 194
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_file_transfer_send(LinphoneCore *lc, LinphoneChatMessage *message,  const LinphoneContent* content, char* buff, size_t* size) {
195
	NOTIFY_IF_EXIST(file_transfer_send, lc,message,content,buff,size);
Simon Morlat's avatar
Simon Morlat committed
196 197 198 199
	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) {
200
	NOTIFY_IF_EXIST(file_transfer_progress_indication, lc,message,content,offset,total);
Simon Morlat's avatar
Simon Morlat committed
201 202
	cleanup_dead_vtable_refs(lc);
}
jehan's avatar
jehan committed
203
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
204
#pragma GCC diagnostic pop
jehan's avatar
jehan committed
205
#endif
Simon Morlat's avatar
Simon Morlat committed
206
void linphone_core_notify_is_composing_received(LinphoneCore *lc, LinphoneChatRoom *room) {
207
	NOTIFY_IF_EXIST(is_composing_received, lc,room);
Simon Morlat's avatar
Simon Morlat committed
208 209 210 211
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_dtmf_received(LinphoneCore* lc, LinphoneCall *call, int dtmf) {
212
	NOTIFY_IF_EXIST(dtmf_received, lc,call,dtmf);
Simon Morlat's avatar
Simon Morlat committed
213 214 215 216
	cleanup_dead_vtable_refs(lc);
}

bool_t linphone_core_dtmf_received_has_listener(const LinphoneCore* lc) {
217
	bctbx_list_t* iterator;
Simon Morlat's avatar
Simon Morlat committed
218 219 220 221 222 223 224 225 226
	for (iterator=lc->vtable_refs; iterator!=NULL; iterator=iterator->next){
		VTableReference *ref=(VTableReference*)iterator->data;
		if (ref->valid && ref->vtable->dtmf_received)
			return TRUE;
	}
	return FALSE;
}

void linphone_core_notify_refer_received(LinphoneCore *lc, const char *refer_to) {
227
	NOTIFY_IF_EXIST(refer_received, lc,refer_to);
Simon Morlat's avatar
Simon Morlat committed
228 229 230 231
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_buddy_info_updated(LinphoneCore *lc, LinphoneFriend *lf) {
232
	NOTIFY_IF_EXIST(buddy_info_updated, lc,lf);
Simon Morlat's avatar
Simon Morlat committed
233 234 235 236
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_transfer_state_changed(LinphoneCore *lc, LinphoneCall *transfered, LinphoneCallState new_call_state) {
237
	NOTIFY_IF_EXIST(transfer_state_changed, lc,transfered,new_call_state);
Simon Morlat's avatar
Simon Morlat committed
238 239 240 241
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_call_stats_updated(LinphoneCore *lc, LinphoneCall *call, const LinphoneCallStats *stats) {
242
	NOTIFY_IF_EXIST(call_stats_updated, lc,call,stats);
Simon Morlat's avatar
Simon Morlat committed
243 244 245 246
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_info_received(LinphoneCore *lc, LinphoneCall *call, const LinphoneInfoMessage *msg) {
247
	NOTIFY_IF_EXIST(info_received, lc,call,msg);
Simon Morlat's avatar
Simon Morlat committed
248 249 250 251
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_configuring_status(LinphoneCore *lc, LinphoneConfiguringState status, const char *message) {
252
	NOTIFY_IF_EXIST(configuring_status, lc,status,message);
Simon Morlat's avatar
Simon Morlat committed
253 254 255 256
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_network_reachable(LinphoneCore *lc, bool_t reachable) {
257
	NOTIFY_IF_EXIST(network_reachable, lc,reachable);
Simon Morlat's avatar
Simon Morlat committed
258 259 260 261
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *notified_event, const LinphoneContent *body) {
262
	NOTIFY_IF_EXIST_INTERNAL(notify_received, linphone_event_is_internal(lev), lc, lev, notified_event, body);
Simon Morlat's avatar
Simon Morlat committed
263 264 265 266
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_subscription_state_changed(LinphoneCore *lc, LinphoneEvent *lev, LinphoneSubscriptionState state) {
267
	NOTIFY_IF_EXIST_INTERNAL(subscription_state_changed,linphone_event_is_internal(lev), lc,lev,state);
Simon Morlat's avatar
Simon Morlat committed
268 269 270 271
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_publish_state_changed(LinphoneCore *lc, LinphoneEvent *lev, LinphonePublishState state) {
272
	NOTIFY_IF_EXIST_INTERNAL(publish_state_changed, linphone_event_is_internal(lev), lc, lev, state);
Simon Morlat's avatar
Simon Morlat committed
273 274 275 276
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_log_collection_upload_state_changed(LinphoneCore *lc, LinphoneCoreLogCollectionUploadState state, const char *info) {
277
	NOTIFY_IF_EXIST(log_collection_upload_state_changed, lc, state, info);
Simon Morlat's avatar
Simon Morlat committed
278 279 280 281
	cleanup_dead_vtable_refs(lc);
}

void linphone_core_notify_log_collection_upload_progress_indication(LinphoneCore *lc, size_t offset, size_t total) {
282
	NOTIFY_IF_EXIST(log_collection_upload_progress_indication, lc, offset, total);
Simon Morlat's avatar
Simon Morlat committed
283 284 285
	cleanup_dead_vtable_refs(lc);
}

286 287 288 289 290 291 292 293 294 295
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);
}

296
static VTableReference * v_table_reference_new(LinphoneCoreVTable *vtable, bool_t autorelease, bool_t internal){
Simon Morlat's avatar
Simon Morlat committed
297 298 299
	VTableReference *ref=ms_new0(VTableReference,1);
	ref->valid=1;
	ref->autorelease=autorelease;
300
	ref->internal = internal;
Simon Morlat's avatar
Simon Morlat committed
301 302 303 304 305 306 307 308 309
	ref->vtable=vtable;
	return ref;
}

void v_table_reference_destroy(VTableReference *ref){
	if (ref->autorelease) linphone_core_v_table_destroy(ref->vtable);
	ms_free(ref);
}

310
void _linphone_core_add_listener(LinphoneCore *lc, LinphoneCoreVTable *vtable, bool_t autorelease, bool_t internal) {
Simon Morlat's avatar
Simon Morlat committed
311
	ms_message("Vtable [%p] registered on core [%p]",vtable, lc);
312
	lc->vtable_refs=bctbx_list_append(lc->vtable_refs,v_table_reference_new(vtable, autorelease, internal));
Simon Morlat's avatar
Simon Morlat committed
313 314 315
}

void linphone_core_add_listener(LinphoneCore *lc, LinphoneCoreVTable *vtable){
316
	_linphone_core_add_listener(lc, vtable, FALSE, FALSE);
Simon Morlat's avatar
Simon Morlat committed
317 318 319
}

void linphone_core_remove_listener(LinphoneCore *lc, const LinphoneCoreVTable *vtable) {
320
	bctbx_list_t *it;
Simon Morlat's avatar
Simon Morlat committed
321 322 323 324 325 326 327
	ms_message("Vtable [%p] unregistered on core [%p]",lc,vtable);
	for(it=lc->vtable_refs; it!=NULL; it=it->next){
		VTableReference *ref=(VTableReference*)it->data;
		if (ref->vtable==vtable)
			ref->valid=0;
	}
}