From 8007ef7aa2b58f038b99691f402d32fa6c01f8f5 Mon Sep 17 00:00:00 2001 From: Alberto Garcia Date: Mon, 22 Oct 2007 14:22:21 +0000 Subject: [PATCH] Refactored the code of modest_ui_actions_on_new_msg() and the ComposeMessage DBUS handler pmo-trunk-r3553 --- src/dbus_api/modest-dbus-callbacks.c | 140 ++++++++++------------------------ src/modest-ui-actions.c | 118 ++++++++++++++-------------- src/modest-ui-actions.h | 17 +++++ 3 files changed, 118 insertions(+), 157 deletions(-) diff --git a/src/dbus_api/modest-dbus-callbacks.c b/src/dbus_api/modest-dbus-callbacks.c index 83c8dbd..032b8ec 100644 --- a/src/dbus_api/modest-dbus-callbacks.c +++ b/src/dbus_api/modest-dbus-callbacks.c @@ -258,12 +258,11 @@ on_idle_compose_mail(gpointer user_data) return FALSE; ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data; - gchar **list = NULL; - gint i = 0; + ModestWindow *win = NULL; + TnyMsg *msg = NULL; /* Get the TnyTransportAccount so we can instantiate a mail operation: */ - ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr(); - gchar *account_name = modest_account_mgr_get_default_account (account_mgr); + gchar *account_name = modest_account_mgr_get_default_account(modest_runtime_get_account_mgr()); if (!account_name) { g_printerr ("modest: no account found.\n"); @@ -272,104 +271,43 @@ on_idle_compose_mail(gpointer user_data) * until the account exists instead of just failing. */ } - - TnyAccount *account = NULL; - if (account_name && account_mgr) { - account = modest_tny_account_store_get_transport_account_for_open_connection ( - modest_runtime_get_account_store(), account_name); - } - - if (!account) { - g_printerr ("modest: failed to get tny account folder'%s'\n", account_name); - } else { - gchar * from = modest_account_mgr_get_from_string (account_mgr, - account_name); - if (!from) { - g_printerr ("modest: no from address for account '%s'\n", account_name); - } else { - /* Get the signature. - * TODO: This, like much of this function is copy/pasted from - * modest_ui_actions_on_new_msg(): */ - gboolean use_signature = FALSE; - gchar *signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr (), account_name, &use_signature); - - gchar* blank_and_signature = NULL; - if (use_signature) { - blank_and_signature = g_strconcat ("\n", signature, NULL); - } else { - blank_and_signature = g_strdup (""); - } - g_free (signature); - - /* Add it to the body. */ - gchar *body_with_sig = NULL; - if (!(idle_data->body)) - body_with_sig = g_strdup (blank_and_signature); - else { - body_with_sig = g_strconcat (idle_data->body, blank_and_signature, NULL); - } - - /* Create the message: */ - TnyMsg *msg = modest_tny_msg_new (idle_data->to, from, - idle_data->cc, idle_data->bcc, idle_data->subject, body_with_sig, - NULL); /* NULL because m_t_m_n doesn't use it */ - - g_free (body_with_sig); - g_free (blank_and_signature); - - if (!msg) { - g_printerr ("modest: failed to create message\n"); - } else - { - /* Add the message to a folder and show its UI for editing: */ - TnyFolder *folder = modest_tny_account_get_special_folder (account, - TNY_FOLDER_TYPE_DRAFTS); - if (!folder) { - g_printerr ("modest: failed to find Drafts folder\n"); - } else { - - tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */ - - /* This is a GDK lock because we are an idle callback and - * the code below is or does Gtk+ code */ - - gdk_threads_enter (); /* CHECKED */ - - ModestWindow *win = modest_msg_edit_window_new (msg, account_name, FALSE); - - /* it seems Sketch at least sends a leading ',' -- take that into account, - * ie strip that ,*/ - if (idle_data->attachments && idle_data->attachments[0]==',') { - gchar *tmp = g_strdup (idle_data->attachments + 1); - g_free(idle_data->attachments); - idle_data->attachments = tmp; - } - - if (idle_data->attachments != NULL) { - list = g_strsplit(idle_data->attachments, ",", 0); - for (i=0; list[i] != NULL; i++) { - modest_msg_edit_window_attach_file_one( - (ModestMsgEditWindow *)win, list[i]); - } - g_strfreev(list); - } - - modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win); - gtk_widget_show_all (GTK_WIDGET (win)); - - gdk_threads_leave (); /* CHECKED */ - - g_object_unref (G_OBJECT(folder)); - g_object_unref (win); - } - - g_object_unref (G_OBJECT(msg)); - } - - g_object_unref (G_OBJECT(account)); - } - } + msg = modest_ui_actions_create_msg(account_name, idle_data->to, idle_data->cc, + idle_data->bcc, idle_data->subject, idle_data->body); + if (msg == NULL) goto cleanup; + + /* This is a GDK lock because we are an idle callback and + * the code below is or does Gtk+ code */ + + gdk_threads_enter (); /* CHECKED */ + + win = modest_msg_edit_window_new (msg, account_name, FALSE); + + /* it seems Sketch at least sends a leading ',' -- take that into account, + * ie strip that ,*/ + if (idle_data->attachments && idle_data->attachments[0]==',') { + gchar *tmp = g_strdup (idle_data->attachments + 1); + g_free(idle_data->attachments); + idle_data->attachments = tmp; + } + if (idle_data->attachments != NULL) { + gchar **list = g_strsplit(idle_data->attachments, ",", 0); + gint i = 0; + for (i=0; list[i] != NULL; i++) { + modest_msg_edit_window_attach_file_one( + (ModestMsgEditWindow *)win, list[i]); + } + g_strfreev(list); + } + + modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win); + gtk_widget_show_all (GTK_WIDGET (win)); + + gdk_threads_leave (); /* CHECKED */ + +cleanup: + if (win) g_object_unref (win); + if (msg) g_object_unref (msg); /* Free the idle data: */ g_free (idle_data->to); g_free (idle_data->cc); diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index 7f335bc..006dc22 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -765,94 +765,100 @@ modest_ui_actions_on_smtp_servers (GtkAction *action, ModestWindow *win) #endif /* MODEST_PLATFORM_MAEMO */ } -void -modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win) +TnyMsg * +modest_ui_actions_create_msg(const gchar *account_name, + const gchar *to_str, + const gchar *cc_str, + const gchar *bcc_str, + const gchar *subject_str, + const gchar *body_str) { - ModestWindow *msg_win = NULL; TnyMsg *msg = NULL; - TnyFolder *folder = NULL; - gchar *account_name = NULL; - gchar *from_str = NULL; -/* GError *err = NULL; */ TnyAccount *account = NULL; - ModestWindowMgr *mgr; - gchar *signature = NULL, *blank_and_signature = NULL; + TnyFolder *folder = NULL; + gchar *from_str = NULL, *signature = NULL, *body = NULL; + gboolean use_signature = FALSE; + ModestAccountMgr *mgr = modest_runtime_get_account_mgr(); + ModestTnyAccountStore *store = modest_runtime_get_account_store(); - /* if there are no accounts yet, just show the wizard */ - if (!modest_account_mgr_has_accounts (modest_runtime_get_account_mgr(), TRUE)) { - if (!modest_ui_actions_run_account_setup_wizard (win)) - return; - } - - account_name = g_strdup (modest_window_get_active_account (win)); - if (!account_name) - account_name = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ()); - if (!account_name) { - g_printerr ("modest: no account found\n"); - goto cleanup; - } - - account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store(), - account_name, - TNY_ACCOUNT_TYPE_STORE); + account = modest_tny_account_store_get_server_account (store, account_name, TNY_ACCOUNT_TYPE_STORE); if (!account) { g_printerr ("modest: failed to get tnyaccount for '%s'\n", account_name); goto cleanup; } - - from_str = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(), account_name); + folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS); + if (!folder) { + g_printerr ("modest: failed to find Drafts folder\n"); + goto cleanup; + } + from_str = modest_account_mgr_get_from_string (mgr, account_name); if (!from_str) { g_printerr ("modest: failed get from string for '%s'\n", account_name); goto cleanup; } - gboolean use_signature = FALSE; - signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr (), account_name, &use_signature); - - if (use_signature) { - blank_and_signature = g_strconcat ("\n", signature, NULL); + signature = modest_account_mgr_get_signature (mgr, account_name, &use_signature); + if (body_str != NULL) { + body = use_signature ? g_strconcat(body_str, "\n", signature, NULL) : g_strdup(body_str); } else { - blank_and_signature = g_strdup (""); + body = use_signature ? g_strconcat("\n", signature, NULL) : g_strdup(""); } - g_free (signature); - - msg = modest_tny_msg_new ("", from_str, "", "", "", blank_and_signature, NULL); + msg = modest_tny_msg_new (to_str, from_str, cc_str, bcc_str, subject_str, body, NULL); if (!msg) { g_printerr ("modest: failed to create new msg\n"); goto cleanup; } - - folder = modest_tny_account_get_special_folder (account, TNY_FOLDER_TYPE_DRAFTS); - if (!folder) { - g_printerr ("modest: failed to find Drafts folder\n"); + +cleanup: + g_free (from_str); + g_free (signature); + g_free (body); + if (account) g_object_unref (G_OBJECT(account)); + if (folder) g_object_unref (G_OBJECT(folder)); + + return msg; +} + +void +modest_ui_actions_on_new_msg (GtkAction *action, ModestWindow *win) +{ + ModestWindow *msg_win = NULL; + TnyMsg *msg = NULL; + gchar *account_name = NULL; + ModestWindowMgr *win_mgr; + ModestAccountMgr *acc_mgr = modest_runtime_get_account_mgr(); + + /* if there are no accounts yet, just show the wizard */ + if (!modest_account_mgr_has_accounts (acc_mgr, TRUE)) { + if (!modest_ui_actions_run_account_setup_wizard (win)) return; + } + + account_name = g_strdup (modest_window_get_active_account (win)); + if (!account_name) account_name = modest_account_mgr_get_default_account(acc_mgr); + if (!account_name) { + g_printerr ("modest: no account found\n"); goto cleanup; } - - + msg = modest_ui_actions_create_msg(account_name, NULL, NULL, NULL, NULL, NULL); + if (msg == NULL) goto cleanup; + /* Create and register edit window */ /* This is destroyed by TODO. */ msg_win = modest_msg_edit_window_new (msg, account_name, FALSE); - mgr = modest_runtime_get_window_mgr (); - modest_window_mgr_register_window (mgr, msg_win); + win_mgr = modest_runtime_get_window_mgr (); + modest_window_mgr_register_window (win_mgr, msg_win); - if (win) + if (win) { gtk_window_set_transient_for (GTK_WINDOW (msg_win), GTK_WINDOW (win)); + } gtk_widget_show_all (GTK_WIDGET (msg_win)); 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) - g_object_unref (G_OBJECT(msg)); - if (folder) - g_object_unref (G_OBJECT(folder)); + if (msg_win) g_object_unref (msg_win); + if (msg) g_object_unref (G_OBJECT(msg)); } gboolean diff --git a/src/modest-ui-actions.h b/src/modest-ui-actions.h index 7206eab..1d6d7d7 100644 --- a/src/modest-ui-actions.h +++ b/src/modest-ui-actions.h @@ -487,5 +487,22 @@ void modest_ui_actions_on_send_queue_status_changed (ModestTnySendQueue *send_qu guint status, gpointer user_data); +/** + * Create a new TnyMsg to be used in a compose window + * @param account_name Name of the account that will be used to send this message + * @param to_str "To:" header, or NULL + * @param cc_str "Cc:" header, or NULL + * @param bcc_str "Bcc:" header, or NULL + * @param subject_str Subject of the message, or NULL + * @param body_str Body of the message (without signature), or NULL + * @return A new TnyMsg (or NULL if an error happened) + */ +TnyMsg *modest_ui_actions_create_msg(const gchar *account_name, + const gchar *to_str, + const gchar *cc_str, + const gchar *bcc_str, + const gchar *subject_str, + const gchar *body_str); + G_END_DECLS #endif /* __MODEST_UI_ACTIONS_H__ */ -- 1.7.9.5