* all:
[modest] / src / modest-main.c
index 7446ef5..05ebd48 100644 (file)
 #include <modest-tny-platform-factory.h>
 #include <modest-mail-operation.h>
 
-static int start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
-                    const gchar* subject, const gchar *body);
-static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
-                     const gchar* subject, const gchar *body);
-
+#include <modest-account-mgr.h>
+#include <modest-account-mgr-helpers.h>
+
+static gchar*  check_account (const gchar *account);
+static int     start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
+                        const gchar* subject, const gchar *body);
+static int     send_mail (const gchar* account,
+                         const gchar* mailto, const gchar *cc, const gchar *bcc,
+                         const gchar* subject, const gchar *body);
 int
 main (int argc, char *argv[])
 {
@@ -61,8 +65,9 @@ main (int argc, char *argv[])
        int retval  = MODEST_ERR_NONE;
                
        static gboolean batch = FALSE;
-       static gchar    *mailto, *subject, *bcc, *cc, *body, *account;
-
+       static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
+               *cc=NULL, *body=NULL, *account=NULL;
+       
        static GOptionEntry options[] = {
                { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
                  N_("New email to <addresses> (comma-separated)"), NULL},
@@ -75,7 +80,7 @@ main (int argc, char *argv[])
                { "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 specified, default account is used)"), NULL},
+                 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 }
@@ -93,6 +98,7 @@ main (int argc, char *argv[])
                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;
                goto cleanup;
        }
@@ -105,10 +111,27 @@ main (int argc, char *argv[])
                        goto cleanup;
                } else
                        retval = start_ui (mailto, cc, bcc, subject, body);
-       } else 
-               retval = send_mail (mailto, cc, bcc, subject, body);
+       } else {
+               gchar *account_or_default;
+               account_or_default = check_account (account);
+               g_free (account);
+
+               if (!account_or_default) {
+                       g_printerr ("modest: account is not valid\n");
+                       retval = MODEST_ERR_PARAM;
+                       goto cleanup;
+               } 
+               retval = send_mail (account, 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_runtime_uninit ()) 
                g_printerr ("modest: modest_runtime_uninit failed\n");
 
@@ -148,52 +171,65 @@ start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
        return retval;
 }
 
+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 (!modest_account_mgr_account_exists (account_mgr, account, FALSE, NULL)) {
+               g_free (retval);
+               retval = NULL;
+       }
+       return retval;
+}
 
 static int
-send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
+send_mail (const gchar* account_name,
+          const gchar* mailto, const gchar *cc, const gchar *bcc,
           const gchar* subject, const gchar *body)
 {
-       ModestMailOperation *mail_operation = NULL;
-       TnyList *accounts = NULL;
-       TnyIterator *iter = NULL;
-       TnyTransportAccount *account = NULL;    
        int retval;
-
-       accounts = TNY_LIST(tny_simple_list_new ());
-       tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
-                                       accounts,
-                                       TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
-
-       iter = tny_list_create_iterator(accounts);
-       tny_iterator_first (iter);
-       if (tny_iterator_is_done (iter)) {
-               g_printerr("modest: no transport accounts defined\n");
-               retval = MODEST_ERR_SEND;
-               goto cleanup;
+       TnyTransportAccount *account;
+       ModestMailOperation *mail_operation = NULL;
+       ModestAccountMgr    *account_mgr;
+       gchar               *from_string;
+       
+       g_return_val_if_fail (account_name, MODEST_ERR_SEND);
+
+       account_mgr = modest_runtime_get_account_mgr ();        
+       account = modest_account_mgr_get_tny_account (account_mgr, account_name,
+                                                     TNY_ACCOUNT_TYPE_TRANSPORT);      
+       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 (account_mgr, account_name);
 
-       account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
        mail_operation = modest_mail_operation_new ();
-
-       modest_mail_operation_send_new_mail (mail_operation,
-                                            account, "test@example.com",
-                                            mailto, cc, bcc, 
-                                            subject, body, NULL);
-               
+       modest_mail_operation_send_new_mail (mail_operation, account,
+                                            from_string, mailto,
+                                            cc, bcc, subject, body,
+                                            NULL);
        if (modest_mail_operation_get_status (mail_operation) == 
            MODEST_MAIL_OPERATION_STATUS_FAILED) {
                retval = MODEST_ERR_SEND;
-               goto cleanup;
        } else
                retval = MODEST_ERR_NONE; /* hurray! */
-cleanup:
-       if (iter)
-               g_object_unref (G_OBJECT (iter));
-       if (accounts)
-               g_object_unref (G_OBJECT (accounts));
-       if (mail_operation)
-               g_object_unref (G_OBJECT (mail_operation));
+
+       g_object_unref (G_OBJECT(mail_operation));
+       g_free (from_string);
+       
        return retval;
 }
 
+