friend.c 57.9 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4
/***************************************************************************
 *            friend.c
 *
 *  Sat May 15 15:25:16 2004
5
 *  Copyright  2004-2009  Simon Morlat
aymeric's avatar
aymeric committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *  Email
 ****************************************************************************/

/*
 *  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 Library 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
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

25
#include "linphone/core.h"
aymeric's avatar
aymeric committed
26
#include "private.h"
27
#include "linphone/lpconfig.h"
aymeric's avatar
aymeric committed
28

29
#ifdef SQLITE_STORAGE_ENABLED
30
#ifndef _WIN32
31
#if !defined(__ANDROID__) && !defined(__QNXNTO__)
32 33 34 35 36 37 38 39 40 41 42 43
#	include <langinfo.h>
#	include <iconv.h>
#	include <string.h>
#endif
#else
#include <Windows.h>
#endif

#define MAX_PATH_SIZE 1024
#include "sqlite3.h"
#endif

aymeric's avatar
aymeric committed
44 45 46
const char *linphone_online_status_to_string(LinphoneOnlineStatus ss){
	const char *str=NULL;
	switch(ss){
47
		case LinphoneStatusOnline:
aymeric's avatar
aymeric committed
48 49
		str=_("Online");
		break;
50
		case LinphoneStatusBusy:
aymeric's avatar
aymeric committed
51 52
		str=_("Busy");
		break;
53
		case LinphoneStatusBeRightBack:
aymeric's avatar
aymeric committed
54 55
		str=_("Be right back");
		break;
56
		case LinphoneStatusAway:
aymeric's avatar
aymeric committed
57 58
		str=_("Away");
		break;
59
		case LinphoneStatusOnThePhone:
aymeric's avatar
aymeric committed
60 61
		str=_("On the phone");
		break;
62
		case LinphoneStatusOutToLunch:
aymeric's avatar
aymeric committed
63 64
		str=_("Out to lunch");
		break;
65
		case LinphoneStatusDoNotDisturb:
aymeric's avatar
aymeric committed
66 67
		str=_("Do not disturb");
		break;
68
		case LinphoneStatusMoved:
aymeric's avatar
aymeric committed
69 70
		str=_("Moved");
		break;
71
		case LinphoneStatusAltService:
aymeric's avatar
aymeric committed
72 73
		str=_("Using another messaging service");
		break;
74
		case LinphoneStatusOffline:
aymeric's avatar
aymeric committed
75 76
		str=_("Offline");
		break;
77
		case LinphoneStatusPending:
aymeric's avatar
aymeric committed
78 79
		str=_("Pending");
		break;
80 81
		case LinphoneStatusVacation:
		str=_("Vacation");
aymeric's avatar
aymeric committed
82
		default:
83
		str=_("Unknown status");
aymeric's avatar
aymeric committed
84 85 86 87
	}
	return str;
}

88 89 90
static int friend_compare(const void * a, const void * b) {
	LinphoneFriend *lfa = (LinphoneFriend *)a;
	LinphoneFriend *lfb = (LinphoneFriend *)b;
91 92 93 94
	const bctbx_list_t *addressesa = linphone_friend_get_addresses(lfa);
	const bctbx_list_t *addressesb = linphone_friend_get_addresses(lfb);
	bctbx_list_t *iteratora = (bctbx_list_t *)addressesa;
	bctbx_list_t *iteratorb = (bctbx_list_t *)addressesb;
95 96 97 98 99 100 101 102 103 104 105 106 107
	int ret = 1;

	while (iteratora && (ret == 1)) {
		LinphoneAddress *fa = (LinphoneAddress *)bctbx_list_get_data(iteratora);
		while (iteratorb && (ret == 1)) {
			LinphoneAddress *fb = (LinphoneAddress *)bctbx_list_get_data(iteratorb);
			if (linphone_address_weak_equal(fa, fb)) ret = 0;
			iteratorb = bctbx_list_next(iteratorb);
		}
		iteratora = bctbx_list_next(iteratora);
	}

	return ret;
aymeric's avatar
aymeric committed
108 109
}

110
static LinphoneFriendPresence * find_presence_model_for_uri_or_tel(const LinphoneFriend *lf, const char *uri_or_tel) {
111 112
	bctbx_list_t *iterator = NULL;
	LinphoneAddress *uri_or_tel_addr = NULL;
113
	LinphoneFriendPresence *result=NULL;
114 115 116 117
	if (!lf->lc) {
		ms_warning("Cannot find uri of tel [%s] from friend [%p] because not associated to any Linphone core object",uri_or_tel,lf);
		return NULL;
	}
118 119 120 121 122
	if ((iterator = lf->presence_models) == NULL) {
		/*no need to move forward, just reutn to avoid useless uri parsing*/
		return NULL;
	};
	
123 124
	uri_or_tel_addr = linphone_core_interpret_url(lf->lc, uri_or_tel);

125
	while (uri_or_tel_addr && iterator) {
126
		LinphoneFriendPresence *lfp = (LinphoneFriendPresence *)bctbx_list_get_data(iterator);
127 128 129 130 131 132 133 134 135
		LinphoneAddress *lfp_addr = linphone_core_interpret_url(lf->lc, lfp->uri_or_tel);
		if (lfp_addr && linphone_address_weak_equal(uri_or_tel_addr, lfp_addr)) {
			result = lfp;
		}
		if (lfp_addr) linphone_address_unref(lfp_addr);
		if (result == NULL)
			iterator = bctbx_list_next(iterator);
		else
			break;
136
	}
137 138
	if (uri_or_tel_addr) linphone_address_unref(uri_or_tel_addr);
	return result;
139 140 141
}

static void add_presence_model_for_uri_or_tel(LinphoneFriend *lf, const char *uri_or_tel, LinphonePresenceModel *presence) {
142
	LinphoneFriendPresence *lfp = ms_new0(LinphoneFriendPresence, 1);
143 144 145 146 147 148 149 150
	lfp->uri_or_tel = ms_strdup(uri_or_tel);
	lfp->presence = presence;
	lf->presence_models = bctbx_list_append(lf->presence_models, lfp);
}

static void free_friend_presence(LinphoneFriendPresence *lfp) {
	ms_free(lfp->uri_or_tel);
	if (lfp->presence) linphone_presence_model_unref(lfp->presence);
151
	ms_free(lfp);
152 153
}

154 155 156
static void free_phone_number_sip_uri(LinphoneFriendPhoneNumberSipUri *lfpnsu) {
	ms_free(lfpnsu->number);
	ms_free(lfpnsu->uri);
157
	ms_free(lfpnsu);
158 159
}

160

aymeric's avatar
aymeric committed
161

162 163
bctbx_list_t *linphone_find_friend_by_address(bctbx_list_t *fl, const LinphoneAddress *addr, LinphoneFriend **lf){
	bctbx_list_t *res=NULL;
aymeric's avatar
aymeric committed
164 165
	LinphoneFriend dummy;
	if (lf!=NULL) *lf=NULL;
166
	memset(&dummy, 0, sizeof(LinphoneFriend));
167
	dummy.uri=(LinphoneAddress*)addr;
168
	res=bctbx_list_find_custom(fl,friend_compare,&dummy);
169
	if (lf!=NULL && res!=NULL) *lf=(LinphoneFriend*)bctbx_list_get_data(res);
aymeric's avatar
aymeric committed
170 171 172 173
	return res;
}

void __linphone_friend_do_subscribe(LinphoneFriend *fr){
Simon Morlat's avatar
Simon Morlat committed
174
	LinphoneCore *lc=fr->lc;
175
	const LinphoneAddress *addr = linphone_friend_get_address(fr);
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	if (addr != NULL) {
		if (fr->outsub==NULL){
			/* people for which we don't have yet an answer should appear as offline */
			fr->presence_models = bctbx_list_free_with_data(fr->presence_models, (bctbx_list_free_func)free_friend_presence);
			/*
			if (fr->lc->vtable.notify_recv)
				fr->lc->vtable.notify_recv(fr->lc,(LinphoneFriend*)fr);
			*/
		}else{
			sal_op_release(fr->outsub);
			fr->outsub=NULL;
		}
		fr->outsub=sal_op_new(lc->sal);
		linphone_configure_op(lc,fr->outsub,addr,NULL,TRUE);
		sal_subscribe_presence(fr->outsub,NULL,NULL,lp_config_get_int(lc->config,"sip","subscribe_expires",600));
		fr->subscribe_active=TRUE;
	}
aymeric's avatar
aymeric committed
194 195
}

196
LinphoneFriend * linphone_friend_new(void){
197 198 199
	LinphoneFriend *obj = belle_sip_object_new(LinphoneFriend);
	obj->pol = LinphoneSPAccept;
	obj->subscribe = TRUE;
200
	obj->vcard = NULL;
201
	obj->storage_id = 0;
202
	return obj;
aymeric's avatar
aymeric committed
203 204
}

205 206 207 208 209 210 211 212
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
213
LinphoneFriend *linphone_friend_new_with_address(const char *addr){
jehan's avatar
jehan committed
214
	LinphoneAddress* linphone_address = linphone_address_new(addr);
215 216
	LinphoneFriend *fr;

jehan's avatar
jehan committed
217 218 219 220
	if (linphone_address == NULL) {
		ms_error("Cannot create friend for address [%s]",addr?addr:"null");
		return NULL;
	}
221
	fr=linphone_friend_new();
222
	linphone_friend_set_address(fr,linphone_address);
223
	linphone_address_unref(linphone_address);
aymeric's avatar
aymeric committed
224 225
	return fr;
}
226 227 228
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif
aymeric's avatar
aymeric committed
229

Simon Morlat's avatar
Simon Morlat committed
230
void linphone_friend_set_user_data(LinphoneFriend *lf, void *data){
231
	lf->user_data=data;
Simon Morlat's avatar
Simon Morlat committed
232 233 234
}

void* linphone_friend_get_user_data(const LinphoneFriend *lf){
235
	return lf->user_data;
Simon Morlat's avatar
Simon Morlat committed
236 237
}

238 239
bool_t linphone_friend_in_list(const LinphoneFriend *lf) {
	return lf->friend_list != NULL;
240 241
}

smorlat's avatar
smorlat committed
242
void linphone_core_interpret_friend_uri(LinphoneCore *lc, const char *uri, char **result){
243
	LinphoneAddress *fr=NULL;
244
	*result=NULL;
245
	fr=linphone_address_new(uri);
246
	if (fr==NULL){
smorlat's avatar
smorlat committed
247 248
		char *tmp=NULL;
		if (strchr(uri,'@')!=NULL){
249
			LinphoneAddress *u;
smorlat's avatar
smorlat committed
250 251
			/*try adding sip:*/
			tmp=ms_strdup_printf("sip:%s",uri);
252
			u=linphone_address_new(tmp);
253 254 255
			if (u!=NULL){
				*result=tmp;
			}
smorlat's avatar
smorlat committed
256 257
		}else if (lc->default_proxy!=NULL){
			/*try adding domain part from default current proxy*/
258
			LinphoneAddress * id=linphone_address_new(linphone_core_get_identity(lc));
259
			if ((id!=NULL) && (uri[0] != '\0')){
260
				linphone_address_set_display_name(id,NULL);
261 262
				linphone_address_set_username(id,uri);
				*result=linphone_address_as_string(id);
263
				linphone_address_unref(id);
smorlat's avatar
smorlat committed
264 265
			}
		}
266
		if (*result){
smorlat's avatar
smorlat committed
267
			/*looks good */
268 269 270 271
			ms_message("%s interpreted as %s",uri,*result);
		}else{
			ms_warning("Fail to interpret friend uri %s",uri);
		}
jehan's avatar
jehan committed
272 273
	}else {
		*result=linphone_address_as_string(fr);
274
		linphone_address_unref(fr);
jehan's avatar
jehan committed
275
	}
smorlat's avatar
smorlat committed
276
}
aymeric's avatar
aymeric committed
277

278
const LinphoneAddress * linphone_friend_get_address(const LinphoneFriend *lf) {
279 280
	if (linphone_core_vcard_supported()) {
		if (lf->vcard) {
281
			const bctbx_list_t *sip_addresses = linphone_vcard_get_sip_addresses(lf->vcard);
282
			if (sip_addresses) {
283
				LinphoneAddress *addr = (LinphoneAddress *)bctbx_list_nth_data(sip_addresses, 0);
Ghislain MARY's avatar
Ghislain MARY committed
284
				return addr;
285 286 287
			}
		}
		return NULL;
288
	}
289
	if (lf->uri) return lf->uri;
290
	return NULL;
291 292
}

293
LinphoneStatus linphone_friend_set_address(LinphoneFriend *lf, const LinphoneAddress *addr) {
294
	LinphoneAddress *fr = linphone_address_clone(addr);
295
	char *address;
296
	const LinphoneAddress *mAddr = linphone_friend_get_address(lf);
297 298 299 300 301 302 303 304 305
	if(mAddr && lf->friend_list) {
		char *mainAddress = linphone_address_as_string_uri_only(mAddr);
		bctbx_iterator_t *it = bctbx_map_cchar_find_key(lf->friend_list->friends_map_uri, mainAddress);
		if (!bctbx_iterator_cchar_equals(it, bctbx_map_cchar_end(lf->friend_list->friends_map_uri))){
			linphone_friend_unref((LinphoneFriend*)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it)));
			bctbx_map_cchar_erase(lf->friend_list->friends_map_uri, it);
		}
		bctbx_iterator_cchar_delete(it);
	}
306
	linphone_address_clean(fr);
307 308 309 310 311 312
	address = linphone_address_as_string_uri_only(fr);
	if(lf->friend_list) {
		bctbx_pair_t *pair = (bctbx_pair_t*) bctbx_pair_cchar_new(address, linphone_friend_ref(lf));
		bctbx_map_cchar_insert_and_delete(lf->friend_list->friends_map_uri, pair);
	}

313 314
	if (linphone_core_vcard_supported()) {
		if (!lf->vcard) {
315 316
			const char *dpname = linphone_address_get_display_name(fr) ? linphone_address_get_display_name(fr) : linphone_address_get_username(fr);
			linphone_friend_create_vcard(lf, dpname);
317 318 319 320 321 322
		}
		linphone_vcard_edit_main_sip_address(lf->vcard, address);
		linphone_address_unref(fr);
	} else {
		if (lf->uri != NULL) linphone_address_unref(lf->uri);
		lf->uri = fr;
323
	}
324

325
	ms_free(address);
aymeric's avatar
aymeric committed
326 327 328
	return 0;
}

329
void linphone_friend_add_address(LinphoneFriend *lf, const LinphoneAddress *addr) {
330
	LinphoneAddress *fr;
331
	char *uri;
332 333 334 335
	if (!lf || !addr) return;

	fr = linphone_address_clone(addr);
	linphone_address_clean(fr);
336 337 338 339 340
	uri = linphone_address_as_string_uri_only(fr);
	if(lf->friend_list) {
		bctbx_pair_t *pair = (bctbx_pair_t*) bctbx_pair_cchar_new(uri, linphone_friend_ref(lf));
		bctbx_map_cchar_insert_and_delete(lf->friend_list->friends_map_uri, pair);
	}
341

342 343
	if (linphone_core_vcard_supported()) {
		if (lf->vcard) {
344
			linphone_vcard_add_sip_address(lf->vcard, uri);
345 346 347 348 349 350
			linphone_address_unref(fr);
		}
	} else {
		if (lf->uri == NULL) lf->uri = fr;
		else linphone_address_unref(fr);
	}
351
	ms_free(uri);
352 353
}

354
const bctbx_list_t* linphone_friend_get_addresses(const LinphoneFriend *lf) {
355
	if (!lf) return NULL;
356

357
	if (linphone_core_vcard_supported()) {
358
		const bctbx_list_t * addresses = linphone_vcard_get_sip_addresses(lf->vcard);
359
		return addresses;
360
	} else {
361
		bctbx_list_t *addresses = NULL;
362
		return lf->uri ? bctbx_list_append(addresses, lf->uri) : NULL;
363 364 365 366
	}
}

void linphone_friend_remove_address(LinphoneFriend *lf, const LinphoneAddress *addr) {
367
	char *address ;
368
	if (!lf || !addr || !lf->vcard) return;
369

370 371 372 373 374 375 376 377 378 379
	address = linphone_address_as_string_uri_only(addr);
	if(lf->friend_list) {
		bctbx_iterator_t *it = bctbx_map_cchar_find_key(lf->friend_list->friends_map_uri, address);
		if (!bctbx_iterator_cchar_equals(it, bctbx_map_cchar_end(lf->friend_list->friends_map_uri))){
			linphone_friend_unref((LinphoneFriend*)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it)));
			bctbx_map_cchar_erase(lf->friend_list->friends_map_uri, it);
		}
		bctbx_iterator_cchar_delete(it);
	}

380 381
	if (linphone_core_vcard_supported()) {
		linphone_vcard_remove_sip_address(lf->vcard, address);
382
	}
383
	ms_free(address);
384 385 386
}

void linphone_friend_add_phone_number(LinphoneFriend *lf, const char *phone) {
387
	if (!lf || !phone) return;
388

389 390 391 392 393 394 395 396
	if(lf->friend_list) {
		const char *uri = linphone_friend_phone_number_to_sip_uri(lf, phone);
		if(uri) {
			bctbx_pair_t *pair = (bctbx_pair_t*) bctbx_pair_cchar_new(uri, linphone_friend_ref(lf));
			bctbx_map_cchar_insert_and_delete(lf->friend_list->friends_map_uri, pair);
		}
	}

397 398 399 400 401
	if (linphone_core_vcard_supported()) {
		if (!lf->vcard) {
			linphone_friend_create_vcard(lf, phone);
		}
		linphone_vcard_add_phone_number(lf->vcard, phone);
402 403 404
	}
}

405
bctbx_list_t* linphone_friend_get_phone_numbers(LinphoneFriend *lf) {
406
	if (!lf || !lf->vcard) return NULL;
407

408 409
	if (linphone_core_vcard_supported()) {
		return linphone_vcard_get_phone_numbers(lf->vcard);
410
	}
411
	return NULL;
412 413 414
}

void linphone_friend_remove_phone_number(LinphoneFriend *lf, const char *phone) {
415
	if (!lf || !phone || !lf->vcard) return;
416

417 418 419 420 421 422 423 424 425 426 427 428
	if(lf->friend_list) {
		const char *uri = linphone_friend_phone_number_to_sip_uri(lf, phone);
		if(uri) {
			bctbx_iterator_t *it = bctbx_map_cchar_find_key(lf->friend_list->friends_map_uri, uri);
			if (!bctbx_iterator_cchar_equals(it, bctbx_map_cchar_end(lf->friend_list->friends_map_uri))){
				linphone_friend_unref((LinphoneFriend*)bctbx_pair_cchar_get_second(bctbx_iterator_cchar_get_pair(it)));
				bctbx_map_cchar_erase(lf->friend_list->friends_map_uri, it);
			}
			bctbx_iterator_cchar_delete(it);
		}
	}

429 430
	if (linphone_core_vcard_supported()) {
		linphone_vcard_remove_phone_number(lf->vcard, phone);
431 432 433
	}
}

434
LinphoneStatus linphone_friend_set_name(LinphoneFriend *lf, const char *name){
435 436 437 438 439 440 441
	if (linphone_core_vcard_supported()) {
		if (!lf->vcard) linphone_friend_create_vcard(lf, name);
		linphone_vcard_set_full_name(lf->vcard, name);
	} else {
		if (!lf->uri) {
			ms_warning("linphone_friend_set_address() must be called before linphone_friend_set_name() to be able to set display name.");
			return -1;
442
		}
443
		linphone_address_set_display_name(lf->uri, name);
444
	}
aymeric's avatar
aymeric committed
445 446 447
	return 0;
}

448
LinphoneStatus linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val){
aymeric's avatar
aymeric committed
449 450 451 452
	fr->subscribe=val;
	return 0;
}

453
LinphoneStatus linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol) {
aymeric's avatar
aymeric committed
454 455 456 457
	fr->pol=pol;
	return 0;
}

458
void linphone_friend_notify(LinphoneFriend *lf, LinphonePresenceModel *presence){
459
	bctbx_list_t *elem;
460
	if (lf->insubs){
461
		const LinphoneAddress *addr = linphone_friend_get_address(lf);
462 463 464 465 466
		if (addr) {
			char *addr_str = linphone_address_as_string(addr);
			ms_message("Want to notify %s", addr_str);
			ms_free(addr_str);
		}
467
	}
468 469
	for(elem=lf->insubs; elem!=NULL; elem=bctbx_list_next(elem)){
		SalOp *op = (SalOp*)bctbx_list_get_data(elem);
470
		sal_notify_presence(op,(SalPresenceModel *)presence);
aymeric's avatar
aymeric committed
471 472 473
	}
}

474
void linphone_friend_add_incoming_subscription(LinphoneFriend *lf, SalOp *op){
475
	/*ownership of the op is transfered from sal to the LinphoneFriend*/
476
	lf->insubs = bctbx_list_append(lf->insubs, op);
477 478 479
}

void linphone_friend_remove_incoming_subscription(LinphoneFriend *lf, SalOp *op){
480
	if (bctbx_list_find(lf->insubs, op)){
481
		sal_op_release(op);
482
		lf->insubs = bctbx_list_remove(lf->insubs, op);
483
	}
484 485
}

aymeric's avatar
aymeric committed
486
static void linphone_friend_unsubscribe(LinphoneFriend *lf){
487 488
	if (lf->outsub!=NULL) {
		sal_unsubscribe(lf->outsub);
aymeric's avatar
aymeric committed
489
	}
490 491
	/* for friend list there is no necessary outsub*/
	lf->subscribe_active=FALSE;
aymeric's avatar
aymeric committed
492 493
}

494
void linphone_friend_invalidate_subscription(LinphoneFriend *lf){
495
	bctbx_list_t *iterator;
496 497
	LinphoneCore *lc=lf->lc;

Simon Morlat's avatar
Simon Morlat committed
498 499 500 501
	if (lf->outsub!=NULL) {
		sal_op_release(lf->outsub);
		lf->outsub=NULL;
	}
502

503 504 505
	// To resend a subscribe on the next network_reachable(TRUE)
	lf->subscribe_active=FALSE;

506
	/* Notify application that we no longer know the presence activity */
507 508 509 510
	iterator = lf->presence_models;
	while (iterator) {
		LinphoneFriendPresence *lfp = (LinphoneFriendPresence *)bctbx_list_get_data(iterator);
		linphone_presence_model_unref(lfp->presence);
511 512
		lfp->presence = linphone_presence_model_new();
		linphone_presence_model_set_basic_status(lfp->presence, LinphonePresenceBasicStatusClosed);
513 514
		linphone_core_notify_notify_presence_received_for_uri_or_tel(lc, lf, lfp->uri_or_tel, lfp->presence);
		iterator = bctbx_list_next(iterator);
515
	}
516 517
	if (bctbx_list_size(lf->presence_models) > 0)
		linphone_core_notify_notify_presence_received(lc, lf);
518

519
	lf->initial_subscribes_sent=FALSE;
Simon Morlat's avatar
Simon Morlat committed
520 521
}

522
static void linphone_friend_close_incoming_subscriptions(LinphoneFriend *lf) {
523 524
	bctbx_list_for_each(lf->insubs, (MSIterateFunc) sal_notify_presence_close);
	lf->insubs = bctbx_list_free_with_data(lf->insubs, (MSIterateFunc)sal_op_release);
525 526
}

527 528 529 530 531
void linphone_friend_close_subscriptions(LinphoneFriend *lf){
	linphone_friend_unsubscribe(lf);
	linphone_friend_close_incoming_subscriptions(lf);
}

532
static void _linphone_friend_release_ops(LinphoneFriend *lf){
533
	lf->insubs = bctbx_list_free_with_data(lf->insubs, (MSIterateFunc) sal_op_release);
534 535 536 537
	if (lf->outsub){
		sal_op_release(lf->outsub);
		lf->outsub=NULL;
	}
538 539 540 541
}

static void _linphone_friend_destroy(LinphoneFriend *lf){
	_linphone_friend_release_ops(lf);
542
	if (lf->presence_models) bctbx_list_free_with_data(lf->presence_models, (bctbx_list_free_func)free_friend_presence);
543
	if (lf->phone_number_sip_uri_map) bctbx_list_free_with_data(lf->phone_number_sip_uri_map, (bctbx_list_free_func)free_phone_number_sip_uri);
544
	if (lf->uri!=NULL) linphone_address_unref(lf->uri);
545
	if (lf->info!=NULL) buddy_info_free(lf->info);
546
	if (lf->vcard != NULL) linphone_vcard_unref(lf->vcard);
Sylvain Berfini's avatar
Sylvain Berfini committed
547
	if (lf->refkey != NULL) ms_free(lf->refkey);
aymeric's avatar
aymeric committed
548 549
}

550 551 552 553 554 555 556 557 558 559 560
static belle_sip_error_code _linphone_friend_marshall(belle_sip_object_t *obj, char* buff, size_t buff_size, size_t *offset) {
	LinphoneFriend *lf = (LinphoneFriend*)obj;
	belle_sip_error_code err = BELLE_SIP_OK;
	if (lf->uri){
		char *tmp = linphone_address_as_string(lf->uri);
		err = belle_sip_snprintf(buff, buff_size, offset, "%s", tmp);
		ms_free(tmp);
	}
	return err;
}

561
const char * linphone_friend_get_name(const LinphoneFriend *lf) {
562 563 564 565 566 567
	if (!lf) return NULL;

	if (linphone_core_vcard_supported()) {
		if (lf->vcard) return linphone_vcard_get_full_name(lf->vcard);
	} else if (lf->uri) {
		return linphone_address_get_display_name(lf->uri);
568 569
	}
	return NULL;
570 571
}

aymeric's avatar
aymeric committed
572 573 574 575 576 577 578 579 580
bool_t linphone_friend_get_send_subscribe(const LinphoneFriend *lf){
	return lf->subscribe;
}

LinphoneSubscribePolicy linphone_friend_get_inc_subscribe_policy(const LinphoneFriend *lf){
	return lf->pol;
}

LinphoneOnlineStatus linphone_friend_get_status(const LinphoneFriend *lf){
581
	const LinphonePresenceModel *presence = linphone_friend_get_presence_model(lf);
582 583
	LinphoneOnlineStatus online_status = LinphoneStatusOffline;
	LinphonePresenceBasicStatus basic_status = LinphonePresenceBasicStatusClosed;
584
	LinphonePresenceActivity *activity = NULL;
585
	const char *description = NULL;
586 587
	unsigned int nb_activities = 0;

588 589 590
	if (presence != NULL) {
		basic_status = linphone_presence_model_get_basic_status(presence);
		nb_activities = linphone_presence_model_get_nb_activities(presence);
591 592 593
		online_status = (basic_status == LinphonePresenceBasicStatusOpen) ? LinphoneStatusOnline : LinphoneStatusOffline;
		if (nb_activities > 1) {
			char *tmp = NULL;
594
			const LinphoneAddress *addr = linphone_friend_get_address(lf);
595 596
			if (addr) tmp = linphone_address_as_string(addr);
			ms_warning("Friend %s has several activities, get status from the first one", tmp ? tmp : "unknown");
597 598 599
			if (tmp) {
				ms_free(tmp);
			}
600 601 602
			nb_activities = 1;
		}
		if (nb_activities == 1) {
603
			activity = linphone_presence_model_get_activity(presence);
604
			description = linphone_presence_activity_get_description(activity);
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
			switch (linphone_presence_activity_get_type(activity)) {
				case LinphonePresenceActivityBreakfast:
				case LinphonePresenceActivityDinner:
				case LinphonePresenceActivityLunch:
				case LinphonePresenceActivityMeal:
					online_status = LinphoneStatusOutToLunch;
					break;
				case LinphonePresenceActivityAppointment:
				case LinphonePresenceActivityMeeting:
				case LinphonePresenceActivityPerformance:
				case LinphonePresenceActivityPresentation:
				case LinphonePresenceActivitySpectator:
				case LinphonePresenceActivityWorking:
				case LinphonePresenceActivityWorship:
					online_status = LinphoneStatusDoNotDisturb;
					break;
				case LinphonePresenceActivityAway:
				case LinphonePresenceActivitySleeping:
					online_status = LinphoneStatusAway;
					break;
				case LinphonePresenceActivityHoliday:
				case LinphonePresenceActivityTravel:
				case LinphonePresenceActivityVacation:
					online_status = LinphoneStatusVacation;
					break;
				case LinphonePresenceActivityBusy:
631 632 633 634 635 636
					if (description && strcmp(description, "Do not disturb") == 0) { // See linphonecore.c linphone_core_set_presence_info() method
						online_status = LinphoneStatusDoNotDisturb;
					} else {
						online_status = LinphoneStatusBusy;
					}
					break;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
				case LinphonePresenceActivityLookingForWork:
				case LinphonePresenceActivityPlaying:
				case LinphonePresenceActivityShopping:
				case LinphonePresenceActivityTV:
					online_status = LinphoneStatusBusy;
					break;
				case LinphonePresenceActivityInTransit:
				case LinphonePresenceActivitySteering:
					online_status = LinphoneStatusBeRightBack;
					break;
				case LinphonePresenceActivityOnThePhone:
					online_status = LinphoneStatusOnThePhone;
					break;
				case LinphonePresenceActivityOther:
				case LinphonePresenceActivityPermanentAbsence:
					online_status = LinphoneStatusMoved;
					break;
				case LinphonePresenceActivityUnknown:
					/* Rely on the basic status information. */
					break;
657 658 659 660 661 662 663
			}
		}
	}

	return online_status;
}

664 665
const LinphonePresenceModel * linphone_friend_get_presence_model(const LinphoneFriend *lf) {
	const LinphonePresenceModel *presence = NULL;
666 667
	LinphoneFriend* const_lf = (LinphoneFriend*)lf;
	const bctbx_list_t* addrs = linphone_friend_get_addresses(const_lf);
668
	bctbx_list_t* phones = NULL;
669
	bctbx_list_t *it;
670

671
	for (it = (bctbx_list_t *)addrs; it!= NULL; it = it->next) {
672
		LinphoneAddress *addr = (LinphoneAddress*)it->data;
673
		char *uri = linphone_address_as_string_uri_only(addr);
674
		presence = linphone_friend_get_presence_model_for_uri_or_tel(const_lf, uri);
675
		ms_free(uri);
676
		if (presence) break;
677
	}
678
	if (presence) return presence;
679

680
	phones = linphone_friend_get_phone_numbers(const_lf);
681
	for (it = phones; it!= NULL; it = it->next) {
682
		presence = linphone_friend_get_presence_model_for_uri_or_tel(const_lf, it->data);
683
		if (presence) break;
684
	}
685 686
	bctbx_list_free(phones);
	return presence;
687 688
}

689 690 691 692 693 694
LinphoneConsolidatedPresence linphone_friend_get_consolidated_presence(const LinphoneFriend *lf) {
	const LinphonePresenceModel *model = linphone_friend_get_presence_model(lf);
	if (!model) return LinphoneConsolidatedPresenceOffline;
	return linphone_presence_model_get_consolidated_presence(model);
}

695 696 697 698
const LinphonePresenceModel * linphone_friend_get_presence_model_for_uri_or_tel(const LinphoneFriend *lf, const char *uri_or_tel) {
	LinphoneFriendPresence *lfp = find_presence_model_for_uri_or_tel(lf, uri_or_tel);
	if (lfp) return lfp->presence;
	return NULL;
699 700
}

701
void linphone_friend_set_presence_model(LinphoneFriend *lf, LinphonePresenceModel *presence) {
702
	const LinphoneAddress *addr = linphone_friend_get_address(lf);
703 704 705 706 707 708 709 710 711 712 713 714 715 716
	if (addr) {
		char *uri = linphone_address_as_string_uri_only(addr);
		linphone_friend_set_presence_model_for_uri_or_tel(lf, uri, presence);
		ms_free(uri);
	}
}

void linphone_friend_set_presence_model_for_uri_or_tel(LinphoneFriend *lf, const char *uri_or_tel, LinphonePresenceModel *presence) {
	LinphoneFriendPresence *lfp = find_presence_model_for_uri_or_tel(lf, uri_or_tel);
	if (lfp) {
		if (lfp->presence) linphone_presence_model_unref(lfp->presence);
		lfp->presence = presence;
	} else {
		add_presence_model_for_uri_or_tel(lf, uri_or_tel, presence);
717 718 719
	}
}

720 721 722 723
bool_t linphone_friend_is_presence_received(const LinphoneFriend *lf) {
	return lf->presence_received;
}

724 725 726
BuddyInfo * linphone_friend_get_info(const LinphoneFriend *lf){
	return lf->info;
}
aymeric's avatar
aymeric committed
727

728
/*
729
 * updates the p2p subscriptions.
730 731 732 733
 * If only_when_registered is TRUE, subscribe will be sent only if the friend's corresponding proxy config is in registered.
 * Otherwise if the proxy config goes to unregistered state, the subscription refresh will be suspended.
 * An optional proxy whose state has changed can be passed to optimize the processing.
**/
734
void linphone_friend_update_subscribes(LinphoneFriend *fr, bool_t only_when_registered){
735
	int can_subscribe=1;
736

737
	if (only_when_registered && (fr->subscribe || fr->subscribe_active)){
738
		const LinphoneAddress *addr = linphone_friend_get_address(fr);
739 740 741 742 743 744 745 746 747
		if (addr != NULL) {
			LinphoneProxyConfig *cfg=linphone_core_lookup_known_proxy(fr->lc, addr);
			if (cfg && cfg->state!=LinphoneRegistrationOk){
				char *tmp=linphone_address_as_string(addr);
				ms_message("Friend [%s] belongs to proxy config with identity [%s], but this one isn't registered. Subscription is suspended.",
					tmp,linphone_proxy_config_get_identity(cfg));
				ms_free(tmp);
				can_subscribe=0;
			}
748 749 750 751 752 753 754 755 756 757 758 759 760
		}
	}
	if (can_subscribe && fr->subscribe && fr->subscribe_active==FALSE){
		ms_message("Sending a new SUBSCRIBE");
		__linphone_friend_do_subscribe(fr);
	}else if (can_subscribe && fr->subscribe_active && !fr->subscribe){
		linphone_friend_unsubscribe(fr);
	}else if (!can_subscribe && fr->outsub){
		fr->subscribe_active=FALSE;
		sal_op_stop_refreshing(fr->outsub);
	}
}

761
void linphone_friend_save(LinphoneFriend *fr, LinphoneCore *lc) {
762
	if (!lc) return;
763
#ifdef SQLITE_STORAGE_ENABLED
764 765 766 767 768
	if (lc->friends_db_file) {
		linphone_core_store_friend_in_db(lc, fr);
	} else {
		linphone_core_write_friends_config(lc);
	}
769
#else
770
	linphone_core_write_friends_config(lc);
771
#endif
772 773 774
}

void linphone_friend_apply(LinphoneFriend *fr, LinphoneCore *lc) {
775
	LinphonePresenceModel *model;
776
	const LinphoneAddress *addr = linphone_friend_get_address(fr);
777

778
	if (!addr) {
779
		ms_debug("No sip url defined in friend %s", linphone_friend_get_name(fr));
aymeric's avatar
aymeric committed
780 781
		return;
	}
782 783 784 785 786
	if (!linphone_core_ready(lc)) {
		/* lc not ready, deffering subscription */
		fr->commit=TRUE;
		return;
	}
jehan's avatar
jehan committed
787

788 789
	if (fr->inc_subscribe_pending) {
		switch(fr->pol) {
aymeric's avatar
aymeric committed
790
			case LinphoneSPWait:
791
				model = linphone_presence_model_new_with_activity(LinphonePresenceActivityOther, "Waiting for user acceptance");
792
				linphone_friend_notify(fr, model);
793
				linphone_presence_model_unref(model);
aymeric's avatar
aymeric committed
794 795
				break;
			case LinphoneSPAccept:
796 797
				if (fr->lc)
					linphone_friend_notify(fr, fr->lc->presence_model);
aymeric's avatar
aymeric committed
798 799
				break;
			case LinphoneSPDeny:
800
				linphone_friend_notify(fr, NULL);
aymeric's avatar
aymeric committed
801 802
				break;
		}
803 804
		fr->inc_subscribe_pending = FALSE;
	}
805 806 807 808
	
	if (fr->pol == LinphoneSPDeny && fr->insubs) {
		linphone_friend_close_incoming_subscriptions(fr);
	}
809

810 811
	linphone_friend_update_subscribes(fr, linphone_core_should_subscribe_friends_only_when_registered(lc));

812
	ms_debug("linphone_friend_apply() done.");
813
	lc->bl_refresh=TRUE;
814
	fr->commit=FALSE;
aymeric's avatar
aymeric committed
815 816
}

817
void linphone_friend_edit(LinphoneFriend *fr) {
818
	if (fr && linphone_core_vcard_supported() && fr->vcard) {
819 820
		linphone_vcard_compute_md5_hash(fr->vcard);
	}
aymeric's avatar
aymeric committed
821 822
}

823
void linphone_friend_done(LinphoneFriend *fr) {
824
	ms_return_if_fail(fr);
825
	if (!fr->lc) return;
826

827
	if (fr && linphone_core_vcard_supported() && fr->vcard) {
828
		if (linphone_vcard_compare_md5_hash(fr->vcard) != 0) {
829 830 831 832 833
			ms_debug("vCard's md5 has changed, mark friend as dirty and clear sip addresses list cache");
			linphone_vcard_clean_cache(fr->vcard);
			if (fr->friend_list) {
				fr->friend_list->dirty_friends_to_update = bctbx_list_append(fr->friend_list->dirty_friends_to_update, linphone_friend_ref(fr));
			}
834 835
		}
	}
836 837
	linphone_friend_apply(fr, fr->lc);
	linphone_friend_save(fr, fr->lc);
aymeric's avatar
aymeric committed
838 839
}

840 841 842
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
843 844 845
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
846
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
847
#endif
848
LinphoneFriend * linphone_core_create_friend(LinphoneCore *lc) {
849 850 851
	LinphoneFriend * lf = linphone_friend_new();
	lf->lc = lc;
	return lf;
852 853 854
}

LinphoneFriend * linphone_core_create_friend_with_address(LinphoneCore *lc, const char *address) {
855 856 857 858
	LinphoneFriend * lf = linphone_friend_new_with_address(address);
	if (lf)
		lf->lc = lc;
	return lf;
859
}
860 861 862
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif
863

864
void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *lf) {
865
	if (linphone_friend_list_add_friend(linphone_core_get_default_friend_list(lc), lf) != LinphoneFriendListOK) return;
866
	if (bctbx_list_find(lc->subscribers, lf)) {
867
		/*if this friend was in the pending subscriber list, now remove it from this list*/
868
		lc->subscribers = bctbx_list_remove(lc->subscribers, lf);
869 870
		linphone_friend_unref(lf);
	}
aymeric's avatar
aymeric committed
871 872
}

873
void linphone_core_remove_friend(LinphoneCore *lc, LinphoneFriend *lf) {
874 875 876 877
	if (lf && lf->friend_list) {
		if (linphone_friend_list_remove_friend(lf->friend_list, lf) == LinphoneFriendListNonExistentFriend) {
			ms_error("linphone_core_remove_friend(): friend [%p] is not part of core's list.", lf);
		}
aymeric's avatar
aymeric committed
878 879 880
	}
}

881
void linphone_core_update_friends_subscriptions(LinphoneCore *lc) {
882
	bctbx_list_t *lists = lc->friends_lists;
883
	while (lists) {
884
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
885
		linphone_friend_list_update_subscriptions(list);
886
		lists = bctbx_list_next(lists);
Simon Morlat's avatar
Simon Morlat committed
887
	}
888 889 890 891 892
}

bool_t linphone_core_should_subscribe_friends_only_when_registered(const LinphoneCore *lc){
	return lp_config_get_int(lc->config,"sip","subscribe_presence_only_when_registered",1);
}
893

894
void linphone_core_send_initial_subscribes(LinphoneCore *lc) {
Ghislain MARY's avatar
Ghislain MARY committed
895

896 897
	if (lc->initial_subscribes_sent) return;
	lc->initial_subscribes_sent=TRUE;
898

899
	linphone_core_update_friends_subscriptions(lc);
Simon Morlat's avatar
Simon Morlat committed
900 901
}

902
void linphone_core_invalidate_friend_subscriptions(LinphoneCore *lc) {
903
	bctbx_list_t *lists = lc->friends_lists;
904
	while (lists) {
905
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
906
		linphone_friend_list_invalidate_subscriptions(list);
907
		lists = bctbx_list_next(lists);
908
	}
Simon Morlat's avatar
Simon Morlat committed
909
	lc->initial_subscribes_sent=FALSE;
910 911
}

912
void linphone_friend_set_ref_key(LinphoneFriend *lf, const char *key){
913
	if (lf->refkey != NULL) {
914
		ms_free(lf->refkey);
915 916 917 918 919 920 921
		lf->refkey = NULL;
	}
	if (key) {
		lf->refkey = ms_strdup(key);
	}
	if (lf->lc) {
		linphone_friend_save(lf, lf->lc);
922 923 924 925 926 927 928
	}
}

const char *linphone_friend_get_ref_key(const LinphoneFriend *lf){
	return lf->refkey;
}

929
LinphoneFriend *linphone_core_find_friend(const LinphoneCore *lc, const LinphoneAddress *addr) {
930
	bctbx_list_t *lists = lc->friends_lists;
931 932
	LinphoneFriend *lf = NULL;
	while (lists && !lf) {
933
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
934
		lf = linphone_friend_list_find_friend_by_address(list, addr);
935
		lists = bctbx_list_next(lists);
936 937
	}
	return lf;
938 939
}

940
LinphoneFriend *linphone_core_get_friend_by_address(const LinphoneCore *lc, const char *uri) {
941
	bctbx_list_t *lists = lc->friends_lists;
942 943
	LinphoneFriend *lf = NULL;
	while (lists && !lf) {
944
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
945
		lf = linphone_friend_list_find_friend_by_uri(list, uri);
946
		lists = bctbx_list_next(lists);
947 948
	}
	return lf;
949 950
}

951
LinphoneFriend *linphone_core_get_friend_by_ref_key(const LinphoneCore *lc, const char *key) {
952
	bctbx_list_t *lists = lc->friends_lists;
953 954
	LinphoneFriend *lf = NULL;
	while (lists && !lf) {
955
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
956
		lf = linphone_friend_list_find_friend_by_ref_key(list, key);
957
		lists = bctbx_list_next(lists);
958 959
	}
	return lf;
960 961
}

962
LinphoneFriend *linphone_core_find_friend_by_out_subscribe(const LinphoneCore *lc, SalOp *op) {
963
	bctbx_list_t *lists = lc->friends_lists;
964 965
	LinphoneFriend *lf = NULL;
	while (lists && !lf) {
966
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
967
		lf = linphone_friend_list_find_friend_by_out_subscribe(list, op);
968
		lists = bctbx_list_next(lists);
969 970 971 972
	}
	return lf;
}

973
LinphoneFriend *linphone_core_find_friend_by_inc_subscribe(const LinphoneCore *lc, SalOp *op) {
974
	bctbx_list_t *lists = lc->friends_lists;
975 976
	LinphoneFriend *lf = NULL;
	while (lists && !lf) {
977
		LinphoneFriendList *list = (LinphoneFriendList *)bctbx_list_get_data(lists);
978
		lf = linphone_friend_list_find_friend_by_inc_subscribe(list, op);
979
		lists = bctbx_list_next(lists);
980 981 982 983
	}
	return lf;
}

smorlat's avatar
smorlat committed
984
#define key_compare(s1,s2)	strcmp(s1,s2)
aymeric's avatar
aymeric committed
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

LinphoneSubscribePolicy __policy_str_to_enum(const char* pol){
	if (key_compare("accept",pol)==0){
		return LinphoneSPAccept;
	}
	if (key_compare("deny",pol)==0){
		return LinphoneSPDeny;
	}
	if (key_compare("wait",pol)==0){
		return LinphoneSPWait;
	}
	ms_warning("Unrecognized subscribe policy: %s",pol);
	return LinphoneSPWait;
}

LinphoneProxyConfig *__index_to_proxy(LinphoneCore *lc, int index){
1001
	if (index>=0) return (LinphoneProxyConfig*)bctbx_list_nth_data(lc->sip_conf.proxies,index);
aymeric's avatar
aymeric committed
1002 1003 1004 1005 1006 1007 1008 1009 1010
	else return NULL;
}

LinphoneFriend * linphone_friend_new_from_config_file(LinphoneCore *lc, int index){
	const char *tmp;
	char item[50];
	int a;
	LinphoneFriend *lf;
	LpConfig *config=lc->config;
1011

aymeric's avatar
aymeric committed
1012
	sprintf(item,"friend_%i",index);
1013

aymeric's avatar
aymeric committed
1014 1015 1016
	if (!lp_config_has_section(config,item)){
		return NULL;
	}
1017

aymeric's avatar
aymeric committed
1018 1019 1020 1021
	tmp=lp_config_get_string(config,item,"url",NULL);
	if (tmp==NULL) {
		return NULL;
	}
1022
	lf=linphone_core_create_friend_with_address(lc, tmp);
aymeric's avatar
aymeric committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	if (lf==NULL) {
		return NULL;
	}
	tmp=lp_config_get_string(config,item,"pol",NULL);
	if (tmp==NULL) linphone_friend_set_inc_subscribe_policy(lf,LinphoneSPWait);
	else{
		linphone_friend_set_inc_subscribe_policy(lf,__policy_str_to_enum(tmp));
	}
	a=lp_config_get_int(config,item,"subscribe",0);
	linphone_friend_send_subscribe(lf,a);
1033 1034
	a = lp_config_get_int(config, item, "presence_received", 0);
	lf->presence_received = (bool_t)a;
1035

1036
	linphone_friend_set_ref_key(lf,lp_config_get_string(config,item,"refkey",NULL));
aymeric's avatar
aymeric committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	return lf;
}

const char *__policy_enum_to_str(LinphoneSubscribePolicy pol){
	switch(pol){
		case LinphoneSPAccept:
			return "accept";
			break;
		case LinphoneSPDeny:
			return "deny";
			break;
		case LinphoneSPWait:
			return "wait";
			break;
	}
	ms_warning("Invalid policy enum value.");
	return "wait";
}

void linphone_friend_write_to_config_file(LpConfig *config, LinphoneFriend *lf, int index){
	char key[50];
	char *tmp;
1059
	const char *refkey;
1060

aymeric's avatar
aymeric committed
1061
	sprintf(key,"friend_%i",index);
1062

aymeric's avatar
aymeric committed
1063 1064 1065 1066
	if (lf==NULL){
		lp_config_clean_section(config,key);
		return;
	}
1067
	if (lf->uri!=NULL){
1068
		tmp=linphone_address_as_string(lf->uri);
aymeric's avatar
aymeric committed
1069 1070 1071 1072
		if (tmp==NULL) {
			return;
		}
		lp_config_set_string(config,key,"url",tmp);
Simon Morlat's avatar
Simon Morlat committed
1073
		ms_free(tmp);
aymeric's avatar
aymeric committed
1074 1075 1076
	}
	lp_config_set_string(config,key,"pol",__policy_enum_to_str(lf->pol));
	lp_config_set_int(config,key,"subscribe",lf->subscribe);
1077
	lp_config_set_int(config, key, "presence_received", lf->presence_received);
1078 1079 1080 1081 1082

	refkey=linphone_friend_get_ref_key(lf);
	if (refkey){
		lp_config_set_string(config,key,"refkey",refkey);
	}
aymeric's avatar
aymeric committed
1083
}
1084

1085
void linphone_core_write_friends_config(LinphoneCore* lc) {
1086
	bctbx_list_t *elem;
1087
	int i;
1088
	int store_friends;
1089

1090
	if (! linphone_core_ready(lc)) return; /*dont write config when reading it !*/
1091 1092
	store_friends = lp_config_get_int(lc->config, "misc", "store_friends", 1);
	if (store_friends) {
1093
		for (elem=linphone_core_get_default_friend_list(lc)->friends,i=0; elem!=NULL; elem=bctbx_list_next(elem),i++){
1094
			linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)bctbx_list_get_data(elem),i);
1095 1096
		}
		linphone_friend_write_to_config_file(lc->config,NULL,i);	/* set the end */
1097 1098 1099
	}
}

1100 1101 1102 1103
LinphoneCore *linphone_friend_get_core(const LinphoneFriend *fr){
	return fr->lc;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
LinphoneFriend *linphone_friend_ref(LinphoneFriend *lf) {
	belle_sip_object_ref(lf);
	return lf;
}

void linphone_friend_unref(LinphoneFriend *lf) {
	belle_sip_object_unref(lf);
}

/* DEPRECATED */
void linphone_friend_destroy(LinphoneFriend *lf) {
	linphone_friend_unref(lf);
}

1118
LinphoneVcard* linphone_friend_get_vcard(LinphoneFriend *fr) {
1119
	if (fr && linphone_core_vcard_supported()) return fr->vcard;
1120
	return NULL;
1121 1122
}

1123
void linphone_friend_set_vcard(LinphoneFriend *fr, LinphoneVcard *vcard) {
1124
	if (!fr || !linphone_core_vcard_supported()) return;
1125

1126
	if (fr->vcard) linphone_vcard_unref(fr->vcard);
1127
	fr->vcard = vcard;
1128
	linphone_friend_save(fr, fr->lc);
1129 1130
}

1131
bool_t linphone_friend_create_vcard(LinphoneFriend *fr, const char *name) {
1132
	LinphoneVcard *vcard = NULL;
1133 1134
	LinphoneCore *lc = NULL;
	bool_t skip = FALSE;
1135

1136 1137
	if (!fr || !name) {
		ms_error("Friend or name is null");
1138 1139
		return FALSE;
	}
1140 1141
	if (!linphone_core_vcard_supported()) {
		ms_warning("VCard support is not builtin");
1142 1143
		return FALSE;
	}
1144 1145
	if (fr->vcard) {
		ms_error("Friend already has a VCard");
1146 1147
		return FALSE;
	}
1148

1149
	vcard = linphone_factory_create_vcard(linphone_factory_get());
1150 1151 1152 1153 1154 1155 1156 1157 1158
	
	lc = fr->lc;
	if (!lc && fr->friend_list) {
		lc = fr->friend_list->lc;
	}
	if (lc) {
		skip = 1 - lp_config_get_int(fr->lc->config, "misc", "store_friends", 1);
		linphone_vcard_set_skip_validation(vcard, skip);
	}
1159
	linphone_vcard_set_full_name(vcard, name);
1160
	linphone_friend_set_vcard(fr, vcard);