Fixes NB#106346, added a missing fix
[modest] / src / hildon2 / modest-msg-edit-window.c
index 6ccbc82..6e5aed5 100644 (file)
@@ -117,8 +117,6 @@ static void  subject_field_insert_text (GtkEditable *editable,
                                        ModestMsgEditWindow *window);
 static void  modest_msg_edit_window_color_button_change (ModestMsgEditWindow *window,
                                                         gpointer userdata);
-static void  modest_msg_edit_window_font_change (GtkCheckMenuItem *menu_item,
-                                                gpointer userdata);
 static void  modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window);
 
 static void modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window,
@@ -176,6 +174,8 @@ static void from_field_changed (HildonPickerButton *button,
                                ModestMsgEditWindow *self);
 static void font_size_clicked (GtkToolButton *button,
                               ModestMsgEditWindow *window);
+static void font_face_clicked (GtkToolButton *button,
+                              ModestMsgEditWindow *window);
 static void DEBUG_BUFFER (WPTextBuffer *buffer)
 {
 #ifdef DEBUG
@@ -263,6 +263,8 @@ struct _ModestMsgEditWindowPrivate {
        GtkWidget   *font_color_button;
        GtkWidget   *font_color_toolitem;
        GSList      *font_items_group;
+       GtkTreeModel *faces_model;
+       gint         current_face_index;
        GtkWidget   *font_tool_button_label;
        GtkTreeModel *sizes_model;
        gint         current_size_index;
@@ -350,19 +352,10 @@ static void
 restore_settings (ModestMsgEditWindow *self)
 {
        ModestConf *conf = NULL;
-       GtkAction *action;
-       ModestWindowPrivate *parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
 
        conf = modest_runtime_get_conf ();
 
        /* Dim at start clipboard actions */
-       action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/CutMenu");
-       gtk_action_set_sensitive (action, FALSE);
-       action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/CopyMenu");
-       gtk_action_set_sensitive (action, FALSE);
-       action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/AttachmentsMenu/RemoveAttachmentsMenu");
-       gtk_action_set_sensitive (action, FALSE);
-
        modest_widget_memory_restore (conf, G_OBJECT(self), MODEST_CONF_EDIT_WINDOW_KEY);
 }
 
@@ -492,6 +485,10 @@ scroll_drag_timeout (gpointer userdata)
        ModestMsgEditWindow *win = (ModestMsgEditWindow *) userdata;
        ModestMsgEditWindowPrivate *priv;
 
+       /* It could happen that the window was already closed */
+       if (!GTK_WIDGET_VISIBLE (win))
+               return FALSE;
+
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(win);
 
        correct_scroll_without_drag_check (win, TRUE);
@@ -512,6 +509,10 @@ correct_scroll_without_drag_check_idle (gpointer userdata)
        GtkTextMark *insert;
        GtkAdjustment *vadj;
 
+       /* It could happen that the window was already closed */
+       if (!GTK_WIDGET_VISIBLE (w))
+               return FALSE;
+
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(w);
 
        insert = gtk_text_buffer_get_insert (priv->text_buffer);
@@ -545,8 +546,10 @@ correct_scroll_without_drag_check (ModestMsgEditWindow *w, gboolean only_if_focu
                return;
        }
 
-       priv->correct_scroll_idle = g_idle_add ((GSourceFunc) correct_scroll_without_drag_check_idle,
-                                               (gpointer) w);
+       priv->correct_scroll_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+                                                    (GSourceFunc) correct_scroll_without_drag_check_idle,
+                                                    g_object_ref (w),
+                                                    g_object_unref);
 }
 
 static void
@@ -557,8 +560,11 @@ correct_scroll (ModestMsgEditWindow *w)
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE(w);
        if (gtk_grab_get_current () == priv->msg_body) {
                if (priv->scroll_drag_timeout_id == 0) {
-                       priv->scroll_drag_timeout_id = g_timeout_add (500, (GSourceFunc) scroll_drag_timeout,
-                                                                     (gpointer) w);
+                       priv->scroll_drag_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
+                                                                          500,
+                                                                          (GSourceFunc) scroll_drag_timeout,
+                                                                          g_object_ref (w),
+                                                                          g_object_unref);
                }
                return;
        }
@@ -968,7 +974,7 @@ modest_msg_edit_window_finalize (GObject *obj)
                g_free (priv->original_account_name);
        g_free (priv->msg_uid);
        g_free (priv->last_search);
-       g_slist_free (priv->font_items_group);
+       g_object_unref (priv->faces_model);
        g_object_unref (priv->sizes_model);
        g_object_unref (priv->attachments);
        g_object_unref (priv->images);
@@ -979,13 +985,38 @@ modest_msg_edit_window_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
+static void
+pixbuf_size_prepared (GdkPixbufLoader *loader,
+                     gint width,
+                     gint height,
+                     ModestMsgEditWindow *self)
+{
+       gint new_height, new_width;
+       gboolean set_size;
+       
+       new_height = height;
+       new_width = width;
+       set_size = FALSE;
+
+       if (width > IMAGE_MAX_WIDTH) {
+               new_height = height * IMAGE_MAX_WIDTH / width;
+               new_width = IMAGE_MAX_WIDTH;
+       }
+
+       gdk_pixbuf_loader_set_size (loader, new_width, new_height);
+}
+
 static GdkPixbuf *
-pixbuf_from_stream (TnyStream *stream, const gchar *mime_type, guint64 *stream_size)
+pixbuf_from_stream (TnyStream *stream,
+                   const gchar *mime_type,
+                   guint64 *stream_size,
+                   ModestMsgEditWindow *self)
 {
        GdkPixbufLoader *loader;
        GdkPixbuf *pixbuf;
        guint64 size;
-       
+       GError *error = NULL;
+
        size = 0;
 
        loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL);
@@ -995,25 +1026,32 @@ pixbuf_from_stream (TnyStream *stream, const gchar *mime_type, guint64 *stream_s
                        *stream_size = 0;
                return NULL;
        }
+       g_signal_connect (G_OBJECT (loader), "size-prepared", G_CALLBACK (pixbuf_size_prepared), self);
+
+       hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
 
        tny_stream_reset (TNY_STREAM (stream));
        while (!tny_stream_is_eos (TNY_STREAM (stream))) {
-               GError *error = NULL;
                unsigned char read_buffer[128];
                gint readed;
                readed = tny_stream_read (TNY_STREAM (stream), (char *) read_buffer, 128);
                size += readed;
                if (!gdk_pixbuf_loader_write (loader, read_buffer, readed, &error)) {
-                       if (error)
-                               g_error_free (error);
                        break;
                }
+               /* Allow some UI responsiveness */
+               while (gtk_events_pending ())
+                       gtk_main_iteration ();
        }
+       hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), FALSE);
+
+       gdk_pixbuf_loader_close (loader, &error);
 
+       if (error)
+               g_error_free (error);
        pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
        if (pixbuf) 
                g_object_ref (pixbuf);
-       gdk_pixbuf_loader_close (loader, NULL);
        g_object_unref (loader);
 
        if (!pixbuf)
@@ -1043,6 +1081,7 @@ replace_with_images (ModestMsgEditWindow *self, TnyList *attachments)
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
 
+       g_object_ref (self);
        for (iter = tny_list_create_iterator (attachments);
             !tny_iterator_is_done (iter);
             tny_iterator_next (iter)) {
@@ -1052,7 +1091,7 @@ replace_with_images (ModestMsgEditWindow *self, TnyList *attachments)
                if ((cid != NULL)&&(mime_type != NULL)) {
                        guint64 stream_size;
                        TnyStream *stream = tny_mime_part_get_decoded_stream (part);
-                       GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size);
+                       GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size, self);
 
 
                        g_object_unref (stream);
@@ -1067,6 +1106,7 @@ replace_with_images (ModestMsgEditWindow *self, TnyList *attachments)
                g_object_unref (part);
        }
        g_object_unref (iter);
+       g_object_unref (self);
 }
 
 static void
@@ -1077,7 +1117,7 @@ get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
 
        content_type = tny_mime_part_get_content_type (TNY_MIME_PART (msg));
-       
+
        if (content_type && !g_strcasecmp (content_type, "multipart/related")) {
                parent = g_object_ref (msg);
        } else if (content_type && !g_strcasecmp (content_type, "multipart/mixed")) {
@@ -1292,45 +1332,6 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
        g_free (bcc);
 }
 
-static void
-menu_tool_button_clicked_popup (GtkMenuToolButton *item,
-                               gpointer data)
-{
-       GList *item_children, *node;
-       GtkWidget *bin_child;
-
-       bin_child = gtk_bin_get_child (GTK_BIN(item));
-
-       item_children = gtk_container_get_children (GTK_CONTAINER (bin_child));
-       
-       for (node = item_children; node != NULL; node = g_list_next (node)) {
-               if (GTK_IS_TOGGLE_BUTTON (node->data)) {
-                       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (node->data), TRUE);
-               }
-       }
-       g_list_free (item_children);
-}
-
-static void
-menu_tool_button_dont_expand (GtkMenuToolButton *item)
-{
-       GtkWidget *box;
-       GList *item_children, *node;
-
-       box = gtk_bin_get_child (GTK_BIN (item));
-       gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
-       item_children = gtk_container_get_children (GTK_CONTAINER (box));
-       
-       for (node = item_children; node != NULL; node = g_list_next (node)) {
-               gtk_box_set_child_packing (GTK_BOX (box), GTK_WIDGET (node->data), TRUE, TRUE, 0, GTK_PACK_START);
-               if (GTK_IS_TOGGLE_BUTTON (node->data))
-                       gtk_button_set_alignment (GTK_BUTTON (node->data), 0.0, 0.5);
-               else if (GTK_IS_BUTTON (node->data))
-                       gtk_button_set_alignment (GTK_BUTTON (node->data), 1.0, 0.5);
-       }
-       g_list_free (item_children);
-}
-
 
 static void
 modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
@@ -1345,11 +1346,7 @@ modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
        gint font_index;
        GtkWidget *sizes_menu;
        GtkWidget *fonts_menu;
-       GSList *radio_group = NULL;
-       GSList *node = NULL;
        gchar *markup;
-       gchar ldots[8];
-       gint ldots_len;
 
        /* Toolbar */
        parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
@@ -1382,15 +1379,9 @@ modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
        /* font_size */
        tool_item = GTK_WIDGET (gtk_tool_button_new (NULL, NULL));
        priv->size_tool_button_label = gtk_label_new (NULL);
-       hildon_helper_set_logical_color (GTK_WIDGET (priv->size_tool_button_label), GTK_RC_TEXT,
-                                        GTK_STATE_INSENSITIVE, "SecondaryTextColor");
-       hildon_helper_set_logical_color (GTK_WIDGET (priv->size_tool_button_label), GTK_RC_FG,
-                                        GTK_STATE_INSENSITIVE, "SecondaryTextColor");
        snprintf(size_text, sizeof(size_text), "%d", wp_font_size[DEFAULT_FONT_SIZE]);
-       ldots_len = g_unichar_to_utf8 (0x2026, ldots);
-       ldots[ldots_len] = '\0';
        markup = g_strconcat ("<span font_family='", DEFAULT_SIZE_BUTTON_FONT_FAMILY, "'>",
-                             size_text, ldots, "</span>", NULL);
+                             size_text, "</span>", NULL);
        gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
        g_free (markup);
        hildon_helper_set_logical_font (priv->size_tool_button_label, "LargeSystemFont");
@@ -1414,57 +1405,36 @@ modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
 
        g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (font_size_clicked), window);
        gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
-       gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), FALSE);
-       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), FALSE);
-       menu_tool_button_dont_expand (GTK_MENU_TOOL_BUTTON (tool_item));
+       gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
+       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
        priv->font_size_toolitem = tool_item;
 
        /* font face */
-       tool_item = GTK_WIDGET (gtk_menu_tool_button_new (NULL, NULL));
+       tool_item = GTK_WIDGET (gtk_tool_button_new (NULL, NULL));
        priv->font_tool_button_label = gtk_label_new (NULL);
-       hildon_helper_set_logical_color (GTK_WIDGET (priv->font_tool_button_label), GTK_RC_TEXT,
-                                        GTK_STATE_INSENSITIVE, "SecondaryTextColor");
-       hildon_helper_set_logical_color (GTK_WIDGET (priv->font_tool_button_label), GTK_RC_FG,
-                                        GTK_STATE_INSENSITIVE, "SecondaryTextColor");
        markup = g_strconcat ("<span font_family='", wp_get_font_name(DEFAULT_FONT), "'>Tt</span>", NULL);
        gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
        g_free(markup);
        hildon_helper_set_logical_font (priv->font_tool_button_label, "LargeSystemFont");
        gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (tool_item), priv->font_tool_button_label);
        fonts_menu = gtk_menu_new ();
-       priv->font_items_group = NULL;
-       radio_group = NULL;
+       priv->faces_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
        for (font_index = 0; font_index < wp_get_font_count (); font_index++) {
-               GtkWidget *font_menu_item;
-               GtkWidget *child_label;
-
-               font_menu_item = gtk_radio_menu_item_new_with_label (radio_group, "");
-               child_label = gtk_bin_get_child (GTK_BIN (font_menu_item));
-               markup = g_strconcat ("<span font_family='", wp_get_font_name (font_index),"'>", 
-                                     wp_get_font_name (font_index), "</span>", NULL);
-               gtk_label_set_markup (GTK_LABEL (child_label), markup);
-               g_free (markup);
-               
-               radio_group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (font_menu_item));
-               gtk_menu_shell_append (GTK_MENU_SHELL (fonts_menu), font_menu_item);
-               gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (font_menu_item), (font_index == DEFAULT_FONT));
-               gtk_widget_show (font_menu_item);
+               GtkTreeIter iter;
 
-               priv->font_items_group = g_slist_prepend (priv->font_items_group, font_menu_item);
-                       
-       }
-       for (node = radio_group; node != NULL; node = g_slist_next (node)) {
-               GtkWidget *item = (GtkWidget *) node->data;
-               g_signal_connect (G_OBJECT (item), "toggled", G_CALLBACK (modest_msg_edit_window_font_change),
-                                 window);
+               gtk_list_store_append (GTK_LIST_STORE (priv->faces_model), &iter);
+
+               gtk_list_store_set (GTK_LIST_STORE (priv->faces_model), &iter, 
+                                   0, wp_get_font_name (font_index),
+                                   -1);
+
+               if (font_index == DEFAULT_FONT)
+                       priv->current_face_index = font_index;
        }
-       priv->font_items_group = g_slist_reverse (priv->font_items_group);
-       gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (tool_item), fonts_menu);
-       g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (menu_tool_button_clicked_popup), NULL);
+       g_signal_connect (G_OBJECT (tool_item), "clicked", G_CALLBACK (font_face_clicked), window);
        gtk_toolbar_insert (GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM (tool_item), insert_index);
-       gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), FALSE);
-       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), FALSE);
-       menu_tool_button_dont_expand (GTK_MENU_TOOL_BUTTON (tool_item));
+       gtk_tool_item_set_expand (GTK_TOOL_ITEM (tool_item), TRUE);
+       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (tool_item), TRUE);
        priv->font_face_toolitem = tool_item;
 
        /* Set expand and homogeneous for remaining items */
@@ -1814,9 +1784,11 @@ modest_msg_edit_window_get_format_state (ModestMsgEditWindow *self)
 {
        ModestMsgEditFormatState *format_state = NULL;
        ModestMsgEditWindowPrivate *priv;
-       WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
+       WPTextBufferFormat *buffer_format;
 
        g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self), NULL);
+
+       buffer_format = g_new0 (WPTextBufferFormat, 1);
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
 
        wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, TRUE);
@@ -1840,11 +1812,15 @@ modest_msg_edit_window_set_format_state (ModestMsgEditWindow *self,
                                         const ModestMsgEditFormatState *format_state)
 {
        ModestMsgEditWindowPrivate *priv;
-       WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
-       WPTextBufferFormat *current_format = g_new0 (WPTextBufferFormat, 1);
+       WPTextBufferFormat *buffer_format;
+       WPTextBufferFormat *current_format;
+
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
        g_return_if_fail (format_state != NULL);
 
+       buffer_format = g_new0 (WPTextBufferFormat, 1);
+       current_format = g_new0 (WPTextBufferFormat, 1);
+
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
        gtk_widget_grab_focus (priv->msg_body);
        buffer_format->bold = (format_state->bold != FALSE);
@@ -1929,7 +1905,6 @@ text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *windo
        GtkAction *action;
        ModestWindowPrivate *parent_priv;
        ModestMsgEditWindowPrivate *priv;
-       GtkWidget *new_font_menuitem;
        
        parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
@@ -1990,16 +1965,12 @@ text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *windo
                if (gtk_tree_model_get_iter (priv->sizes_model, &iter, path)) {
                        gchar *size_text;
                        gchar *markup;
-                       gchar ldots[8];
-                       gint ldots_len;
 
                        priv->current_size_index = buffer_format->font_size;
 
                        gtk_tree_model_get (priv->sizes_model, &iter, 0, &size_text, -1);
-                       ldots_len = g_unichar_to_utf8 (0x2026, ldots);
-                       ldots[ldots_len] = '\0';
                        markup = g_strconcat ("<span font_family='Sans'>", 
-                                             size_text, ldots, "</span>", NULL);
+                                             size_text, "</span>", NULL);
                        
                        gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
                        g_free (markup);
@@ -2008,23 +1979,23 @@ text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *windo
                gtk_tree_path_free (path);              
        }
 
-       new_font_menuitem = GTK_WIDGET ((g_slist_nth (priv->font_items_group, 
-                                                     buffer_format->font))->data);
-       if (!gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (new_font_menuitem))) {
-               GtkWidget *label;
-               gchar *markup;
+       if (priv->current_face_index != buffer_format->font) {
+               GtkTreeIter iter;
+               GtkTreePath *path;
+
+               path = gtk_tree_path_new_from_indices (buffer_format->font, -1);
+               if (gtk_tree_model_get_iter (priv->faces_model, &iter, path)) {
+                       gchar *face_name;
+                       gchar *markup;
+
+                       priv->current_face_index = buffer_format->font;
+                       gtk_tree_model_get (priv->faces_model, &iter, 0, &face_name, -1);
+                       markup = g_strconcat ("<span font_family='", face_name, "'>Tt</span>", NULL);
+                       gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
+                       g_free (face_name);
+                       g_free (markup);
+               }
 
-               label = gtk_bin_get_child (GTK_BIN (new_font_menuitem));
-               markup = g_strconcat ("<span font_family='", gtk_label_get_text (GTK_LABEL (label)),"'>Tt</span>", NULL);
-               gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
-               g_free (markup);
-               g_signal_handlers_block_by_func (G_OBJECT (new_font_menuitem),
-                                                G_CALLBACK (modest_msg_edit_window_font_change),
-                                                window);
-               gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (new_font_menuitem), TRUE);
-               g_signal_handlers_unblock_by_func (G_OBJECT (new_font_menuitem),
-                                                  G_CALLBACK (modest_msg_edit_window_font_change),
-                                                  window);
        }
 
        g_free (buffer_format);
@@ -2178,9 +2149,9 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
        gint response = 0;
        GSList *uris = NULL;
        GSList *uri_node = NULL;
-       
+
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
-       
+
        dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_OPEN);
        gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ia_select_inline_image_title"));
        gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE);
@@ -2200,6 +2171,11 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
        }
        gtk_widget_destroy (dialog);
 
+       g_object_ref (window);
+       /* The operation could take some time so allow the dialog to be closed */
+       while (gtk_events_pending ())
+               gtk_main_iteration ();
+
        for (uri_node = uris; uri_node != NULL; uri_node = g_slist_next (uri_node)) {
                const gchar *uri;
                GnomeVFSHandle *handle = NULL;
@@ -2242,7 +2218,7 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
 
                                modest_platform_information_banner (NULL, NULL, 
                                                                    _FM("sfil_ib_opening_not_allowed"));
-
+                               g_free (filename);
                                g_object_unref (mime_part);
                                gnome_vfs_file_info_unref (info);
                                continue;
@@ -2259,27 +2235,31 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        tny_mime_part_set_filename (mime_part, basename);
                        g_free (basename);
 
-                       pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size);
+                       pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size, window);
 
                        if (pixbuf != NULL) {
                                priv->images_size += stream_size;
                                priv->images_count ++;
                                insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
                                gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &position, insert_mark);
-                               wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, g_strdup (tny_mime_part_get_content_id (mime_part)), pixbuf);
+                               wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, 
+                                                            tny_mime_part_get_content_id (mime_part), pixbuf);
                                g_object_unref (pixbuf);
-                       } 
 
-                       tny_list_prepend (priv->images, (GObject *) mime_part);
-                       gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
+                               tny_list_prepend (priv->images, (GObject *) mime_part);
+                               gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
+                       } else {
+                               modest_platform_information_banner (NULL, NULL,
+                                                                   _("mail_ib_file_operation_failed"));
+                       }
+
                        g_free (filename);
                        g_object_unref (mime_part);
                        gnome_vfs_file_info_unref (info);
 
                }
        }
-
-
+       g_object_unref (window);
 }
 
 static void
@@ -2413,6 +2393,7 @@ modest_msg_edit_window_attach_file_one (ModestMsgEditWindow *window,
                        if (size > allowed_size) {
                                modest_platform_information_banner (NULL, NULL, 
                                                                    _FM("sfil_ib_opening_not_allowed"));
+                               g_free (filename);
                                return 0;
                        }
                } else
@@ -2425,6 +2406,7 @@ modest_msg_edit_window_attach_file_one (ModestMsgEditWindow *window,
                        modest_platform_information_banner (NULL, NULL, _FM("sfil_ib_opening_not_allowed"));
 
                        g_object_unref (mime_part);
+                       g_free (filename);
                        gnome_vfs_file_info_unref (info);
                        return 0;
                }
@@ -2590,8 +2572,6 @@ font_size_clicked (GtkToolButton *button,
                gchar *size_text;
                gchar *markup;
                WPTextBufferFormat format;
-               gchar ldots[8];
-               gint ldots_len;
 
                new_index = hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (selector), 0);
 
@@ -2610,10 +2590,8 @@ font_size_clicked (GtkToolButton *button,
                
                text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
                size_text = hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector));
-               ldots_len = g_unichar_to_utf8 (0x2026, ldots);
-               ldots[ldots_len] = '\0';
                markup = g_strconcat ("<span font_family='", DEFAULT_SIZE_BUTTON_FONT_FAMILY, "'>", 
-                                     size_text, ldots, "</span>", NULL);
+                                     size_text, "</span>", NULL);
                g_free (size_text);
                gtk_label_set_markup (GTK_LABEL (priv->size_tool_button_label), markup);
                g_free (markup);
@@ -2626,34 +2604,55 @@ font_size_clicked (GtkToolButton *button,
 }
 
 static void
-modest_msg_edit_window_font_change (GtkCheckMenuItem *menu_item,
-                                   gpointer userdata)
+font_face_clicked (GtkToolButton *button,
+                  ModestMsgEditWindow *window)
 {
        ModestMsgEditWindowPrivate *priv;
-       gint new_font_index;
-       ModestMsgEditWindow *window;
-       GtkWidget *label;
+       GtkWidget *selector, *dialog;
+       GtkCellRenderer *renderer;
        
-       window = MODEST_MSG_EDIT_WINDOW (userdata);
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
-       gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
 
-       if (gtk_check_menu_item_get_active (menu_item)) {
-               gchar *markup;
+       selector = hildon_touch_selector_new ();
+       renderer = gtk_cell_renderer_text_new ();
+       hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), priv->faces_model, 
+                                            renderer, "family", 0, "text", 0, NULL);
+       hildon_touch_selector_set_active (HILDON_TOUCH_SELECTOR (selector), 0, priv->current_face_index);
 
-               label = gtk_bin_get_child (GTK_BIN (menu_item));
-               
-               new_font_index = wp_get_font_index (gtk_label_get_text (GTK_LABEL (label)), DEFAULT_FONT);
+       dialog = hildon_picker_dialog_new (GTK_WINDOW (window));
+       hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
+               gint new_font_index;
+               GtkTreePath *path;
+               GtkTreeIter iter;
+
+               new_font_index = hildon_touch_selector_get_active (HILDON_TOUCH_SELECTOR (selector), 0);
+               path = gtk_tree_path_new_from_indices (new_font_index, -1);
+               if (gtk_tree_model_get_iter (priv->faces_model, &iter, path)) {
+                       gchar *face_name;
+                       gchar *markup;
+
+                       gtk_tree_model_get (priv->faces_model, &iter, 0, &face_name, -1);
+
+                       if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT, 
+                                                          GINT_TO_POINTER(new_font_index)))
+                               wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
+
+                       markup = g_strconcat ("<span font_family='", face_name, "'>Tt</span>", NULL);
+                       gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
+
+                       text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
+                       g_free (face_name);
+                       g_free (markup);
+               }
+               gtk_tree_path_free (path);
 
-               if (!wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FONT, 
-                                                  GINT_TO_POINTER(new_font_index)))
-                       wp_text_view_reset_and_show_im (WP_TEXT_VIEW (priv->msg_body));
-               
-               text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (window));
-                   markup = g_strconcat ("<span font_family='",gtk_label_get_text (GTK_LABEL (label)),"'>Tt</span>", NULL);
-               gtk_label_set_markup (GTK_LABEL (priv->font_tool_button_label), markup);
-               g_free (markup);
        }
+       gtk_widget_destroy (dialog);
+
+       gtk_widget_grab_focus (GTK_WIDGET (priv->msg_body));
+
 }
 
 void
@@ -3250,6 +3249,8 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        GtkClipboard *selection_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
        gchar *text = NULL;
+
+       /* It could happen that the window was already closed */
        if (!GTK_WIDGET_VISIBLE (window))
                return;
 
@@ -3261,9 +3262,8 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        }
        priv->clipboard_text = text;
 
-       if (GTK_WIDGET_VISIBLE (window)) {
-               modest_window_check_dimming_rules_group (MODEST_WINDOW (window), MODEST_DIMMING_RULES_CLIPBOARD);
-       }
+       modest_window_check_dimming_rules_group (MODEST_WINDOW (window), MODEST_DIMMING_RULES_CLIPBOARD);
+
        g_object_unref (window);
 }
 
@@ -3288,7 +3288,10 @@ modest_msg_edit_window_clipboard_owner_handle_change_in_idle (ModestMsgEditWindo
 {
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        if (priv->clipboard_owner_idle == 0) {
-               priv->clipboard_owner_idle = g_idle_add (clipboard_owner_change_idle, window);
+               priv->clipboard_owner_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+                                                             clipboard_owner_change_idle, 
+                                                             g_object_ref (window),
+                                                             g_object_unref);
        }
 }
 
@@ -3299,6 +3302,7 @@ subject_field_move_cursor (GtkEntry *entry,
                           gboolean a2,
                           gpointer window)
 {
+       /* It could happen that the window was already closed */
        if (!GTK_WIDGET_VISIBLE (window))
                return;