diff --git a/coreapi/carddav.c b/coreapi/carddav.c index 9f0ffa658..67d6184dd 100644 --- a/coreapi/carddav.c +++ b/coreapi/carddav.c @@ -129,6 +129,7 @@ static void linphone_carddav_vcards_pulled(LinphoneCardDavContext *cdc, bctbx_li ms_debug("Downloaded vCard etag/url are %s and %s", vCard->etag, full_url); lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); /*ref is now owned by friend*/ if (lf) { local_friend = bctbx_list_find_custom(friends, (int (*)(const void*, const void*))find_matching_friend, lf); diff --git a/coreapi/friend.c b/coreapi/friend.c index b6c883b91..30c3dd852 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -1397,6 +1397,7 @@ static int create_friend(void *data, int argc, char **argv, char **colName) { linphone_vcard_set_etag(vcard, argv[7]); linphone_vcard_set_url(vcard, argv[8]); lf = linphone_friend_new_from_vcard(vcard); + linphone_vcard_unref(vcard); } if (!lf) { lf = linphone_friend_new(); diff --git a/coreapi/friendlist.c b/coreapi/friendlist.c index 9c4bc9eeb..4738b7f3a 100644 --- a/coreapi/friendlist.c +++ b/coreapi/friendlist.c @@ -1007,10 +1007,41 @@ LinphoneCore* linphone_friend_list_get_core(const LinphoneFriendList *list) { return list->lc; } -LinphoneStatus linphone_friend_list_import_friends_from_vcard4_file(LinphoneFriendList *list, const char *vcard_file) { - bctbx_list_t *vcards = NULL; +static LinphoneStatus linphone_friend_list_import_friends_from_vcard4(LinphoneFriendList *list, bctbx_list_t *vcards) { bctbx_list_t *vcards_iterator = NULL; int count = 0; + + if (!linphone_core_vcard_supported()) { + ms_error("vCard support wasn't enabled at compilation time"); + return -1; + } + if (!list) { + ms_error("Can't import into a NULL list"); + return -1; + } + + vcards_iterator = vcards; + + while (vcards_iterator != NULL && bctbx_list_get_data(vcards_iterator) != NULL) { + LinphoneVcard *vcard = (LinphoneVcard *)bctbx_list_get_data(vcards_iterator); + LinphoneFriend *lf = linphone_friend_new_from_vcard(vcard); + linphone_vcard_unref(vcard); + if (lf) { + if (LinphoneFriendListOK == linphone_friend_list_import_friend(list, lf, TRUE)) { + linphone_friend_save(lf, lf->lc); + count++; + } + linphone_friend_unref(lf); + } + vcards_iterator = bctbx_list_next(vcards_iterator); + } + bctbx_list_free(vcards); + linphone_core_store_friends_list_in_db(list->lc, list); + return count; + +} +LinphoneStatus linphone_friend_list_import_friends_from_vcard4_file(LinphoneFriendList *list, const char *vcard_file) { + bctbx_list_t *vcards = NULL; if (!linphone_core_vcard_supported()) { ms_error("vCard support wasn't enabled at compilation time"); @@ -1022,35 +1053,15 @@ LinphoneStatus linphone_friend_list_import_friends_from_vcard4_file(LinphoneFrie } vcards = linphone_vcard_context_get_vcard_list_from_file(list->lc->vcard_context, vcard_file); - vcards_iterator = vcards; if (!vcards) { ms_error("Failed to parse the file %s", vcard_file); return -1; } - - while (vcards_iterator != NULL && bctbx_list_get_data(vcards_iterator) != NULL) { - LinphoneVcard *vcard = (LinphoneVcard *)bctbx_list_get_data(vcards_iterator); - LinphoneFriend *lf = linphone_friend_new_from_vcard(vcard); - if (lf) { - if (LinphoneFriendListOK == linphone_friend_list_import_friend(list, lf, TRUE)) { - linphone_friend_save(lf, lf->lc); - count++; - } - linphone_friend_unref(lf); - } else { - linphone_vcard_unref(vcard); - } - vcards_iterator = bctbx_list_next(vcards_iterator); - } - bctbx_list_free(vcards); - linphone_core_store_friends_list_in_db(list->lc, list); - return count; + return linphone_friend_list_import_friends_from_vcard4(list,vcards); } LinphoneStatus linphone_friend_list_import_friends_from_vcard4_buffer(LinphoneFriendList *list, const char *vcard_buffer) { bctbx_list_t *vcards = NULL; - bctbx_list_t *vcards_iterator = NULL; - int count = 0; if (!linphone_core_vcard_supported()) { ms_error("vCard support wasn't enabled at compilation time"); @@ -1062,29 +1073,12 @@ LinphoneStatus linphone_friend_list_import_friends_from_vcard4_buffer(LinphoneFr } vcards = linphone_vcard_context_get_vcard_list_from_buffer(list->lc->vcard_context, vcard_buffer); - vcards_iterator = vcards; if (!vcards) { ms_error("Failed to parse the buffer"); return -1; } - while (vcards_iterator != NULL && bctbx_list_get_data(vcards_iterator) != NULL) { - LinphoneVcard *vcard = (LinphoneVcard *)bctbx_list_get_data(vcards_iterator); - LinphoneFriend *lf = linphone_friend_new_from_vcard(vcard); - if (lf) { - if (LinphoneFriendListOK == linphone_friend_list_import_friend(list, lf, TRUE)) { - count++; - } - linphone_friend_unref(lf); - } else { - linphone_vcard_unref(vcard); - } - vcards_iterator = bctbx_list_next(vcards_iterator); - } - bctbx_list_free(vcards); - linphone_core_store_friends_list_in_db(list->lc, list); - return count; -} + return linphone_friend_list_import_friends_from_vcard4(list,vcards);} void linphone_friend_list_export_friends_as_vcard4_file(LinphoneFriendList *list, const char *vcard_file) { FILE *file = NULL; diff --git a/tester/vcard_tester.c b/tester/vcard_tester.c index 89274ff62..d272ee06b 100644 --- a/tester/vcard_tester.c +++ b/tester/vcard_tester.c @@ -147,6 +147,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { LinphoneCoreManager* manager = linphone_core_manager_new2("empty_rc", FALSE); LinphoneVcard *lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini@sip.linphone.org\r\nIMPP;TYPE=home:sip:sylvain@sip.linphone.org\r\nTEL;TYPE=work:0952636505\r\nEND:VCARD\r\n"); LinphoneFriend *lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); const bctbx_list_t *sip_addresses = linphone_friend_get_addresses(lf); bctbx_list_t *phone_numbers = linphone_friend_get_phone_numbers(lf); LinphoneAddress *addr = NULL; @@ -158,6 +159,7 @@ static void linphone_vcard_phone_numbers_and_sip_addresses(void) { lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nTEL;TYPE=work:0952636505\r\nTEL:0476010203\r\nEND:VCARD\r\n"); lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); lf->lc = manager->lc; sip_addresses = linphone_friend_get_addresses(lf); phone_numbers = linphone_friend_get_phone_numbers(lf); @@ -301,6 +303,7 @@ static void friends_sqlite_storage(void) { linphone_vcard_set_etag(lvc, "\"123-456789\""); linphone_vcard_set_url(lvc, "http://dav.somewhere.fr/addressbook/me/someone.vcf"); lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); linphone_friend_set_address(lf, addr); linphone_friend_set_name(lf, "Sylvain"); @@ -635,6 +638,7 @@ static void carddav_sync_3(void) { LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc); LinphoneCardDavContext *c = NULL; + linphone_vcard_unref(lvc); linphone_friend_list_set_uri(lfl, CARDDAV_SERVER); linphone_core_add_friend_list(manager->lc, lfl); linphone_friend_list_unref(lfl); @@ -674,6 +678,7 @@ static void carddav_sync_4(void) { LinphoneFriendList *lfl = linphone_core_create_friend_list(manager->lc); LinphoneCardDavContext *c = NULL; + linphone_vcard_unref(lvc); linphone_friend_list_set_uri(lfl, CARDDAV_SERVER); linphone_core_add_friend_list(manager->lc, lfl); linphone_friend_list_unref(lfl); @@ -740,6 +745,7 @@ static void carddav_integration(void) { char *address = NULL; const LinphoneAddress *addr; + linphone_vcard_unref(lvc); linphone_friend_list_set_uri(lfl, CARDDAV_SERVER); cbs = linphone_friend_list_get_callbacks(lfl); linphone_friend_list_cbs_set_user_data(cbs, stats); @@ -766,12 +772,14 @@ static void carddav_integration(void) { lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Ghislain Mary\r\nIMPP;TYPE=work:sip:ghislain@sip.linphone.org\r\nEND:VCARD\r\n"); lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf), LinphoneFriendListOK, int, "%d"); linphone_friend_unref(lf); lvc2 = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sberfini@sip.linphone.org\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nEND:VCARD\r\n"); linphone_vcard_set_url(lvc2, "/card.php/addressbooks/tester/default/me.vcf"); lf2 = linphone_friend_new_from_vcard(lvc2); + linphone_vcard_unref(lvc2); linphone_friend_set_ref_key(lf2, refkey); BC_ASSERT_EQUAL(linphone_friend_list_add_local_friend(lfl, lf2), LinphoneFriendListOK, int, "%d"); @@ -853,6 +861,7 @@ static void carddav_clean(void) { // This is to ensure the content of the test lvc = linphone_vcard_context_get_vcard_from_buffer(manager->lc->vcard_context, "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Sylvain Berfini\r\nIMPP:sip:sylvain@sip.linphone.org\r\nUID:1f08dd48-29ac-4097-8e48-8596d7776283\r\nEND:VCARD\r\n"); linphone_vcard_set_url(lvc, "http://dav.linphone.org/card.php/addressbooks/tester/default/me.vcf"); lf = linphone_friend_new_from_vcard(lvc); + linphone_vcard_unref(lvc); linphone_friend_list_add_friend(lfl, lf); wait_for_until(manager->lc, NULL, &stats->sync_done_count, 1, CARDDAV_SYNC_TIMEOUT); BC_ASSERT_EQUAL(stats->sync_done_count, 1, int, "%i"); @@ -904,6 +913,8 @@ static void carddav_server_to_client_and_client_to_sever_sync(void) { LinphoneFriend *lf2 = linphone_friend_new_from_vcard(lvc2); bctbx_list_t *friends = NULL, *friends_iterator = NULL; + linphone_vcard_unref(lvc1); + linphone_vcard_unref(lvc2); linphone_friend_list_cbs_set_user_data(cbs, stats); linphone_friend_list_cbs_set_contact_created(cbs, carddav_contact_created); linphone_friend_list_cbs_set_contact_deleted(cbs, carddav_contact_deleted);