diff options
author | Milan Crha <mcrha@redhat.com> | 2010-05-19 04:47:29 +0800 |
---|---|---|
committer | Milan Crha <mcrha@redhat.com> | 2010-05-19 04:47:29 +0800 |
commit | ef7690c3845e3c1cebcf3caba7f7667a10e7123d (patch) | |
tree | ab4a5bfa25bc5e25e0fed851c25226ce1930ec52 /mail/importers/evolution-mbox-importer.c | |
parent | 0f92771be57383e5d6e7d0f3e05743d192066eb3 (diff) | |
download | gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar.gz gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar.bz2 gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar.lz gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar.xz gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.tar.zst gsoc2013-evolution-ef7690c3845e3c1cebcf3caba7f7667a10e7123d.zip |
Bug #499320 - Preview before import from command line
Diffstat (limited to 'mail/importers/evolution-mbox-importer.c')
-rw-r--r-- | mail/importers/evolution-mbox-importer.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/mail/importers/evolution-mbox-importer.c b/mail/importers/evolution-mbox-importer.c index 4fa4bf96f0..dc419dc89e 100644 --- a/mail/importers/evolution-mbox-importer.c +++ b/mail/importers/evolution-mbox-importer.c @@ -33,6 +33,7 @@ #include <stdio.h> #include <ctype.h> #include <string.h> +#include <errno.h> #include <gtk/gtk.h> #include <glib/gi18n.h> @@ -53,6 +54,7 @@ #include "mail-importer.h" #include "e-util/e-import.h" +#include "misc/e-web-view-preview.h" typedef struct { EImport *import; @@ -247,6 +249,153 @@ mbox_cancel(EImport *ei, EImportTarget *target, EImportImporter *im) camel_operation_cancel(importer->cancel); } +static MboxImporterCreatePreviewFunc create_preview_func = NULL; +static MboxImporterFillPreviewFunc fill_preview_func = NULL; + +void +mbox_importer_set_preview_funcs (MboxImporterCreatePreviewFunc create_func, MboxImporterFillPreviewFunc fill_func) +{ + create_preview_func = create_func; + fill_preview_func = fill_func; +} + +static void +preview_selection_changed_cb (GtkTreeSelection *selection, EWebViewPreview *preview) +{ + GtkTreeIter iter; + GtkTreeModel *model = NULL; + gboolean found = FALSE; + + g_return_if_fail (selection != NULL); + g_return_if_fail (preview != NULL); + g_return_if_fail (fill_preview_func != NULL); + + if (gtk_tree_selection_get_selected (selection, &model, &iter) && model) { + CamelMimeMessage *msg = NULL; + + gtk_tree_model_get (model, &iter, 2, &msg, -1); + + if (msg) { + found = TRUE; + fill_preview_func (G_OBJECT (preview), msg); + g_object_unref (msg); + } + } + + if (!found) { + e_web_view_preview_begin_update (preview); + e_web_view_preview_end_update (preview); + } +} + +static GtkWidget * +mbox_get_preview (EImport *ei, EImportTarget *target, EImportImporter *im) +{ + GtkWidget *preview = NULL; + EImportTargetURI *s = (EImportTargetURI *)target; + gchar *filename; + gint fd; + CamelMimeParser *mp; + GtkListStore *store = NULL; + GtkTreeIter iter; + GtkWidget *preview_widget = NULL; + + if (!create_preview_func || !fill_preview_func) + return NULL; + + filename = g_filename_from_uri (s->uri_src, NULL, NULL); + if (!filename) { + g_message (G_STRLOC ": Couldn't get filename from URI '%s'", s->uri_src); + return NULL; + } + + fd = g_open (filename, O_RDONLY|O_BINARY, 0); + if (fd == -1) { + g_warning ("Cannot find source file to import '%s': %s", filename, g_strerror (errno)); + g_free (filename); + return NULL; + } + + g_free (filename); + + mp = camel_mime_parser_new(); + camel_mime_parser_scan_from (mp, TRUE); + if (camel_mime_parser_init_with_fd (mp, fd) == -1) { + g_object_unref (mp); + return NULL; + } + + while (camel_mime_parser_step (mp, NULL, NULL) == CAMEL_MIME_PARSER_STATE_FROM) { + CamelMimeMessage *msg; + gchar *from; + + msg = camel_mime_message_new(); + if (camel_mime_part_construct_from_parser ((CamelMimePart *)msg, mp) == -1) { + g_object_unref (msg); + break; + } + + if (!store) + store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, CAMEL_TYPE_MIME_MESSAGE); + + from = NULL; + if (camel_mime_message_get_from (msg)) + from = camel_address_encode (CAMEL_ADDRESS (camel_mime_message_get_from (msg))); + + gtk_list_store_append (store, &iter); + gtk_list_store_set (store, &iter, + 0, camel_mime_message_get_subject (msg) ? camel_mime_message_get_subject (msg) : "", + 1, from ? from : "", + 2, msg, + -1); + + g_object_unref (msg); + g_free (from); + + camel_mime_parser_step (mp, NULL, NULL); + } + + if (store) { + GtkTreeView *tree_view; + GtkTreeSelection *selection; + + 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); + + /* Translators: Column header for a message subject */ + gtk_tree_view_insert_column_with_attributes (tree_view, -1, C_("mboxImp", "Subject"), + gtk_cell_renderer_text_new (), "text", 0, NULL); + + /* Translators: Column header for a message From address */ + gtk_tree_view_insert_column_with_attributes (tree_view, -1, C_("mboxImp", "From"), + gtk_cell_renderer_text_new (), "text", 1, 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)); + + create_preview_func (G_OBJECT (preview), &preview_widget); + g_return_val_if_fail (preview_widget != NULL, NULL); + + e_web_view_preview_set_preview (E_WEB_VIEW_PREVIEW (preview), preview_widget); + gtk_widget_show (preview_widget); + + selection = gtk_tree_view_get_selection (tree_view); + g_return_val_if_fail (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter), NULL); + 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; +} + static EImportImporter mbox_importer = { E_IMPORT_TARGET_URI, 0, @@ -254,6 +403,7 @@ static EImportImporter mbox_importer = { mbox_getwidget, mbox_import, mbox_cancel, + mbox_get_preview, }; EImportImporter * |