* all:
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Wed, 24 Jan 2007 16:41:33 +0000 (16:41 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Wed, 24 Jan 2007 16:41:33 +0000 (16:41 +0000)
  - set default account on startup if it hasn't been set yet

pmo-trunk-r730

src/modest-init.c
src/modest-init.h
src/modest-main.c

index 9a5f0ba..887f551 100644 (file)
@@ -37,6 +37,8 @@
 #include <modest-local-folder-info.h>
 #include <modest-init.h>
 #include <glib/gstdio.h>
+#include <modest-account-mgr.h>
+#include <modest-account-mgr-helpers.h>
 
 typedef struct {
        ModestHeaderViewColumn col;
@@ -109,6 +111,21 @@ get_modest_conf (void)
 }
 
 
+static ModestAccountMgr*
+get_account_mgr (void)
+{
+       ModestTnyPlatformFactory *fact =
+               get_platform_factory ();
+       ModestAccountMgr *acc_mgr =
+               modest_tny_platform_factory_get_account_mgr_instance (fact);
+       if (!acc_mgr) {
+               g_printerr ("modest: cannot get modest account mgr instance\n");
+               return NULL;
+       }
+       return acc_mgr;
+}
+
+
 /* NOTE: the exact details of this format are important, as they
  * are also used in modest-widget-memory. FIXME: make a shared function
  * for this with widget-memory
@@ -227,3 +244,49 @@ modest_init_local_folders  (void)
        g_free (maildir_path);
        return TRUE;
 }
+
+
+
+static void
+free_element (gpointer data, gpointer user_data)
+{
+       g_free (data);
+}
+
+
+gboolean
+modest_init_default_account_maybe  (void)
+{
+       ModestAccountMgr *acc_mgr;
+
+       GSList *all_accounts = NULL;
+       gchar *default_account;
+       gboolean retval = TRUE;
+       
+       acc_mgr = get_account_mgr ();
+       if (!acc_mgr) {
+               g_printerr ("modest: cannot get modest account mgr\n");
+               return FALSE;
+       }
+
+       all_accounts = modest_account_mgr_account_names (acc_mgr, NULL);
+       if (all_accounts) { /* if there are any accounts, there should be a default one */
+               default_account = 
+                       modest_account_mgr_get_default_account (acc_mgr);
+               if (!default_account) {
+                       gchar *first_account;
+                       g_printerr ("modest: no default account defined\n");
+                       first_account = (gchar*)all_accounts->data;
+                       if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
+                               g_printerr ("modest: set '%s' as the default account\n",
+                                           first_account);
+                       else
+                               g_printerr ("modest: failed to set '%s' as the default account\n",
+                                           first_account);
+                       g_free (default_account);
+               }
+               g_slist_foreach (all_accounts, free_element, NULL);
+               g_slist_free    (all_accounts);
+       }
+       return retval;
+}
index 74355e8..7a536e5 100644 (file)
@@ -61,6 +61,19 @@ gboolean modest_init_header_columns (gboolean overwrite);
 gboolean modest_init_local_folders  (void);
 
 
+/**
+ * modest_init_default_account_maybe:
+ *
+ * if there are accounts defined, but there is no default account,
+ * it will be defined.
+ * 
+ * Returns: TRUE if there was a default account already,
+ *  or one has been created or there are no accounts yet,
+ *  returns FALSE in case of error
+ */
+gboolean modest_init_default_account_maybe  (void);
+
+
 G_END_DECLS
 
 #endif /*__MODEST_INIT_H__*/
index 28bf361..efb2828 100644 (file)
@@ -101,9 +101,11 @@ main (int argc, char *argv[])
 
        modest_debug_g_type_init  ();           
        modest_debug_logging_init ();
-
+       
        g_thread_init (NULL);
-       gdk_threads_init (); /* hmmm... not really needed if we're not doing */
+       modest_init_default_account_maybe ();
+
+       gdk_threads_init (); /* hmmm... not really needed if we're not doing  ui*/
        
        context = g_option_context_new (NULL);
        g_option_context_add_main_entries (context, options, NULL);
@@ -144,7 +146,7 @@ main (int argc, char *argv[])
                }
                modest_init_header_columns (factory_settings);  
                retval = start_ui (mailto, cc, bcc, subject, body, account_store);
-               
+
        } else 
                retval = send_mail (mailto, cc, bcc, subject, body);
        
@@ -153,6 +155,8 @@ cleanup:
                g_object_unref (G_OBJECT(fact));
 
        /* this will clean up account_store as well */
+
+
        return retval;
 }