+
+void VCard_set_property(VCard* object, const gchar* property, GSList* props) {
+ GSList *values = NULL;
+
+ if (object && object->vcard && property && props) {
+ //print_slist(props);
+ values = g_hash_table_lookup(object->vcard, property);
+ if (values) {
+ /* Remove before adding new */
+ g_hash_table_remove(object->vcard, property);
+ }
+ g_hash_table_replace(object->vcard, g_strdup(property), props);
+ }
+}
+
+void VCard_set_etag(VCard* vcard, const gchar* etag) {
+ if (vcard == NULL) return;
+
+ if (vcard->etag)
+ g_free(vcard->etag);
+
+ vcard->etag = g_strdup(etag);
+}
+
+void VCard_set_name(VCard* vcard, const gchar* firstname, const gchar* lastname) {
+ GSList *fn_value = NULL;
+ GSList *n_value = NULL;
+
+ if (vcard == NULL) return;
+
+ if (firstname == NULL || lastname == NULL) {
+ VCard_set_property(vcard, "N", NULL);
+ VCard_set_property(vcard, "FN", NULL);
+ } else {
+ gchar* name = g_strconcat(firstname, " ", lastname, NULL);
+ VCardProperty* prop = VCardProperty_new("FN", name);
+ fn_value = g_slist_prepend(fn_value, prop);
+ VCard_set_property(vcard, "FN", fn_value);
+ g_free(name);
+ name = g_strconcat(lastname, ";", firstname, ";", NULL);
+ prop = VCardProperty_new("N", name);
+ n_value = g_slist_prepend(n_value, prop);
+ VCard_set_property(vcard, "N", n_value);
+ g_free(name);
+ }
+}
+
+void VCard_set_company(VCard* vcard, const gchar* company) {
+ GSList* org_value = NULL;
+ if (vcard == NULL) return;
+
+ if (company == NULL) {
+ VCard_set_property(vcard, "ORG", NULL);
+ }
+
+ VCardProperty* prop = VCardProperty_new("ORG", company);
+ org_value = g_slist_prepend(org_value, prop);
+ VCard_set_property(vcard, "ORG", org_value);
+}
+
+void VCard_set_telefon(VCard* vcard, TelefonType type, const gchar* number) {
+ gchar* tel = NULL;
+ GSList* values = NULL;
+ if (vcard == NULL) return;
+
+ GSList* tel_value = VCard_get_property(vcard, "TEL");
+ if (number == NULL) {
+ if (tel_value) {
+ remove_tel_prop(tel_value, type);
+ VCard_set_property(vcard, "TEL", tel_value);
+ }
+ } else {
+ switch (type) {
+ case TEL_DEFAULT:
+ case TEL_MOBIL:
+ tel = g_strconcat("TYPE=MOBIL,VOICE:", number, NULL);
+ break;
+ case TEL_HOME:
+ tel = g_strconcat("TYPE=HOME,VOICE:", number, NULL);
+ break;
+ case TEL_WORK:
+ tel = g_strconcat("TYPE=WORK,VOICE:", number, NULL);
+ break;
+ }
+ if (tel_value) {
+ values = g_slist_copy_deep(tel_value, slist_copy_func, NULL);
+ }
+ VCardProperty* prop = VCardProperty_new("TEL", tel);
+ values = g_slist_prepend(values, prop);
+ VCard_set_property(vcard, "TEL", values);
+ g_free(tel);
+ }
+}
+
+void VCard_set_address(VCard* vcard, AddressType type, const gchar* address, const gchar* state, const gchar* city, const gchar* zip, const gchar* country) {
+ gchar* addr = NULL;
+ GSList* values = NULL;
+ if (vcard == NULL) return;
+
+ GSList* addr_value = VCard_get_property(vcard, "ADR");
+ if (address == NULL || city == NULL || zip == NULL || country == NULL) {
+ if (addr_value) {
+ remove_adr_prop(addr_value, type);
+ VCard_set_property(vcard, "ADR", addr_value);
+ }
+ } else {
+ gchar* state = NULL;
+ if (state)
+ state = g_strconcat(state, ";", NULL);
+ else
+ state = g_strdup("");
+
+ switch (type) {
+ case ADDR_DEFAULT:
+ case ADDR_HOME:
+ addr = g_strdup_printf("TYPE=HOME:;;%s;%s;%s;%s;%s;", address, city, zip, state, country);
+ //, address, ";", city, ";", zip, ";", state, ";", country, ";" , NULL);
+ break;
+ case ADDR_WORK:
+ addr = g_strconcat("TYPE=WORK:;;", address, ";", city, ";", zip, ";", state, country, ";" , NULL);
+ break;
+ case ADDR_OTHER:
+ addr = g_strconcat("TYPE=OTHER:;;", address, ";", city, ";", zip, ";", state, country, ";" , NULL);
+ break;
+ }
+ g_free(state);
+ if (addr_value) {
+ values = g_slist_copy_deep(addr_value, slist_copy_func, NULL);
+ }
+ VCardProperty* prop = VCardProperty_new("ADR", addr);
+ values = g_slist_prepend(values, prop);
+ VCard_set_property(vcard, "ADR", values);
+ g_free(addr);
+ }
+}
+
+void VCard_set_photo_uri(VCard* vcard, PhotoType type, const gchar* uri) {
+ gchar* photo = NULL;
+ if (vcard == NULL) return;
+
+ GSList* photo_value = VCard_get_property(vcard, "PHOTO");
+ if (uri == NULL) {
+ if (photo_value) {
+ VCard_set_property(vcard, "PHOTO", NULL);
+ }
+ } else {
+ switch (type) {
+ case PHOTO_DEFAULT:
+ case PHOTO_JPG:
+ photo = g_strconcat("VALUE=URI;TYPE=", "JPG:;", uri, NULL);
+ break;
+ case PHOTO_GIF:
+ photo = g_strconcat("VALUE=URI;TYPE=", "GIF:;", uri, NULL);
+ break;
+ case PHOTO_PNG:
+ photo = g_strconcat("VALUE=URI;TYPE=", "PNG:;", uri, NULL);
+ break;
+ }
+ VCardProperty* prop = VCardProperty_new("PHOTO", photo);
+ photo_value = g_slist_prepend(photo_value, prop);
+ VCard_set_property(vcard, "PHOTO", photo_value);
+ g_free(photo);
+ }
+}
+
+void VCard_set_photo_image(VCard* vcard, PhotoType type, const gchar* image) {
+ gchar* photo = NULL;
+ if (vcard == NULL) return;
+
+ GSList* photo_value = VCard_get_property(vcard, "PHOTO");
+ if (image == NULL) {
+ if (photo_value) {
+ VCard_set_property(vcard, "PHOTO", NULL);
+ }
+ } else {
+ switch (type) {
+ case PHOTO_DEFAULT:
+ case PHOTO_JPG:
+ photo = g_strconcat("TYPE=jpeg;", "ENCODING=b:", image, NULL);
+ break;
+ case PHOTO_GIF:
+ photo = g_strconcat("TYPE=gif;", "ENCODING=b:", image, NULL);
+ break;
+ case PHOTO_PNG:
+ photo = g_strconcat("TYPE=png;", "ENCODING=b:", image, NULL);
+ break;
+ }
+ VCardProperty* prop = VCardProperty_new("PHOTO", photo);
+ photo_value = g_slist_prepend(photo_value, prop);
+ VCard_set_property(vcard, "PHOTO", photo_value);
+ g_free(photo);
+ }
+}
+
+gchar* VCard_get_etag(VCard* vcard) {
+ if (vcard == NULL) return NULL;
+
+ return g_strdup(vcard->etag);
+}
+
+gchar* VCard_get_name(VCard* vcard) {
+ gchar* res = NULL;
+ if (vcard == NULL) return NULL;
+
+ GSList* fn = VCard_get_property(vcard, "FN");
+ if (fn) {
+ res = g_strdup(fn->data);
+ vcard_slist_property_free(fn);
+ }
+ return res;
+}
+
+gchar* VCard_get_company(VCard* vcard) {
+ gchar* res = NULL;
+ if (vcard == NULL) return NULL;
+
+ GSList* org = VCard_get_property(vcard, "ORG");
+ if (org) {
+ res = g_strdup(org->data);
+ vcard_slist_property_free(org);
+ }
+ return res;
+}
+
+GSList* VCard_get_telefon(VCard* vcard, TelefonType type) {
+ GSList* tel = NULL;
+ if (vcard == NULL) return NULL;
+
+ // TODO: return only telefon according to type
+
+ tel = VCard_get_property(vcard, "TEL");
+ return tel;
+}
+
+GSList* VCard_get_address(VCard* vcard, AddressType type) {
+ GSList* addr = NULL;
+ if (vcard == NULL) return NULL;
+
+ // TODO: return only address according to type
+
+ addr = VCard_get_property(vcard, "ADR");
+ return addr;
+}
+
+gchar* VCard_get_photo(VCard* vcard) {
+ gchar* res = NULL;
+ if (vcard == NULL) return NULL;
+
+ GSList* photo = VCard_get_property(vcard, "PHOTO");
+ if (photo) {
+ res = g_strdup(photo->data);
+ vcard_slist_property_free(photo);
+ }
+ return res;
+}
+
+void VCard_set_email(VCard* vcard, EmailType type, const gchar* email_addr) {
+ gchar* email = NULL;
+ GSList* values = NULL;
+ if (vcard == NULL) return;
+
+ GSList* email_value = VCard_get_property(vcard, "EMAIL");
+ if (email_addr == NULL) {
+ if (email_value) {
+ remove_email_prop(email_value, type);
+ VCard_set_property(vcard, "EMAIL", email_value);
+ }
+ } else {
+ switch (type) {
+ case EMAIL_DEFAULT:
+ case EMAIL_HOME:
+ email = g_strconcat("TYPE=HOME:", email_addr, NULL);
+ break;
+ case EMAIL_WORK:
+ email = g_strconcat("TYPE=WORK:", email_addr, NULL);
+ break;
+ case EMAIL_OTHER:
+ email = g_strconcat("TYPE=OTHER:", email_addr, NULL);
+ break;
+ }
+ if (email_value) {
+ //print_slist(email_value);
+ values = g_slist_copy_deep(email_value, slist_copy_func, NULL);
+ }
+ VCardProperty* prop = VCardProperty_new("EMAIL", email);
+ values = g_slist_prepend(values, prop);
+ VCard_set_property(vcard, "EMAIL", values);
+ g_free(email);
+ }
+}
+
+GSList* VCard_get_email(VCard* vcard, EmailType type) {
+ GSList* email = NULL;
+ if (vcard == NULL) return NULL;
+
+ // TODO: return only email according to type
+
+ email = VCard_get_property(vcard, "EMAIL");
+ return email;
+}
+