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