static gboolean csv_import_contacts (gpointer d) { CSVImporter *gci = d; EContact *contact = NULL; while ((contact = getNextCSVEntry (gci, gci->file))) { gchar *uid = NULL; e_book_client_add_contact_sync ( gci->book_client, contact, &uid, NULL, NULL); if (uid != NULL) { e_contact_set (contact, E_CONTACT_UID, uid); g_free (uid); } gci->contacts = g_slist_prepend (gci->contacts, contact); } if (contact == NULL) { gci->state = 1; } if (gci->state == 1) { csv_import_done (gci); return FALSE; } else { e_import_status ( gci->import, gci->target, _("Importing..."), ftell (gci->file) * 100 / gci->size); return TRUE; } }
gboolean add_contact_from_test_case_verify (EBookClient *book_client, const gchar *case_name, EContact **contact) { gchar *vcard; EContact *contact_orig; EContact *contact_final; gchar *uid; GError *error = NULL; vcard = new_vcard_from_test_case (case_name); contact_orig = e_contact_new_from_vcard (vcard); g_free (vcard); if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error)) g_error ("add contact sync: %s", error->message); e_contact_set (contact_orig, E_CONTACT_UID, uid); if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) g_error ("get contact sync: %s", error->message); /* verify the contact was added "successfully" (not thorough) */ g_assert (contacts_are_equal_shallow (contact_orig, contact_final)); if (contact) *contact = contact_final; else g_object_unref (contact_final); g_object_unref (contact_orig); g_free (uid); return TRUE; }
gboolean add_contact_verify (EBookClient *book_client, EContact *contact) { EContact *contact_final; gchar *uid; GError *error = NULL; if (!e_book_client_add_contact_sync (book_client, contact, &uid, NULL, &error)) g_error ("add contact sync: %s", error->message); e_contact_set (contact, E_CONTACT_UID, uid); if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error)) g_error ("get contact sync: %s", error->message); /* verify the contact was added "successfully" (not thorough) */ g_assert (contacts_are_equal_shallow (contact, contact_final)); g_object_unref (contact_final); g_free (uid); return TRUE; }
static void import_contact (EBookClient *book_client, gchar *line) { gchar **strings, *addr, **addrs; gint i; GList *list; /*EContactName *name;*/ EContact *card; gsize len; GError *error = NULL; card = e_contact_new (); strings = g_strsplit (line, "\t", 5); if (strings[0] && strings[1] && strings[2]) { gchar *new_uid = NULL; e_contact_set (card, E_CONTACT_NICKNAME, strings[0]); e_contact_set (card, E_CONTACT_FULL_NAME, strings[1]); addr = strings[2]; len = strlen (addr); if (addr[0] == '(' && addr[len - 1] == ')') { addr[0] = 0; addr[len - 1] = 0; addrs = g_strsplit (addr + 1, ",", 0); list = NULL; /* XXX So ... this api is just insane ... we set * plain strings as the contact email if it * is a normal contact, but need to do this * XML crap for mailing lists. */ for (i = 0; addrs[i]; i++) { EDestination *d; EVCardAttribute *attr; d = e_destination_new (); e_destination_set_email (d, addrs[i]); attr = e_vcard_attribute_new (NULL, EVC_EMAIL); e_destination_export_to_vcard_attribute (d, attr); list = g_list_append (list, attr); g_object_unref (d); } e_contact_set_attributes (card, E_CONTACT_EMAIL, list); g_list_foreach (list, (GFunc) e_vcard_attribute_free, NULL); g_list_free (list); g_strfreev (addrs); e_contact_set (card, E_CONTACT_IS_LIST, GINT_TO_POINTER (TRUE)); } else { e_contact_set (card, E_CONTACT_EMAIL_1, strings[2]); } /*name = e_contact_name_from_string(strings[1]);*/ if (strings[3] && strings[4]) e_contact_set (card, E_CONTACT_NOTE, strings[4]); e_book_client_add_contact_sync ( book_client, card, &new_uid, NULL, &error); if (error != NULL) { g_warning ( "%s: Failed to add contact: %s", G_STRFUNC, error->message); g_error_free (error); } else { g_free (new_uid); } g_object_unref (card); } g_strfreev (strings); }
static void vcard_import_contact (VCardImporter *gci, EContact *contact) { EContactPhoto *photo; GList *attrs, *attr; gchar *uid = NULL; /* Apple's addressbook.app exports PHOTO's without a TYPE * param, so let's figure out the format here if there's a * PHOTO attribute missing a TYPE param. * * this is sort of a hack, as EContact sets the type for us if * we use the setter. so let's e_contact_get + e_contact_set * on E_CONTACT_PHOTO. */ photo = e_contact_get (contact, E_CONTACT_PHOTO); if (photo) { e_contact_set (contact, E_CONTACT_PHOTO, photo); e_contact_photo_free (photo); } /* Deal with our XML EDestination stuff in EMAIL attributes, if there is any. */ attrs = e_contact_get_attributes (contact, E_CONTACT_EMAIL); for (attr = attrs; attr; attr = attr->next) { EVCardAttribute *a = attr->data; GList *v = e_vcard_attribute_get_values (a); if (v && v->data) { if (!strncmp ((gchar *)v->data, "<?xml", 5)) { EDestination *dest = e_destination_import ((gchar *) v->data); e_destination_export_to_vcard_attribute (dest, a); g_object_unref (dest); } } } e_contact_set_attributes (contact, E_CONTACT_EMAIL, attrs); /* Deal with TEL attributes that don't conform to what we need. * * 1. if there's no location (HOME/WORK/OTHER), default to OTHER. * 2. if there's *only* a location specified, default to VOICE. */ attrs = e_vcard_get_attributes (E_VCARD (contact)); for (attr = attrs; attr; attr = attr->next) { EVCardAttribute *a = attr->data; gboolean location_only = TRUE; gboolean no_location = TRUE; gboolean is_work_home = FALSE; GList *params, *param; if (g_ascii_strcasecmp (e_vcard_attribute_get_name (a), EVC_TEL)) continue; params = e_vcard_attribute_get_params (a); for (param = params; param; param = param->next) { EVCardAttributeParam *p = param->data; GList *vs, *v; if (g_ascii_strcasecmp (e_vcard_attribute_param_get_name (p), EVC_TYPE)) continue; vs = e_vcard_attribute_param_get_values (p); for (v = vs; v; v = v->next) { is_work_home = is_work_home || !g_ascii_strcasecmp ((gchar *)v->data, "WORK") || !g_ascii_strcasecmp ((gchar *)v->data, "HOME"); if (!g_ascii_strcasecmp ((gchar *)v->data, "WORK") || !g_ascii_strcasecmp ((gchar *)v->data, "HOME") || !g_ascii_strcasecmp ((gchar *)v->data, "OTHER")) no_location = FALSE; else location_only = FALSE; } } if (is_work_home) { /* only WORK and HOME phone numbers require locations, * the rest should be kept as is */ if (location_only) { /* add VOICE */ e_vcard_attribute_add_param_with_value (a, e_vcard_attribute_param_new (EVC_TYPE), "VOICE"); } if (no_location) { /* add OTHER */ e_vcard_attribute_add_param_with_value (a, e_vcard_attribute_param_new (EVC_TYPE), "OTHER"); } } } /* Deal with ADR and EMAIL attributes that don't conform to what * we need. If HOME or WORK isn't specified, add TYPE=OTHER. */ attrs = e_vcard_get_attributes (E_VCARD (contact)); for (attr = attrs; attr; attr = attr->next) { EVCardAttribute *a = attr->data; gboolean no_location = TRUE; GList *params, *param; if (g_ascii_strcasecmp (e_vcard_attribute_get_name (a), EVC_ADR) && g_ascii_strcasecmp (e_vcard_attribute_get_name (a), EVC_EMAIL)) continue; params = e_vcard_attribute_get_params (a); for (param = params; param; param = param->next) { EVCardAttributeParam *p = param->data; GList *vs, *v; if (g_ascii_strcasecmp (e_vcard_attribute_param_get_name (p), EVC_TYPE)) continue; vs = e_vcard_attribute_param_get_values (p); for (v = vs; v; v = v->next) { if (!g_ascii_strcasecmp ((gchar *)v->data, "WORK") || !g_ascii_strcasecmp ((gchar *)v->data, "HOME")) no_location = FALSE; } } if (no_location) { /* add OTHER */ e_vcard_attribute_add_param_with_value (a, e_vcard_attribute_param_new (EVC_TYPE), "OTHER"); } } /* Work around the fact that these fields no longer show up in the UI */ add_to_notes (contact, E_CONTACT_OFFICE); add_to_notes (contact, E_CONTACT_SPOUSE); add_to_notes (contact, E_CONTACT_BLOG_URL); /* FIXME Error checking */ if (e_book_client_add_contact_sync (gci->book_client, contact, &uid, NULL, NULL) && uid) { e_contact_set (contact, E_CONTACT_UID, uid); g_free (uid); } }
static gboolean ldif_import_contacts (gpointer d) { LDIFImporter *gci = d; EContact *contact; GSList *iter; gint count = 0; /* We process all normal cards immediately and keep the list * ones till the end */ if (gci->state == 0) { while (count < 50 && (contact = getNextLDIFEntry ( gci->dn_contact_hash, gci->file))) { if (e_contact_get (contact, E_CONTACT_IS_LIST)) { gci->list_contacts = g_slist_prepend ( gci->list_contacts, contact); } else { gchar *uid = NULL; add_to_notes (contact, E_CONTACT_OFFICE); add_to_notes (contact, E_CONTACT_SPOUSE); add_to_notes (contact, E_CONTACT_BLOG_URL); if (e_book_client_add_contact_sync (gci->book_client, contact, &uid, NULL, NULL) && uid) { e_contact_set (contact, E_CONTACT_UID, uid); g_free (uid); } gci->contacts = g_slist_prepend (gci->contacts, contact); } count++; } if (contact == NULL) { gci->state = 1; gci->list_iterator = gci->list_contacts; } } if (gci->state == 1) { for (iter = gci->list_iterator; count < 50 && iter; iter = iter->next) { gchar *uid = NULL; contact = iter->data; resolve_list_card (gci, contact); if (e_book_client_add_contact_sync (gci->book_client, contact, &uid, NULL, NULL) && uid) { e_contact_set (contact, E_CONTACT_UID, uid); g_free (uid); } count++; } gci->list_iterator = iter; if (iter == NULL) gci->state = 2; } if (gci->state == 2) { ldif_import_done (gci); return FALSE; } else { e_import_status ( gci->import, gci->target, _("Importing..."), ftell (gci->file) * 100 / gci->size); return TRUE; } }