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