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-defs.h>
44 #include <modest-ui.h>
45 #include <modest-tny-account-store.h>
46 #include <modest-tny-platform-factory.h>
47 #include <modest-mail-operation.h>
48 #include <modest-tny-account.h>
49 #include <modest-account-mgr.h>
50 #include <modest-account-mgr-helpers.h>
52 static gchar* check_account (const gchar *account);
53 static int start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
54 const gchar* subject, const gchar *body);
55 static int send_mail (const gchar* account,
56 const gchar* mailto, const gchar *cc, const gchar *bcc,
57 const gchar* subject, const gchar *body);
59 main (int argc, char *argv[])
61 GOptionContext *context = NULL;
64 int retval = MODEST_ERR_NONE;
66 static gboolean batch = FALSE;
67 static gchar *mailto=NULL, *subject=NULL, *bcc=NULL,
68 *cc=NULL, *body=NULL, *account=NULL;
70 static GOptionEntry options[] = {
71 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
72 N_("New email to <addresses> (comma-separated)"), NULL},
73 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
74 N_("Subject for a new mail"), NULL},
75 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
76 N_("Body for a new email"), NULL},
77 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
78 N_("Cc: addresses for a new mail (comma-separated)"), NULL},
79 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
80 N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
81 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
82 N_("Account to use (if none specified, the default account will be used)"), NULL},
83 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
84 N_("Run in batch mode (don't show UI)"), NULL},
85 { NULL, 0, 0, 0, NULL, NULL, NULL }
88 if (!modest_runtime_init ()) {
89 g_printerr ("modest: cannot init runtime\n");
90 return MODEST_ERR_INIT;
93 context = g_option_context_new (NULL);
94 g_option_context_add_main_entries (context, options, NULL);
96 if (!g_option_context_parse (context, &argc, &argv, &err)) {
97 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
98 err ? err->message : "");
100 g_option_context_free (context);
101 retval = MODEST_ERR_OPTIONS;
104 g_option_context_free (context);
107 if (!modest_runtime_init_ui (argc, argv)) {
108 g_printerr ("modest: cannot start UI\n");
109 retval = MODEST_ERR_UI;
112 retval = start_ui (mailto, cc, bcc, subject, body);
114 gchar *account_or_default;
115 account_or_default = check_account (account);
118 if (!account_or_default) {
119 g_printerr ("modest: account is not valid\n");
120 retval = MODEST_ERR_PARAM;
123 retval = send_mail (account, mailto, cc, bcc, subject, body);
134 if (!modest_runtime_uninit ())
135 g_printerr ("modest: modest_runtime_uninit failed\n");
142 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
143 const gchar* subject, const gchar *body)
145 ModestWindow *win = NULL;
146 ModestUI *modest_ui = NULL;
149 modest_ui = modest_ui_new ();
151 if (mailto||cc||bcc||subject||body) {
152 g_warning ("FIXME: implement this");
153 /* ok = modest_ui_new_edit_window (modest_ui, */
154 /* mailto, /\* to *\/ */
156 /* bcc, /\* bcc *\/ */
157 /* subject, /\* subject *\/ */
158 /* body, /\* body *\/ */
159 /* NULL); /\* attachments *\/ */
161 win = modest_ui_main_window (modest_ui);
164 gtk_widget_show_all (GTK_WIDGET (win));
168 g_object_unref (G_OBJECT(modest_ui));
174 check_account (const gchar* account)
177 ModestAccountMgr *account_mgr;
179 account_mgr = modest_runtime_get_account_mgr();
182 retval = modest_account_mgr_get_default_account (account_mgr);
184 retval = g_strdup (account);
187 if (!modest_account_mgr_account_exists (account_mgr, account, FALSE, NULL)) {
195 send_mail (const gchar* account_name,
196 const gchar* mailto, const gchar *cc, const gchar *bcc,
197 const gchar* subject, const gchar *body)
200 TnyTransportAccount *account;
201 ModestMailOperation *mail_operation = NULL;
204 g_return_val_if_fail (account_name, MODEST_ERR_SEND);
206 account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_tny_account_by_account
207 (modest_runtime_get_account_store(), account_name,
208 TNY_ACCOUNT_TYPE_TRANSPORT));
210 g_printerr ("modest: no transport defined account for %s\n",
212 return MODEST_ERR_SEND;
214 from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(),
217 mail_operation = modest_mail_operation_new ();
218 modest_mail_operation_send_new_mail (mail_operation, account,
220 cc, bcc, subject, body,
222 if (modest_mail_operation_get_status (mail_operation) ==
223 MODEST_MAIL_OPERATION_STATUS_FAILED) {
224 retval = MODEST_ERR_SEND;
226 retval = MODEST_ERR_NONE; /* hurray! */
228 g_object_unref (G_OBJECT(account));
229 g_object_unref (G_OBJECT(mail_operation));
230 g_free (from_string);