Work to improve drafts handling, mainly from the UI pov. This, with
[modest] / src / maemo / modest-msg-edit-window.c
index 176dd2b..1a1c242 100644 (file)
@@ -55,6 +55,7 @@
 #include "modest-mail-operation.h"
 #include "modest-tny-platform-factory.h"
 #include "modest-tny-msg.h"
+#include "modest-tny-folder.h"
 #include "modest-address-book.h"
 #include "modest-text-utils.h"
 #include <tny-simple-list.h>
@@ -436,7 +437,9 @@ init_window (ModestMsgEditWindow *obj)
        priv->text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
        g_object_set (priv->text_buffer, "font_scale", 1.0, NULL);
        wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
-/*     gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
+       gtk_text_buffer_register_serialize_tagset(GTK_TEXT_BUFFER(priv->text_buffer), "wp-text-buffer");
+       gtk_text_buffer_register_deserialize_tagset(GTK_TEXT_BUFFER(priv->text_buffer), "wp-text-buffer");
+
        wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
 
        priv->find_toolbar = hildon_find_toolbar_new (NULL);
@@ -543,6 +546,11 @@ modest_msg_edit_window_finalize (GObject *obj)
        }
        
        if (priv->draft_msg != NULL) {
+               TnyHeader *header = tny_msg_get_header (priv->draft_msg);
+               if (TNY_IS_HEADER (header)) {
+                       ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
+                       modest_window_mgr_unregister_header (mgr, header);
+               }
                g_object_unref (priv->draft_msg);
                priv->draft_msg = NULL;
        }
@@ -588,6 +596,7 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
        ModestMsgEditWindowPrivate *priv;
        GtkTextIter iter;
        TnyHeaderFlags priority_flags;
+       TnyFolder *msg_folder;
        
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
        g_return_if_fail (TNY_IS_MSG (msg));
@@ -626,7 +635,6 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
 
        update_window_title (self);
 
-/*     gtk_text_buffer_set_can_paste_rich_text (priv->text_buffer, TRUE); */
        wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
        body = modest_tny_msg_get_body (msg, TRUE);
 
@@ -676,7 +684,14 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
        text_buffer_can_undo (priv->text_buffer, FALSE, self);
        text_buffer_can_redo (priv->text_buffer, FALSE, self);
 
-       priv->draft_msg = g_object_ref(msg);
+       /* we should set a reference to the incoming message if it is a draft */
+       msg_folder = tny_msg_get_folder (msg);
+       if (msg_folder) {
+               if (modest_tny_folder_is_local_folder (msg_folder) &&
+                   modest_tny_folder_get_local_folder_type (msg_folder) == TNY_FOLDER_TYPE_DRAFTS)
+                       priv->draft_msg = g_object_ref(msg);
+               g_object_unref (msg_folder);
+       }
 }
 
 static void
@@ -1075,18 +1090,29 @@ modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
        GList *cursor = priv->attachments;
        data->attachments = NULL;
        while (cursor) {
+               if (!(TNY_IS_MIME_PART(cursor->data))) {
+                       g_warning ("strange data in attachment list");
+                       continue;
+               }
                data->attachments = g_list_append (data->attachments,
-                                                  g_strdup ((gchar*)cursor->data));
+                                                  g_object_ref (cursor->data));
                cursor = g_list_next (cursor);
        }
        
-
        data->priority_flags = priv->priority_flags;
 
        return data;
 }
 
-/* TODO: We must duplicate this implementation for GNOME and Maemo, but that is unwise. */
+
+static void
+unref_gobject (GObject *obj, gpointer data)
+{
+       if (!G_IS_OBJECT(obj))
+               return;
+       g_object_unref (obj);
+}
+
 void 
 modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
                                                      MsgData *data)
@@ -1108,8 +1134,8 @@ modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
                g_object_unref (data->draft_msg);
                data->draft_msg = NULL;
        }
-
-       g_list_foreach (data->attachments, (GFunc)g_free, NULL);
+       
+       g_list_foreach (data->attachments, (GFunc)unref_gobject,  NULL);
        g_list_free (data->attachments);
        
        g_slice_free (MsgData, data);
@@ -2676,20 +2702,20 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        ModestWindowPrivate *parent_priv;
        ModestMsgEditWindowPrivate *priv;
        GtkAction *action;
-       gchar *selection;
+       gboolean has_selection;
        GtkWidget *focused;
        GList *selected_attachments = NULL;
        gint n_att_selected = 0;
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
-       selection = gtk_clipboard_wait_for_text (clipboard);
+       has_selection = gtk_clipboard_wait_for_targets (clipboard, NULL, NULL);
        focused = gtk_window_get_focus (GTK_WINDOW (window));
 
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/CutMenu");
-       gtk_action_set_sensitive (action, (selection != NULL) && (!MODEST_IS_ATTACHMENTS_VIEW (focused)));
+       gtk_action_set_sensitive (action, (has_selection) && (!MODEST_IS_ATTACHMENTS_VIEW (focused)));
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/EditMenu/CopyMenu");
-       gtk_action_set_sensitive (action, (selection != NULL) && (!MODEST_IS_ATTACHMENTS_VIEW (focused)));
+       gtk_action_set_sensitive (action, (has_selection) && (!MODEST_IS_ATTACHMENTS_VIEW (focused)));
 
        selected_attachments = modest_attachments_view_get_selection (MODEST_ATTACHMENTS_VIEW (priv->attachments_view));
        n_att_selected = g_list_length (selected_attachments);
@@ -2698,7 +2724,6 @@ modest_msg_edit_window_clipboard_owner_change (GtkClipboard *clipboard,
        action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/AttachmentsMenu/RemoveAttachmentsMenu");
        gtk_action_set_sensitive (action, n_att_selected == 1);
        
-
        update_paste_dimming (window);
 }
 
@@ -2996,3 +3021,33 @@ modest_msg_edit_window_set_sent (ModestMsgEditWindow *window,
 }
 
 
+void            
+modest_msg_edit_window_set_draft (ModestMsgEditWindow *window,
+                                 TnyMsg *draft)
+{
+       ModestMsgEditWindowPrivate *priv;
+       TnyHeader *header = NULL;
+
+       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window));
+       g_return_if_fail ((draft == NULL)||(TNY_IS_MSG (draft)));
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+       ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
+
+       if (priv->draft_msg != NULL) {
+               header = tny_msg_get_header (priv->draft_msg);
+               if (TNY_IS_HEADER (header)) {
+                       modest_window_mgr_unregister_header (mgr, header);
+               }
+               g_object_unref (priv->draft_msg);
+       }
+
+       if (draft != NULL) {
+               g_object_ref (draft);
+               header = tny_msg_get_header (draft);
+               if (TNY_IS_HEADER (header))
+                       modest_window_mgr_register_header (mgr, header);
+       }
+
+       priv->draft_msg = draft;
+}