Commit 47e0b047 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩
Browse files

Added export functions for contacts as vCards + added documentation for vCard API

parent 132f389b
......@@ -834,6 +834,31 @@ int linphone_core_import_friends_from_vcard4_file(LinphoneCore *lc, const char *
return count;
}
void linphone_core_export_friends_as_vcard4_file(LinphoneCore *lc, const char *vcard_file) {
FILE *file = NULL;
MSList *friends = lc->friends;
file = fopen(vcard_file, "w");
if (file == NULL) {
ms_warning("Could not write %s ! Maybe it is read-only. Contacts will not be saved.", vcard_file);
return;
}
while (friends != NULL && friends->data != NULL) {
LinphoneFriend *lf = (LinphoneFriend *)friends->data;
LinphoneVCard *vcard = linphone_friend_get_vcard(lf);
if (vcard) {
const char *vcard_text = linphone_vcard_as_vcard4_string(vcard);
fprintf(file, "%s", vcard_text);
} else {
ms_warning("Couldn't export friend %s because it doesn't have a vCard attached", linphone_address_as_string(linphone_friend_get_address(lf)));
}
friends = ms_list_next(friends);
}
fclose(file);
}
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneFriend);
BELLE_SIP_INSTANCIATE_VPTR(LinphoneFriend, belle_sip_object_t,
......
......@@ -415,20 +415,20 @@ LINPHONE_PUBLIC void linphone_friend_unref(LinphoneFriend *lf);
LINPHONE_PUBLIC LinphoneCore *linphone_friend_get_core(const LinphoneFriend *fr);
/**
* Returns the VCard object associated to this friend, if any
* Returns the vCard object associated to this friend, if any
* @param[in] fr LinphoneFriend object
*/
LINPHONE_PUBLIC LinphoneVCard* linphone_friend_get_vcard(LinphoneFriend *fr);
/**
* Binds a VCard object to a friend
* Binds a vCard object to a friend
* @param[in] fr LinphoneFriend object
* @param[in] vcard The VCard object to bind
* @param[in] vcard The vCard object to bind
*/
LINPHONE_PUBLIC void linphone_friend_set_vcard(LinphoneFriend *fr, LinphoneVCard *vcard);
/**
* Creates a VCard object associated to this friend if there isn't one yet and if the full name is available, either by the parameter or the one in the friend's SIP URI
* Creates a vCard object associated to this friend if there isn't one yet and if the full name is available, either by the parameter or the one in the friend's SIP URI
* @param[in] fr LinphoneFriend object
* @param[in] name The full name of the friend or NULL to use the one from the friend's SIP URI
* @return true if the vCard has been created, false if it wasn't possible (for exemple if name and the friend's SIP URI are null or if the friend's SIP URI doesn't have a display name), or if there is already one vcard
......@@ -437,19 +437,26 @@ LINPHONE_PUBLIC bool_t linphone_friend_create_vcard(LinphoneFriend *fr, const ch
/**
* Contructor same as linphone_friend_new() + linphone_friend_set_address()
* @param vcard a VCard object
* @return a new #LinphoneFriend with \link linphone_friend_get_vcard() vcard initialized \endlink
* @param vcard a vCard object
* @return a new #LinphoneFriend with \link linphone_friend_get_vcard() vCard initialized \endlink
*/
LINPHONE_PUBLIC LinphoneFriend *linphone_friend_new_from_vcard(LinphoneVCard *vcard);
/**
* Creates and adds LinphoneFriend objects to LinphoneCore from a file that contains the vcard(s) to parse
* Creates and adds LinphoneFriend objects to LinphoneCore from a file that contains the vCard(s) to parse
* @param[in] lc the LinphoneCore object
* @param[in] vcard_file the path to a file that contains the vcard(s) to parse
* @param[in] vcard_file the path to a file that contains the vCard(s) to parse
* @return the amount of linphone friends created
*/
LINPHONE_PUBLIC int linphone_core_import_friends_from_vcard4_file(LinphoneCore *lc, const char *vcard_file);
/**
* Creates and export LinphoneFriend objects from LinphoneCore to a file using vCard 4 format
* @param[in] lc the LinphoneCore object
* @param[in] vcard_file the path to a file that will contain the vCards
*/
LINPHONE_PUBLIC void linphone_core_export_friends_as_vcard4_file(LinphoneCore *lc, const char *vcard_file);
/**
* @}
*/
......
......@@ -53,6 +53,10 @@ extern "C" MSList* linphone_vcard_new_from_vcard4_file(const char *filename) {
return result;
}
extern "C" const char * linphone_vcard_as_vcard4_string(LinphoneVCard *vcard) {
return vcard->belcard->toFoldedString().c_str();
}
extern "C" void linphone_vcard_set_full_name(LinphoneVCard *vcard, const char *name) {
shared_ptr<belcard::BelCardFullName> fn = belcard::BelCardGeneric::create<belcard::BelCardFullName>();
fn->setValue(name);
......
......@@ -31,17 +31,63 @@ extern "C"
{
#endif
/**
* @addtogroup buddy_list
* @{
*/
typedef struct _LinphoneVCard LinphoneVCard;
/**
* Creates a LinphoneVCard object that has a pointer to an empty vCard
*/
LINPHONE_PUBLIC LinphoneVCard* linphone_vcard_new(void);
/**
* Deletes a LinphoneVCard object properly
* @param[in] vcard the LinphoneVCard to destroy
*/
LINPHONE_PUBLIC void linphone_vcard_free(LinphoneVCard *vcard);
/**
* Uses belcard to parse the content of a file and returns all the vcards it contains as LinphoneVCards, or NULL if it contains none.
* @param[in] file the path to the file to parse
* @return \mslist{LinphoneVCard}
*/
LINPHONE_PUBLIC MSList* linphone_vcard_new_from_vcard4_file(const char *file);
/**
* Returns the vCard4 representation of the LinphoneVCard.
* @param[in] vcard the LinphoneVCard
* @return a const char * that represents the vcard
*/
LINPHONE_PUBLIC const char* linphone_vcard_as_vcard4_string(LinphoneVCard *vcard);
/**
* Sets the FN attribute of the vCard (which is mandatory).
* @param[in] vcard the LinphoneVCard
* @param[in] name the display name to set for the vCard
*/
LINPHONE_PUBLIC void linphone_vcard_set_full_name(LinphoneVCard *vcard, const char *name);
/**
* Returns the FN attribute of the vCard, or NULL if it isn't set yet.
* @param[in] vcard the LinphoneVCard
* @return the display name of the vCard, or NULL
*/
LINPHONE_PUBLIC const char* linphone_vcard_get_full_name(const LinphoneVCard *vcard);
/**
* Returns the list of SIP addresses (as const char *) in the vCard (all the IMPP attributes that has an URI value starting by "sip:") or NULL
* @param[in] vcard the LinphoneVCard
* @return \mslist{const char *}
*/
LINPHONE_PUBLIC MSList* linphone_vcard_get_sip_addresses(const LinphoneVCard *vcard);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
......
......@@ -1834,17 +1834,29 @@ void linphone_gtk_show_keypad_checked(GtkCheckMenuItem *check_menu_item) {
void linphone_gtk_import_contacts(void) {
GtkWidget *mw = linphone_gtk_get_main_window();
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open File", (GtkWindow *)mw, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open vCard file", (GtkWindow *)mw, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
LinphoneCore *lc = linphone_gtk_get_core();
char *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
linphone_core_import_friends_from_vcard4_file(lc, filename);
g_free (filename);
g_free(filename);
linphone_gtk_show_friends();
}
gtk_widget_destroy (dialog);
gtk_widget_destroy(dialog);
}
void linphone_gtk_export_contacts(void) {
GtkWidget *mw = linphone_gtk_get_main_window();
GtkWidget *dialog = gtk_file_chooser_dialog_new("Save vCards as", (GtkWindow *)mw, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
LinphoneCore *lc = linphone_gtk_get_core();
char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
linphone_core_export_friends_as_vcard4_file(lc, filename);
g_free(filename);
}
gtk_widget_destroy(dialog);
}
gboolean linphone_gtk_keypad_destroyed_handler(void) {
......
......@@ -232,11 +232,20 @@
<object class="GtkMenuItem" id="contacts_import_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Import contacts</property>
<property name="label" translatable="yes">Import contacts from vCards</property>
<property name="use_underline">True</property>
<signal name="activate" handler="linphone_gtk_import_contacts" swapped="no"/>
</object>
</child>
<child>
<object class="GtkMenuItem" id="contacts_export_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Export contacts as vCards</property>
<property name="use_underline">True</property>
<signal name="activate" handler="linphone_gtk_export_contacts" swapped="no"/>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem3">
<property name="visible">True</property>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment