From 5fe2c68b44bf3be237887bc0adcd8f19cc7671d5 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 1 Feb 2016 15:41:22 +0100 Subject: [PATCH] Added method to import friends from buffer instead of file --- coreapi/friend.c | 30 +++++++++++++++++++ coreapi/linphonecore_jni.cc | 7 +++++ coreapi/linphonefriend.h | 8 +++++ .../org/linphone/core/LinphoneCore.java | 6 ++++ .../org/linphone/core/LinphoneCoreImpl.java | 6 ++++ 5 files changed, 57 insertions(+) diff --git a/coreapi/friend.c b/coreapi/friend.c index c5ab26db2..ca5a3a13b 100644 --- a/coreapi/friend.c +++ b/coreapi/friend.c @@ -942,6 +942,36 @@ int linphone_core_import_friends_from_vcard4_file(LinphoneCore *lc, const char * return count; } +int linphone_core_import_friends_from_vcard4_buffer(LinphoneCore *lc, const char *vcard_buffer) { + MSList *vcards = linphone_vcard_list_from_vcard4_buffer(vcard_buffer); + int count = 0; + +#ifndef VCARD_ENABLED + ms_error("vCard support wasn't enabled at compilation time"); +#endif + if (!vcards) { + ms_error("Failed to parse the buffer"); + return -1; + } + while (vcards != NULL && vcards->data != NULL) { + LinphoneVCard *vcard = (LinphoneVCard *)vcards->data; + LinphoneFriend *lf = linphone_friend_new_from_vcard(vcard); + if (lf) { + if (LinphoneFriendListOK == linphone_friend_list_import_friend(linphone_core_get_default_friend_list(lc), lf, TRUE)) { + count++; + } + linphone_friend_unref(lf); + } else { + linphone_vcard_free(vcard); + } + vcards = ms_list_next(vcards); + } +#ifndef FRIENDS_SQL_STORAGE_ENABLED + linphone_core_write_friends_config(lc); +#endif + return count; +} + void linphone_core_export_friends_as_vcard4_file(LinphoneCore *lc, const char *vcard_file) { FILE *file = NULL; const MSList *friends = linphone_core_get_friend_list(lc); diff --git a/coreapi/linphonecore_jni.cc b/coreapi/linphonecore_jni.cc index c73ed9ca0..6ce4611af 100644 --- a/coreapi/linphonecore_jni.cc +++ b/coreapi/linphonecore_jni.cc @@ -1955,6 +1955,13 @@ extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_importFriendsFromVCardFi return count; } +extern "C" jint Java_org_linphone_core_LinphoneCoreImpl_importFriendsFromVCardBuffer(JNIEnv* env, jobject thiz, jlong lc, jstring jbuffer) { + const char* buffer = env->GetStringUTFChars(jbuffer, NULL); + int count = linphone_core_import_friends_from_vcard4_buffer((LinphoneCore*)lc, buffer); + env->ReleaseStringUTFChars(jbuffer, buffer); + return count; +} + extern "C" void Java_org_linphone_core_LinphoneCoreImpl_exportFriendsToVCardFile(JNIEnv* env, jobject thiz, jlong lc, jstring jpath) { const char* path = env->GetStringUTFChars(jpath, NULL); linphone_core_export_friends_as_vcard4_file((LinphoneCore*)lc, path); diff --git a/coreapi/linphonefriend.h b/coreapi/linphonefriend.h index 86c4a641a..52afbae69 100644 --- a/coreapi/linphonefriend.h +++ b/coreapi/linphonefriend.h @@ -464,6 +464,14 @@ LINPHONE_PUBLIC LinphoneFriend *linphone_friend_new_from_vcard(LinphoneVCard *vc */ LINPHONE_PUBLIC int linphone_core_import_friends_from_vcard4_file(LinphoneCore *lc, const char *vcard_file); +/** + * Creates and adds LinphoneFriend objects to LinphoneCore from a buffer that contains the vCard(s) to parse + * @param[in] lc the LinphoneCore object + * @param[in] vcard_buffer the buffer that contains the vCard(s) to parse + * @return the amount of linphone friends created + */ +LINPHONE_PUBLIC int linphone_core_import_friends_from_vcard4_buffer(LinphoneCore *lc, const char *vcard_buffer); + /** * Creates and export LinphoneFriend objects from LinphoneCore to a file using vCard 4 format * @param[in] lc the LinphoneCore object diff --git a/java/common/org/linphone/core/LinphoneCore.java b/java/common/org/linphone/core/LinphoneCore.java index 389d59f87..6afcb11b9 100644 --- a/java/common/org/linphone/core/LinphoneCore.java +++ b/java/common/org/linphone/core/LinphoneCore.java @@ -2270,6 +2270,12 @@ public interface LinphoneCore { **/ public int importFriendsFromVCardFile(String file); + /** + * Imports LinphoneFriends from a vCard 4 buffer + * @return the number of friend imported + **/ + public int importFriendsFromVCardBuffer(String buffer); + /** * Exports LinphoneFriends to a vCard 4 file **/ diff --git a/java/impl/org/linphone/core/LinphoneCoreImpl.java b/java/impl/org/linphone/core/LinphoneCoreImpl.java index 54a904c79..feb0716a6 100644 --- a/java/impl/org/linphone/core/LinphoneCoreImpl.java +++ b/java/impl/org/linphone/core/LinphoneCoreImpl.java @@ -1627,6 +1627,12 @@ class LinphoneCoreImpl implements LinphoneCore { return importFriendsFromVCardFile(nativePtr, file); } + private native int importFriendsFromVCardBuffer(long nativePtr, String buffer); + @Override + public int importFriendsFromVCardBuffer(String buffer) { + return importFriendsFromVCardBuffer(nativePtr, buffer); + } + private native void exportFriendsToVCardFile(long nativePtr, String file); @Override public void exportFriendsToVCardFile(String file) {