* update to new iterator API in tinymail
[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
35 #include "modest-conf.h"
36 #include "modest-account-mgr.h"
37 #include "modest-identity-mgr.h"
38 #include "modest-ui.h"
39 #include "modest-icon-factory.h"
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif /*HAVE_CONFIG_H*/
44
45 #ifdef MODEST_ENABLE_HILDON /* Hildon includes */
46 #include <libosso.h>
47 #endif /* MODEST_ENABLE_HILDON */
48
49 /* return values */
50 #define MODEST_ERR_NONE    0
51 #define MODEST_ERR_OPTIONS 1
52 #define MODEST_ERR_CONF    2
53 #define MODEST_ERR_UI      3
54 #define MODEST_ERR_HILDON  4
55 #define MODEST_ERR_RUN     5
56
57
58 static gboolean hildon_init (); /* NOP if HILDON is not defined */
59
60 static int start_ui (ModestConf *conf, const gchar* mailto, const gchar *cc,
61                      const gchar *bcc, const gchar* subject, const gchar *body);
62
63 int
64 main (int argc, char *argv[])
65 {
66         GOptionContext   *context        = NULL;
67         ModestConf       *modest_conf    = NULL;
68         ModestUI         *modest_ui      = NULL;
69
70         GError *err = NULL;
71         int retval  = MODEST_ERR_NONE;
72                 
73         static gboolean update, debug, batch;
74         static gchar    *mailto, *subject, *bcc, *cc, *body;
75
76         static GOptionEntry options[] = {
77                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
78                   "Run in debug mode" },
79                 { "update", 'u', 0, G_OPTION_ARG_NONE, &update,
80                   "Send/receive active accounts"},
81                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
82                   "Start writing a new email to <addresses>"},
83                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
84                   "Subject for a new mail"},
85                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
86                   "Body for a new email"},
87                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
88                   "Cc: addresses for a new mail (comma-separated)"},
89                 { "bcc", 'd', 0, G_OPTION_ARG_STRING, &bcc,
90                   "Bcc: addresses for a new mail (comma-separated)"},
91                 { "batch", 'b', 0, G_OPTION_ARG_NONE, &batch,
92                   "Run in batch mode (don't show UI)"},
93                 { NULL }
94         };
95
96         g_type_init ();
97
98         context = g_option_context_new (NULL);
99         g_option_context_add_main_entries (context, options, NULL);
100         
101         if (!g_option_context_parse (context, &argc, &argv, &err)) {
102                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
103                             err ? err->message : "");
104                 g_error_free (err);
105                 retval = MODEST_ERR_OPTIONS;
106                 goto cleanup;
107         }
108         g_option_context_free (context);
109
110         modest_conf = MODEST_CONF(modest_conf_new());
111         if (!modest_conf) {
112                 g_printerr ("modest: failed to initialize config system, exiting\n");
113                 retval = MODEST_ERR_CONF;
114                 goto cleanup;
115         }
116         
117         gtk_init (&argc, &argv);
118         retval = start_ui (modest_conf, mailto, cc, bcc, subject, body);
119
120 cleanup:
121         g_object_unref (modest_conf);
122         
123         return retval;
124 }
125
126
127 int
128 start_ui (ModestConf *conf, const gchar* mailto, const gchar *cc, const gchar *bcc,
129           const gchar* subject, const gchar *body)
130 {
131         ModestUI *modest_ui;
132         gint ok, retval = 0;
133
134         modest_ui = MODEST_UI(modest_ui_new (conf));
135         if (!modest_ui) {
136                 g_printerr ("modest: failed to initialize ui, exiting\n");
137                 retval = MODEST_ERR_UI;
138                 goto cleanup;
139         }
140         
141         modest_icon_factory_init ();    
142
143         if (!hildon_init ()) { /* NOP  if hildon is not defined */
144                 g_printerr ("modest: failed to initialize hildon, exiting\n");
145                 retval = MODEST_ERR_HILDON;
146                 goto cleanup;
147         }
148
149         if (mailto||cc||bcc||subject||body) {
150
151 /*              ok = modest_ui_new_edit_window (modest_ui, */
152 /*                                              mailto,  /\* to *\/ */
153 /*                                              cc,      /\* cc *\/ */
154 /*                                              bcc,     /\* bcc *\/ */
155 /*                                              subject,    /\* subject *\/ */
156 /*                                              body,    /\* body *\/ */
157 /*                                              NULL);   /\* attachments *\/ */
158         } else
159                 ok = modest_ui_show_main_window (modest_ui);
160         
161         if (!ok) {
162                 g_printerr ("modest: showing window failed");
163                 retval = MODEST_ERR_RUN;
164                 goto cleanup;
165         }       
166         
167         gtk_main();
168         
169 cleanup:
170         if (modest_ui)
171                 g_object_unref (modest_ui);
172
173         modest_icon_factory_uninit ();
174         return retval;
175 }
176
177
178 static gboolean
179 hildon_init ()
180 {
181 #ifdef MODEST_ENABLE_HILDON
182
183         osso_context_t *osso_context =
184                 osso_initialize(PACKAGE, PACKAGE_VERSION,
185                                 TRUE, NULL);
186         
187         if (!osso_context) {
188                 g_printerr ("modest: failed to aquire osso context, exiting");
189                 return FALSE;
190         }
191 #endif /* MODEST_ENABLE_HILDON */
192
193         return TRUE;
194 }