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