* all:
[modest] / src / modest-main.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gtk/gtkwidget.h>
35
36 #include <tny-list.h>
37 #include <tny-transport-account.h>
38 #include <tny-account-store.h>
39 #include <tny-list.h>
40 #include <tny-simple-list.h>
41
42 #include <modest-runtime.h>
43 #include <modest-defs.h>
44 #include <modest-ui.h>
45 #include <modest-icon-factory.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-tny-platform-factory.h>
48 #include <modest-mail-operation.h>
49
50 #include <modest-account-mgr.h>
51 #include <modest-account-mgr-helpers.h>
52
53 static gchar*  check_account (const gchar *account);
54 static int     start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
55                          const gchar* subject, const gchar *body);
56 static int     send_mail (const gchar* account,
57                           const gchar* mailto, const gchar *cc, const gchar *bcc,
58                           const gchar* subject, const gchar *body);
59 int
60 main (int argc, char *argv[])
61 {
62         GOptionContext     *context = NULL;
63         
64         GError *err = NULL;
65         int retval  = MODEST_ERR_NONE;
66                 
67         static gboolean batch = FALSE;
68         static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
69                 *cc=NULL, *body=NULL, *account=NULL;
70         
71         static GOptionEntry options[] = {
72                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
73                   N_("New email to <addresses> (comma-separated)"), NULL},
74                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
75                   N_("Subject for a new mail"), NULL},
76                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
77                   N_("Body for a new email"), NULL},
78                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
79                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
80                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
81                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
82                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
83                   N_("Account to use (if none specified, the default account will be used)"), NULL},
84                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
85                   N_("Run in batch mode (don't show UI)"), NULL},
86                 { NULL, 0, 0, 0, NULL, NULL, NULL }
87         };
88
89         if (!modest_runtime_init ()) {
90                 g_printerr ("modest: cannot init runtime\n");
91                 return MODEST_ERR_INIT;
92         }
93         
94         context = g_option_context_new (NULL);
95         g_option_context_add_main_entries (context, options, NULL);
96         
97         if (!g_option_context_parse (context, &argc, &argv, &err)) {
98                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
99                             err ? err->message : "");
100                 g_error_free (err);
101                 g_option_context_free (context);
102                 retval = MODEST_ERR_OPTIONS;
103                 goto cleanup;
104         }
105         g_option_context_free (context);
106         
107         if (!batch) {
108                 if (!modest_runtime_init_ui (argc, argv)) {
109                         g_printerr ("modest: cannot start UI\n");
110                         retval = MODEST_ERR_UI;
111                         goto cleanup;
112                 } else
113                         retval = start_ui (mailto, cc, bcc, subject, body);
114         } else {
115                 gchar *account_or_default;
116                 account_or_default = check_account (account);
117                 g_free (account);
118
119                 if (!account_or_default) {
120                         g_printerr ("modest: account is not valid\n");
121                         retval = MODEST_ERR_PARAM;
122                         goto cleanup;
123                 } 
124                 retval = send_mail (account, mailto, cc, bcc, subject, body);
125         }
126         
127 cleanup:
128         g_free (mailto);
129         g_free (subject);
130         g_free (bcc);
131         g_free (cc);
132         g_free (body);
133         g_free (account);
134
135         if (!modest_runtime_uninit ()) 
136                 g_printerr ("modest: modest_runtime_uninit failed\n");
137
138         return retval;
139 }
140
141
142 static int
143 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
144           const gchar* subject, const gchar *body)
145 {
146         ModestWindow *win = NULL;
147         ModestUI *modest_ui = NULL;
148         
149         gint retval = 0;
150         modest_ui = modest_ui_new ();
151
152         if (mailto||cc||bcc||subject||body) {
153                 g_warning ("FIXME: implement this");
154 /*              ok = modest_ui_new_edit_window (modest_ui, */
155 /*                                              mailto,  /\* to *\/ */
156 /*                                              cc,      /\* cc *\/ */
157 /*                                              bcc,     /\* bcc *\/ */
158 /*                                              subject,    /\* subject *\/ */
159 /*                                              body,    /\* body *\/ */
160 /*                                              NULL);   /\* attachments *\/ */
161         } else 
162                 win = modest_ui_main_window (modest_ui);
163         
164         if (win) {
165                 gtk_widget_show_all (GTK_WIDGET (win));
166                 gtk_main();
167         }
168         if (modest_ui)
169                 g_object_unref (G_OBJECT(modest_ui));
170         
171         return retval;
172 }
173
174 static gchar*
175 check_account (const gchar* account)
176 {
177         gchar *retval;
178         ModestAccountMgr *account_mgr;
179
180         account_mgr = modest_runtime_get_account_mgr();
181         
182         if (!account)
183                 retval = modest_account_mgr_get_default_account (account_mgr);
184         else
185                 retval = g_strdup (account);
186
187         /* sanity check */
188         if (!modest_account_mgr_account_exists (account_mgr, account, FALSE, NULL)) {
189                 g_free (retval);
190                 retval = NULL;
191         }
192         return retval;
193 }
194
195 static int
196 send_mail (const gchar* account_name,
197            const gchar* mailto, const gchar *cc, const gchar *bcc,
198            const gchar* subject, const gchar *body)
199 {
200         int retval;
201         TnyTransportAccount *account;
202         ModestMailOperation *mail_operation = NULL;
203         ModestAccountMgr    *account_mgr;
204         gchar               *from_string;
205         
206         g_return_val_if_fail (account_name, MODEST_ERR_SEND);
207
208         account_mgr = modest_runtime_get_account_mgr ();        
209         account = modest_account_mgr_get_tny_account (account_mgr, account_name,
210                                                       TNY_ACCOUNT_TYPE_TRANSPORT);      
211         if (!account) {
212                 g_printerr ("modest: no transport defined account for %s\n",
213                             account_name);
214                 return MODEST_ERR_SEND;
215         }
216         from_string = modest_account_mgr_get_from_string (account_mgr, account_name);
217
218         mail_operation = modest_mail_operation_new ();
219         modest_mail_operation_send_new_mail (mail_operation, account,
220                                              from_string, mailto,
221                                              cc, bcc, subject, body,
222                                              NULL);
223         if (modest_mail_operation_get_status (mail_operation) == 
224             MODEST_MAIL_OPERATION_STATUS_FAILED) {
225                 retval = MODEST_ERR_SEND;
226         } else
227                 retval = MODEST_ERR_NONE; /* hurray! */
228
229         g_object_unref (G_OBJECT(mail_operation));
230         g_free (from_string);
231         
232         return retval;
233 }
234
235