2007-06-29 Murray Cumming <murrayc@murrayc.com
[modest] / src / modest-main.c
index ca96b75..15a51ac 100644 (file)
@@ -71,11 +71,24 @@ 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);
+                                      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[])
 {
@@ -106,6 +119,11 @@ main (int argc, char *argv[])
                { NULL, 0, 0, 0, NULL, NULL, NULL }
        };
 
+       /* Enable threads before anything else, to prevent memory corruption */
+       if (!g_thread_supported())
+               g_thread_init(NULL);
+       gdk_threads_init ();
+
        context = g_option_context_new (NULL);
        g_option_context_add_main_entries (context, options, NULL);
        
@@ -119,27 +137,27 @@ main (int argc, char *argv[])
        }
        g_option_context_free (context);
        
-       if (!modest_init_init_core ()) {
+       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 {
-                       if (modest_conf_get_bool (modest_runtime_get_conf(),
-                                                 MODEST_CONF_CONNECT_AT_STARTUP, NULL))
-                               modest_platform_connect_and_wait(NULL);
-                       
+                       GtkWidget *ui = NULL;
                        retval = start_ui (account_or_default,
-                                          mailto, cc, bcc, subject, body);
+                                          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) {
@@ -168,7 +186,7 @@ cleanup:
 
 static ModestErrorCode 
 start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc,
-         const gchar* subject, const gchar *body)
+         const gchar* subject, const gchar *body, GtkWidget **ui)
 {
        ModestWindow *win = NULL;
 
@@ -227,10 +245,13 @@ start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const
        } 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;
 }
@@ -283,9 +304,10 @@ send_mail (const gchar* account_name,
        from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
                                                          account_name);
 
-       mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND, NULL);
+       mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
 
        modest_mail_operation_send_new_mail (mail_operation, account,
+                                            NULL,
                                             from_string, mailto,
                                             cc, bcc, subject, body, NULL /* html_body */,
                                             NULL /* attachments */, 0 /* priority */);