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