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