+/*
+ * This function is used by both modest_ui_actions_on_open and
+ * modest_ui_actions_on_header_activated. This way we always do the
+ * same when trying to open messages.
+ */
+static void
+_modest_ui_actions_open (TnyList *headers, ModestWindow *win)
+{
+ ModestWindowMgr *mgr;
+ TnyIterator *iter;
+ ModestMailOperation *mail_op;
+
+ /* Look if we already have a message view for each header. If
+ true, then remove the header from the list of headers to
+ open */
+ mgr = modest_runtime_get_window_mgr ();
+ iter = tny_list_create_iterator (headers);
+ while (!tny_iterator_is_done (iter)) {
+ ModestWindow *window;
+ TnyHeader *header;
+
+ header = TNY_HEADER (tny_iterator_get_current (iter));
+ window = modest_window_mgr_find_window_by_msguid (mgr, tny_header_get_uid (header));
+ if (window)
+ tny_list_remove (headers, G_OBJECT (header));
+
+ g_object_unref (header);
+ tny_iterator_next (iter);
+ }
+
+ /* Open each message */
+ mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, G_OBJECT (win));
+ modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
+ modest_mail_operation_get_msgs_full (mail_op,
+ headers,
+ open_msg_cb,
+ NULL,
+ NULL);
+
+ /* Clean */
+ g_object_unref(mail_op);
+}
+
+void
+modest_ui_actions_on_open (GtkAction *action, ModestWindow *win)
+{
+ TnyList *headers;
+
+ /* Get headers */
+ headers = get_selected_headers (win);
+ if (!headers)
+ return;
+
+ /* Open them */
+ _modest_ui_actions_open (headers, win);
+
+ g_object_unref(headers);
+}
+
+