c7ba04758b5681b347be9fd94d54d8b0656278d9
[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-defs.h>
43 #include <modest-conf.h>
44 #include <modest-account-mgr.h>
45 #include <modest-ui.h>
46 #include <modest-debug.h>
47 #include <modest-icon-factory.h>
48 #include <modest-tny-account-store.h>
49 #include <modest-tny-platform-factory.h>
50 #include <modest-mail-operation.h>
51
52 #if MODEST_PLATFORM_ID==2 /* maemo */
53 #include <libosso.h>
54 #endif /* MODEST_PLATFORM==2 */
55
56 static gboolean hildon_init (); /* NOP if HILDON is not defined */
57 static int start_ui (const gchar* mailto, const gchar *cc,
58                      const gchar *bcc, const gchar* subject, const gchar *body,
59                      TnyAccountStore *account_store);
60
61 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
62                       const gchar* subject, const gchar *body);
63
64 int
65 main (int argc, char *argv[])
66 {
67         GOptionContext     *context       = NULL;
68         TnyPlatformFactory *fact          = NULL;
69         TnyAccountStore    *account_store = NULL;
70         
71         GError *err = NULL;
72         int retval  = MODEST_ERR_NONE;
73                 
74         static gboolean batch=FALSE, factory_settings=FALSE;
75         static gchar    *mailto, *subject, *bcc, *cc, *body, *account;
76
77         static GOptionEntry options[] = {
78                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
79                   N_("New email to <addresses> (comma-separated)"), NULL},
80                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
81                   N_("Subject for a new mail"), NULL},
82                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
83                   N_("Body for a new email"), NULL},
84                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
85                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
86                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
87                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
88                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
89                   N_("Account to use (if specified, default account is used)"), NULL},
90                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
91                   N_("Run in batch mode (don't show UI)"), NULL},
92                 { "factory-settings", 0, 0, G_OPTION_ARG_NONE, &factory_settings,
93                   N_("return to factory settings"), NULL},
94                 { NULL, 0, 0, 0, NULL, NULL, NULL }
95         };
96
97         bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
98         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
99         textdomain (GETTEXT_PACKAGE);
100
101         modest_debug_g_type_init  ();           
102         modest_debug_logging_init ();
103
104         g_thread_init (NULL);
105         
106         context = g_option_context_new (NULL);
107         g_option_context_add_main_entries (context, options, NULL);
108         
109         if (!g_option_context_parse (context, &argc, &argv, &err)) {
110                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
111                             err ? err->message : "");
112                 g_error_free (err);
113                 retval = MODEST_ERR_OPTIONS;
114                 goto cleanup;
115         }
116         g_option_context_free (context);
117
118         /* Get platform factory */      
119         fact = modest_tny_platform_factory_get_instance ();
120
121         if (!modest_init_local_folders ()) {
122                 g_printerr ("modest: failed to initialize local folders, exiting\n");
123                 retval = MODEST_ERR_INIT;
124                 goto cleanup;
125         }
126         
127         /* Get the account store */
128         account_store = tny_platform_factory_new_account_store (fact);
129         if (!account_store) {
130                 g_printerr ("modest: could not initialize a ModestTnyAccountStore instance\n");
131                 retval = MODEST_ERR_RUN;
132                 goto cleanup;
133         }
134
135         
136         if (!getenv("DISPLAY"))
137                 batch = TRUE; 
138         
139         if (!batch) {
140                 gdk_threads_init ();
141                 gtk_init (&argc, &argv);
142                 modest_init_header_columns (factory_settings);
143                 
144                 retval = start_ui (mailto, cc, bcc, subject, body, account_store);
145         } else 
146                 retval = send_mail (mailto, cc, bcc, subject, body);
147         
148 cleanup:
149         if (fact)
150                 g_object_unref (G_OBJECT(fact));
151         /* this will clean up account_store as well */
152         
153         return retval;
154 }
155
156
157 static int
158 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
159           const gchar* subject, const gchar *body,
160           TnyAccountStore *account_store)
161 {
162         ModestUI *modest_ui;
163         ModestWindow *win;
164         gint retval = 0;
165         
166         modest_ui = MODEST_UI(modest_ui_new (account_store));
167         if (!modest_ui) {
168                 g_printerr ("modest: failed to initialize ui, exiting\n");
169                 retval = MODEST_ERR_UI;
170                 goto cleanup;
171         }
172         
173         if (!hildon_init ()) { /* NOP  if hildon is not defined */
174                 g_printerr ("modest: failed to initialize hildon, exiting\n");
175                 retval = MODEST_ERR_HILDON;
176                 goto cleanup;
177         }
178
179         if (mailto||cc||bcc||subject||body) {
180
181 /*              ok = modest_ui_new_edit_window (modest_ui, */
182 /*                                              mailto,  /\* to *\/ */
183 /*                                              cc,      /\* cc *\/ */
184 /*                                              bcc,     /\* bcc *\/ */
185 /*                                              subject,    /\* subject *\/ */
186 /*                                              body,    /\* body *\/ */
187 /*                                              NULL);   /\* attachments *\/ */
188         } else 
189                 win = modest_ui_main_window (modest_ui);
190         
191         if (win) {
192                 TnyDevice *device;
193
194                 gtk_widget_show_all (GTK_WIDGET (win));
195         
196                 /* Go online */
197                 device = tny_account_store_get_device (account_store);
198                 tny_device_force_online (device);
199                 g_object_unref (G_OBJECT (device));
200
201                 gtk_main();
202         }
203 cleanup:
204         if (modest_ui)
205                 g_object_unref (modest_ui);
206
207         return retval;
208 }
209         
210
211 static gboolean
212 hildon_init ()
213 {
214 #if MODEST_PLATFORM_ID==2 
215         
216         osso_context_t *osso_context =
217                 osso_initialize(PACKAGE, PACKAGE_VERSION,
218                                 TRUE, NULL);    
219         if (!osso_context) {
220                 g_printerr ("modest: failed to acquire osso context\n");
221                 return FALSE;
222         }
223         
224 #endif /* MODEST_PLATFORM_ID==2 */
225
226         return TRUE;
227 }
228
229
230
231 static int
232 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
233            const gchar* subject, const gchar *body)
234 {
235         ModestAccountMgr *acc_mgr = NULL;
236         TnyPlatformFactory *fact = NULL;
237         TnyAccountStore *acc_store = NULL;
238         ModestMailOperation *mail_operation = NULL;
239
240         TnyList *accounts = NULL;
241         TnyIterator *iter = NULL;
242         TnyTransportAccount *account = NULL;    
243         int retval;
244
245         fact = modest_tny_platform_factory_get_instance ();
246         acc_mgr = modest_tny_platform_factory_get_account_mgr_instance
247                 (MODEST_TNY_PLATFORM_FACTORY(fact));
248         acc_store = tny_platform_factory_new_account_store (fact);      
249
250         accounts = TNY_LIST(tny_simple_list_new ());
251         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
252                                               TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
253
254         iter = tny_list_create_iterator(accounts);
255         tny_iterator_first (iter);
256         if (tny_iterator_is_done (iter)) {
257                 g_printerr("modest: no transport accounts defined\n");
258                 retval = MODEST_ERR_SEND;
259                 goto cleanup;
260         }
261
262         account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
263
264         mail_operation = modest_mail_operation_new ();
265
266         modest_mail_operation_send_new_mail (mail_operation,
267                                              account, "test@example.com",
268                                              mailto, cc, bcc, 
269                                              subject, body, NULL);
270                 
271         if (modest_mail_operation_get_status (mail_operation) == 
272             MODEST_MAIL_OPERATION_STATUS_FAILED) {
273                 retval = MODEST_ERR_SEND;
274                 goto cleanup;
275         } else
276                 retval = MODEST_ERR_NONE; /* hurray! */
277
278 cleanup:
279         if (iter)
280                 g_object_unref (G_OBJECT (iter));
281         if (accounts)
282                 g_object_unref (G_OBJECT (accounts));
283         if (mail_operation)
284                 g_object_unref (G_OBJECT (mail_operation));
285
286         return retval;
287 }
288