7327119a4ede28aa30481131f67bc63c4a83ca83
[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-init.h>
44
45 #include <modest-defs.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-tny-platform-factory.h>
48 #include <modest-mail-operation.h>
49 #include <modest-tny-account.h>
50 #include <modest-tny-msg.h>
51 #include <modest-account-mgr.h>
52 #include <modest-account-mgr-helpers.h>
53
54 #include <widgets/modest-main-window.h>
55 #include <widgets/modest-msg-edit-window.h>
56
57 typedef enum {
58         MODEST_ERR_NONE    = 0,   /* no error */
59         MODEST_ERR_OPTIONS = 1,   /* error in the options */
60         MODEST_ERR_CONF    = 2,   /* error getting confuration db */
61         MODEST_ERR_UI      = 3,   /* error in the UI */
62         MODEST_ERR_HILDON  = 4,   /* error with Hildon (maemo-only) */
63         MODEST_ERR_RUN     = 5,   /* error running */
64         MODEST_ERR_SEND    = 6,   /* error sending mail */
65         MODEST_ERR_PARAM   = 7,   /* error in one or more of the parameters */
66         MODEST_ERR_INIT    = 8    /* error in initialization */
67 } ModestErrorCode;
68
69 static gchar*           check_account (const gchar *account);
70
71 static ModestErrorCode  start_ui      (const gchar *account,
72                                        const gchar* mailto, const gchar *cc,
73                                        const gchar *bcc, const gchar* subject, const gchar *body);
74
75 static ModestErrorCode  send_mail     (const gchar* account,
76                                        const gchar* mailto, const gchar *cc, const gchar *bcc,
77                                        const gchar* subject, const gchar *body);
78 int
79 main (int argc, char *argv[])
80 {
81         GOptionContext     *context = NULL;
82         
83         GError *err = NULL;
84         int retval  = MODEST_ERR_NONE;
85                 
86         static gboolean batch = FALSE;
87         static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
88                 *cc=NULL, *body=NULL, *account=NULL;
89         gchar *account_or_default;
90         static GOptionEntry options[] = {
91                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
92                   N_("New email to <addresses> (comma-separated)"), NULL},
93                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
94                   N_("Subject for a new mail"), NULL},
95                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
96                   N_("Body for a new email"), NULL},
97                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
98                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
99                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
100                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
101                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
102                   N_("Account to use (if none specified, the default account will be used)"), NULL},
103                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
104                   N_("Run in batch mode (don't show UI)"), NULL},
105                 { NULL, 0, 0, 0, NULL, NULL, NULL }
106         };
107
108         context = g_option_context_new (NULL);
109         g_option_context_add_main_entries (context, options, NULL);
110         
111         if (!g_option_context_parse (context, &argc, &argv, &err)) {
112                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
113                             err ? err->message : "");
114                 g_error_free (err);
115                 g_option_context_free (context);
116                 retval = MODEST_ERR_OPTIONS;
117                 goto cleanup;
118         }
119         g_option_context_free (context);
120         
121         if (!modest_init_init_core ()) {
122                 g_printerr ("modest: cannot init modest core\n");
123                 return MODEST_ERR_INIT;
124                 
125         }
126         
127         account_or_default = check_account (account);
128         g_free (account);
129         
130         if (!batch) {
131                 if (!modest_init_init_ui (argc, argv)) {
132                         g_printerr ("modest: cannot init modest ui\n");
133                         retval = MODEST_ERR_UI;
134                         goto cleanup;
135                 } else {
136                         if (modest_conf_get_bool (modest_runtime_get_conf(),
137                                                   MODEST_CONF_CONNECT_AT_STARTUP, NULL))
138                                 tny_device_force_online (modest_runtime_get_device());
139                         
140                         retval = start_ui (account_or_default,
141                                            mailto, cc, bcc, subject, body);
142                 }
143         } else {
144                 if (!account_or_default) {
145                         g_printerr ("modest: no account has been defined yet\n");
146                         retval = MODEST_ERR_CONF;
147                         goto cleanup;
148                 }       
149                 retval = send_mail (account_or_default,
150                                     mailto, cc, bcc, subject, body);
151         }
152         
153 cleanup:
154         g_free (mailto);
155         g_free (subject);
156         g_free (bcc);
157         g_free (cc);
158         g_free (body);
159         g_free (account);
160         
161         if (!modest_init_uninit ()) 
162                 g_printerr ("modest: modest_init_uninit failed\n");
163
164         return retval;
165 }
166
167
168 static ModestErrorCode 
169 start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc,
170           const gchar* subject, const gchar *body)
171 {
172         ModestWindow *win = NULL;
173
174         if (mailto||cc||bcc||subject||body) {           
175                 gchar *from;
176                 TnyMsg     *msg;
177                 TnyFolder  *folder;
178                 TnyAccount *account;
179                 
180                 if (!account_name) {
181                         g_printerr ("modest: no valid account provided, "
182                                     "nor is default one available\n");
183                         return MODEST_ERR_PARAM;
184                 }
185                 from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
186                                                            account_name);
187                 msg  = modest_tny_msg_new (mailto,from,cc,bcc,subject,body,NULL);
188                 if (!msg) {
189                         g_printerr ("modest: failed to create message\n");
190                         g_free (from);
191                         return MODEST_ERR_SEND;
192                 }
193
194                 account = modest_tny_account_store_get_tny_account_by_account (
195                         modest_runtime_get_account_store(), account_name,
196                         TNY_ACCOUNT_TYPE_TRANSPORT);
197                 if (!account) {
198                         g_printerr ("modest: failed to get tny account folder\n");
199                         g_free (from);
200                         g_object_unref (G_OBJECT(msg));
201                         return MODEST_ERR_SEND;
202                 }
203                 
204                 folder = modest_tny_account_get_special_folder (account,
205                                                                 TNY_FOLDER_TYPE_DRAFTS);
206                 if (!folder) {
207                         g_printerr ("modest: failed to find Drafts folder\n");
208                         g_free (from);
209                         g_object_unref (G_OBJECT(msg));
210                         g_object_unref (G_OBJECT(account));
211                         return MODEST_ERR_SEND;
212                 }
213                 tny_folder_add_msg (folder, msg, NULL); /* FIXME: check err */
214
215                 win = modest_msg_edit_window_new (msg, account_name);
216                 
217                 g_object_unref (G_OBJECT(msg));
218                 g_object_unref (G_OBJECT(account));
219                 g_object_unref (G_OBJECT(folder));
220                 g_free (from);
221         } else 
222                 win = modest_main_window_new ();
223
224         if (!win) {
225                 g_printerr ("modest: failed to create window\n");
226                 return MODEST_ERR_UI;
227         } else {
228                 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
229                 modest_window_mgr_register_window (mgr, win);
230         }
231         
232         gtk_widget_show_all (GTK_WIDGET (win));
233         gtk_main();
234         
235         return MODEST_ERR_NONE;
236 }
237
238 static gchar*
239 check_account (const gchar* account)
240 {
241         gchar *retval;
242         ModestAccountMgr *account_mgr;
243         
244         account_mgr = modest_runtime_get_account_mgr();
245         
246         if (!account)
247                 retval = modest_account_mgr_get_default_account (account_mgr);
248         else
249                 retval = g_strdup (account);
250
251         /* sanity check */
252         if (!account || !modest_account_mgr_account_exists (account_mgr, account, FALSE)) {
253                 g_free (retval);
254                 retval = NULL;
255         }
256         return retval;
257 }
258
259 static ModestErrorCode
260 send_mail (const gchar* account_name,
261            const gchar* mailto, const gchar *cc, const gchar *bcc,
262            const gchar* subject, const gchar *body)
263 {
264         int retval;
265         TnyTransportAccount *account;
266         ModestMailOperation *mail_operation = NULL;
267         gchar               *from_string;
268         
269         g_return_val_if_fail (account_name, MODEST_ERR_SEND);
270
271         ////////////////////// FIXME ////////
272         modest_runtime_not_implemented (NULL);
273         return MODEST_ERR_NONE;
274         //////////////////////////////////////
275         
276         account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_tny_account_by_account
277                                          (modest_runtime_get_account_store(), account_name,
278                                           TNY_ACCOUNT_TYPE_TRANSPORT));
279         if (!account) {
280                 g_printerr ("modest: no transport defined account for %s\n",
281                             account_name);
282                 return MODEST_ERR_SEND;
283         }
284         from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
285                                                           account_name);
286
287         mail_operation = modest_mail_operation_new ();
288         modest_mail_operation_send_new_mail (mail_operation, account,
289                                              from_string, mailto,
290                                              cc, bcc, subject, body, NULL,
291                                              NULL, 0);
292         if (modest_mail_operation_get_status (mail_operation) == 
293             MODEST_MAIL_OPERATION_STATUS_FAILED) {
294                 retval = MODEST_ERR_SEND;
295         } else
296                 retval = MODEST_ERR_NONE; /* hurray! */
297         
298         g_object_unref (G_OBJECT(account));
299         g_object_unref (G_OBJECT(mail_operation));
300         g_free (from_string);
301         
302         return retval;
303 }
304
305