* Fixes NB#75550, after deleting a folder the focus is not set over the INBOX
[modest] / src / maemo / modest-msg-edit-window.c
index e09c165..6c770fb 100644 (file)
@@ -56,6 +56,7 @@
 #include "modest-tny-platform-factory.h"
 #include "modest-tny-msg.h"
 #include "modest-tny-folder.h"
+#include "modest-tny-account.h"
 #include "modest-address-book.h"
 #include "modest-text-utils.h"
 #include <tny-simple-list.h>
@@ -152,6 +153,11 @@ static gboolean gtk_text_iter_forward_search_insensitive (const GtkTextIter *ite
 
 static void remove_tags (WPTextBuffer *buffer);
 
+static void on_account_removed (TnyAccountStore *account_store, 
+                               TnyAccount *account,
+                               gpointer user_data);
+
+
 static void DEBUG_BUFFER (WPTextBuffer *buffer)
 {
 #ifdef DEBUG
@@ -253,6 +259,7 @@ struct _ModestMsgEditWindowPrivate {
        gboolean    can_undo, can_redo;
        gulong      clipboard_change_handler_id;
        gulong      default_clipboard_change_handler_id;
+       gulong      account_removed_handler_id;
        gchar       *clipboard_text;
 
        TnyMsg      *draft_msg;
@@ -369,10 +376,14 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->can_redo = FALSE;
        priv->clipboard_change_handler_id = 0;
        priv->default_clipboard_change_handler_id = 0;
+       priv->account_removed_handler_id = 0;
        priv->clipboard_text = NULL;
        priv->sent = FALSE;
 
        priv->last_vadj_upper = 0;
+
+       modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
+                                           GTK_WINDOW(obj),"applications_email_editor");
 }
 
 
@@ -628,6 +639,12 @@ modest_msg_edit_window_disconnect_signals (ModestWindow *window)
                                           priv->default_clipboard_change_handler_id))
                g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), 
                                             priv->default_clipboard_change_handler_id);
+
+       if (priv->account_removed_handler_id && 
+           g_signal_handler_is_connected (modest_runtime_get_account_store (), 
+                                          priv->account_removed_handler_id))
+               g_signal_handler_disconnect(modest_runtime_get_account_store (), 
+                                          priv->account_removed_handler_id);
 }
 
 static void
@@ -859,7 +876,7 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
        cc      = tny_header_get_cc (header);
        bcc     = tny_header_get_bcc (header);
        subject = tny_header_get_subject (header);
-       priority_flags = tny_header_get_flags (header) & TNY_HEADER_FLAG_PRIORITY;
+       priority_flags = tny_header_get_priority (header);
 
        if (to)
                modest_recpt_editor_set_recipients (MODEST_RECPT_EDITOR (priv->to_field),  to);
@@ -954,6 +971,9 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
        if (msg_folder) {               
                if (modest_tny_folder_is_local_folder (msg_folder)) {
                        TnyFolderType type = modest_tny_folder_get_local_or_mmc_folder_type (msg_folder);
+                       if (type == TNY_FOLDER_TYPE_INVALID)
+                               g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
+                       
                        if (type == TNY_FOLDER_TYPE_DRAFTS) 
                                priv->draft_msg = g_object_ref(msg);
                        if (type == TNY_FOLDER_TYPE_OUTBOX)
@@ -1327,6 +1347,15 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean pre
        priv->update_caption_visibility = TRUE;
 
        modest_msg_edit_window_reset_modified (MODEST_MSG_EDIT_WINDOW (obj));
+
+       /* Track account-removed signal, this window should be closed
+          in the case we're creating a mail associated to the account
+          that is deleted */
+       priv->account_removed_handler_id = 
+               g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
+                                 "account_removed",
+                                 G_CALLBACK(on_account_removed),
+                                 obj);
        
        return (ModestWindow*) obj;
 }
@@ -1706,7 +1735,7 @@ text_buffer_refresh_attributes (WPTextBuffer *buffer, ModestMsgEditWindow *windo
 
 }
 
-
+#ifdef MODEST_HILDON_VERSION_0
 void
 modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
 {
@@ -1715,43 +1744,26 @@ modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
        ModestMsgEditWindowPrivate *priv;
        GtkWidget *dialog = NULL;
        gint response;
-       const GdkColor *new_color = NULL;
-       
+       GdkColor *new_color = NULL;
+
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
        
-#ifdef MODEST_HILDON_VERSION_0 
        dialog = hildon_color_selector_new (GTK_WINDOW (window));
        hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog), &(buffer_format->color));
-#else
-       dialog = hildon_color_chooser_new ();
-       hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog), &(buffer_format->color));
-#endif /*MODEST_HILDON_VERSION_0*/             
        g_free (buffer_format);
 
-       response = gtk_dialog_run (GTK_DIALOG (dialog));
-       switch (response) {
-       case GTK_RESPONSE_OK: {
-#ifdef MODEST_HILDON_VERSION_0
+       if (gtk_dialog_run (GTK_DIALOG (dialog) == GTK_RESPONSE_OK)) {
                new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
-#else
-               GdkColor col;
-               hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER(dialog), &col);
-               new_color = &col;
-#endif /*MODEST_HILDON_VERSION_0*/
-       }
-
-       break;
-       default:
-               break;
+               if (new_color != NULL) {
+                       wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR,
+                                                     (gpointer) new_color);
+               }
        }
        gtk_widget_destroy (dialog);
-
-       if (new_color != NULL)
-               wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR, (gpointer) new_color);
-
 }
 
+
 void
 modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
 {
@@ -1765,36 +1777,70 @@ modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        old_color = (GdkColor*)wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
        
-#ifdef MODEST_HILDON_VERSION_0 
        dialog = hildon_color_selector_new (GTK_WINDOW (window));
        hildon_color_selector_set_color (HILDON_COLOR_SELECTOR (dialog),(GdkColor*)old_color);
-#else
-       dialog = hildon_color_chooser_new ();
-       hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog),(GdkColor*)old_color);
-#endif /*MODEST_HILDON_VERSION_9*/             
 
-       response = gtk_dialog_run (GTK_DIALOG (dialog));
-       switch (response) {
-       case GTK_RESPONSE_OK: {
-#ifdef MODEST_HILDON_VERSION_0
+       if (gtk_dialog_run (GTK_DIALOG (dialog) == GTK_RESPONSE_OK)) {
                new_color = hildon_color_selector_get_color (HILDON_COLOR_SELECTOR (dialog));
-#else
+               if (new_color != NULL)
+                       wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), new_color);
+       }
+       gtk_widget_destroy (dialog);
+
+}
+
+#else 
+void
+modest_msg_edit_window_select_color (ModestMsgEditWindow *window)
+{
+       
+       WPTextBufferFormat *buffer_format = g_new0 (WPTextBufferFormat, 1);
+       ModestMsgEditWindowPrivate *priv;
+       GtkWidget *dialog = NULL;
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+       wp_text_buffer_get_attributes (WP_TEXT_BUFFER (priv->text_buffer), buffer_format, FALSE);
+               
+       dialog = hildon_color_chooser_new ();
+       hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog), &(buffer_format->color));
+       g_free (buffer_format);
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
                GdkColor col;
                hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER(dialog), &col);
-               new_color = &col;
-#endif /*MODEST_HILDON_VERSION_0*/
-          }
-               break;
-       default:
-               break;
+               wp_text_buffer_set_attribute (WP_TEXT_BUFFER (priv->text_buffer), WPT_FORECOLOR,
+                                             (gpointer) &col);
        }
        gtk_widget_destroy (dialog);
+}
+
 
-       if (new_color != NULL)
-               wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), new_color);
+void
+modest_msg_edit_window_select_background_color (ModestMsgEditWindow *window)
+{
+       
+       ModestMsgEditWindowPrivate *priv;
+       GtkWidget *dialog = NULL;
+       GdkColor *old_color = NULL;
+       
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+       old_color = (GdkColor*)wp_text_buffer_get_background_color (WP_TEXT_BUFFER (priv->text_buffer));
+       
+       dialog = hildon_color_chooser_new ();
+       hildon_color_chooser_set_color (HILDON_COLOR_CHOOSER (dialog),(GdkColor*)old_color);
 
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) { 
+               GdkColor col;
+               hildon_color_chooser_get_color (HILDON_COLOR_CHOOSER(dialog), &col);
+               wp_text_buffer_set_background_color (WP_TEXT_BUFFER (priv->text_buffer), &col);
+       }
+       gtk_widget_destroy (dialog);
 }
 
+#endif /*!MODEST_HILDON_VERSION_0*/
+
+
+
 static TnyStream* create_stream_for_uri (const gchar* uri)
 {
        if (!uri)
@@ -2023,15 +2069,27 @@ modest_msg_edit_window_remove_attachments (ModestMsgEditWindow *window,
                gboolean dialog_response;
                GList *node;
                gchar *message = NULL;
-               const gchar *filename = NULL;
+               gchar *filename = NULL;
 
                if (att_list->next == NULL) {
-                       filename = tny_mime_part_get_filename (TNY_MIME_PART (att_list->data));
+                       if (TNY_IS_MSG (att_list->data)) {
+                               TnyHeader *header = tny_msg_get_header (TNY_MSG (att_list->data));
+                               if (header) {
+                                       filename = g_strdup (tny_header_get_subject (header));
+                                       g_object_unref (header);
+                               }
+                               if (filename == NULL) {
+                                       filename = g_strdup (_("mail_va_no_subject"));
+                               }
+                       } else {
+                               filename = g_strdup (tny_mime_part_get_filename (TNY_MIME_PART (att_list->data)));
+                       }
                } else {
-                       filename = "";
+                       filename = g_strdup ("");
                }
                message = g_strdup_printf (ngettext("emev_nc_delete_attachment", "emev_nc_delete_attachments",
                                                    att_list->next == NULL), filename);
+               g_free (filename);
                confirmation_dialog = hildon_note_new_confirmation (GTK_WINDOW (window), message);
                g_free (message);
                dialog_response = (gtk_dialog_run (GTK_DIALOG (confirmation_dialog))==GTK_RESPONSE_OK);
@@ -2428,7 +2486,6 @@ modest_msg_edit_window_set_priority_flags (ModestMsgEditWindow *window,
 
        priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
        parent_priv = MODEST_WINDOW_GET_PRIVATE (window);
-       priority_flags = priority_flags & (TNY_HEADER_FLAG_PRIORITY);
 
        if (priv->priority_flags != priority_flags) {
                GtkAction *priority_action = NULL;
@@ -3156,23 +3213,20 @@ modest_msg_edit_window_set_draft (ModestMsgEditWindow *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);
-               }
+               modest_window_mgr_unregister_window (mgr, MODEST_WINDOW (window));
                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);
                if (priv->msg_uid) {
                        g_free (priv->msg_uid);
                        priv->msg_uid = NULL;
                }
                priv->msg_uid = modest_tny_folder_get_header_unique_id (header);
+               if (GTK_WIDGET_REALIZED (window))
+                       modest_window_mgr_register_window (mgr, MODEST_WINDOW (window));
        }
 
        priv->draft_msg = draft;
@@ -3263,3 +3317,22 @@ remove_tags (WPTextBuffer *buffer)
 
        gtk_text_buffer_remove_all_tags (GTK_TEXT_BUFFER (buffer), &start, &end);
 }
+
+static void
+on_account_removed (TnyAccountStore *account_store, 
+                   TnyAccount *account,
+                   gpointer user_data)
+{
+       /* Do nothing if it's a store account, because we use the
+          transport to send the messages */
+       if (tny_account_get_account_type(account) == TNY_ACCOUNT_TYPE_TRANSPORT) {
+               const gchar *parent_acc = NULL;
+               const gchar *our_acc = NULL;
+
+               our_acc = modest_window_get_active_account (MODEST_WINDOW (user_data));
+               parent_acc = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+               /* Close this window if I'm showing a message of the removed account */
+               if (strcmp (parent_acc, our_acc) == 0)
+                       modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (user_data));
+       }
+}