+static void
+find_msg_async_cb (TnyFolder *folder,
+ gboolean cancelled,
+ TnyMsg *msg,
+ GError *err,
+ gpointer user_data)
+{
+ TnyHeader *header = NULL;
+ gchar *msg_uid = NULL;
+ ModestWindowMgr *win_mgr;
+ ModestWindow *msg_view = NULL;
+ gboolean is_draft = FALSE;
+ OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
+
+ if (err || cancelled) {
+ modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
+ g_idle_add (notify_error_in_dbus_callback, NULL);
+ goto end;
+ }
+
+ header = tny_msg_get_header (msg);
+ if (!header || (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
+ modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
+ g_idle_add (notify_error_in_dbus_callback, NULL);
+ goto end;
+ }
+
+ msg_uid = modest_tny_folder_get_header_unique_id (header);
+ win_mgr = modest_runtime_get_window_mgr ();
+
+ if (modest_tny_folder_is_local_folder (folder) &&
+ (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
+ is_draft = TRUE;
+ }
+
+ if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
+ gtk_window_present (GTK_WINDOW(msg_view));
+ } else {
+ const gchar *modest_account_name;
+ TnyAccount *account;
+
+ modest_window_mgr_register_header (win_mgr, header, NULL);
+
+ account = tny_folder_get_account (folder);
+ if (account) {
+ modest_account_name =
+ modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+ } else {
+ modest_account_name = NULL;
+ }
+
+ /* Drafts will be opened in the editor, and others will be opened in the viewer */
+ if (is_draft) {
+ gchar *modest_account_name = NULL;
+ gchar *mailbox = NULL;
+ gchar *from_header;
+
+ /* we cannot edit without a valid account... */
+ if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
+ if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
+ modest_window_mgr_unregister_header (win_mgr,
+ header);
+ goto end;
+ }
+ }
+
+ from_header = tny_header_dup_from (header);
+ modest_account_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
+ g_free (from_header);
+
+ if (modest_account_name == NULL) {
+ ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
+ modest_account_name = modest_account_mgr_get_default_account (mgr);
+ }
+ msg_view = modest_msg_edit_window_new (msg, modest_account_name, mailbox, TRUE);
+ if (mailbox)
+ g_free (mailbox);
+ g_free (modest_account_name);
+ } else {
+ TnyHeader *header;
+ const gchar *modest_account_name;
+
+ if (account) {
+ modest_account_name =
+ modest_tny_account_get_parent_modest_account_name_for_server_account (account);
+ } else {
+ modest_account_name = NULL;
+ }