* first part of changes to make batch operation work correctly
[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 #include <modest-account-mgr-helpers.h>
50
51 static gchar*  check_account (ModestAccountMgr* account_mgr, const gchar *account);
52 TnyAccount*    get_transport_account (ModestAccountMgr *account_mgr,
53                                       ModestAccountData *account_data,
54                                       const gchar *account);
55
56 static int     start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
57                          const gchar* subject, const gchar *body);
58 static int     send_mail (const gchar* account,
59                           const gchar* mailto, const gchar *cc, const gchar *bcc,
60                           const gchar* subject, const gchar *body);
61 int
62 main (int argc, char *argv[])
63 {
64         GOptionContext     *context = NULL;
65         
66         GError *err = NULL;
67         int retval  = MODEST_ERR_NONE;
68                 
69         static gboolean batch = FALSE;
70         static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
71                         *cc=NULL, *body=NULL, *account=NULL;
72
73         static GOptionEntry options[] = {
74                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
75                   N_("New email to <addresses> (comma-separated)"), NULL},
76                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
77                   N_("Subject for a new mail"), NULL},
78                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
79                   N_("Body for a new email"), NULL},
80                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
81                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
82                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
83                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
84                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
85                   N_("Account to use (if none specified, the default account will be used)"), NULL},
86                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
87                   N_("Run in batch mode (don't show UI)"), NULL},
88                 { NULL, 0, 0, 0, NULL, NULL, NULL }
89         };
90
91         if (!modest_runtime_init ()) {
92                 g_printerr ("modest: cannot init runtime\n");
93                 return MODEST_ERR_INIT;
94         }
95         
96         context = g_option_context_new (NULL);
97         g_option_context_add_main_entries (context, options, NULL);
98         
99         if (!g_option_context_parse (context, &argc, &argv, &err)) {
100                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
101                             err ? err->message : "");
102                 g_error_free (err);
103                 g_option_context_free (context);
104                 retval = MODEST_ERR_OPTIONS;
105                 goto cleanup;
106         }
107         g_option_context_free (context);
108
109         
110         if (!batch) {
111                 if (!modest_runtime_init_ui (argc, argv)) {
112                         g_printerr ("modest: cannot start UI\n");
113                         retval = MODEST_ERR_UI;
114                         goto cleanup;
115                 } else
116                         retval = start_ui (mailto, cc, bcc, subject, body);
117         } else 
118                 retval = send_mail (account, mailto, cc, bcc, subject, body);
119         
120 cleanup:
121         g_free (mailto);
122         g_free (subject);
123         g_free (bcc);
124         g_free (cc);
125         g_free (body);
126         g_free (account);
127
128         if (!modest_runtime_uninit ()) 
129                 g_printerr ("modest: modest_runtime_uninit failed\n");
130
131         return retval;
132 }
133
134
135 static int
136 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
137           const gchar* subject, const gchar *body)
138 {
139         ModestWindow *win = NULL;
140         ModestUI *modest_ui = NULL;
141         
142         gint retval = 0;
143         modest_ui = modest_ui_new ();
144
145         if (mailto||cc||bcc||subject||body) {
146                 g_warning ("FIXME: implement this");
147 /*              ok = modest_ui_new_edit_window (modest_ui, */
148 /*                                              mailto,  /\* to *\/ */
149 /*                                              cc,      /\* cc *\/ */
150 /*                                              bcc,     /\* bcc *\/ */
151 /*                                              subject,    /\* subject *\/ */
152 /*                                              body,    /\* body *\/ */
153 /*                                              NULL);   /\* attachments *\/ */
154         } else 
155                 win = modest_ui_main_window (modest_ui);
156         
157         if (win) {
158                 gtk_widget_show_all (GTK_WIDGET (win));
159                 gtk_main();
160         }
161         if (modest_ui)
162                 g_object_unref (G_OBJECT(modest_ui));
163         
164         return retval;
165 }
166
167
168 static int
169 send_mail (const gchar* account_,
170            const gchar* mailto, const gchar *cc, const gchar *bcc,
171            const gchar* subject, const gchar *body)
172 {
173         ModestAccountMgr *account_mgr;
174         ModestMailOperation *mail_operation = NULL;
175         TnyTransportAccount *account = NULL;
176         ModestAccountData *account_data;
177         gchar *account_name = NULL, *sender_name = NULL;
178         int retval;
179
180         account_mgr = modest_runtime_get_account_mgr ();
181         
182         account_name = check_account (account_mgr, account_);   
183         if (!account_name) 
184                 return MODEST_ERR_SEND;
185
186         account_data = modest_account_mgr_get_account_data (account_mgr, account_name);
187         if (!account_data) {
188                 g_printerr ("modest: cannot get account data for %s\n", account_name);
189                 g_free (account_name);
190                 return MODEST_ERR_SEND;
191         }
192         account = TNY_TRANSPORT_ACCOUNT(get_transport_account (account_mgr, account_data,
193                                                                account_name));
194         if (!account) {
195                 g_printerr ("modest: cannot get transport account for %s\n", account_name);
196                 g_free (account_name);
197                 modest_account_mgr_free_account_data (account_mgr, account_data);
198                 return MODEST_ERR_SEND;
199         }
200
201         sender_name = g_strdup_printf ("%s <%s>",
202                                        account_data->fullname ?  account_data->fullname : "",
203                                        account_data->email    ?  account_data->email : "");
204
205         mail_operation = modest_mail_operation_new ();
206         modest_mail_operation_send_new_mail (mail_operation,
207                                              account,
208                                              sender_name,
209                                              mailto,
210                                              cc, bcc, subject, body,
211                                              NULL);
212
213         if (modest_mail_operation_get_status (mail_operation) == 
214             MODEST_MAIL_OPERATION_STATUS_FAILED) {
215                 retval = MODEST_ERR_SEND;
216         } else
217                 retval = MODEST_ERR_NONE; /* hurray! */
218
219         g_free (sender_name);
220         g_free (account_name);
221         modest_account_mgr_free_account_data (account_mgr, account_data);
222                         
223         return retval;
224 }
225
226
227
228                 
229 static gchar*
230 check_account (ModestAccountMgr* account_mgr, const gchar *account)
231 {
232         gchar *account_or_default;
233
234         if (account)
235                 account_or_default = g_strdup(account);
236         else {
237                 account_or_default = modest_account_mgr_get_default_account (account_mgr);
238                 if (!account_or_default) {
239                         g_printerr ("modest: no default account has been defined\n");
240                         return NULL;
241                 }
242         }               
243
244         if (!modest_account_mgr_account_exists (account_mgr, account_or_default, FALSE, NULL)) {
245                 g_printerr ("modest: account %s is undefined\n", account_or_default);
246                 return NULL;
247         }
248
249         return account_or_default;
250 }
251
252
253 TnyAccount*
254 get_transport_account (ModestAccountMgr *account_mgr,
255                        ModestAccountData *account_data,
256                        const gchar *account_name)
257 {
258         TnyAccount *account;
259         
260         if (!account_data->transport_account || !account_data->transport_account->account_name) {
261                 g_printerr ("modest: no transport account defined for %s\n", account_name);
262                 return NULL;
263         }
264
265         account = (modest_tny_account_store_get_tny_account_from_server_account (
266                            modest_runtime_get_account_store(),
267                            account_data->transport_account->account_name));
268         
269         if (!TNY_IS_TRANSPORT_ACCOUNT(account)) {
270                 g_printerr ("modest: no valid transport account defined for %s\n", account_name);
271                 g_object_unref (G_OBJECT(account));
272                 return NULL;
273         }
274
275         return account;
276 }
277
278