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>
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);
56 main (int argc, char *argv[])
58 GOptionContext *context = NULL;
61 int retval = MODEST_ERR_NONE;
63 static gboolean batch = FALSE;
64 static gchar *mailto, *subject, *bcc, *cc, *body, *account;
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 }
84 if (!modest_runtime_init ()) {
85 g_printerr ("modest: cannot init runtime\n");
86 return MODEST_ERR_INIT;
89 context = g_option_context_new (NULL);
90 g_option_context_add_main_entries (context, options, NULL);
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 : "");
96 retval = MODEST_ERR_OPTIONS;
99 g_option_context_free (context);
102 if (!modest_runtime_init_ui (argc, argv)) {
103 g_printerr ("modest: cannot start UI\n");
104 retval = MODEST_ERR_UI;
107 retval = start_ui (mailto, cc, bcc, subject, body);
109 retval = send_mail (mailto, cc, bcc, subject, body);
112 if (!modest_runtime_uninit ())
113 g_printerr ("modest: modest_runtime_uninit failed\n");
120 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
121 const gchar* subject, const gchar *body)
123 ModestWindow *win = NULL;
124 ModestUI *modest_ui = NULL;
127 modest_ui = modest_ui_new ();
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 *\/ */
134 /* bcc, /\* bcc *\/ */
135 /* subject, /\* subject *\/ */
136 /* body, /\* body *\/ */
137 /* NULL); /\* attachments *\/ */
139 win = modest_ui_main_window (modest_ui);
142 gtk_widget_show_all (GTK_WIDGET (win));
146 g_object_unref (G_OBJECT(modest_ui));
154 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
155 const gchar* subject, const gchar *body)
157 ModestMailOperation *mail_operation = NULL;
158 TnyList *accounts = NULL;
159 TnyIterator *iter = NULL;
160 TnyTransportAccount *account = NULL;
163 accounts = TNY_LIST(tny_simple_list_new ());
164 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()),
166 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
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;
176 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
177 mail_operation = modest_mail_operation_new ();
179 modest_mail_operation_send_new_mail (mail_operation,
180 account, "test@example.com",
182 subject, body, NULL);
184 if (modest_mail_operation_get_status (mail_operation) ==
185 MODEST_MAIL_OPERATION_STATUS_FAILED) {
186 retval = MODEST_ERR_SEND;
189 retval = MODEST_ERR_NONE; /* hurray! */
192 g_object_unref (G_OBJECT (iter));
194 g_object_unref (G_OBJECT (accounts));
196 g_object_unref (G_OBJECT (mail_operation));