* Fixes NB#59138, message is saved to drafts when main window is closed
authorSergio Villar Senin <svillar@igalia.com>
Mon, 25 Jun 2007 11:36:41 +0000 (11:36 +0000)
committerSergio Villar Senin <svillar@igalia.com>
Mon, 25 Jun 2007 11:36:41 +0000 (11:36 +0000)
* modest_msg_edit_window_is_modified is now public
* modest_window_mgr.c: added a hash table to store the "window-destroy" signal handlers
* modest_window_mgr.c: replaced a g_object_unref by gtk_widget_destroy
* moved the save_to_drafts call from the edit window to the window manager
* added some missing unrefs to the UI actions
* fixed save_to_drafts mail operation, it was not reporting the mail operation status

pmo-trunk-r2397

src/maemo/modest-msg-edit-window.c
src/modest-mail-operation.c
src/modest-ui-actions.c
src/widgets/modest-msg-edit-window.h
src/widgets/modest-window-mgr.c

index aa1bfa0..6310ed6 100644 (file)
@@ -95,7 +95,6 @@ static void  remove_attachment_insensitive_press (GtkWidget *widget, ModestMsgEd
 static void  zoom_insensitive_press (GtkWidget *widget, ModestMsgEditWindow *editor);
 static void  setup_insensitive_handlers (ModestMsgEditWindow *editor);
 static void  reset_modified (ModestMsgEditWindow *editor);
-static gboolean is_modified (ModestMsgEditWindow *editor);
 
 static void  text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *window);
 static void  text_buffer_delete_range (GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end, gpointer userdata);
@@ -525,44 +524,6 @@ modest_msg_edit_window_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-static gboolean
-on_delete_event (GtkWidget *widget, GdkEvent *event, ModestMsgEditWindow *self)
-{
-       GtkWidget *close_dialog;
-       ModestMsgEditWindowPrivate *priv;
-       gint response;
-
-       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (self);
-       modest_window_save_state (MODEST_WINDOW (self));
-       if (is_modified (self)) {
-               close_dialog = hildon_note_new_confirmation (GTK_WINDOW (self), _("mcen_nc_no_email_message_modified_save_changes"));
-               response = gtk_dialog_run (GTK_DIALOG (close_dialog));
-               gtk_widget_destroy (close_dialog);
-
-               if (response != GTK_RESPONSE_CANCEL) {
-                       modest_ui_actions_on_save_to_drafts (NULL, self);
-               }
-       } 
-/*     /\* remove old message from drafts *\/ */
-/*     if (priv->draft_msg) { */
-/*             TnyHeader *header = tny_msg_get_header (priv->draft_msg); */
-/*             TnyAccount *account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(), */
-/*                                                                                        account_name, */
-/*                                                                                        TNY_ACCOUNT_TYPE_STORE); */
-/*             TnyFolder *folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS); */
-/*             g_return_val_if_fail (TNY_IS_HEADER (header), FALSE); */
-/*             g_return_val_if_fail (TNY_IS_FOLDER (folder), FALSE); */
-/*             tny_folder_remove_msg (folder, header, NULL); */
-/*             g_object_unref (folder); */
-/*             g_object_unref (header); */
-/*             g_object_unref (priv->draft_msg); */
-/*             priv->draft_msg = NULL; */
-/*     } */
-       gtk_widget_destroy (GTK_WIDGET (self));
-       
-       return TRUE;
-}
-
 static GtkWidget *
 menubar_to_menu (GtkUIManager *ui_manager)
 {
@@ -945,9 +906,6 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
                
        gtk_window_set_icon_from_file (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);
 
-       g_signal_connect (G_OBJECT(obj), "delete-event",
-                         G_CALLBACK(on_delete_event), obj);
-
        modest_window_set_active_account (MODEST_WINDOW(obj), account_name);
 
        modest_msg_edit_window_setup_toolbar (MODEST_MSG_EDIT_WINDOW (obj));
@@ -2561,8 +2519,8 @@ reset_modified (ModestMsgEditWindow *editor)
        gtk_text_buffer_set_modified (priv->text_buffer, FALSE);
 }
 
-static gboolean
-is_modified (ModestMsgEditWindow *editor)
+gboolean
+modest_msg_edit_window_is_modified (ModestMsgEditWindow *editor)
 {
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (editor);
        GtkTextBuffer *buffer;
index 043d711..c3fa967 100644 (file)
@@ -619,6 +619,7 @@ modest_mail_operation_save_to_drafts (ModestMailOperation *self,
                msg = modest_tny_msg_new_html_plain (to, from, cc, bcc, subject, html_body, plain_body, (GSList *) attachments_list);
        }
        if (!msg) {
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
                g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
                             MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED,
                             "modest: failed to create a new msg\n");
@@ -631,6 +632,7 @@ modest_mail_operation_save_to_drafts (ModestMailOperation *self,
 
        folder = modest_tny_account_get_special_folder (TNY_ACCOUNT (transport_account), TNY_FOLDER_TYPE_DRAFTS);
        if (!folder) {
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
                g_set_error (&(priv->error), MODEST_MAIL_OPERATION_ERROR,
                             MODEST_MAIL_OPERATION_ERROR_ITEM_NOT_FOUND,
                             "modest: failed to create a new msg\n");
@@ -646,8 +648,8 @@ modest_mail_operation_save_to_drafts (ModestMailOperation *self,
        }
        
        tny_folder_add_msg (folder, msg, &(priv->error));
-       if (priv->error)
-               goto end;
+       if (!priv->error)
+               priv->status = MODEST_MAIL_OPERATION_STATUS_SUCCESS;
 
 end:
        if (msg)
index 98748fa..439343f 100644 (file)
@@ -562,6 +562,8 @@ cleanup:
        g_free (account_name);
        g_free (from_str);
        g_free (blank_and_signature);
+       if (msg_win)
+               g_object_unref (msg_win);
        if (account)
                g_object_unref (G_OBJECT(account));
        if (msg)
@@ -652,6 +654,7 @@ open_msg_cb (ModestMailOperation *mail_op,
        if (win != NULL) {
                mgr = modest_runtime_get_window_mgr ();
                modest_window_mgr_register_window (mgr, win);
+               g_object_unref (win);
                gtk_window_set_transient_for (GTK_WINDOW (win), GTK_WINDOW (parent_win));
                gtk_widget_show_all (GTK_WIDGET(win));
        }
@@ -857,6 +860,8 @@ reply_forward_cb (ModestMailOperation *mail_op,
        gtk_widget_show_all (GTK_WIDGET (msg_win));
 
 cleanup:
+       if (msg_win)
+               g_object_unref (msg_win);
        if (new_msg)
                g_object_unref (G_OBJECT (new_msg));
        if (account)
@@ -1539,9 +1544,6 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
        modest_platform_information_banner (NULL, NULL, info_text);
        g_free (info_text);
-
-       /* Save settings and close the window */
-       gtk_widget_destroy (GTK_WIDGET (edit_window));
 }
 
 /* For instance, when clicking the Send toolbar button when editing a message: */
index bcbf164..bbc30d0 100644 (file)
@@ -322,6 +322,19 @@ gboolean        modest_msg_edit_window_check_names    (ModestMsgEditWindow *wind
  */
 void            modest_msg_edit_window_toggle_find_toolbar (ModestMsgEditWindow *window,
                                                            gboolean show);
+
+
+/**
+ * modest_msg_edit_window_is_modified:
+ * @window: a #ModestMsgEditWindow
+ *
+ * Examines whether or not the message has been modified
+ *
+ * Returns: %TRUE if any field has been modified, %FALSE otherwise
+ */
+gboolean        modest_msg_edit_window_is_modified         (ModestMsgEditWindow *window);
+
+
 G_END_DECLS
 
 #endif /* __MODEST_MSG_EDIT_WINDOW_H__ */
index e30be7e..91348d7 100644 (file)
@@ -31,6 +31,8 @@
 #include "modest-window-mgr.h"
 #include "modest-runtime.h"
 #include "modest-tny-folder.h"
+#include "modest-ui-actions.h"
+#include "modest-platform.h"
 #include "widgets/modest-main-window.h"
 #include "widgets/modest-msg-edit-window.h"
 #include "widgets/modest-msg-view-window.h"
@@ -53,13 +55,14 @@ enum {
 
 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
 struct _ModestWindowMgrPrivate {
-       GList *window_list;
+       GList        *window_list;
        ModestWindow *main_window;
-       gboolean fullscreen_mode;
-       gboolean show_toolbars;
-       gboolean show_toolbars_fullscreen;
+       gboolean     fullscreen_mode;
+       gboolean     show_toolbars;
+       gboolean     show_toolbars_fullscreen;
        
-       GSList* windows_that_prevent_hibernation;
+       GSList       *windows_that_prevent_hibernation;
+       GHashTable   *destroy_handlers;
 };
 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                MODEST_TYPE_WINDOW_MGR, \
@@ -120,6 +123,7 @@ modest_window_mgr_init (ModestWindowMgr *obj)
           ready yet */
        priv->show_toolbars = FALSE;
        priv->show_toolbars_fullscreen = FALSE;
+       priv->destroy_handlers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
 }
 
 static void
@@ -139,6 +143,12 @@ modest_window_mgr_finalize (GObject *obj)
                priv->window_list = NULL;
        }
 
+       /* Free the hash table with the handlers */
+       if (priv->destroy_handlers) {
+               g_hash_table_unref (priv->destroy_handlers);
+               priv->destroy_handlers = NULL;
+       }
+
        /* Do not unref priv->main_window because it does not hold a
           new reference */
 
@@ -159,6 +169,7 @@ modest_window_mgr_register_window (ModestWindowMgr *self,
        GList *win;
        gboolean show;
        ModestWindowMgrPrivate *priv;
+       gint *handler_id;
 
        g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
        g_return_if_fail (MODEST_IS_WINDOW (window));
@@ -186,7 +197,9 @@ modest_window_mgr_register_window (ModestWindowMgr *self,
        priv->window_list = g_list_prepend (priv->window_list, window);
 
        /* Listen to object destruction */
-       g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), self);
+       handler_id = g_malloc0 (sizeof (gint));
+       *handler_id = g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), self);
+       g_hash_table_insert (priv->destroy_handlers, window, handler_id);
 
        /* Put into fullscreen if needed */
        if (priv->fullscreen_mode)
@@ -237,10 +250,16 @@ on_window_destroy (ModestWindow *window, ModestWindowMgr *self)
                        }
                }
        } else {
-               if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
-                       /* TODO: Save currently edited message to Drafts
-                          folder */
-               }
+               if (MODEST_IS_MSG_EDIT_WINDOW (window))
+                       /* Save currently edited message to Drafts */
+                       if (modest_msg_edit_window_is_modified (MODEST_MSG_EDIT_WINDOW (window))) {
+                               gint response = 
+                                       modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
+                                                                                _("mcen_nc_no_email_message_modified_save_changes"));
+                               if (response != GTK_RESPONSE_CANCEL) {
+                                       modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));
+                               }
+                       }
        }
 
        /* Unregister window */
@@ -253,6 +272,7 @@ modest_window_mgr_unregister_window (ModestWindowMgr *self,
 {
        GList *win;
        ModestWindowMgrPrivate *priv;
+       gint *handler_id;
 
        g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
        g_return_if_fail (MODEST_IS_WINDOW (window));
@@ -269,10 +289,23 @@ modest_window_mgr_unregister_window (ModestWindowMgr *self,
        if (priv->main_window == window)
                priv->main_window = NULL;
 
-       /* Remove from list. Remove the reference to the window */
-       g_object_unref (win->data);
+       /* Save state */
+       modest_window_save_state (window);
+
+       /* Remove from list */
        priv->window_list = g_list_remove_link (priv->window_list, win);
 
+       /* Remove the reference to the window. We need to block the
+          destroy event handler to avoid recursive calls */
+       handler_id = g_hash_table_lookup (priv->destroy_handlers, window);
+       g_signal_handler_block (window, *handler_id);
+       gtk_widget_destroy (win->data);
+       if (G_IS_OBJECT (window)) {
+               g_warning ("This should not happen the window was not completely destroyed");
+               g_signal_handler_unblock (window, *handler_id);
+       }
+       g_hash_table_remove (priv->destroy_handlers, window);
+
        /* If there are no more windows registered then exit program */
        if (priv->window_list == NULL) {
                ModestConf *conf = modest_runtime_get_conf ();