* src/modest-tny-attachment.h:
[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 #include "modest-tny-platform-factory.h"
47
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 (const gchar* mailto, const gchar *cc,
65                      const gchar *bcc, const gchar* subject, const gchar *body);
66
67 static int send_mail (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         TnyPlatformFactory *fact         = NULL;
75         ModestConf       *modest_conf    = 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", NULL},
86                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
87                   "New email to <addresses> (comma-separated)", NULL},
88                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
89                   "Subject for a new mail", NULL},
90                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
91                   "Body for a new email", NULL},
92                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
93                   "Cc: addresses for a new mail (comma-separated)", NULL},
94                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
95                   "Bcc: addresses for a new mail (comma-separated)", NULL},
96                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
97                   "Run in batch mode (don't show UI)", NULL},
98                 { NULL, 0, 0, 0, NULL, NULL, 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         fact = modest_tny_platform_factory_get_instance ();
116         modest_conf = modest_tny_platform_factory_get_modest_conf_instance (fact);
117         if (!modest_conf) {
118                 g_printerr ("modest: failed to initialize config system, exiting\n");
119                 retval = MODEST_ERR_CONF;
120                 goto cleanup;
121         }
122
123         if (debug)
124                 g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
125         
126         if (!getenv("DISPLAY"))
127                 batch = TRUE; 
128         
129         if (!batch) {
130                 gtk_init (&argc, &argv);
131                 retval = start_ui (mailto, cc, bcc, subject, body);
132         } else 
133                 retval = send_mail (mailto, cc, bcc, subject, body);
134                 
135         
136 cleanup:
137         
138         return retval;
139 }
140
141
142 static int
143 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
144           const gchar* subject, const gchar *body)
145 {
146
147         ModestUI *modest_ui;
148         gint retval = 0;
149
150         #ifndef OLD_UI_STUFF
151         GtkWidget *win;
152         #endif
153         
154         modest_ui = MODEST_UI(modest_ui_new ());
155         if (!modest_ui) {
156                 g_printerr ("modest: failed to initialize ui, exiting\n");
157                 retval = MODEST_ERR_UI;
158                 goto cleanup;
159         }
160         
161         modest_icon_factory_init ();    
162
163         if (!hildon_init ()) { /* NOP  if hildon is not defined */
164                 g_printerr ("modest: failed to initialize hildon, exiting\n");
165                 retval = MODEST_ERR_HILDON;
166                 goto cleanup;
167         }
168
169         if (mailto||cc||bcc||subject||body) {
170
171 /*              ok = modest_ui_new_edit_window (modest_ui, */
172 /*                                              mailto,  /\* to *\/ */
173 /*                                              cc,      /\* cc *\/ */
174 /*                                              bcc,     /\* bcc *\/ */
175 /*                                              subject,    /\* subject *\/ */
176 /*                                              body,    /\* body *\/ */
177 /*                                              NULL);   /\* attachments *\/ */
178         } else
179 #ifndef OLD_UI_STUFF
180         win = modest_ui_main_window (modest_ui);
181         gtk_widget_show (win);
182 #else
183         modest_ui_show_main_window (modest_ui);
184 #endif
185         gtk_main();
186         
187 cleanup:
188         if (modest_ui)
189                 g_object_unref (modest_ui);
190
191         modest_icon_factory_uninit ();
192         return retval;
193 }
194
195
196 static gboolean
197 hildon_init ()
198 {
199 #ifdef MODEST_ENABLE_HILDON
200
201         osso_context_t *osso_context =
202                 osso_initialize(PACKAGE, PACKAGE_VERSION,
203                                 TRUE, NULL);    
204         if (!osso_context) {
205                 g_printerr ("modest: failed to aquire osso context, exiting\n");
206
207                 return FALSE;
208                 
209         }
210 #endif /* MODEST_ENABLE_HILDON */
211
212         return TRUE;
213 }
214
215
216
217 static int
218 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
219            const gchar* subject, const gchar *body)
220 {
221         ModestAccountMgr *acc_mgr = NULL;
222         TnyPlatformFactory *fact = NULL;
223         TnyAccountStore *acc_store = NULL;
224
225         TnyList *accounts = NULL;
226         TnyIterator *iter = NULL;
227         TnyTransportAccount *account = NULL;    
228         int retval;
229
230         fact = modest_tny_platform_factory_get_instance ();
231         acc_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
232         acc_store = tny_platform_factory_new_account_store (fact);      
233
234         accounts = TNY_LIST(tny_simple_list_new ());
235         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
236                                               TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
237
238         iter = tny_list_create_iterator(accounts);
239         tny_iterator_first (iter);
240         if (tny_iterator_is_done (iter)) {
241                 g_printerr("modest: no transport accounts defined\n");
242                 retval = MODEST_ERR_SEND;
243                 goto cleanup;
244         }
245
246         account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
247
248         if (!modest_mail_operation_send_mail (account,
249                                               "djcb@djcbsoftware.nl", mailto, cc, bcc, subject, body,
250                                               NULL)) {
251                 retval = MODEST_ERR_SEND;
252                 goto cleanup;
253         } else
254                 retval = MODEST_ERR_NONE; /* hurray! */
255
256 cleanup:
257         if (iter)
258                 g_object_unref (G_OBJECT(iter));
259         if (accounts)
260                 g_object_unref (G_OBJECT(accounts));
261         
262         return retval;
263 }
264