Avoid a crash on requesting to open a msg view that window mgr rejects.
[modest] / src / hildon2 / modest-msg-view-window.c
index db1a8be..cf2f3f3 100644 (file)
 #include <tny-camel-msg.h>
 #include <tny-camel-bs-mime-part.h>
 #include <tny-camel-bs-msg.h>
+#include <gdk/gdkx.h>
+#include <X11/Xatom.h>
+#include <X11/XKBlib.h>
+#include <X11/Xdmcp.h>
 
 #define MYDOCS_ENV "MYDOCSDIR"
 #define DOCS_FOLDER ".documents"
@@ -124,6 +128,7 @@ struct _ModestMsgViewWindowPrivate {
 
        gchar *msg_uid;
        TnyMimePart *other_body;
+       TnyMsg * top_msg;
 
        GSList *sighandlers;
 };
@@ -235,6 +240,11 @@ static gboolean _modest_msg_view_window_map_event (GtkWidget *widget,
                                                   gpointer userdata);
 static void update_branding (ModestMsgViewWindow *self);
 static void sync_flags      (ModestMsgViewWindow *self);
+static gboolean on_handle_calendar (ModestMsgView *msgview, TnyMimePart *calendar_part, 
+                                   GtkContainer *container, ModestMsgViewWindow *self);
+
+static gboolean on_realize (GtkWidget *widget,
+                           gpointer userdata);
 
 /* list my signals */
 enum {
@@ -482,15 +492,20 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
        priv->remove_attachment_banner = NULL;
        priv->msg_uid = NULL;
        priv->other_body = NULL;
-       
+
        priv->sighandlers = NULL;
-       
+
        /* Init window */
        init_window (MODEST_MSG_VIEW_WINDOW(obj));
-       
+
        hildon_program_add_window (hildon_program_get_instance(),
                                   HILDON_WINDOW(obj));
 
+       /* Grab the zoom keys, it will be used for Zoom and not for
+          changing volume */
+       g_signal_connect (G_OBJECT (obj), "realize",
+                         G_CALLBACK (on_realize),
+                         NULL);
 }
 
 static void
@@ -632,6 +647,11 @@ modest_msg_view_window_finalize (GObject *obj)
                priv->other_body = NULL;
        }
 
+       if (priv->top_msg != NULL) {
+               g_object_unref (priv->top_msg);
+               priv->top_msg = NULL;
+       }
+
        if (priv->header_model != NULL) {
                g_object_unref (priv->header_model);
                priv->header_model = NULL;
@@ -801,6 +821,8 @@ modest_msg_view_window_construct (ModestMsgViewWindow *self,
                          G_CALLBACK (modest_ui_actions_on_msg_link_contextual), obj);
        g_signal_connect (G_OBJECT(priv->msg_view), "limit_error",
                          G_CALLBACK (modest_ui_actions_on_limit_error), obj);
+       g_signal_connect (G_OBJECT(priv->msg_view), "handle_calendar",
+                         G_CALLBACK (on_handle_calendar), obj);
        g_signal_connect (G_OBJECT (priv->msg_view), "fetch_image",
                          G_CALLBACK (on_fetch_image), obj);
 
@@ -882,6 +904,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes
         * and change the list selection when necessary: */
@@ -960,6 +983,7 @@ modest_msg_view_window_new_from_uid (const gchar *modest_account_name,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        is_merge = g_str_has_prefix (msg_uid, "merge:");
 
@@ -1040,6 +1064,7 @@ modest_msg_view_window_new_from_header_view (ModestHeaderView *header_view,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember the message list's TreeModel so we can detect changes 
         * and change the list selection when necessary: */
@@ -1133,6 +1158,7 @@ modest_msg_view_window_new_for_search_result (TnyMsg *msg,
        modest_msg_view_window_construct (window, modest_account_name, mailbox, msg_uid);
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
+       priv->top_msg = NULL;
 
        /* Remember that this is a search result, 
         * so we can disable some UI appropriately: */
@@ -1167,6 +1193,7 @@ modest_msg_view_window_is_other_body (ModestMsgViewWindow *self)
 ModestWindow *
 modest_msg_view_window_new_with_other_body (TnyMsg *msg, 
                                            TnyMimePart *other_body,
+                                           TnyMsg *top_msg,
                                            const gchar *modest_account_name,
                                            const gchar *mailbox,
                                            const gchar *msg_uid)
@@ -1188,6 +1215,11 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
        } else {
                tny_msg_view_set_msg (TNY_MSG_VIEW (priv->msg_view), msg);
        }
+       if (top_msg) {
+               priv->top_msg = g_object_ref (top_msg);
+       } else {
+               priv->top_msg = NULL;
+       }
        update_window_title (MODEST_MSG_VIEW_WINDOW (obj));
        update_branding (MODEST_MSG_VIEW_WINDOW (obj));
 
@@ -1202,12 +1234,13 @@ modest_msg_view_window_new_with_other_body (TnyMsg *msg,
 }
 
 ModestWindow *
-modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
+modest_msg_view_window_new_for_attachment (TnyMsg *msg,
+                                          TnyMsg *top_msg,
                                           const gchar *modest_account_name,
                                           const gchar *mailbox,
                                           const gchar *msg_uid)
 {
-       return modest_msg_view_window_new_with_other_body (msg, NULL, modest_account_name, mailbox, msg_uid);
+       return modest_msg_view_window_new_with_other_body (msg, NULL, top_msg, modest_account_name, mailbox, msg_uid);
 }
 
 static void
@@ -1514,6 +1547,21 @@ modest_msg_view_window_get_message (ModestMsgViewWindow *self)
        return tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
 }
 
+TnyMsg*
+modest_msg_view_window_get_top_message (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       
+       g_return_val_if_fail (self, NULL);
+       
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+       if (priv->top_msg)
+               return g_object_ref (priv->top_msg);
+       else
+               return NULL;
+}
+
 const gchar*
 modest_msg_view_window_get_message_uid (ModestMsgViewWindow *self)
 {
@@ -2161,10 +2209,10 @@ view_msg_cb (ModestMailOperation *mail_op,
        modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (), header);
 
        row_reference = (GtkTreeRowReference *) user_data;
-       if (canceled) {
+       self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
+       if (canceled || !self || MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self)->msg_view == NULL ) {
                if (row_reference)
                        gtk_tree_row_reference_free (row_reference);
-               self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
                if (self) {
                        /* Restore window title */
                        update_window_title (self);
@@ -2178,7 +2226,6 @@ view_msg_cb (ModestMailOperation *mail_op,
        if (!modest_ui_actions_msg_retrieval_check (mail_op, header, msg)) {
                if (row_reference)
                        gtk_tree_row_reference_free (row_reference);
-               self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
                if (self) {
                        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
                        /* First we check if the parent is a folder window */
@@ -2241,32 +2288,7 @@ view_msg_cb (ModestMailOperation *mail_op,
                return;
        }
 
-       if (msg && TNY_IS_CAMEL_BS_MSG (msg)) {
-               TnyMimePart *body;
-               body = modest_tny_msg_find_body_part (msg, TRUE);
-               
-               if (body && !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (body))) {
-                       /* We have body structure but not the body mime part. What we do
-                        * is restarting load of message */
-                       self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
-                       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
-
-                       tny_header_unset_flag (TNY_HEADER (header), TNY_HEADER_FLAG_CACHED);
-
-                       modest_msg_view_window_reload (self);
-
-                       if (row_reference)
-                               gtk_tree_row_reference_free (row_reference);
-                       g_object_unref (body);
-                       return;
-               }
-
-               if (body)
-                       g_object_unref  (body);
-       }
-
        /* Get the window */ 
-       self = (ModestMsgViewWindow *) modest_mail_operation_get_source (mail_op);
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
 
@@ -2286,8 +2308,14 @@ view_msg_cb (ModestMailOperation *mail_op,
        }
 
        /* Mark header as read */
-       if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN))
+       if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
+               gchar *uid;
+
                tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
+               uid = modest_tny_folder_get_header_unique_id (header);
+               modest_platform_emit_msg_read_changed_signal (uid, TRUE);
+               g_free (uid);
+       }
 
        /* Set new message */
        if (priv->msg_view != NULL && TNY_IS_MSG_VIEW (priv->msg_view)) {
@@ -2672,6 +2700,9 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 {
        DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
        const gchar *content_type;
+       ModestMsgViewWindowPrivate *priv;
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (helper->self);
 
        if (cancelled || err) {
                if (err) {
@@ -2698,7 +2729,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
        set_progress_hint (helper->self, FALSE);
 
        content_type = tny_mime_part_get_content_type (mime_part);
-       if (g_str_has_prefix (content_type, "message/rfc822")) {
+       if (content_type && g_str_has_prefix (content_type, "message/rfc822")) {
                ModestWindowMgr *mgr;
                ModestWindow *msg_win = NULL;
                TnyMsg * msg;
@@ -2709,6 +2740,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                fd = g_open (helper->file_path, O_RDONLY, 0644);
                if (fd != -1) {
+                       TnyMsg *top_msg;
                        file_stream = tny_fs_stream_new (fd);
 
                        mgr = modest_runtime_get_window_mgr ();
@@ -2721,7 +2753,15 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
 
                        msg = tny_camel_msg_new ();
                        tny_camel_msg_parse (msg, file_stream);
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), account, mailbox, helper->attachment_uid);
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+
+                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (msg), top_msg, 
+                                                                            account, mailbox, helper->attachment_uid);
+                       if (top_msg) g_object_unref (top_msg);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (helper->self)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (helper->self)))
@@ -2740,7 +2780,7 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
                g_chmod(helper->file_path, 0444);
 
                /* Activate the file */
-               modest_platform_activate_file (helper->file_path, tny_mime_part_get_content_type (mime_part));
+               modest_platform_activate_file (helper->file_path, content_type);
        }
 
  free:
@@ -2924,6 +2964,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                if (found) {
                        g_debug ("window for this body is already being created");
                } else {
+                       TnyMsg *top_msg;
 
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
@@ -2931,9 +2972,16 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
+
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
                        
-                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part),
+                       msg_win = modest_msg_view_window_new_with_other_body (TNY_MSG (current_msg), TNY_MIME_PART (mime_part), top_msg,
                                                                              account, mailbox, attachment_uid);
+
+                       if (top_msg) g_object_unref (top_msg);
                        
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
@@ -2959,14 +3007,20 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                         * thus, we don't do anything */
                        g_debug ("window for is already being created");
                } else {
+                       TnyMsg *top_msg;
                        /* it's not found, so create a new window for it */
                        modest_window_mgr_register_header (mgr, header, attachment_uid); /* register the uid before building the window */
                        gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
                        const gchar *mailbox = modest_window_get_active_mailbox (MODEST_WINDOW (window));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, 
-                                                                            mailbox, attachment_uid);
+                       if (priv->top_msg)
+                               top_msg = g_object_ref (priv->top_msg);
+                       else
+                               top_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
+                       msg_win = modest_msg_view_window_new_for_attachment (
+                               TNY_MSG (mime_part), top_msg, account, 
+                               mailbox, attachment_uid);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
                        if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window)))
@@ -3047,6 +3101,7 @@ idle_save_mime_part_show_result (SaveMimePartInfo *info)
        } else {
                hildon_banner_show_information (NULL, NULL, _("mail_ib_file_operation_failed"));
        }
+       set_progress_hint (info->window, FALSE);
        save_mime_part_info_free (info, FALSE);
        gdk_threads_leave (); /* CHECKED */
 
@@ -3092,6 +3147,10 @@ save_mime_part_to_file_connect_idle (SaveMimePartInfo *info)
                                             TNY_ACCOUNT (account),
                                             (ModestConnectedPerformer) save_mime_part_to_file_connect_handler,
                                             info);
+
+       if (account)
+               g_object_unref (account);
+
        return FALSE;
 }
 
@@ -3104,8 +3163,34 @@ save_mime_part_to_file (SaveMimePartInfo *info)
 
        if (TNY_IS_CAMEL_BS_MIME_PART (pair->part) &&
            !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (pair->part))) {
-               g_idle_add ((GSourceFunc) save_mime_part_to_file_connect_idle, info);
-               return NULL;
+               gboolean check_online = TRUE;
+               ModestMsgViewWindowPrivate *priv = NULL;
+
+               /* Check if we really need to connect to save the mime part */
+               priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (info->window);
+               if (g_str_has_prefix (priv->msg_uid, "merge:")) {
+                       check_online = FALSE;
+               } else {
+                       TnyAccountStore *acc_store;
+                       TnyAccount *account = NULL;
+
+                       acc_store = (TnyAccountStore*) modest_runtime_get_account_store ();
+                       account = tny_account_store_find_account (acc_store, priv->msg_uid);
+
+                       if (account) {
+                               if (tny_account_get_connection_status (account) ==
+                                   TNY_CONNECTION_STATUS_CONNECTED)
+                                       check_online = FALSE;
+                               g_object_unref (account);
+                       } else {
+                               check_online = !tny_device_is_online (tny_account_store_get_device (acc_store));
+                       }
+               }
+
+               if (check_online) {
+                       g_idle_add ((GSourceFunc) save_mime_part_to_file_connect_idle, info);
+                       return NULL;
+               }
        }
 
        info->result = gnome_vfs_create (&handle, pair->filename, GNOME_VFS_OPEN_WRITE, FALSE, 0644);
@@ -3140,7 +3225,7 @@ save_mime_part_to_file (SaveMimePartInfo *info)
                if (written < 0) {
                        g_warning ("modest: could not save attachment %s: %d (%s)\n", pair->filename, error?error->code:-1, error?error->message:"Unknown error");
 
-                       if ((error->domain == TNY_ERROR_DOMAIN) && 
+                       if (error && (error->domain == TNY_ERROR_DOMAIN) &&
                            (error->code == TNY_IO_ERROR_WRITE) &&
                            (errno == ENOSPC)) {
                                info->result = GNOME_VFS_ERROR_NO_SPACE;
@@ -3211,6 +3296,8 @@ save_mime_parts_to_file_with_checks (GtkWindow *parent,
        if (!is_ok) {
                save_mime_part_info_free (info, TRUE);
        } else {
+               /* Start progress and launch thread */
+               set_progress_hint (info->window, TRUE);
                g_thread_create ((GThreadFunc)save_mime_part_to_file, info, FALSE, NULL);
        }
 
@@ -3311,6 +3398,28 @@ save_attachments_response (GtkDialog *dialog,
        gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
+static gboolean
+msg_is_attachment (TnyList *mime_parts)
+{
+       TnyIterator *iter;
+       gboolean retval = FALSE;
+
+       if (tny_list_get_length (mime_parts) > 1)
+               return FALSE;
+
+       iter = tny_list_create_iterator (mime_parts);
+       if (iter) {
+               TnyMimePart *part = TNY_MIME_PART (tny_iterator_get_current (iter));
+               if (part) {
+                       if (TNY_IS_MSG (part))
+                               retval = TRUE;
+                       g_object_unref (part);
+               }
+               g_object_unref (iter);
+       }
+       return retval;
+}
+
 void
 modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, 
                                         TnyList *mime_parts)
@@ -3326,10 +3435,31 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window,
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
 
        if (mime_parts == NULL) {
+               gboolean allow_msgs = FALSE;
+
                /* In Hildon 2.2 save and delete operate over all the attachments as there's no
                 * selection available */
                mime_parts = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view));
-               if (mime_parts && !modest_maemo_utils_select_attachments (GTK_WINDOW (window), mime_parts, FALSE)) {
+
+               /* Check if the message is composed by an unique MIME
+                  part whose content disposition is attachment. There
+                  could be messages like this:
+
+                  Date: Tue, 12 Jan 2010 20:40:59 +0000
+                  From: <sender@example.org>
+                  To: <recipient@example.org>
+                  Subject: no body
+                  Content-Type: image/jpeg
+                  Content-Disposition: attachment; filename="bug7718.jpeg"
+
+                  whose unique MIME part is the message itself whose
+                  content disposition is attachment
+               */
+               if (mime_parts && msg_is_attachment (mime_parts))
+                       allow_msgs = TRUE;
+
+               if (mime_parts &&
+                   !modest_maemo_utils_select_attachments (GTK_WINDOW (window), mime_parts, allow_msgs)) {
                        g_object_unref (mime_parts);
                        return;
                }
@@ -3651,7 +3781,9 @@ on_fetch_image_timeout_refresh_view (gpointer userdata)
 
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (userdata);
        update_progress_hint (MODEST_MSG_VIEW_WINDOW (userdata));
-       if (GTK_WIDGET_DRAWABLE (priv->msg_view)) {
+       /* Note that priv->msg_view is set to NULL when this window is
+          distroyed */
+       if (priv->msg_view && GTK_WIDGET_DRAWABLE (priv->msg_view)) {
                gtk_widget_queue_draw (GTK_WIDGET (priv->msg_view));
        }
        priv->fetch_image_redraw_handler = 0;
@@ -3980,3 +4112,45 @@ sync_flags (ModestMsgViewWindow *self)
                g_object_unref (header);
        }
 }
+
+static gboolean
+on_realize (GtkWidget *widget,
+           gpointer userdata)
+{
+       GdkDisplay *display;
+       Atom atom;
+       unsigned long val = 1;
+
+       display = gdk_drawable_get_display (widget->window);
+       atom = gdk_x11_get_xatom_by_name_for_display (display, "_HILDON_ZOOM_KEY_ATOM");
+       XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
+                        GDK_WINDOW_XID (widget->window), atom,
+                        XA_INTEGER, 32, PropModeReplace,
+                        (unsigned char *) &val, 1);
+
+       return FALSE;
+}
+
+static gboolean
+on_handle_calendar (ModestMsgView *msgview, TnyMimePart *calendar_part, GtkContainer *container, ModestMsgViewWindow *self)
+{
+       const gchar *account_name;
+       ModestProtocolType proto_type;
+       ModestProtocol *protocol;
+       gboolean retval = FALSE;
+
+       account_name = modest_window_get_active_account (MODEST_WINDOW (self));
+       
+       /* Get proto */
+       proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
+                                                           account_name);
+       protocol = 
+               modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), 
+                                                              proto_type);
+
+       if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
+               retval = modest_account_protocol_handle_calendar (MODEST_ACCOUNT_PROTOCOL (protocol), MODEST_WINDOW (self),
+                                                                 calendar_part, container);
+       }
+       return retval;
+}