diff options
Diffstat (limited to 'addressbook/importers/evolution-vcard-importer.c')
-rw-r--r-- | addressbook/importers/evolution-vcard-importer.c | 345 |
1 files changed, 345 insertions, 0 deletions
diff --git a/addressbook/importers/evolution-vcard-importer.c b/addressbook/importers/evolution-vcard-importer.c index 080ef940f2..49e7bef70b 100644 --- a/addressbook/importers/evolution-vcard-importer.c +++ b/addressbook/importers/evolution-vcard-importer.c @@ -42,6 +42,8 @@ #include <libebook/e-destination.h> #include "e-util/e-import.h" +#include "e-util/e-datetime-format.h" +#include "misc/e-web-view-preview.h" #include "evolution-addressbook-importers.h" @@ -550,6 +552,61 @@ vcard_cancel(EImport *ei, EImportTarget *target, EImportImporter *im) gci->state = 1; } +static GtkWidget * +vcard_get_preview (EImport *ei, EImportTarget *target, EImportImporter *im) +{ + GtkWidget *preview; + GList *contacts; + gchar *contents; + VCardEncoding encoding; + EImportTargetURI *s = (EImportTargetURI *)target; + gchar *filename; + + 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; + } + + encoding = guess_vcard_encoding (filename); + if (encoding == VCARD_ENCODING_NONE) { + g_free (filename); + return NULL; + } + + if (!g_file_get_contents (filename, &contents, NULL, NULL)) { + g_message (G_STRLOC ": Couldn't read file."); + g_free (filename); + return NULL; + } + + g_free (filename); + + if (encoding == VCARD_ENCODING_UTF16) { + gchar *tmp; + + gunichar2 *contents_utf16 = (gunichar2 *) contents; + tmp = utf16_to_utf8 (contents_utf16); + g_free (contents); + contents = tmp; + } else if (encoding == VCARD_ENCODING_LOCALE) { + gchar *tmp; + tmp = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL); + g_free (contents); + contents = tmp; + } + + contacts = eab_contact_list_from_string (contents); + g_free (contents); + + preview = evolution_contact_importer_get_preview_widget (contacts); + + g_list_foreach (contacts, (GFunc) g_object_unref, NULL); + g_list_free (contacts); + + return preview; +} + static EImportImporter vcard_importer = { E_IMPORT_TARGET_URI, 0, @@ -557,6 +614,7 @@ static EImportImporter vcard_importer = { vcard_getwidget, vcard_import, vcard_cancel, + vcard_get_preview, }; EImportImporter * @@ -567,3 +625,290 @@ evolution_vcard_importer_peek(void) return &vcard_importer; } + +/* utility functions shared between all contact importers */ +static void +preview_contact (EWebViewPreview *preview, EContact *contact) +{ + gint idx; + gboolean had_value = FALSE; + + const gint fields[] = { + E_CONTACT_FILE_AS, + E_CONTACT_CATEGORIES, + + E_CONTACT_IS_LIST, + E_CONTACT_LIST_SHOW_ADDRESSES, + E_CONTACT_WANTS_HTML, + + E_CONTACT_FULL_NAME, + E_CONTACT_GIVEN_NAME, + E_CONTACT_FAMILY_NAME, + E_CONTACT_NICKNAME, + E_CONTACT_SPOUSE, + E_CONTACT_BIRTH_DATE, + E_CONTACT_ANNIVERSARY, + E_CONTACT_MAILER, + E_CONTACT_EMAIL, + + -1, + + E_CONTACT_ORG, + E_CONTACT_ORG_UNIT, + E_CONTACT_OFFICE, + E_CONTACT_TITLE, + E_CONTACT_ROLE, + E_CONTACT_MANAGER, + E_CONTACT_ASSISTANT, + + -1, + + E_CONTACT_PHONE_ASSISTANT, + E_CONTACT_PHONE_BUSINESS, + E_CONTACT_PHONE_BUSINESS_2, + E_CONTACT_PHONE_BUSINESS_FAX, + E_CONTACT_PHONE_CALLBACK, + E_CONTACT_PHONE_CAR, + E_CONTACT_PHONE_COMPANY, + E_CONTACT_PHONE_HOME, + E_CONTACT_PHONE_HOME_2, + E_CONTACT_PHONE_HOME_FAX, + E_CONTACT_PHONE_ISDN, + E_CONTACT_PHONE_MOBILE, + E_CONTACT_PHONE_OTHER, + E_CONTACT_PHONE_OTHER_FAX, + E_CONTACT_PHONE_PAGER, + E_CONTACT_PHONE_PRIMARY, + E_CONTACT_PHONE_RADIO, + E_CONTACT_PHONE_TELEX, + E_CONTACT_PHONE_TTYTDD, + + -1, + + E_CONTACT_ADDRESS_HOME, + E_CONTACT_ADDRESS_WORK, + E_CONTACT_ADDRESS_OTHER, + + -1, + + E_CONTACT_HOMEPAGE_URL, + E_CONTACT_BLOG_URL, + E_CONTACT_CALENDAR_URI, + E_CONTACT_FREEBUSY_URL, + E_CONTACT_ICS_CALENDAR, + E_CONTACT_VIDEO_URL, + + -1, + + E_CONTACT_IM_AIM, + E_CONTACT_IM_GROUPWISE, + E_CONTACT_IM_JABBER, + E_CONTACT_IM_YAHOO, + E_CONTACT_IM_MSN, + E_CONTACT_IM_ICQ, + E_CONTACT_IM_GADUGADU, + E_CONTACT_IM_SKYPE, + + -1, + + E_CONTACT_NOTE + }; + + g_return_if_fail (preview != NULL); + g_return_if_fail (contact != NULL); + + for (idx = 0; idx < G_N_ELEMENTS (fields); idx++) { + EContactField field; + + if (fields[idx] == -1) { + if (had_value) + e_web_view_preview_add_empty_line (preview); + had_value = FALSE; + continue; + } + + field = fields[idx]; + + if (field == E_CONTACT_BIRTH_DATE || field == E_CONTACT_ANNIVERSARY) { + EContactDate *dt = e_contact_get (contact, field); + if (dt) { + GDate gd = { 0 }; + struct tm tm; + gchar *value; + + g_date_set_dmy (&gd, dt->day, dt->month, dt->year); + g_date_to_struct_tm (&gd, &tm); + + value = e_datetime_format_format_tm ("addressbook", "table", DTFormatKindDate, &tm); + if (value) { + e_web_view_preview_add_section (preview, e_contact_pretty_name (field), value); + had_value = TRUE; + } + + g_free (value); + e_contact_date_free (dt); + } + } else if (field == E_CONTACT_IS_LIST || field == E_CONTACT_WANTS_HTML || field == E_CONTACT_LIST_SHOW_ADDRESSES) { + if (e_contact_get (contact, field)) { + e_web_view_preview_add_text (preview, e_contact_pretty_name (field)); + had_value = TRUE; + } + } else if (field == E_CONTACT_ADDRESS_HOME || field == E_CONTACT_ADDRESS_WORK || field == E_CONTACT_ADDRESS_OTHER) { + EContactAddress *addr = e_contact_get (contact, field); + if (addr) { + gboolean have = FALSE; + + #define add_it(_what) \ + if (addr->_what && *addr->_what) { \ + e_web_view_preview_add_section (preview, have ? NULL : e_contact_pretty_name (field), addr->_what); \ + have = TRUE; \ + had_value = TRUE; \ + } + + add_it (po); + add_it (ext); + add_it (street); + add_it (locality); + add_it (region); + add_it (code); + add_it (country); + + #undef add_it + + e_contact_address_free (addr); + } + } else if (field == E_CONTACT_IM_AIM || field == E_CONTACT_IM_GROUPWISE || + field == E_CONTACT_IM_JABBER || field == E_CONTACT_IM_YAHOO || + field == E_CONTACT_IM_MSN || field == E_CONTACT_IM_ICQ || + field == E_CONTACT_IM_GADUGADU || field == E_CONTACT_IM_SKYPE || + field == E_CONTACT_EMAIL) { + GList *attrs, *a; + gboolean have = FALSE; + + attrs = e_contact_get_attributes (contact, field); + for (a = attrs; a; a = a->next) { + EVCardAttribute *attr = a->data; + GList *value; + + if (!attr) + continue; + + for (value = e_vcard_attribute_get_values (attr); value; value = value->next) { + const gchar *str = value->data; + + if (str && *str) { + e_web_view_preview_add_section (preview, have ? NULL : e_contact_pretty_name (field), str); + have = TRUE; + had_value = TRUE; + } + } + e_vcard_attribute_free (attr); + } + + g_list_free (attrs); + } else if (field == E_CONTACT_CATEGORIES) { + gchar *value = e_contact_get (contact, field); + + if (value && *value) { + e_web_view_preview_add_section (preview, e_contact_pretty_name (field), value); + had_value = TRUE; + } + + g_free (value); + } else { + const gchar *value = e_contact_get_const (contact, field); + + if (value && *value) { + e_web_view_preview_add_section (preview, e_contact_pretty_name (field), value); + had_value = TRUE; + } + } + } +} + +static void +preview_selection_changed_cb (GtkTreeSelection *selection, EWebViewPreview *preview) +{ + GtkTreeIter iter; + GtkTreeModel *model = NULL; + + g_return_if_fail (selection != NULL); + g_return_if_fail (preview != NULL); + + e_web_view_preview_begin_update (preview); + + if (gtk_tree_selection_get_selected (selection, &model, &iter) && model) { + EContact *contact = NULL; + + gtk_tree_model_get (model, &iter, 1, &contact, -1); + + if (contact) { + preview_contact (preview, contact); + g_object_unref (contact); + } + } + + e_web_view_preview_end_update (preview); +} + +GtkWidget * +evolution_contact_importer_get_preview_widget (const GList *contacts) +{ + GtkWidget *preview; + GtkTreeView *tree_view; + GtkTreeSelection *selection; + GtkListStore *store; + GtkTreeIter iter; + const GList *c; + + if (!contacts) + return NULL; + + store = gtk_list_store_new (2, G_TYPE_STRING, E_TYPE_CONTACT); + + for (c = contacts; c; c = c->next) { + const gchar *description; + EContact *contact = (EContact *) c->data; + + if (!contact || !E_IS_CONTACT (contact)) + continue; + + description = e_contact_get_const (contact, E_CONTACT_FILE_AS); + if (!description) + description = e_contact_get_const (contact, E_CONTACT_UID); + + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + 0, description ? description : "", + 1, contact, + -1 ); + } + + if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) { + g_object_unref (store); + return NULL; + } + + preview = e_web_view_preview_new (); + gtk_widget_show (preview); + + tree_view = e_web_view_preview_get_tree_view (E_WEB_VIEW_PREVIEW (preview)); + g_return_val_if_fail (tree_view != NULL, NULL); + + gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL (store)); + g_object_unref (store); + + gtk_tree_view_insert_column_with_attributes (tree_view, -1, _("Contact"), + gtk_cell_renderer_text_new (), "text", 0, NULL); + + if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) > 1) + e_web_view_preview_show_tree_view (E_WEB_VIEW_PREVIEW (preview)); + + selection = gtk_tree_view_get_selection (tree_view); + gtk_tree_selection_select_iter (selection, &iter); + g_signal_connect (selection, "changed", G_CALLBACK (preview_selection_changed_cb), preview); + + preview_selection_changed_cb (selection, E_WEB_VIEW_PREVIEW (preview)); + + return preview; +} |