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