* src/maemo/modest-msg-edit-window.c:
[modest] / src / maemo / modest-msg-edit-window.c
index 79370b4..3b15b43 100644 (file)
@@ -138,6 +138,7 @@ static void modest_msg_edit_window_show_toolbar   (ModestWindow *window,
 static void modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
                                                           GdkEvent *event,
                                                           ModestMsgEditWindow *window);
+static void modest_msg_edit_window_clipboard_owner_handle_change_in_idle (ModestMsgEditWindow *window);
 static void subject_field_move_cursor (GtkEntry *entry,
                                       GtkMovementStep step,
                                       gint a1,
@@ -277,7 +278,7 @@ struct _ModestMsgEditWindowPrivate {
        guint        scroll_drag_timeout_id;
        gdouble      last_upper;
 
-       gint last_cid;
+       gint next_cid;
        TnyList *attachments;
        TnyList *images;
        guint64 images_size;
@@ -289,6 +290,7 @@ struct _ModestMsgEditWindowPrivate {
        gulong      clipboard_change_handler_id;
        gulong      default_clipboard_change_handler_id;
        gulong      account_removed_handler_id;
+       guint       clipboard_owner_idle;
        gchar       *clipboard_text;
 
        TnyMsg      *draft_msg;
@@ -417,7 +419,7 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->images        = TNY_LIST (tny_simple_list_new ());
        priv->images_size   = 0;
        priv->images_count  = 0;
-       priv->last_cid      = 0;
+       priv->next_cid      = 0;
 
        priv->cc_caption    = NULL;
        priv->bcc_caption    = NULL;
@@ -437,6 +439,7 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->clipboard_change_handler_id = 0;
        priv->default_clipboard_change_handler_id = 0;
        priv->account_removed_handler_id = 0;
+       priv->clipboard_owner_idle = 0;
        priv->clipboard_text = NULL;
        priv->sent = FALSE;
 
@@ -983,6 +986,10 @@ modest_msg_edit_window_finalize (GObject *obj)
                g_source_remove (priv->scroll_drag_timeout_id);
                priv->scroll_drag_timeout_id = 0;
        }
+       if (priv->clipboard_owner_idle > 0) {
+               g_source_remove (priv->clipboard_owner_idle);
+               priv->clipboard_owner_idle = 0;
+       }
        g_free (priv->msg_uid);
        g_free (priv->last_search);
        g_slist_free (priv->font_items_group);
@@ -1140,7 +1147,7 @@ get_related_images (ModestMsgEditWindow *self, TnyMsg *msg)
 }
 
 static void
-update_last_cid (ModestMsgEditWindow *self, TnyList *attachments)
+update_next_cid (ModestMsgEditWindow *self, TnyList *attachments)
 {
        TnyIterator *iter;
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
@@ -1153,8 +1160,8 @@ update_last_cid (ModestMsgEditWindow *self, TnyList *attachments)
                if (cid != NULL) {
                        char *invalid = NULL;
                        gint int_cid = strtol (cid, &invalid, 10);
-                       if ((invalid != NULL) && (*invalid == '\0') && (int_cid > priv->last_cid)) {
-                               priv->last_cid = int_cid;
+                       if ((invalid != NULL) && (*invalid == '\0') && (int_cid >= priv->next_cid)) {
+                               priv->next_cid = int_cid + 1;
                        }
                }
                g_object_unref (part);
@@ -1236,8 +1243,8 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
                gtk_widget_show_all (priv->attachments_caption);
        }
        get_related_images (self, msg);
-       update_last_cid (self, priv->attachments);
-       update_last_cid (self, priv->images);
+       update_next_cid (self, priv->attachments);
+       update_next_cid (self, priv->images);
        replace_with_images (self, priv->images);
 
        if (preserve_is_rich && !is_html) {
@@ -1563,8 +1570,6 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean pre
        g_object_unref (menu_rules_group);
        g_object_unref (toolbar_rules_group);
        g_object_unref (clipboard_rules_group);
-       modest_msg_edit_window_clipboard_owner_change (NULL, NULL, MODEST_MSG_EDIT_WINDOW (obj));
-
        set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg, preserve_is_rich);
 
        text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (obj));
@@ -1584,7 +1589,9 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean pre
                                  "account_removed",
                                  G_CALLBACK(on_account_removed),
                                  obj);
-       
+
+       modest_msg_edit_window_clipboard_owner_handle_change_in_idle (MODEST_MSG_EDIT_WINDOW (obj));
+
        return (ModestWindow*) obj;
 }
 
@@ -2211,10 +2218,10 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        TnyStream *stream = create_stream_for_uri (uri);
                        tny_mime_part_construct (mime_part, stream, mime_type, "base64");
                        
-                       content_id = g_strdup_printf ("%d", priv->last_cid);
+                       content_id = g_strdup_printf ("%d", priv->next_cid);
                        tny_mime_part_set_content_id (mime_part, content_id);
                        g_free (content_id);
-                       priv->last_cid++;
+                       priv->next_cid++;
                        
                        basename = g_path_get_basename (filename);
                        tny_mime_part_set_filename (mime_part, basename);
@@ -2321,10 +2328,10 @@ modest_msg_edit_window_attach_file_one (
 
                g_object_unref (stream);
                
-               content_id = g_strdup_printf ("%d", priv->last_cid);
+               content_id = g_strdup_printf ("%d", priv->next_cid);
                tny_mime_part_set_content_id (mime_part, content_id);
                g_free (content_id);
-               priv->last_cid++;
+               priv->next_cid++;
                
                basename = g_path_get_basename (filename);
                tny_mime_part_set_filename (mime_part, basename);
@@ -2750,6 +2757,7 @@ modest_msg_edit_window_set_file_format (ModestMsgEditWindow *window,
                        break;
                }
                modest_ui_actions_check_toolbar_dimming_rules (MODEST_WINDOW (window));
+               text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), window);
        }
 }
 
@@ -3137,6 +3145,7 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        if (!GTK_WIDGET_VISIBLE (window))
                return;
 
+       g_object_ref (window);
        text = gtk_clipboard_wait_for_text (selection_clipboard);
 
        if (priv->clipboard_text != NULL) {
@@ -3144,8 +3153,37 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        }
        priv->clipboard_text = text;
 
-       modest_window_check_dimming_rules_group (MODEST_WINDOW (window), MODEST_DIMMING_RULES_CLIPBOARD);
+       if (GTK_WIDGET_VISIBLE (window)) {
+               modest_window_check_dimming_rules_group (MODEST_WINDOW (window), MODEST_DIMMING_RULES_CLIPBOARD);
+       }
+       g_object_unref (window);
 }
+
+static gboolean clipboard_owner_change_idle (gpointer userdata)
+{
+       ModestMsgEditWindow *window = (ModestMsgEditWindow *) userdata;
+       ModestMsgEditWindowPrivate *priv;
+
+       gdk_threads_enter ();
+       g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window), FALSE);
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+
+       priv->clipboard_owner_idle = 0;
+       modest_msg_edit_window_clipboard_owner_change (NULL, NULL, window);
+       gdk_threads_leave ();
+
+       return FALSE;
+}
+
+static void
+modest_msg_edit_window_clipboard_owner_handle_change_in_idle (ModestMsgEditWindow *window)
+{
+       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);
+       }
+}
+
 static void 
 subject_field_move_cursor (GtkEntry *entry,
                           GtkMovementStep step,