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-defs.h>
43 #include <modest-init.h>
44 #include <modest-conf.h>
45 #include <modest-account-mgr.h>
46 #include <modest-ui.h>
47 #include <modest-debug.h>
48 #include <modest-icon-factory.h>
49 #include <modest-tny-account-store.h>
50 #include <modest-tny-platform-factory.h>
51 #include <modest-mail-operation.h>
53 #if MODEST_PLATFORM_ID==2 /* maemo */
55 #endif /* MODEST_PLATFORM==2 */
57 static gboolean hildon_init (); /* NOP if HILDON is not defined */
58 static int start_ui (const gchar* mailto, const gchar *cc,
59 const gchar *bcc, const gchar* subject, const gchar *body,
60 TnyAccountStore *account_store);
62 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
63 const gchar* subject, const gchar *body);
66 main (int argc, char *argv[])
68 GOptionContext *context = NULL;
69 TnyPlatformFactory *fact = NULL;
70 TnyAccountStore *account_store = NULL;
73 int retval = MODEST_ERR_NONE;
75 static gboolean batch=FALSE, factory_settings=FALSE;
76 static gchar *mailto, *subject, *bcc, *cc, *body, *account;
78 static GOptionEntry options[] = {
79 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
80 N_("New email to <addresses> (comma-separated)"), NULL},
81 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
82 N_("Subject for a new mail"), NULL},
83 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
84 N_("Body for a new email"), NULL},
85 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
86 N_("Cc: addresses for a new mail (comma-separated)"), NULL},
87 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
88 N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
89 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
90 N_("Account to use (if specified, default account is used)"), NULL},
91 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
92 N_("Run in batch mode (don't show UI)"), NULL},
93 { "factory-settings", 0, 0, G_OPTION_ARG_NONE, &factory_settings,
94 N_("return to factory settings"), NULL},
95 { NULL, 0, 0, 0, NULL, NULL, NULL }
98 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
99 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
100 textdomain (GETTEXT_PACKAGE);
102 modest_debug_g_type_init ();
103 modest_debug_logging_init ();
105 g_thread_init (NULL);
106 modest_init_default_account_maybe ();
108 gdk_threads_init (); /* hmmm... not really needed if we're not doing ui*/
110 context = g_option_context_new (NULL);
111 g_option_context_add_main_entries (context, options, NULL);
113 if (!g_option_context_parse (context, &argc, &argv, &err)) {
114 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
115 err ? err->message : "");
117 retval = MODEST_ERR_OPTIONS;
120 g_option_context_free (context);
122 /* Get platform factory */
123 fact = modest_tny_platform_factory_get_instance ();
125 if (!modest_init_local_folders ()) {
126 g_printerr ("modest: failed to initialize local folders, exiting\n");
127 retval = MODEST_ERR_INIT;
131 /* Get the account store */
132 account_store = tny_platform_factory_new_account_store (fact);
133 if (!account_store) {
134 g_printerr ("modest: could not initialize a ModestTnyAccountStore instance\n");
135 retval = MODEST_ERR_RUN;
139 if (!getenv("DISPLAY"))
143 if (!gtk_init_check(&argc, &argv)) {
144 g_printerr ("modest: failed to start graphical ui\n");
147 modest_init_header_columns (factory_settings);
148 retval = start_ui (mailto, cc, bcc, subject, body, account_store);
151 retval = send_mail (mailto, cc, bcc, subject, body);
155 g_object_unref (G_OBJECT(fact));
157 /* this will clean up account_store as well */
165 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
166 const gchar* subject, const gchar *body,
167 TnyAccountStore *account_store)
170 ModestWindow *win = NULL;
173 modest_ui = MODEST_UI(modest_ui_new (account_store));
175 g_printerr ("modest: failed to initialize ui, exiting\n");
176 retval = MODEST_ERR_UI;
180 if (!hildon_init ()) { /* NOP if hildon is not defined */
181 g_printerr ("modest: failed to initialize hildon, exiting\n");
182 retval = MODEST_ERR_HILDON;
186 if (mailto||cc||bcc||subject||body) {
187 /* ok = modest_ui_new_edit_window (modest_ui, */
188 /* mailto, /\* to *\/ */
190 /* bcc, /\* bcc *\/ */
191 /* subject, /\* subject *\/ */
192 /* body, /\* body *\/ */
193 /* NULL); /\* attachments *\/ */
195 win = modest_ui_main_window (modest_ui);
200 gtk_widget_show_all (GTK_WIDGET (win));
203 device = tny_account_store_get_device (account_store);
204 tny_device_force_online (device);
205 g_object_unref (G_OBJECT (device));
211 g_object_unref (modest_ui);
220 #if MODEST_PLATFORM_ID==2
222 osso_context_t *osso_context =
223 osso_initialize(PACKAGE, PACKAGE_VERSION,
226 g_printerr ("modest: failed to acquire osso context\n");
229 #endif /* MODEST_PLATFORM_ID==2 */
236 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
237 const gchar* subject, const gchar *body)
239 ModestAccountMgr *acc_mgr = NULL;
240 TnyPlatformFactory *fact = NULL;
241 TnyAccountStore *acc_store = NULL;
242 ModestMailOperation *mail_operation = NULL;
244 TnyList *accounts = NULL;
245 TnyIterator *iter = NULL;
246 TnyTransportAccount *account = NULL;
249 fact = modest_tny_platform_factory_get_instance ();
250 acc_mgr = modest_tny_platform_factory_get_account_mgr_instance
251 (MODEST_TNY_PLATFORM_FACTORY(fact));
252 acc_store = tny_platform_factory_new_account_store (fact);
254 accounts = TNY_LIST(tny_simple_list_new ());
255 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
256 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
258 iter = tny_list_create_iterator(accounts);
259 tny_iterator_first (iter);
260 if (tny_iterator_is_done (iter)) {
261 g_printerr("modest: no transport accounts defined\n");
262 retval = MODEST_ERR_SEND;
266 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
268 mail_operation = modest_mail_operation_new ();
270 modest_mail_operation_send_new_mail (mail_operation,
271 account, "test@example.com",
273 subject, body, NULL);
275 if (modest_mail_operation_get_status (mail_operation) ==
276 MODEST_MAIL_OPERATION_STATUS_FAILED) {
277 retval = MODEST_ERR_SEND;
280 retval = MODEST_ERR_NONE; /* hurray! */
284 g_object_unref (G_OBJECT (iter));
286 g_object_unref (G_OBJECT (accounts));
288 g_object_unref (G_OBJECT (mail_operation));