From db884009546eaa55ea71bcd46db03931dff08b98 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 30 Jun 2007 16:39:14 +0000 Subject: [PATCH] * remove unneeded stuff from modest-main (the batch mode stuff which wasn't working anyway * cleanup the initialization, which should make it more clear what happens * don't do separate ui/core init pmo-trunk-r2510 --- src/modest-init.c | 27 +++-- src/modest-init.h | 14 +-- src/modest-main.c | 297 +++++------------------------------------------------ 3 files changed, 42 insertions(+), 296 deletions(-) diff --git a/src/modest-init.c b/src/modest-init.c index 99e183f..8f4108e 100644 --- a/src/modest-init.c +++ b/src/modest-init.c @@ -61,6 +61,7 @@ static void init_debug_g_type (void); static void init_debug_logging (void); static void init_default_settings (ModestConf *conf); static void init_device_name (ModestConf *conf); +static gboolean init_ui (gint argc, gchar** argv); /* * defaults for the column headers @@ -171,8 +172,9 @@ modest_init_get_default_header_view_column_ids (TnyFolderType folder_type, Modes return result; } + gboolean -modest_init_init_core (int argc, char *argv[]) +modest_init (int argc, char *argv[]) { gboolean reset; static gboolean invoked = FALSE; @@ -183,7 +185,7 @@ modest_init_init_core (int argc, char *argv[]) return FALSE; } else invoked = TRUE; - + init_i18n(); init_debug_g_type(); init_debug_logging(); @@ -200,11 +202,9 @@ modest_init_init_core (int argc, char *argv[]) g_printerr ("modest: failed to initialize the modest runtime\n"); return FALSE; } - /* do an initial guess for the device name */ init_device_name (modest_runtime_get_conf()); - if (!modest_platform_init(argc, argv)) { modest_init_uninit (); @@ -231,20 +231,21 @@ modest_init_init_core (int argc, char *argv[]) modest_init_uninit (); g_printerr ("modest: failed to init default account\n"); return FALSE; + } + + if (!init_ui (argc, argv)) { + modest_init_uninit (); + g_printerr ("modest: failed to init ui\n"); + return FALSE; } return TRUE; } -gboolean -modest_init_init_ui (gint argc, gchar** argv) +static gboolean +init_ui (gint argc, gchar** argv) { - if (!gtk_init_check(&argc, &argv)) { - g_printerr ("modest: failed to initialize graphical ui\n"); - return FALSE; - } - /* Set application name */ g_set_application_name (modest_platform_get_app_name()); /* g_message (modest_platform_get_app_name()); */ @@ -252,12 +253,10 @@ modest_init_init_ui (gint argc, gchar** argv) /* Init stock icons */ init_stock_icons (); - /* Init notification system */ + /* Init notification system */ #ifdef MODEST_HAVE_HILDON_NOTIFY notify_init ("Basics"); #endif - - return TRUE; } diff --git a/src/modest-init.h b/src/modest-init.h index c0eede7..bd99e35 100644 --- a/src/modest-init.h +++ b/src/modest-init.h @@ -49,22 +49,10 @@ G_BEGIN_DECLS * * TRUE if this succeeded, FALSE otherwise. */ -gboolean modest_init_init_core (int argc, char *argv[]); +gboolean modest_init (int argc, char *argv[]); /** - * modest_init_init_ui: - * @argc: the #argc argument to the main function - * @argv: the #argv argument to the main function - * - * initialize the modest UI; this replaces the call to - * gtk_init - * - * TRUE if this succeeded, FALSE otherwise. - */ -gboolean modest_init_init_ui (gint argc, gchar** argv); - -/** * modest_init_uninit: * * uninitialize the modest runtime system; free all the diff --git a/src/modest-main.c b/src/modest-main.c index 15a51ac..7ff8e9a 100644 --- a/src/modest-main.c +++ b/src/modest-main.c @@ -30,298 +30,57 @@ #include #include -#include -#include - -#include -#include -#include -#include -#include - #include -#include #include - -#include -#include -#include -#include -#include -#include -#include -#include - +#include #include -#include - -typedef enum { - MODEST_ERR_NONE = 0, /* no error */ - MODEST_ERR_OPTIONS = 1, /* error in the options */ - MODEST_ERR_CONF = 2, /* error getting confuration db */ - MODEST_ERR_UI = 3, /* error in the UI */ - MODEST_ERR_HILDON = 4, /* error with Hildon (maemo-only) */ - MODEST_ERR_RUN = 5, /* error running */ - MODEST_ERR_SEND = 6, /* error sending mail */ - MODEST_ERR_PARAM = 7, /* error in one or more of the parameters */ - MODEST_ERR_INIT = 8 /* error in initialization */ -} ModestErrorCode; - -static gchar* check_account (const gchar *account); - -static ModestErrorCode start_ui (const gchar *account, - const gchar* mailto, const gchar *cc, - const gchar *bcc, const gchar* subject, const gchar *body, GtkWidget **ui); - -static ModestErrorCode send_mail (const gchar* account, - const gchar* mailto, const gchar *cc, const gchar *bcc, - const gchar* subject, const gchar *body); - -static void -on_show (GtkWidget *widget, gpointer user_data) -{ - /* This key is not used by any preferences UI and - * I don't think this behavior is specified in the UI specification. murrayc: */ -/* - if (modest_conf_get_bool (modest_runtime_get_conf(), - MODEST_CONF_CONNECT_AT_STARTUP, NULL)) - modest_platform_connect_and_wait(NULL); -*/ -} int main (int argc, char *argv[]) { - GOptionContext *context = NULL; - - GError *err = NULL; - int retval = MODEST_ERR_NONE; + ModestWindow *win; + int retval = 0; - static gboolean batch = FALSE; - static gchar *mailto=NULL, *subject=NULL, *bcc=NULL, - *cc=NULL, *body=NULL, *account=NULL; - gchar *account_or_default; - static GOptionEntry options[] = { - { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto, - N_("New email to (comma-separated)"), NULL}, - { "subject", 's', 0, G_OPTION_ARG_STRING, &subject, - N_("Subject for a new mail"), NULL}, - { "body", 'b', 0, G_OPTION_ARG_STRING, &body, - N_("Body for a new email"), NULL}, - { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc, - N_("Cc: addresses for a new mail (comma-separated)"), NULL}, - { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc, - N_("Bcc: addresses for a new mail (comma-separated)"), NULL}, - { "account", 'a', 0, G_OPTION_ARG_STRING, &account, - N_("Account to use (if none specified, the default account will be used)"), NULL}, - { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch, - N_("Run in batch mode (don't show UI)"), NULL}, - { NULL, 0, 0, 0, NULL, NULL, NULL } - }; - - /* Enable threads before anything else, to prevent memory corruption */ if (!g_thread_supported()) - g_thread_init(NULL); + g_thread_init (NULL); + gdk_threads_init (); + gdk_threads_enter (); - context = g_option_context_new (NULL); - g_option_context_add_main_entries (context, options, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &err)) { - g_printerr ("modest: error in command line parameter(s): '%s', exiting\n", - err ? err->message : ""); - g_error_free (err); - g_option_context_free (context); - retval = MODEST_ERR_OPTIONS; + if (!gtk_init_check(&argc, &argv)) { + g_printerr ("modest: failed to initialize gtk\n"); + retval = 1; goto cleanup; } - g_option_context_free (context); - - if (!modest_init_init_core (argc, argv)) { - g_printerr ("modest: cannot init modest core\n"); - return MODEST_ERR_INIT; - } - - account_or_default = check_account (account); - g_free (account); - - if (!batch) { - gdk_threads_enter (); - if (!modest_init_init_ui (argc, argv)) { - g_printerr ("modest: cannot init modest ui\n"); - retval = MODEST_ERR_UI; - gdk_threads_leave (); - goto cleanup; - } else { - GtkWidget *ui = NULL; - retval = start_ui (account_or_default, - mailto, cc, bcc, subject, body, &ui); - if (G_IS_OBJECT(ui)) - g_signal_connect (G_OBJECT (ui), "show", G_CALLBACK(on_show), NULL); - } - } else { - if (!account_or_default) { - g_printerr ("modest: no account has been defined yet\n"); - retval = MODEST_ERR_CONF; - goto cleanup; - } - retval = send_mail (account_or_default, - mailto, cc, bcc, subject, body); - } - -cleanup: - g_free (mailto); - g_free (subject); - g_free (bcc); - g_free (cc); - g_free (body); - g_free (account); - - if (!modest_init_uninit ()) - g_printerr ("modest: modest_init_uninit failed\n"); - - return retval; -} - - -static ModestErrorCode -start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc, - const gchar* subject, const gchar *body, GtkWidget **ui) -{ - ModestWindow *win = NULL; - - if (mailto||cc||bcc||subject||body) { - gchar *from; - TnyMsg *msg; - TnyFolder *folder; - TnyAccount *account; - - if (!account_name) { - g_printerr ("modest: no valid account provided, " - "nor is default one available\n"); - return MODEST_ERR_PARAM; - } - from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(), - account_name); - msg = modest_tny_msg_new (mailto,from,cc,bcc,subject,body,NULL); - if (!msg) { - g_printerr ("modest: failed to create message\n"); - g_free (from); - return MODEST_ERR_SEND; - } - 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\n"); - g_free (from); - g_object_unref (G_OBJECT(msg)); - return MODEST_ERR_SEND; - } - - folder = modest_tny_account_get_special_folder (account, - TNY_FOLDER_TYPE_DRAFTS); - if (!folder) { - g_printerr ("modest: failed to find Drafts folder\n"); - g_free (from); - g_object_unref (G_OBJECT(msg)); - g_object_unref (G_OBJECT(account)); - return MODEST_ERR_SEND; - } - tny_folder_add_msg (folder, msg, NULL); /* FIXME: check err */ + if (!modest_init (argc, argv)) { + g_printerr ("modest: cannot init modest\n"); + retval = 1; + goto cleanup; + } - win = modest_msg_edit_window_new (msg, account_name); + win = modest_main_window_new (); + gtk_widget_show_all (GTK_WIDGET(win)); - g_object_unref (G_OBJECT(msg)); - g_object_unref (G_OBJECT(account)); - g_object_unref (G_OBJECT(folder)); - g_free (from); - } else - win = modest_main_window_new (); - if (!win) { - g_printerr ("modest: failed to create window\n"); - return MODEST_ERR_UI; - } else { - ModestWindowMgr *mgr = modest_runtime_get_window_mgr (); - modest_window_mgr_register_window (mgr, win); - *ui = (GtkWidget*) win; - g_object_unref (win); - } - - gtk_widget_show_all (GTK_WIDGET (win)); - gtk_main(); - gdk_threads_leave (); - - return MODEST_ERR_NONE; -} - -static gchar* -check_account (const gchar* account) -{ - gchar *retval; - ModestAccountMgr *account_mgr; - - account_mgr = modest_runtime_get_account_mgr(); - - if (!account) - retval = modest_account_mgr_get_default_account (account_mgr); - else - retval = g_strdup (account); - - /* sanity check */ - if (!account || !modest_account_mgr_account_exists (account_mgr, account, FALSE)) { - g_free (retval); - retval = NULL; + g_printerr ("modest: failed to create main window\n"); + retval = 1; + goto cleanup; } - return retval; -} - -static ModestErrorCode -send_mail (const gchar* account_name, - const gchar* mailto, const gchar *cc, const gchar *bcc, - const gchar* subject, const gchar *body) -{ - int retval; - TnyTransportAccount *account; - ModestMailOperation *mail_operation = NULL; - gchar *from_string; - g_return_val_if_fail (account_name, MODEST_ERR_SEND); - - ////////////////////// FIXME //////// - modest_runtime_not_implemented (NULL); - return MODEST_ERR_NONE; - ////////////////////////////////////// + modest_window_mgr_register_window (modest_runtime_get_window_mgr(), + win); + g_object_unref (win); - account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_transport_account_for_open_connection - (modest_runtime_get_account_store(), account_name)); - if (!account) { - g_printerr ("modest: no transport defined account for %s\n", - account_name); - return MODEST_ERR_SEND; - } - from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(), - account_name); + gtk_main (); + retval = 1; - mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL); +cleanup: + gdk_threads_leave (); - modest_mail_operation_send_new_mail (mail_operation, account, - NULL, - from_string, mailto, - cc, bcc, subject, body, NULL /* html_body */, - NULL /* attachments */, 0 /* priority */); - if (modest_mail_operation_get_status (mail_operation) == - MODEST_MAIL_OPERATION_STATUS_FAILED) { - retval = MODEST_ERR_SEND; - } else - retval = MODEST_ERR_NONE; /* hurray! */ - - g_object_unref (G_OBJECT(account)); - g_object_unref (G_OBJECT(mail_operation)); - g_free (from_string); + if (!modest_init_uninit ()) + g_printerr ("modest: modest_init_uninit failed\n"); return retval; } - -- 1.7.9.5