static GtkWidget * ldif_get_preview (EImport *ei, EImportTarget *target, EImportImporter *im) { GtkWidget *preview; GSList *contacts = NULL; EContact *contact; EImportTargetURI *s = (EImportTargetURI *) target; gchar *filename; GHashTable *dn_contact_hash; FILE *file; filename = g_filename_from_uri (s->uri_src, NULL, NULL); if (filename == NULL) { g_message (G_STRLOC ": Couldn't get filename from URI '%s'", s->uri_src); return NULL; } file = g_fopen(filename, "r"); g_free (filename); if (file == NULL) { g_message (G_STRLOC ": Can't open .ldif file"); return NULL; } dn_contact_hash = g_hash_table_new_full ( g_str_hash, g_str_equal, (GDestroyNotify) g_free, (GDestroyNotify) NULL); while (contact = getNextLDIFEntry (dn_contact_hash, file), contact != NULL) { if (!e_contact_get (contact, E_CONTACT_IS_LIST)) { add_to_notes (contact, E_CONTACT_OFFICE); add_to_notes (contact, E_CONTACT_SPOUSE); add_to_notes (contact, E_CONTACT_BLOG_URL); } contacts = g_slist_prepend (contacts, contact); } g_hash_table_destroy (dn_contact_hash); contacts = g_slist_reverse (contacts); preview = evolution_contact_importer_get_preview_widget (contacts); e_client_util_free_object_slist (contacts); fclose (file); return preview; }
static gboolean parseLine (CSVImporter *gci, EContact *contact, gchar *buf) { const gchar *pptr = buf, *field_text; gchar *do_free = NULL; GString *value; gint ii = 0, idx; gint flags = 0; gint contact_field; EContactAddress *home_address = NULL, *work_address = NULL, *other_address = NULL; EContactDate *bday = NULL; GString *home_street, *work_street, *other_street; home_street = g_string_new (""); work_street = g_string_new (""); other_street = g_string_new (""); home_address = e_contact_address_new (); work_address = e_contact_address_new (); other_address = e_contact_address_new (); bday = e_contact_date_new (); if (!g_utf8_validate (pptr, -1, NULL)) { do_free = g_convert (pptr, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL); pptr = do_free; } while (value = parseNextValue (&pptr), value != NULL) { contact_field = NOMAP; flags = FLAG_INVALID; field_text = NULL; idx = ii; if (gci->fields_map) { gpointer found; found = g_hash_table_lookup ( gci->fields_map, GINT_TO_POINTER (idx)); if (found == NULL) { g_warning ("%s: No map for index %d, skipping it", G_STRFUNC, idx); idx = -1; } else { idx = GPOINTER_TO_INT (found) - 1; } } if (importer == OUTLOOK_IMPORTER) { if (idx >= 0 && idx < G_N_ELEMENTS (csv_fields_outlook)) { contact_field = csv_fields_outlook[idx].contact_field; flags = csv_fields_outlook[idx].flags; field_text = csv_fields_outlook[idx].csv_attribute; } } else if (importer == MOZILLA_IMPORTER) { if (idx >= 0 && idx < G_N_ELEMENTS (csv_fields_mozilla)) { contact_field = csv_fields_mozilla[idx].contact_field; flags = csv_fields_mozilla[idx].flags; field_text = csv_fields_mozilla[idx].csv_attribute; } } else { if (idx >= 0 && idx < G_N_ELEMENTS (csv_fields_evolution)) { contact_field = csv_fields_evolution[idx].contact_field; flags = csv_fields_evolution[idx].flags; field_text = csv_fields_evolution[idx].csv_attribute; } } if (*value->str) { if (contact_field != NOMAP) { if (importer == OUTLOOK_IMPORTER || importer == MOZILLA_IMPORTER) { e_contact_set (contact, contact_field, value->str); } else { if (contact_field == E_CONTACT_WANTS_HTML) e_contact_set ( contact, contact_field, GINT_TO_POINTER ( g_ascii_strcasecmp ( value->str, "TRUE") == 0)); else e_contact_set (contact, contact_field, value->str); } } else { switch (flags) { case FLAG_HOME_ADDRESS | FLAG_STREET: if (strlen (home_street->str) != 0) { home_street = g_string_append (home_street, ",\n"); } home_street = g_string_append (home_street, value->str); break; case FLAG_HOME_ADDRESS | FLAG_CITY: home_address->locality = g_strdup (value->str); break; case FLAG_HOME_ADDRESS | FLAG_STATE: home_address->region = g_strdup (value->str); break; case FLAG_HOME_ADDRESS | FLAG_POSTAL_CODE: home_address->code = g_strdup (value->str); break; case FLAG_HOME_ADDRESS | FLAG_POBOX: home_address->po = g_strdup (value->str); break; case FLAG_HOME_ADDRESS | FLAG_COUNTRY: home_address->country = g_strdup (value->str); break; case FLAG_WORK_ADDRESS | FLAG_STREET: if (strlen (work_street->str) != 0) { work_street = g_string_append (work_street, ",\n"); } work_street = g_string_append (work_street, value->str); break; case FLAG_WORK_ADDRESS | FLAG_CITY: work_address->locality = g_strdup (value->str); break; case FLAG_WORK_ADDRESS | FLAG_STATE: work_address->region = g_strdup (value->str); break; case FLAG_WORK_ADDRESS | FLAG_POSTAL_CODE: work_address->code = g_strdup (value->str); break; case FLAG_WORK_ADDRESS | FLAG_POBOX: work_address->po = g_strdup (value->str); break; case FLAG_WORK_ADDRESS | FLAG_COUNTRY: work_address->country = g_strdup (value->str); break; case FLAG_OTHER_ADDRESS | FLAG_STREET: if (strlen (other_street->str) != 0) { other_street = g_string_append (other_street, ",\n"); } other_street = g_string_append (other_street, value->str); break; case FLAG_OTHER_ADDRESS | FLAG_CITY: other_address->locality = g_strdup (value->str); break; case FLAG_OTHER_ADDRESS | FLAG_STATE: other_address->region = g_strdup (value->str); break; case FLAG_OTHER_ADDRESS | FLAG_POSTAL_CODE: other_address->code = g_strdup (value->str); break; case FLAG_OTHER_ADDRESS | FLAG_POBOX: other_address->po = g_strdup (value->str); break; case FLAG_OTHER_ADDRESS | FLAG_COUNTRY: other_address->country = g_strdup (value->str); break; case FLAG_DATE_BDAY: e_contact_set ( contact, E_CONTACT_BIRTH_DATE, date_from_string (value->str)); break; case FLAG_DATE_ANNIVERSARY: e_contact_set ( contact, E_CONTACT_ANNIVERSARY, date_from_string (value->str)); break; case FLAG_BIRTH_DAY: bday->day = atoi (value->str); break; case FLAG_BIRTH_YEAR: bday->year = atoi (value->str); break; case FLAG_BIRTH_MONTH: bday->month = atoi (value->str); break; case FLAG_INVALID: break; default: add_to_notes (contact, field_text, value->str); } } } ii++; g_string_free (value, TRUE); } if (strlen (home_street->str) != 0) home_address->street = g_strdup (home_street->str); if (strlen (work_street->str) != 0) work_address->street = g_strdup (work_street->str); if (strlen (other_street->str) != 0) other_address->street = g_strdup (other_street->str); g_string_free (home_street, TRUE); g_string_free (work_street, TRUE); g_string_free (other_street, TRUE); if (home_address->locality || home_address->country || home_address->code || home_address->region || home_address->street) e_contact_set (contact, E_CONTACT_ADDRESS_HOME, home_address); if (work_address->locality || work_address->country || work_address->code || work_address->region || work_address->street) e_contact_set (contact, E_CONTACT_ADDRESS_WORK, work_address); if (other_address->locality || other_address->country || other_address->code || other_address->region || other_address->street) e_contact_set (contact, E_CONTACT_ADDRESS_OTHER, other_address); if (importer != OUTLOOK_IMPORTER) { if (bday->day || bday->year || bday->month) e_contact_set (contact, E_CONTACT_BIRTH_DATE, bday); } e_contact_address_free (home_address); e_contact_address_free (work_address); e_contact_address_free (other_address); e_contact_date_free (bday); g_free (do_free); return TRUE; }
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; } }