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