* A lot of changes in the documentation, now it's generated correctly
[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 #ifdef MODEST_ENABLE_HILDON /* Hildon includes */
51 #include <libosso.h>
52 #endif /* MODEST_ENABLE_HILDON */
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
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         ModestConf       *modest_conf    = NULL;
77
78         GError *err = NULL;
79         int retval  = MODEST_ERR_NONE;
80                 
81         static gboolean debug=FALSE, batch=FALSE;
82         static gchar    *mailto, *subject, *bcc, *cc, *body;
83
84         static GOptionEntry options[] = {
85                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
86                   "Run in debug mode", NULL},
87                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
88                   "New email to <addresses> (comma-separated)", NULL},
89                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
90                   "Subject for a new mail", NULL},
91                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
92                   "Body for a new email", NULL},
93                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
94                   "Cc: addresses for a new mail (comma-separated)", NULL},
95                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
96                   "Bcc: addresses for a new mail (comma-separated)", NULL},
97                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
98                   "Run in batch mode (don't show UI)", NULL},
99                 { NULL, 0, 0, 0, NULL, NULL, NULL }
100         };
101
102         bindtextdomain (GETTEXT_PACKAGE, MODESTLOCALEDIR);
103         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
104         textdomain (GETTEXT_PACKAGE);
105
106         g_type_init ();
107
108         context = g_option_context_new (NULL);
109         g_option_context_add_main_entries (context, options, NULL);
110         
111         if (!g_option_context_parse (context, &argc, &argv, &err)) {
112                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
113                             err ? err->message : "");
114                 g_error_free (err);
115                 retval = MODEST_ERR_OPTIONS;
116                 goto cleanup;
117         }
118         g_option_context_free (context);
119         
120         fact = modest_tny_platform_factory_get_instance ();
121         modest_conf = modest_tny_platform_factory_get_modest_conf_instance (fact);
122         if (!modest_conf) {
123                 g_printerr ("modest: failed to initialize config system, exiting\n");
124                 retval = MODEST_ERR_CONF;
125                 goto cleanup;
126         }
127
128         if (debug)
129                 g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
130         
131         if (!getenv("DISPLAY"))
132                 batch = TRUE; 
133         
134         if (!batch) {
135                 gtk_init (&argc, &argv);
136                 retval = start_ui (mailto, cc, bcc, subject, body);
137         } else 
138                 retval = send_mail (mailto, cc, bcc, subject, body);
139                 
140         
141 cleanup:
142         
143         return retval;
144 }
145
146
147 static int
148 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
149           const gchar* subject, const gchar *body)
150 {
151
152         ModestUI *modest_ui;
153         gint retval = 0;
154
155         #ifndef OLD_UI_STUFF
156         GtkWidget *win;
157         #endif
158         
159         modest_ui = MODEST_UI(modest_ui_new ());
160         if (!modest_ui) {
161                 g_printerr ("modest: failed to initialize ui, exiting\n");
162                 retval = MODEST_ERR_UI;
163                 goto cleanup;
164         }
165         
166         modest_icon_factory_init ();    
167
168         if (!hildon_init ()) { /* NOP  if hildon is not defined */
169                 g_printerr ("modest: failed to initialize hildon, exiting\n");
170                 retval = MODEST_ERR_HILDON;
171                 goto cleanup;
172         }
173
174         if (mailto||cc||bcc||subject||body) {
175
176 /*              ok = modest_ui_new_edit_window (modest_ui, */
177 /*                                              mailto,  /\* to *\/ */
178 /*                                              cc,      /\* cc *\/ */
179 /*                                              bcc,     /\* bcc *\/ */
180 /*                                              subject,    /\* subject *\/ */
181 /*                                              body,    /\* body *\/ */
182 /*                                              NULL);   /\* attachments *\/ */
183         } else
184 #ifndef OLD_UI_STUFF
185         win = modest_ui_main_window (modest_ui);
186         gtk_widget_show (win);
187 #else
188         modest_ui_show_main_window (modest_ui);
189 #endif
190         gtk_main();
191         
192 cleanup:
193         if (modest_ui)
194                 g_object_unref (modest_ui);
195
196         modest_icon_factory_uninit ();
197         return retval;
198 }
199
200
201 static gboolean
202 hildon_init ()
203 {
204 #ifdef MODEST_ENABLE_HILDON
205
206         osso_context_t *osso_context =
207                 osso_initialize(PACKAGE, PACKAGE_VERSION,
208                                 TRUE, NULL);    
209         if (!osso_context) {
210                 g_printerr ("modest: failed to aquire osso context, exiting\n");
211
212                 return FALSE;
213                 
214         }
215 #endif /* MODEST_ENABLE_HILDON */
216
217         return TRUE;
218 }
219
220
221
222 static int
223 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
224            const gchar* subject, const gchar *body)
225 {
226         ModestAccountMgr *acc_mgr = NULL;
227         TnyPlatformFactory *fact = NULL;
228         TnyAccountStore *acc_store = NULL;
229         ModestMailOperation *mail_operation;
230
231         TnyList *accounts = NULL;
232         TnyIterator *iter = NULL;
233         TnyTransportAccount *account = NULL;    
234         int retval;
235
236         fact = modest_tny_platform_factory_get_instance ();
237         acc_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
238         acc_store = tny_platform_factory_new_account_store (fact);      
239
240         accounts = TNY_LIST(tny_simple_list_new ());
241         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
242                                               TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
243
244         iter = tny_list_create_iterator(accounts);
245         tny_iterator_first (iter);
246         if (tny_iterator_is_done (iter)) {
247                 g_printerr("modest: no transport accounts defined\n");
248                 retval = MODEST_ERR_SEND;
249                 goto cleanup;
250         }
251
252         account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
253
254         mail_operation = modest_mail_operation_new ();
255
256         modest_mail_operation_send_new_mail (mail_operation,
257                                              account,
258                                              "djcb@djcbsoftware.nl", mailto, cc, bcc, 
259                                              subject, body, NULL);
260
261
262         if (modest_mail_operation_get_status (mail_operation) == 
263             MODEST_MAIL_OPERATION_STATUS_FAILED) {
264                 retval = MODEST_ERR_SEND;
265                 goto cleanup;
266         } else
267                 retval = MODEST_ERR_NONE; /* hurray! */
268
269 cleanup:
270         if (iter) g_object_unref (G_OBJECT (iter));
271         if (accounts) g_object_unref (G_OBJECT (accounts));
272         if (mail_operation) g_object_unref (G_OBJECT (mail_operation));
273
274         return retval;
275 }
276