VCardParserResponse state;
VCardVersion version;
GHashTable* vcard;
+ gchar* etag;
};
static char *replaceWord(const char *s, const char *oldW, const char *newW) {
return result;
}
+static gchar* make_line(const gchar* name, const gchar* value) {
+ gchar* line = NULL;
+
+ if (name != NULL) {
+ if (g_ascii_strncasecmp("TEL", name, 3) == 0) {
+ line = g_strdup_printf("%s;%s", name, value);
+ } else if (g_ascii_strncasecmp("ADR", name, 3) == 0) {
+ line = g_strdup_printf("%s;%s", name, value);
+ } else if (g_ascii_strncasecmp("PHOTO", name, 3) == 0) {
+ line = g_strdup_printf("%s;%s", name, value);
+ } else if (g_ascii_strncasecmp("EMAIL", name, 3) == 0) {
+ line = g_strdup_printf("%s;%s", name, value);
+ } else {
+ line = g_strdup_printf("%s:%s", name, value);
+ }
+ }
+
+ return line;
+}
+
static void hash_table_cb(gpointer key, gpointer value, gpointer data) {
GSList* list = (GSList*) value;
gchar** str = (gchar**) data;
while (list) {
VCardProperty* vp = (VCardProperty*) list->data;
+
gchar* s0 = replaceWord(vp->value, "\n", "\n ");
+ gchar* line = make_line(vp->name, s0);
+ g_free(s0);
if (!*str)
- s = g_strconcat(vp->name, ":", s0, NULL);
+ s = g_strdup(line);
else {
- s = g_strconcat(*str, "\n", vp->name, ":", s0, NULL);
+ s = g_strdup_printf("%s\n%s", *str, line);
g_free(*str);
}
- g_free(s0);
*str = g_strdup(s);
g_free(s);
list = list->next;
return chunks;
}
-void VCard_free(VCard* object) {
- if (object == NULL) return;
+static VCardProperty* VCardProperty_new(const gchar* key, const gchar* value) {
+ VCardProperty* vp = g_new0(VCardProperty, 1);
+ vp->name = g_strdup(key);
+ vp->value = g_strdup(value);
- if (object->vcard)
- destroy_hash_table(object->vcard);
- g_free(object);
+ return vp;
+}
+
+static void remove_tel_prop(GSList* list, TelefonType type) {
+ VCardProperty* vp = NULL;
+ GSList* search = list;
+
+ if (search) {
+ while (search) {
+ vp = (VCardProperty*) search->data;
+ if (vp) {
+ gchar* tel = (gchar*)vp->value;
+ switch (type) {
+ case TEL_DEFAULT:
+ case TEL_MOBIL:
+ if (g_strstr_len(tel, -1, "TYPE=MOBIL")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case TEL_HOME:
+ if (g_strstr_len(tel, -1, "TYPE=HOME")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case TEL_WORK:
+ if (g_strstr_len(tel, -1, "TYPE=WORK")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ }
+
+ }
+ search = search->next;
+ }
+ }
+}
+
+static void remove_adr_prop(GSList* list, AddressType type) {
+ VCardProperty* vp = NULL;
+ GSList* search = list;
+
+ if (search) {
+ while (search) {
+ vp = (VCardProperty*) search->data;
+ if (vp) {
+ gchar* adr = (gchar*)vp->value;
+ switch (type) {
+ case ADDR_DEFAULT:
+ case ADDR_HOME:
+ if (g_strstr_len(adr, -1, "TYPE=HOME")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case ADDR_WORK:
+ if (g_strstr_len(adr, -1, "TYPE=WORK")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case ADDR_OTHER:
+ if (g_strstr_len(adr, -1, "TYPE=OTHER")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ }
+
+ }
+ search = search->next;
+ }
+ }
}
-void VCard_list_free(GSList* list) {
+static void remove_email_prop(GSList* list, EmailType type) {
+ VCardProperty* vp = NULL;
+ GSList* search = list;
+
+ if (search) {
+ while (search) {
+ vp = (VCardProperty*) search->data;
+ if (vp) {
+ gchar* email = (gchar*)vp->value;
+ switch (type) {
+ case EMAIL_DEFAULT:
+ case EMAIL_HOME:
+ if (g_strstr_len(email, -1, "TYPE=HOME")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case EMAIL_WORK:
+ if (g_strstr_len(email, -1, "TYPE=WORK")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ case EMAIL_OTHER:
+ if (g_strstr_len(email, -1, "TYPE=OTHER")) {
+ list = g_slist_remove(list, vp);
+ vcard_property_free(vp);
+ }
+ break;
+ }
+
+ }
+ search = search->next;
+ }
+ }
+}
+
+gchar* VCard_get_as_string(VCard* object) {
+ gchar* card = NULL;
+ if (object == NULL) return NULL;
+
+ if (object && object->vcard) {
+ gchar* version = vcard_version_2_str(object->version);
+ gchar* s = g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version);
+ g_free(version);
+ g_hash_table_foreach(object->vcard, hash_table_cb, &s);
+ card = g_strdup_printf("%s\nEND:VCARD", s);
+ g_free(s);
+ }
+
+ return card;
+}
+
+void vcard_slist_property_free(gpointer data) {
+ if (data == NULL) return;
+
+ GSList* list = (GSList *) data;
+ g_slist_free_full(list, (GDestroyNotify) vcard_property_free);
+}
+
+void vcard_property_free(gpointer data) {
+ if (!data) return;
+ VCardProperty* vp = (VCardProperty*) data;
+ g_free(vp->name);
+ g_free(vp->value);
+ g_free(vp);
+}
+
+void print_slist(GSList* list) {
GSList* tmp = list;
- while(tmp) {
- VCard* object = (VCard*) tmp->data;
- VCard_free(object);
+ while (tmp) {
+ VCardProperty* vp = (VCardProperty*) tmp->data;
+ g_print("Name: %s\n", vp->name);
+ g_print("Value: %s\n", vp->value);
tmp = tmp->next;
}
+}
+
+static gpointer slist_copy_func(gconstpointer src, gpointer data) {
+ const VCardProperty* vp = (const VCardProperty*) src;
+ VCardProperty* new_vp = VCardProperty_new(vp->name, vp->value);
- if(list)
- g_slist_free(list);
+ return (gpointer) new_vp;
}
-VCard* VCard_new(VCardVersion version) {
+void VCard_free(VCard* object) {
+ if (object == NULL) return;
+
+ if (object->vcard)
+ destroy_hash_table(object->vcard);
+ g_free(object->etag);
+ g_free(object);
+}
+
+VCard* VCard_version_new(VCardVersion version, gboolean init) {
VCard* vc = g_new0(VCard, 1);
vc->version = version;
+ if (init) {
+ vc->vcard = init_g_hash_table(vc->version);
+ gchar* uuid = g_uuid_string_random();
+ VCardProperty* prop = VCardProperty_new("UID", g_strdup(uuid));
+ g_free(uuid);
+ GSList* uid = g_slist_prepend(NULL, prop);
+ VCard_set_property(vc, "UID", uid);
+ }
return vc;
}
+VCard* VCard_new() {
+ return VCard_version_new(DEFAULT_VCARD_VERSION, TRUE);
+}
+
GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken) {
GSList* list = NULL;
VCard* vc = NULL;
gchar** chunks = make_chunks(text);
for (int i = 0; i < g_strv_length(chunks); i++) {
- //vc = g_new0(VCard, 1);
- //vc->version = version;
- vc = VCard_new(version);
+ vc = VCard_version_new(version, FALSE);
VCardParserResponse r = vcard_parse_text(chunks[i], &vc->version, &vc->vcard);
if (r != VCARD_PARSER_OK && skip_broken) {
GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken) {
GSList* list = NULL;
- //VCard* vc = g_new0(VCard, 1);
-
- //vc->version = version;
- VCard* vc = VCard_new(version);
- list = g_slist_prepend(list, vc);
+ gchar* buf = NULL;
+ size_t n = 0;
+ int buf_size = 500;
+ GString* text = g_string_new(NULL);
+
+ if (file) {
+ buf = g_new0(gchar, buf_size);
+ while ((n = fread(buf, sizeof(char), buf_size, file)) == buf_size) {
+ text = g_string_append_len(text, buf, buf_size);
+ g_free(buf);
+ buf = g_new0(gchar, buf_size);
+ }
+ if (n > 0)
+ text = g_string_append_len(text, buf, n);
+ g_free(buf);
+ buf = g_string_free_and_steal(text);
+ list = VCard_new_from_text(buf, version, skip_broken);
+ g_free(buf);
+ }
return list;
}
}
GSList* VCard_get_property(VCard* object, const gchar* property) {
- GSList *values = NULL, *tmp;
- GSList *iter;
+ GSList* values = NULL;
+ GSList* tmp = NULL;
+ GSList *iter = NULL;
VCardProperty* vp = NULL;
if (object && object->vcard && property) {
tmp = g_hash_table_lookup(object->vcard, property);
for (iter = tmp; iter; iter = g_slist_next(iter)) {
vp = (VCardProperty*) iter->data;
- values = g_slist_prepend(values, g_strdup_printf("%s:%s", vp->name, vp->value));
+ VCardProperty* prop = VCardProperty_new(vp->name, vp->value);
+ values = g_slist_prepend(values, prop);
}
}
return values;
}
+
+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;
+}
+