* all:
[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-ui.h>
45 #include <modest-icon-factory.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-tny-platform-factory.h>
48 #include <modest-mail-operation.h>
49
50
51 static int start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
52                      const gchar* subject, const gchar *body);
53 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
54                       const gchar* subject, const gchar *body);
55
56 int
57 main (int argc, char *argv[])
58 {
59         GOptionContext     *context       = NULL;
60         
61         GError *err = NULL;
62         int retval  = MODEST_ERR_NONE;
63                 
64         static gboolean batch=FALSE, factory_settings=FALSE;
65         static gchar    *mailto, *subject, *bcc, *cc, *body, *account;
66
67         static GOptionEntry options[] = {
68                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
69                   N_("New email to <addresses> (comma-separated)"), NULL},
70                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
71                   N_("Subject for a new mail"), NULL},
72                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
73                   N_("Body for a new email"), NULL},
74                 { "cc",  'c', 0, G_OPTION_ARG_STRING, &cc,
75                   N_("Cc: addresses for a new mail (comma-separated)"), NULL},
76                 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
77                   N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
78                 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
79                   N_("Account to use (if specified, default account is used)"), NULL},
80                 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
81                   N_("Run in batch mode (don't show UI)"), NULL},
82                 { "factory-settings", 0, 0, G_OPTION_ARG_NONE, &factory_settings,
83                   N_("return to factory settings"), NULL},
84                 { NULL, 0, 0, 0, NULL, NULL, NULL }
85         };
86
87         if (!modest_runtime_init ()) {
88                 g_printerr ("modest: cannot init runtime\n");
89                 return MODEST_ERR_INIT;
90         }
91         
92         context = g_option_context_new (NULL);
93         g_option_context_add_main_entries (context, options, NULL);
94         
95         if (!g_option_context_parse (context, &argc, &argv, &err)) {
96                 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
97                             err ? err->message : "");
98                 g_error_free (err);
99                 retval = MODEST_ERR_OPTIONS;
100                 goto cleanup;
101         }
102         g_option_context_free (context);
103         
104         if (!getenv("DISPLAY"))
105                 batch = TRUE; 
106         
107         if (!batch) {
108                 if (!gtk_init_check(&argc, &argv)) {
109                         g_printerr ("modest: failed to start graphical ui\n");
110                         goto cleanup;
111                 }
112                 retval = start_ui (mailto, cc, bcc, subject, body);
113
114         } else 
115                 retval = send_mail (mailto, cc, bcc, subject, body);
116         
117 cleanup:
118         if (!modest_runtime_uninit ()) 
119                 g_printerr ("modest: modest_runtime_uninit failed\n");
120
121         return retval;
122 }
123
124
125 static int
126 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
127           const gchar* subject, const gchar *body)
128 {
129         ModestWindow *win = NULL;
130         ModestUI *modest_ui = NULL;
131         
132         gint retval = 0;
133
134         modest_ui = modest_ui_new ();
135
136         if (mailto||cc||bcc||subject||body) {
137                 g_warning ("FIXME: implement this");
138 /*              ok = modest_ui_new_edit_window (modest_ui, */
139 /*                                              mailto,  /\* to *\/ */
140 /*                                              cc,      /\* cc *\/ */
141 /*                                              bcc,     /\* bcc *\/ */
142 /*                                              subject,    /\* subject *\/ */
143 /*                                              body,    /\* body *\/ */
144 /*                                              NULL);   /\* attachments *\/ */
145         } else 
146                 win = modest_ui_main_window (modest_ui);
147         
148         if (win) {
149                 gtk_widget_show_all (GTK_WIDGET (win));
150                 gtk_main();
151         }
152         if (modest_ui)
153                 g_object_unref (G_OBJECT(modest_ui));
154         
155         return retval;
156 }
157
158
159
160 static int
161 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
162            const gchar* subject, const gchar *body)
163 {
164         ModestMailOperation *mail_operation = NULL;
165         TnyList *accounts = NULL;
166         TnyIterator *iter = NULL;
167         TnyTransportAccount *account = NULL;    
168         int retval;
169
170         accounts = TNY_LIST(tny_simple_list_new ());
171         tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
172                                         accounts,
173                                         TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
174
175         iter = tny_list_create_iterator(accounts);
176         tny_iterator_first (iter);
177         if (tny_iterator_is_done (iter)) {
178                 g_printerr("modest: no transport accounts defined\n");
179                 retval = MODEST_ERR_SEND;
180                 goto cleanup;
181         }
182
183         account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
184         mail_operation = modest_mail_operation_new ();
185
186         modest_mail_operation_send_new_mail (mail_operation,
187                                              account, "test@example.com",
188                                              mailto, cc, bcc, 
189                                              subject, body, NULL);
190                 
191         if (modest_mail_operation_get_status (mail_operation) == 
192             MODEST_MAIL_OPERATION_STATUS_FAILED) {
193                 retval = MODEST_ERR_SEND;
194                 goto cleanup;
195         } else
196                 retval = MODEST_ERR_NONE; /* hurray! */
197 cleanup:
198         if (iter)
199                 g_object_unref (G_OBJECT (iter));
200         if (accounts)
201                 g_object_unref (G_OBJECT (accounts));
202         if (mail_operation)
203                 g_object_unref (G_OBJECT (mail_operation));
204         return retval;
205 }
206