* added convenience functions
[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 <tny-account-store-iface.h>
36 #include <tny-list-iface.h>
37
38 #include "modest-conf.h"
39 #include "modest-account-mgr.h"
40 #include "modest-ui.h"
41 #include "modest-icon-factory.h"
42 #include "modest-tny-transport-actions.h"
43 #include "modest-tny-account-store.h"
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif /*HAVE_CONFIG_H*/
48
49 #ifdef MODEST_ENABLE_HILDON /* Hildon includes */
50 #include <libosso.h>
51 #endif /* MODEST_ENABLE_HILDON */
52
53 /* return values */
54 #define MODEST_ERR_NONE    0
55 #define MODEST_ERR_OPTIONS 1
56 #define MODEST_ERR_CONF    2
57 #define MODEST_ERR_UI      3
58 #define MODEST_ERR_HILDON  4
59 #define MODEST_ERR_RUN     5
60 #define MODEST_ERR_SEND    6
61
62 static gboolean hildon_init (); /* NOP if HILDON is not defined */
63
64 static int start_ui (ModestConf *conf, const gchar* mailto, const gchar *cc,
65                      const gchar *bcc, const gchar* subject, const gchar *body);
66
67 static int send_mail (ModestConf *conf, const gchar* mailto, const gchar *cc, const gchar *bcc,
68                       const gchar* subject, const gchar *body);
69
70 int
71 main (int argc, char *argv[])
72 {
73         GOptionContext   *context        = NULL;
74         ModestConf       *modest_conf    = NULL;
75         ModestUI         *modest_ui      = NULL;
76
77         GError *err = NULL;
78         int retval  = MODEST_ERR_NONE;
79                 
80         static gboolean debug=FALSE, batch=FALSE;
81         static gchar    *mailto, *subject, *bcc, *cc, *body;
82
83         static GOptionEntry options[] = {
84                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
85                   "Run in debug mode" },
86                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
87                   "New email to <addresses> (comma-separated)"},
88                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
89                   "Subject for a new mail"},
90                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
91                   "Body for a new email"},
92                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
93                   "Cc: addresses for a new mail (comma-separated)"},
94                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
95                   "Bcc: addresses for a new mail (comma-separated)"},
96                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
97                   "Run in batch mode (don't show UI)"},
98                 { NULL }
99         };
100
101         g_type_init ();
102
103         context = g_option_context_new (NULL);
104         g_option_context_add_main_entries (context, options, NULL);
105         
106         if (!g_option_context_parse (context, &argc, &argv, &err)) {
107                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
108                             err ? err->message : "");
109                 g_error_free (err);
110                 retval = MODEST_ERR_OPTIONS;
111                 goto cleanup;
112         }
113         g_option_context_free (context);
114         
115         modest_conf = MODEST_CONF(modest_conf_new());
116         if (!modest_conf) {
117                 g_printerr ("modest: failed to initialize config system, exiting\n");
118                 retval = MODEST_ERR_CONF;
119                 goto cleanup;
120         }
121
122         if (debug)
123                 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
124         
125         if (!batch) {
126                 gtk_init (&argc, &argv);
127                 retval = start_ui (modest_conf, mailto, cc, bcc, subject, body);
128         } else 
129                 retval = send_mail (modest_conf, mailto, cc, bcc, subject, body);
130                 
131         
132 cleanup:
133         if (modest_conf)
134                 g_object_unref (G_OBJECT(modest_conf));
135         
136         return retval;
137 }
138
139
140 static int
141 start_ui (ModestConf *conf, const gchar* mailto, const gchar *cc, const gchar *bcc,
142           const gchar* subject, const gchar *body)
143 {
144
145         GtkWidget *win;
146         ModestUI *modest_ui;
147         gint ok, retval = 0;
148
149         modest_ui = MODEST_UI(modest_ui_new (conf));
150         if (!modest_ui) {
151                 g_printerr ("modest: failed to initialize ui, exiting\n");
152                 retval = MODEST_ERR_UI;
153                 goto cleanup;
154         }
155         
156         modest_icon_factory_init ();    
157
158         if (!hildon_init ()) { /* NOP  if hildon is not defined */
159                 g_printerr ("modest: failed to initialize hildon, exiting\n");
160                 retval = MODEST_ERR_HILDON;
161                 goto cleanup;
162         }
163
164         if (mailto||cc||bcc||subject||body) {
165
166 /*              ok = modest_ui_new_edit_window (modest_ui, */
167 /*                                              mailto,  /\* to *\/ */
168 /*                                              cc,      /\* cc *\/ */
169 /*                                              bcc,     /\* bcc *\/ */
170 /*                                              subject,    /\* subject *\/ */
171 /*                                              body,    /\* body *\/ */
172 /*                                              NULL);   /\* attachments *\/ */
173         } else
174 #ifndef OLD_UI_STUFF
175         win = modest_ui_main_window (modest_ui);
176         gtk_widget_show (win);
177 #else
178         modest_ui_show_main_window (modest_ui);
179 #endif
180         gtk_main();
181         
182 cleanup:
183         if (modest_ui)
184                 g_object_unref (modest_ui);
185
186         modest_icon_factory_uninit ();
187         return retval;
188 }
189
190
191 static gboolean
192 hildon_init ()
193 {
194 #ifdef MODEST_ENABLE_HILDON
195
196         osso_context_t *osso_context =
197                 osso_initialize(PACKAGE, PACKAGE_VERSION,
198                                 TRUE, NULL);    
199         if (!osso_context) {
200                 g_printerr ("modest: failed to aquire osso context, exiting\n");
201
202                 return FALSE;
203                 
204         }
205 #endif /* MODEST_ENABLE_HILDON */
206
207         return TRUE;
208 }
209
210
211
212 static int
213 send_mail (ModestConf *conf, const gchar* mailto, const gchar *cc, const gchar *bcc,
214            const gchar* subject, const gchar *body)
215 {
216         ModestAccountMgr *acc_mgr = NULL;
217         ModestTnyTransportActions *transport = NULL;
218         ModestTnyAccountStore *acc_store = NULL;
219
220         TnyListIface *accounts = NULL;
221         TnyIteratorIface *iter = NULL;
222
223         TnyTransportAccountIface *account = NULL;
224         
225         int retval;
226         int i = 0;
227         
228         acc_mgr   = modest_account_mgr_new (conf);
229         acc_store = modest_tny_account_store_new (acc_mgr);     
230         transport = modest_tny_transport_actions_new ();
231
232         accounts = TNY_LIST_IFACE(tny_list_new ());
233         tny_account_store_iface_get_accounts (TNY_ACCOUNT_STORE_IFACE(acc_store), accounts,
234                                               TNY_ACCOUNT_STORE_IFACE_TRANSPORT_ACCOUNTS);
235
236         iter = tny_list_iface_create_iterator(accounts);
237         tny_iterator_iface_first (iter);
238         if (tny_iterator_iface_is_done (iter)) {
239                 g_printerr("modest: no transport accounts defined");
240                 retval = MODEST_ERR_SEND;
241                 goto cleanup;
242         }
243
244         account = TNY_TRANSPORT_ACCOUNT_IFACE (tny_iterator_iface_current(iter));
245
246         if (!modest_tny_transport_actions_send_message (transport, account,
247                                                         "<>", mailto, cc, bcc, subject, body,
248                                                         NULL)) {
249                 retval = MODEST_ERR_SEND;
250                 goto cleanup;
251         } else
252                 retval = MODEST_ERR_NONE; /* hurray! */
253                                                          
254 cleanup:
255         if (iter)
256                 g_object_unref (G_OBJECT(iter));
257         if (accounts)
258                 g_object_unref (G_OBJECT(accounts));
259         if (transport)
260                 g_object_unref (G_OBJECT(transport));
261         if (acc_store)
262                 g_object_unref (G_OBJECT(acc_store));
263         if (acc_mgr)
264                 g_object_unref (G_OBJECT(acc_mgr));
265         
266         return retval;
267 }
268