1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
33 #include <glib/gi18n.h>
34 #include <gtk/gtkwidget.h>
37 #include <tny-transport-account.h>
38 #include <tny-account-store.h>
40 #include <tny-simple-list.h>
42 #include <modest-runtime.h>
43 #include <modest-platform.h>
44 #include <modest-init.h>
46 #include <modest-defs.h>
47 #include <modest-tny-account-store.h>
48 #include <modest-tny-platform-factory.h>
49 #include <modest-mail-operation.h>
50 #include <modest-tny-account.h>
51 #include <modest-tny-msg.h>
52 #include <modest-account-mgr.h>
53 #include <modest-account-mgr-helpers.h>
55 #include <widgets/modest-main-window.h>
56 #include <widgets/modest-msg-edit-window.h>
59 MODEST_ERR_NONE = 0, /* no error */
60 MODEST_ERR_OPTIONS = 1, /* error in the options */
61 MODEST_ERR_CONF = 2, /* error getting confuration db */
62 MODEST_ERR_UI = 3, /* error in the UI */
63 MODEST_ERR_HILDON = 4, /* error with Hildon (maemo-only) */
64 MODEST_ERR_RUN = 5, /* error running */
65 MODEST_ERR_SEND = 6, /* error sending mail */
66 MODEST_ERR_PARAM = 7, /* error in one or more of the parameters */
67 MODEST_ERR_INIT = 8 /* error in initialization */
70 static gchar* check_account (const gchar *account);
72 static ModestErrorCode start_ui (const gchar *account,
73 const gchar* mailto, const gchar *cc,
74 const gchar *bcc, const gchar* subject, const gchar *body, GtkWidget **ui);
76 static ModestErrorCode send_mail (const gchar* account,
77 const gchar* mailto, const gchar *cc, const gchar *bcc,
78 const gchar* subject, const gchar *body);
81 on_show (GtkWidget *widget, gpointer user_data)
83 if (modest_conf_get_bool (modest_runtime_get_conf(),
84 MODEST_CONF_CONNECT_AT_STARTUP, NULL))
85 modest_platform_connect_and_wait(NULL);
89 main (int argc, char *argv[])
91 GOptionContext *context = NULL;
94 int retval = MODEST_ERR_NONE;
96 static gboolean batch = FALSE;
97 static gchar *mailto=NULL, *subject=NULL, *bcc=NULL,
98 *cc=NULL, *body=NULL, *account=NULL;
99 gchar *account_or_default;
100 static GOptionEntry options[] = {
101 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
102 N_("New email to <addresses> (comma-separated)"), NULL},
103 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
104 N_("Subject for a new mail"), NULL},
105 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
106 N_("Body for a new email"), NULL},
107 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
108 N_("Cc: addresses for a new mail (comma-separated)"), NULL},
109 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
110 N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
111 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
112 N_("Account to use (if none specified, the default account will be used)"), NULL},
113 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
114 N_("Run in batch mode (don't show UI)"), NULL},
115 { NULL, 0, 0, 0, NULL, NULL, NULL }
118 /* Enable threads before anything else, to prevent memory corruption */
119 if (!g_thread_supported())
123 context = g_option_context_new (NULL);
124 g_option_context_add_main_entries (context, options, NULL);
126 if (!g_option_context_parse (context, &argc, &argv, &err)) {
127 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
128 err ? err->message : "");
130 g_option_context_free (context);
131 retval = MODEST_ERR_OPTIONS;
134 g_option_context_free (context);
136 if (!modest_init_init_core (argc, argv)) {
137 g_printerr ("modest: cannot init modest core\n");
138 return MODEST_ERR_INIT;
141 account_or_default = check_account (account);
145 if (!modest_init_init_ui (argc, argv)) {
146 g_printerr ("modest: cannot init modest ui\n");
147 retval = MODEST_ERR_UI;
150 GtkWidget *ui = NULL;
151 retval = start_ui (account_or_default,
152 mailto, cc, bcc, subject, body, &ui);
154 g_signal_connect (G_OBJECT (ui), "show", G_CALLBACK(on_show), NULL);
157 if (!account_or_default) {
158 g_printerr ("modest: no account has been defined yet\n");
159 retval = MODEST_ERR_CONF;
162 retval = send_mail (account_or_default,
163 mailto, cc, bcc, subject, body);
174 if (!modest_init_uninit ())
175 g_printerr ("modest: modest_init_uninit failed\n");
181 static ModestErrorCode
182 start_ui (const gchar *account_name, const gchar* mailto, const gchar *cc, const gchar *bcc,
183 const gchar* subject, const gchar *body, GtkWidget **ui)
185 ModestWindow *win = NULL;
187 if (mailto||cc||bcc||subject||body) {
194 g_printerr ("modest: no valid account provided, "
195 "nor is default one available\n");
196 return MODEST_ERR_PARAM;
198 from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
200 msg = modest_tny_msg_new (mailto,from,cc,bcc,subject,body,NULL);
202 g_printerr ("modest: failed to create message\n");
204 return MODEST_ERR_SEND;
207 account = modest_tny_account_store_get_transport_account_for_open_connection (
208 modest_runtime_get_account_store(), account_name);
210 g_printerr ("modest: failed to get tny account folder\n");
212 g_object_unref (G_OBJECT(msg));
213 return MODEST_ERR_SEND;
216 folder = modest_tny_account_get_special_folder (account,
217 TNY_FOLDER_TYPE_DRAFTS);
219 g_printerr ("modest: failed to find Drafts folder\n");
221 g_object_unref (G_OBJECT(msg));
222 g_object_unref (G_OBJECT(account));
223 return MODEST_ERR_SEND;
225 tny_folder_add_msg (folder, msg, NULL); /* FIXME: check err */
227 win = modest_msg_edit_window_new (msg, account_name);
229 g_object_unref (G_OBJECT(msg));
230 g_object_unref (G_OBJECT(account));
231 g_object_unref (G_OBJECT(folder));
234 win = modest_main_window_new ();
237 g_printerr ("modest: failed to create window\n");
238 return MODEST_ERR_UI;
240 ModestWindowMgr *mgr = modest_runtime_get_window_mgr ();
241 modest_window_mgr_register_window (mgr, win);
242 *ui = (GtkWidget*) win;
243 g_object_unref (win);
246 gtk_widget_show_all (GTK_WIDGET (win));
249 return MODEST_ERR_NONE;
253 check_account (const gchar* account)
256 ModestAccountMgr *account_mgr;
258 account_mgr = modest_runtime_get_account_mgr();
261 retval = modest_account_mgr_get_default_account (account_mgr);
263 retval = g_strdup (account);
266 if (!account || !modest_account_mgr_account_exists (account_mgr, account, FALSE)) {
273 static ModestErrorCode
274 send_mail (const gchar* account_name,
275 const gchar* mailto, const gchar *cc, const gchar *bcc,
276 const gchar* subject, const gchar *body)
279 TnyTransportAccount *account;
280 ModestMailOperation *mail_operation = NULL;
283 g_return_val_if_fail (account_name, MODEST_ERR_SEND);
285 ////////////////////// FIXME ////////
286 modest_runtime_not_implemented (NULL);
287 return MODEST_ERR_NONE;
288 //////////////////////////////////////
290 account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_transport_account_for_open_connection
291 (modest_runtime_get_account_store(), account_name));
293 g_printerr ("modest: no transport defined account for %s\n",
295 return MODEST_ERR_SEND;
297 from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
300 mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
302 modest_mail_operation_send_new_mail (mail_operation, account,
305 cc, bcc, subject, body, NULL /* html_body */,
306 NULL /* attachments */, 0 /* priority */);
307 if (modest_mail_operation_get_status (mail_operation) ==
308 MODEST_MAIL_OPERATION_STATUS_FAILED) {
309 retval = MODEST_ERR_SEND;
311 retval = MODEST_ERR_NONE; /* hurray! */
313 g_object_unref (G_OBJECT(account));
314 g_object_unref (G_OBJECT(mail_operation));
315 g_free (from_string);