* src/modest-address-book.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 #include <config.h>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gtk/gtkwidget.h>
35
36 #include <tny-list.h>
37 #include <tny-transport-account.h>
38 #include <tny-account-store.h>
39 #include <tny-list.h>
40 #include <tny-simple-list.h>
41
42 #include <modest-runtime.h>
43 #include <modest-defs.h>
44 #include <modest-tny-account-store.h>
45 #include <modest-tny-platform-factory.h>
46 #include <modest-mail-operation.h>
47 #include <modest-tny-account.h>
48 #include <modest-tny-msg.h>
49 #include <modest-account-mgr.h>
50 #include <modest-account-mgr-helpers.h>
51
52 #include <widgets/modest-main-window.h>
53 #include <widgets/modest-msg-edit-window.h>
54
55 typedef enum {
56         MODEST_ERR_NONE    = 0,   /* no error */
57         MODEST_ERR_OPTIONS = 1,   /* error in the options */
58         MODEST_ERR_CONF    = 2,   /* error getting confuration db */
59         MODEST_ERR_UI      = 3,   /* error in the UI */
60         MODEST_ERR_HILDON  = 4,   /* error with Hildon (maemo-only) */
61         MODEST_ERR_RUN     = 5,   /* error running */
62         MODEST_ERR_SEND    = 6,   /* error sending mail */
63         MODEST_ERR_PARAM   = 7,   /* error in one or more of the parameters */
64         MODEST_ERR_INIT    = 8    /* error in initialization */
65 } ModestErrorCode;
66
67 static gchar*           check_account (const gchar *account);
68
69 static ModestErrorCode  start_ui      (const gchar *account,
70                                        const gchar* mailto, const gchar *cc,
71                                        const gchar *bcc, const gchar* subject, const gchar *body);
72
73 static ModestErrorCode  send_mail     (const gchar* account,
74                                        const gchar* mailto, const gchar *cc, const gchar *bcc,
75                                        const gchar* subject, const gchar *body);
76 int
77 main (int argc, char *argv[])
78 {
79         GOptionContext     *context = NULL;
80         
81         GError *err = NULL;
82         int retval  = MODEST_ERR_NONE;
83                 
84         static gboolean batch = FALSE;
85         static gchar    *mailto=NULL, *subject=NULL, *bcc=NULL,
86                 *cc=NULL, *body=NULL, *account=NULL;
87         gchar *account_or_default;
88         static GOptionEntry options[] = {
89                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
90                   N_("New email to <addresses> (comma-separated)"), NULL},
91                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
92                   N_("Subject for a new mail"), NULL},
93                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
94                   N_("Body for a new email"), NULL},
95                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
96                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
97                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
98                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
99                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
100                   N_("Account to use (if none specified, the default account will be used)"), NULL},
101                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
102                   N_("Run in batch mode (don't show UI)"), NULL},
103                 { NULL, 0, 0, 0, NULL, NULL, NULL }
104         };
105
106         context = g_option_context_new (NULL);
107         g_option_context_add_main_entries (context, options, NULL);
108         
109         if (!g_option_context_parse (context, &argc, &argv, &err)) {
110                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
111                             err ? err->message : "");
112                 g_error_free (err);
113                 g_option_context_free (context);
114                 retval = MODEST_ERR_OPTIONS;
115                 goto cleanup;
116         }
117         g_option_context_free (context);
118         
119         if (!modest_runtime_init ()) {
120                 g_printerr ("modest: cannot init runtime\n");
121                 return MODEST_ERR_INIT;
122                 
123         }
124
125         account_or_default = check_account (account);
126         g_free (account);
127         
128         if (!batch) {
129                 if (!modest_runtime_init_ui (argc, argv)) {
130                         g_printerr ("modest: cannot start ui\n");
131                         retval = MODEST_ERR_UI;
132                         goto cleanup;
133                 } else {
134                         if (modest_conf_get_bool (modest_runtime_get_conf(),
135                                                   MODEST_CONF_CONNECT_AT_STARTUP, NULL))
136                                 tny_device_force_online (modest_runtime_get_device());
137                         
138                         retval = start_ui (account_or_default,
139                                            mailto, cc, bcc, subject, body);
140                 }
141         } else {
142                 if (!account_or_default) {
143                         g_printerr ("modest: no account has been defined yet\n");
144                         retval = MODEST_ERR_CONF;
145                         goto cleanup;
146                 }       
147                 retval = send_mail (account_or_default,
148                                     mailto, cc, bcc, subject, body);
149         }
150         
151 cleanup:
152         g_free (mailto);
153         g_free (subject);
154         g_free (bcc);
155         g_free (cc);
156         g_free (body);
157         g_free (account);
158
159         if (!modest_runtime_uninit ()) 
160                 g_printerr ("modest: modest_runtime_uninit failed\n");
161
162         return retval;
163 }
164
165
166 static ModestErrorCode 
167 start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc,
168           const gchar* subject, const gchar *body)
169 {
170         ModestWindow *win = NULL;
171
172         if (mailto||cc||bcc||subject||body) {           
173                 gchar *from;
174                 TnyMsg     *msg;
175                 TnyFolder  *folder;
176                 TnyAccount *account;
177                 
178                 if (!account_name) {
179                         g_printerr ("modest: no valid account provided, "
180                                     "nor is default one available\n");
181                         return MODEST_ERR_PARAM;
182                 }
183                 from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
184                                                            account_name);
185                 msg  = modest_tny_msg_new (mailto,from,cc,bcc,subject,body,NULL);
186                 if (!msg) {
187                         g_printerr ("modest: failed to create message\n");
188                         g_free (from);
189                         return MODEST_ERR_SEND;
190                 }
191
192                 account = modest_tny_account_store_get_tny_account_by_account (
193                         modest_runtime_get_account_store(), account_name,
194                         TNY_ACCOUNT_TYPE_TRANSPORT);
195                 if (!account) {
196                         g_printerr ("modest: failed to get tny account folder\n");
197                         g_free (from);
198                         g_object_unref (G_OBJECT(msg));
199                         return MODEST_ERR_SEND;
200                 }
201                 
202                 folder = modest_tny_account_get_special_folder (account,
203                                                                 TNY_FOLDER_TYPE_DRAFTS);
204                 if (!folder) {
205                         g_printerr ("modest: failed to find Drafts folder\n");
206                         g_free (from);
207                         g_object_unref (G_OBJECT(msg));
208                         g_object_unref (G_OBJECT(account));
209                         return MODEST_ERR_SEND;
210                 }
211                 tny_folder_add_msg (folder, msg, NULL); /* FIXME: check err */
212
213                 win = modest_msg_edit_window_new (msg, account_name);
214                 
215                 g_object_unref (G_OBJECT(msg));
216                 g_object_unref (G_OBJECT(account));
217                 g_object_unref (G_OBJECT(folder));
218                 g_free (from);
219         } else 
220                 win = modest_main_window_new ();
221
222         if (!win) {
223                 g_printerr ("modest: failed to create window\n");
224                 return MODEST_ERR_UI;
225         } else {
226                 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
227                 modest_window_mgr_register_window (mgr, win);
228         }
229         
230         gtk_widget_show_all (GTK_WIDGET (win));
231         gtk_main();
232         
233         return MODEST_ERR_NONE;
234 }
235
236 static gchar*
237 check_account (const gchar* account)
238 {
239         gchar *retval;
240         ModestAccountMgr *account_mgr;
241         
242         account_mgr = modest_runtime_get_account_mgr();
243         
244         if (!account)
245                 retval = modest_account_mgr_get_default_account (account_mgr);
246         else
247                 retval = g_strdup (account);
248
249         /* sanity check */
250         if (!account || !modest_account_mgr_account_exists (account_mgr, account, FALSE)) {
251                 g_free (retval);
252                 retval = NULL;
253         }
254         return retval;
255 }
256
257 static ModestErrorCode
258 send_mail (const gchar* account_name,
259            const gchar* mailto, const gchar *cc, const gchar *bcc,
260            const gchar* subject, const gchar *body)
261 {
262         int retval;
263         TnyTransportAccount *account;
264         ModestMailOperation *mail_operation = NULL;
265         gchar               *from_string;
266         
267         g_return_val_if_fail (account_name, MODEST_ERR_SEND);
268
269         ////////////////////// FIXME ////////
270         modest_runtime_not_implemented (NULL);
271         return MODEST_ERR_NONE;
272         //////////////////////////////////////
273         
274         account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_tny_account_by_account
275                                          (modest_runtime_get_account_store(), account_name,
276                                           TNY_ACCOUNT_TYPE_TRANSPORT));
277         if (!account) {
278                 g_printerr ("modest: no transport defined account for %s\n",
279                             account_name);
280                 return MODEST_ERR_SEND;
281         }
282         from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
283                                                           account_name);
284
285         mail_operation = modest_mail_operation_new ();
286         modest_mail_operation_send_new_mail (mail_operation, account,
287                                              from_string, mailto,
288                                              cc, bcc, subject, body, NULL,
289                                              NULL, 0);
290         if (modest_mail_operation_get_status (mail_operation) == 
291             MODEST_MAIL_OPERATION_STATUS_FAILED) {
292                 retval = MODEST_ERR_SEND;
293         } else
294                 retval = MODEST_ERR_NONE; /* hurray! */
295         
296         g_object_unref (G_OBJECT(account));
297         g_object_unref (G_OBJECT(mail_operation));
298         g_free (from_string);
299         
300         return retval;
301 }
302
303