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-icon-factory.h>
46 #include <modest-tny-account-store.h>
47 #include <modest-tny-platform-factory.h>
48 #include <modest-mail-operation.h>
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);
57 main (int argc, char *argv[])
59 GOptionContext *context = NULL;
62 int retval = MODEST_ERR_NONE;
64 static gboolean batch=FALSE, factory_settings=FALSE;
65 static gchar *mailto, *subject, *bcc, *cc, *body, *account;
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 }
87 if (!modest_runtime_init ()) {
88 g_printerr ("modest: cannot init runtime\n");
89 return MODEST_ERR_INIT;
92 context = g_option_context_new (NULL);
93 g_option_context_add_main_entries (context, options, NULL);
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 : "");
99 retval = MODEST_ERR_OPTIONS;
102 g_option_context_free (context);
104 if (!getenv("DISPLAY"))
108 if (!gtk_init_check(&argc, &argv)) {
109 g_printerr ("modest: failed to start graphical ui\n");
112 retval = start_ui (mailto, cc, bcc, subject, body);
115 retval = send_mail (mailto, cc, bcc, subject, body);
118 if (!modest_runtime_uninit ())
119 g_printerr ("modest: modest_runtime_uninit failed\n");
126 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
127 const gchar* subject, const gchar *body)
129 ModestWindow *win = NULL;
130 ModestUI *modest_ui = NULL;
134 modest_ui = modest_ui_new ();
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 *\/ */
141 /* bcc, /\* bcc *\/ */
142 /* subject, /\* subject *\/ */
143 /* body, /\* body *\/ */
144 /* NULL); /\* attachments *\/ */
146 win = modest_ui_main_window (modest_ui);
149 gtk_widget_show_all (GTK_WIDGET (win));
153 g_object_unref (G_OBJECT(modest_ui));
161 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
162 const gchar* subject, const gchar *body)
164 ModestMailOperation *mail_operation = NULL;
165 TnyList *accounts = NULL;
166 TnyIterator *iter = NULL;
167 TnyTransportAccount *account = NULL;
170 accounts = TNY_LIST(tny_simple_list_new ());
171 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
173 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
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;
183 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
184 mail_operation = modest_mail_operation_new ();
186 modest_mail_operation_send_new_mail (mail_operation,
187 account, "test@example.com",
189 subject, body, NULL);
191 if (modest_mail_operation_get_status (mail_operation) ==
192 MODEST_MAIL_OPERATION_STATUS_FAILED) {
193 retval = MODEST_ERR_SEND;
196 retval = MODEST_ERR_NONE; /* hurray! */
199 g_object_unref (G_OBJECT (iter));
201 g_object_unref (G_OBJECT (accounts));
203 g_object_unref (G_OBJECT (mail_operation));