* Moved back the contents style code to the UI actions
[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         if (modest_conf_get_bool (modest_runtime_get_conf(),
84                 MODEST_CONF_CONNECT_AT_STARTUP, NULL))
85         modest_platform_connect_and_wait(NULL);
86 }
87
88 static void
89 log_default_handler (const gchar *log_domain,
90                      GLogLevelFlags log_level,
91                      const gchar *message,
92                      gpointer unused_data)
93 {
94         if (log_level == G_LOG_LEVEL_ERROR || 
95             log_level == G_LOG_LEVEL_CRITICAL)
96                 g_print ("EEEEE ------ %s\n", message);
97 }
98
99 int
100 main (int argc, char *argv[])
101 {
102         GOptionContext     *context = NULL;
103         
104         GError *err = NULL;
105         int retval  = MODEST_ERR_NONE;
106                 
107         static gboolean batch = FALSE;
108         static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
109                 *cc=NULL, *body=NULL, *account=NULL;
110         gchar *account_or_default;
111         static GOptionEntry options[] = {
112                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
113                   N_("New email to <addresses> (comma-separated)"), NULL},
114                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
115                   N_("Subject for a new mail"), NULL},
116                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
117                   N_("Body for a new email"), NULL},
118                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
119                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
120                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
121                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
122                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
123                   N_("Account to use (if none specified, the default account will be used)"), NULL},
124                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
125                   N_("Run in batch mode (don't show UI)"), NULL},
126                 { NULL, 0, 0, 0, NULL, NULL, NULL }
127         };
128
129         context = g_option_context_new (NULL);
130         g_option_context_add_main_entries (context, options, NULL);
131         
132         if (!g_option_context_parse (context, &argc, &argv, &err)) {
133                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
134                             err ? err->message : "");
135                 g_error_free (err);
136                 g_option_context_free (context);
137                 retval = MODEST_ERR_OPTIONS;
138                 goto cleanup;
139         }
140         g_option_context_free (context);
141         
142         g_log_set_default_handler (log_default_handler, NULL);
143
144         if (!modest_init_init_core ()) {
145                 g_printerr ("modest: cannot init modest core\n");
146                 return MODEST_ERR_INIT;
147                 
148         }
149         
150         account_or_default = check_account (account);
151         g_free (account);
152         
153         if (!batch) {
154                 if (!modest_init_init_ui (argc, argv)) {
155                         g_printerr ("modest: cannot init modest ui\n");
156                         retval = MODEST_ERR_UI;
157                         goto cleanup;
158                 } else {
159                         GtkWidget *ui = NULL;
160                         retval = start_ui (account_or_default,
161                                            mailto, cc, bcc, subject, body, &ui);
162                         if (ui) 
163                                 g_signal_connect (G_OBJECT (ui), "show", G_CALLBACK(on_show), NULL);
164                 }
165         } else {
166                 if (!account_or_default) {
167                         g_printerr ("modest: no account has been defined yet\n");
168                         retval = MODEST_ERR_CONF;
169                         goto cleanup;
170                 }       
171                 retval = send_mail (account_or_default,
172                                     mailto, cc, bcc, subject, body);
173         }
174         
175 cleanup:
176         g_free (mailto);
177         g_free (subject);
178         g_free (bcc);
179         g_free (cc);
180         g_free (body);
181         g_free (account);
182         
183         if (!modest_init_uninit ()) 
184                 g_printerr ("modest: modest_init_uninit failed\n");
185
186         return retval;
187 }
188
189
190 static ModestErrorCode 
191 start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc,
192           const gchar* subject, const gchar *body, GtkWidget **ui)
193 {
194         ModestWindow *win = NULL;
195
196         if (mailto||cc||bcc||subject||body) {           
197                 gchar *from;
198                 TnyMsg     *msg;
199                 TnyFolder  *folder;
200                 TnyAccount *account;
201                 
202                 if (!account_name) {
203                         g_printerr ("modest: no valid account provided, "
204                                     "nor is default one available\n");
205                         return MODEST_ERR_PARAM;
206                 }
207                 from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
208                                                            account_name);
209                 msg  = modest_tny_msg_new (mailto,from,cc,bcc,subject,body,NULL);
210                 if (!msg) {
211                         g_printerr ("modest: failed to create message\n");
212                         g_free (from);
213                         return MODEST_ERR_SEND;
214                 }
215
216                 account = modest_tny_account_store_get_transport_account_for_open_connection (
217                         modest_runtime_get_account_store(), account_name);
218                 if (!account) {
219                         g_printerr ("modest: failed to get tny account folder\n");
220                         g_free (from);
221                         g_object_unref (G_OBJECT(msg));
222                         return MODEST_ERR_SEND;
223                 }
224                 
225                 folder = modest_tny_account_get_special_folder (account,
226                                                                 TNY_FOLDER_TYPE_DRAFTS);
227                 if (!folder) {
228                         g_printerr ("modest: failed to find Drafts folder\n");
229                         g_free (from);
230                         g_object_unref (G_OBJECT(msg));
231                         g_object_unref (G_OBJECT(account));
232                         return MODEST_ERR_SEND;
233                 }
234                 tny_folder_add_msg (folder, msg, NULL); /* FIXME: check err */
235
236                 win = modest_msg_edit_window_new (msg, account_name);
237                 
238                 g_object_unref (G_OBJECT(msg));
239                 g_object_unref (G_OBJECT(account));
240                 g_object_unref (G_OBJECT(folder));
241                 g_free (from);
242         } else 
243                 win = modest_main_window_new ();
244
245         if (!win) {
246                 g_printerr ("modest: failed to create window\n");
247                 return MODEST_ERR_UI;
248         } else {
249                 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
250                 modest_window_mgr_register_window (mgr, win);
251                 *ui = (GtkWidget*) win;
252                 g_object_unref (win);
253         }
254         
255         gtk_widget_show_all (GTK_WIDGET (win));
256         gtk_main();
257         
258         return MODEST_ERR_NONE;
259 }
260
261 static gchar*
262 check_account (const gchar* account)
263 {
264         gchar *retval;
265         ModestAccountMgr *account_mgr;
266         
267         account_mgr = modest_runtime_get_account_mgr();
268         
269         if (!account)
270                 retval = modest_account_mgr_get_default_account (account_mgr);
271         else
272                 retval = g_strdup (account);
273
274         /* sanity check */
275         if (!account || !modest_account_mgr_account_exists (account_mgr, account, FALSE)) {
276                 g_free (retval);
277                 retval = NULL;
278         }
279         return retval;
280 }
281
282 static ModestErrorCode
283 send_mail (const gchar* account_name,
284            const gchar* mailto, const gchar *cc, const gchar *bcc,
285            const gchar* subject, const gchar *body)
286 {
287         int retval;
288         TnyTransportAccount *account;
289         ModestMailOperation *mail_operation = NULL;
290         gchar               *from_string;
291         
292         g_return_val_if_fail (account_name, MODEST_ERR_SEND);
293
294         ////////////////////// FIXME ////////
295         modest_runtime_not_implemented (NULL);
296         return MODEST_ERR_NONE;
297         //////////////////////////////////////
298         
299         account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_transport_account_for_open_connection
300                                          (modest_runtime_get_account_store(), account_name));
301         if (!account) {
302                 g_printerr ("modest: no transport defined account for %s\n",
303                             account_name);
304                 return MODEST_ERR_SEND;
305         }
306         from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
307                                                           account_name);
308
309         mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
310
311         modest_mail_operation_send_new_mail (mail_operation, account,
312                                              NULL,
313                                              from_string, mailto,
314                                              cc, bcc, subject, body, NULL /* html_body */,
315                                              NULL /* attachments */, 0 /* priority */);
316         if (modest_mail_operation_get_status (mail_operation) == 
317             MODEST_MAIL_OPERATION_STATUS_FAILED) {
318                 retval = MODEST_ERR_SEND;
319         } else
320                 retval = MODEST_ERR_NONE; /* hurray! */
321         
322         g_object_unref (G_OBJECT(account));
323         g_object_unref (G_OBJECT(mail_operation));
324         g_free (from_string);
325         
326         return retval;
327 }
328
329