[evolution/wip/webkit-composer] Bug 726513 - [webkit-composer] Image properties dialog's Source not set properly



commit fc8c3ce936e6ebbb82bb7e125ea375826ba06097
Author: Tomas Popela <tpopela redhat com>
Date:   Mon Mar 17 14:41:12 2014 +0100

    Bug 726513 - [webkit-composer] Image properties dialog's Source not set properly
    
    When inserting new image save its uri as well and set is as uri to file
    chooser in Image properties dialog. Also disable that file chooser
    dialog for images that don't have a source uri (i.e. cid image from
    other message). Also allow the user to change the actual source of the
    image through the file chooser.

 e-util/e-editor-image-dialog.c |   31 +++++++++++---
 e-util/e-editor-selection.c    |   90 +++++++++++++++++++++++++++++++++++++---
 e-util/e-editor-selection.h    |    4 ++
 e-util/e-editor-widget.c       |    1 +
 4 files changed, 114 insertions(+), 12 deletions(-)
---
diff --git a/e-util/e-editor-image-dialog.c b/e-util/e-editor-image-dialog.c
index 43847b5..4a9c810 100644
--- a/e-util/e-editor-image-dialog.c
+++ b/e-util/e-editor-image-dialog.c
@@ -62,8 +62,17 @@ G_DEFINE_TYPE (
 static void
 editor_image_dialog_set_src (EEditorImageDialog *dialog)
 {
-       webkit_dom_html_image_element_set_src (
-               dialog->priv->image,
+       EEditor *editor;
+       EEditorSelection *editor_selection;
+       EEditorWidget *editor_widget;
+
+       editor = e_editor_dialog_get_editor (E_EDITOR_DIALOG (dialog));
+       editor_widget = e_editor_get_editor_widget (editor);
+       editor_selection = e_editor_widget_get_selection (editor_widget);
+
+       e_editor_selection_replace_image_src (
+               editor_selection,
+               WEBKIT_DOM_ELEMENT (dialog->priv->image),
                gtk_file_chooser_get_uri (
                        GTK_FILE_CHOOSER (dialog->priv->file_chooser)));
 }
@@ -361,10 +370,20 @@ editor_image_dialog_show (GtkWidget *widget)
                return;
        }
 
-       tmp = webkit_dom_html_image_element_get_src (dialog->priv->image);
-       gtk_file_chooser_set_uri (
-               GTK_FILE_CHOOSER (dialog->priv->file_chooser), tmp);
-       g_free (tmp);
+       tmp = webkit_dom_element_get_attribute (
+               WEBKIT_DOM_ELEMENT (dialog->priv->image), "data-uri");
+       if (tmp && *tmp) {
+               gtk_file_chooser_set_uri (
+                       GTK_FILE_CHOOSER (dialog->priv->file_chooser), tmp);
+               gtk_widget_set_sensitive (
+                       GTK_WIDGET (dialog->priv->file_chooser), TRUE);
+               g_free (tmp);
+       } else {
+               gtk_file_chooser_set_uri (
+                       GTK_FILE_CHOOSER (dialog->priv->file_chooser), "");
+               gtk_widget_set_sensitive (
+                       GTK_WIDGET (dialog->priv->file_chooser), FALSE);
+       }
 
        tmp = webkit_dom_html_image_element_get_alt (dialog->priv->image);
        gtk_entry_set_text (GTK_ENTRY (dialog->priv->description_edit), tmp ? tmp : "");
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 6096181..bd1be6c 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -3424,6 +3424,7 @@ typedef struct _LoadContext LoadContext;
 
 struct _LoadContext {
        EEditorSelection *selection;
+       WebKitDOMElement *element;
        GInputStream *input_stream;
        GOutputStream *output_stream;
        GFile *file;
@@ -3471,9 +3472,37 @@ image_load_context_free (LoadContext *load_context)
 }
 
 static void
+replace_base64_image_src (EEditorSelection *selection,
+                         WebKitDOMElement *element,
+                         const gchar *base64_content,
+                         const gchar *filename,
+                         const gchar *uri)
+{
+       EEditorWidget *editor_widget;
+
+       editor_widget = e_editor_selection_ref_editor_widget (selection);
+       g_return_if_fail (editor_widget != NULL);
+
+       e_editor_widget_set_changed (editor_widget, TRUE);
+       g_object_unref (editor_widget);
+
+       webkit_dom_html_image_element_set_src (
+               WEBKIT_DOM_HTML_IMAGE_ELEMENT (element),
+               base64_content);
+       webkit_dom_element_set_attribute (
+               WEBKIT_DOM_ELEMENT (element), "data-uri", uri, NULL);
+       webkit_dom_element_set_attribute (
+               WEBKIT_DOM_ELEMENT (element), "data-inline", "", NULL);
+       webkit_dom_element_set_attribute (
+               WEBKIT_DOM_ELEMENT (element), "data-name",
+               filename ? filename : "", NULL);
+}
+
+static void
 insert_base64_image (EEditorSelection *selection,
                      const gchar *base64_content,
-                    const gchar *filename)
+                     const gchar *filename,
+                     const gchar *uri)
 {
        EEditorWidget *editor_widget;
        WebKitDOMDocument *document;
@@ -3501,6 +3530,8 @@ insert_base64_image (EEditorSelection *selection,
                WEBKIT_DOM_HTML_IMAGE_ELEMENT (element),
                base64_content);
        webkit_dom_element_set_attribute (
+               WEBKIT_DOM_ELEMENT (element), "data-uri", uri, NULL);
+       webkit_dom_element_set_attribute (
                WEBKIT_DOM_ELEMENT (element), "data-inline", "", NULL);
        webkit_dom_element_set_attribute (
                WEBKIT_DOM_ELEMENT (element), "data-name",
@@ -3537,7 +3568,7 @@ image_load_finish (LoadContext *load_context)
 {
        EEditorSelection *selection;
        GMemoryOutputStream *output_stream;
-       gchar *base64_encoded, *mime_type, *output;
+       gchar *base64_encoded, *mime_type, *output, *uri;
        gsize size;
        gpointer data;
 
@@ -3549,14 +3580,20 @@ image_load_finish (LoadContext *load_context)
 
        data = g_memory_output_stream_get_data (output_stream);
        size = g_memory_output_stream_get_data_size (output_stream);
+       uri = g_file_get_uri (load_context->file);
 
        base64_encoded = g_base64_encode ((const guchar *) data, size);
        output = g_strconcat ("data:", mime_type, ";base64,", base64_encoded, NULL);
-       insert_base64_image (selection, output, load_context->filename);
+       if (load_context->element)
+               replace_base64_image_src (
+                       selection, load_context->element, output, load_context->filename, uri);
+       else
+               insert_base64_image (selection, output, load_context->filename, uri);
 
        g_free (base64_encoded);
        g_free (output);
        g_free (mime_type);
+       g_free (uri);
 
        image_load_context_free (load_context);
 }
@@ -3697,6 +3734,7 @@ image_load_query_info_cb (GFile *file,
 
 static void
 image_load_and_insert_async (EEditorSelection *selection,
+                             WebKitDOMElement *element,
                              const gchar *uri)
 {
        LoadContext *load_context;
@@ -3709,6 +3747,7 @@ image_load_and_insert_async (EEditorSelection *selection,
 
        load_context = image_load_context_new (selection);
        load_context->file = file;
+       load_context->element = element;
 
        g_file_query_info_async (
                file, "standard::*",
@@ -3735,7 +3774,7 @@ e_editor_selection_insert_image (EEditorSelection *selection,
        if (is_in_html_mode (selection)) {
                if (strstr (image_uri, ";base64,")) {
                        if (g_str_has_prefix (image_uri, "data:"))
-                               insert_base64_image (selection, image_uri, "");
+                               insert_base64_image (selection, image_uri, "", "");
                        if (strstr (image_uri, ";data")) {
                                const gchar *base64_data = strstr (image_uri, ";") + 1;
                                gchar *filename;
@@ -3746,15 +3785,54 @@ e_editor_selection_insert_image (EEditorSelection *selection,
                                        g_utf8_strlen (base64_data, -1) - 1;
                                filename = g_strndup (image_uri, filename_length);
 
-                               insert_base64_image (selection, base64_data, filename);
+                               insert_base64_image (selection, base64_data, filename, "");
                                g_free (filename);
                        }
                } else
-                       image_load_and_insert_async (selection, image_uri);
+                       image_load_and_insert_async (selection, NULL, image_uri);
        }
 }
 
 /**
+ * e_editor_selection_replace_image_src:
+ * @selection: an #EEditorSelection
+ * @image: #WebKitDOMElement representation of image
+ * @image_uri: an URI of the source image
+ *
+ * Replace the src attribute of the given @image with @image_uri.
+ */
+void
+e_editor_selection_replace_image_src (EEditorSelection *selection,
+                                      WebKitDOMElement *image,
+                                      const gchar *image_uri)
+{
+       g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
+       g_return_if_fail (image_uri != NULL);
+       g_return_if_fail (WEBKIT_DOM_IS_HTML_IMAGE_ELEMENT (image));
+
+       if (strstr (image_uri, ";base64,")) {
+               if (g_str_has_prefix (image_uri, "data:"))
+                       replace_base64_image_src (
+                               selection, image, image_uri, "", "");
+               if (strstr (image_uri, ";data")) {
+                       const gchar *base64_data = strstr (image_uri, ";") + 1;
+                       gchar *filename;
+                       glong filename_length;
+
+                       filename_length =
+                               g_utf8_strlen (image_uri, -1) -
+                               g_utf8_strlen (base64_data, -1) - 1;
+                       filename = g_strndup (image_uri, filename_length);
+
+                       replace_base64_image_src (
+                               selection, image, base64_data, filename, "");
+                       g_free (filename);
+               }
+       } else
+               image_load_and_insert_async (selection, image, image_uri);
+}
+
+/**
  * e_editor_selection_clear_caret_position_marker:
  * @selection: an #EEditorSelection
  *
diff --git a/e-util/e-editor-selection.h b/e-util/e-editor-selection.h
index 147d917..845bcfc 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -157,6 +157,10 @@ void               e_editor_selection_replace      (EEditorSelection *selection,
                                                 const gchar *new_string);
 void           e_editor_selection_insert_html  (EEditorSelection *selection,
                                                 const gchar *html_text);
+void           e_editor_selection_replace_image_src
+                                               (EEditorSelection *selection,
+                                                WebKitDOMElement *image,
+                                                const gchar *image_uri);
 void           e_editor_selection_insert_image (EEditorSelection *selection,
                                                 const gchar *image_uri);
 void           e_editor_selection_insert_text  (EEditorSelection *selection,
diff --git a/e-util/e-editor-widget.c b/e-util/e-editor-widget.c
index ea31ee7..15404ef 100644
--- a/e-util/e-editor-widget.c
+++ b/e-util/e-editor-widget.c
@@ -4003,6 +4003,7 @@ remove_evolution_attributes (WebKitDOMElement *element)
        webkit_dom_element_remove_attribute (element, "data-converted");
        webkit_dom_element_remove_attribute (element, "data-edit-as-new");
        webkit_dom_element_remove_attribute (element, "data-inline");
+       webkit_dom_element_remove_attribute (element, "data-uri");
        webkit_dom_element_remove_attribute (element, "data-message");
        webkit_dom_element_remove_attribute (element, "data-name");
        webkit_dom_element_remove_attribute (element, "data-new-message");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]