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.
32 #include <glib/gi18n.h>
33 #include <gtk/gtkwidget.h>
35 #include <tny-transport-account.h>
36 #include <tny-account-store.h>
38 #include <tny-simple-list.h>
41 #include <modest-conf.h>
42 #include <modest-account-mgr.h>
43 #include <modest-ui.h>
44 #include <modest-debug.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 #include <camel/camel-url.h>
52 #if MODEST_PLATFORM_ID==2 /* maemo */
54 #endif /* MODEST_PLATFORM==2 */
68 static gboolean hildon_init (); /* NOP if HILDON is not defined */
69 static int start_ui (const gchar* mailto, const gchar *cc,
70 const gchar *bcc, const gchar* subject, const gchar *body,
71 TnyAccountStore *account_store);
73 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
74 const gchar* subject, const gchar *body);
77 main (int argc, char *argv[])
79 GOptionContext *context = NULL;
80 TnyPlatformFactory *fact = NULL;
81 TnyAccountStore *account_store = NULL;
82 ModestConf *modest_conf = NULL;
85 int retval = MODEST_ERR_NONE;
87 static gboolean batch=FALSE, factory_settings=FALSE;
88 static gchar *mailto, *subject, *bcc, *cc, *body, *account;
90 static GOptionEntry options[] = {
91 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
92 N_("New email to <addresses> (comma-separated)"), NULL},
93 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
94 N_("Subject for a new mail"), NULL},
95 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
96 N_("Body for a new email"), NULL},
97 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
98 N_("Cc: addresses for a new mail (comma-separated)"), NULL},
99 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
100 N_("Bcc: addresses for a new mail (comma-separated)"), NULL},
101 { "account", 'a', 0, G_OPTION_ARG_STRING, &account,
102 N_("Account to use (if specified, default account is used)"), NULL},
103 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
104 N_("Run in batch mode (don't show UI)"), NULL},
105 { "factory-settings", 0, 0, G_OPTION_ARG_NONE, &factory_settings,
106 N_("return to factory settings"), NULL},
107 { NULL, 0, 0, 0, NULL, NULL, NULL }
110 bindtextdomain (GETTEXT_PACKAGE, MODEST_LOCALEDIR);
111 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
112 textdomain (GETTEXT_PACKAGE);
114 modest_debug_g_type_init ();
115 modest_debug_logging_init ();
117 g_thread_init (NULL);
119 context = g_option_context_new (NULL);
120 g_option_context_add_main_entries (context, options, NULL);
122 if (!g_option_context_parse (context, &argc, &argv, &err)) {
123 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
124 err ? err->message : "");
126 retval = MODEST_ERR_OPTIONS;
129 g_option_context_free (context);
131 /* Get platform factory */
132 fact = modest_tny_platform_factory_get_instance ();
134 /* Check modest conf */
135 modest_conf = modest_tny_platform_factory_get_conf_instance
136 (MODEST_TNY_PLATFORM_FACTORY(fact));
138 g_printerr ("modest: failed to initialize config system, exiting\n");
139 retval = MODEST_ERR_CONF;
143 if (!modest_init_local_folders ()) {
144 g_printerr ("modest: failed to initialize local folders, exiting\n");
145 retval = MODEST_ERR_INIT;
149 /* Get the account store */
150 account_store = tny_platform_factory_new_account_store (fact);
151 if (!account_store) {
152 g_printerr ("modest: could not initialize a ModestTnyAccountStore instance\n");
153 retval = MODEST_ERR_RUN;
158 if (!getenv("DISPLAY"))
163 gtk_init (&argc, &argv);
164 modest_init_header_columns (factory_settings);
166 retval = start_ui (mailto, cc, bcc, subject, body, account_store);
168 retval = send_mail (mailto, cc, bcc, subject, body);
172 g_object_unref (G_OBJECT(fact));
173 /* this will clean up account_store as well */
180 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
181 const gchar* subject, const gchar *body,
182 TnyAccountStore *account_store)
188 modest_ui = MODEST_UI(modest_ui_new (account_store));
190 g_printerr ("modest: failed to initialize ui, exiting\n");
191 retval = MODEST_ERR_UI;
195 modest_icon_factory_init ();
197 if (!hildon_init ()) { /* NOP if hildon is not defined */
198 g_printerr ("modest: failed to initialize hildon, exiting\n");
199 retval = MODEST_ERR_HILDON;
203 if (mailto||cc||bcc||subject||body) {
205 /* ok = modest_ui_new_edit_window (modest_ui, */
206 /* mailto, /\* to *\/ */
208 /* bcc, /\* bcc *\/ */
209 /* subject, /\* subject *\/ */
210 /* body, /\* body *\/ */
211 /* NULL); /\* attachments *\/ */
213 win = modest_ui_main_window (modest_ui);
218 gtk_widget_show (GTK_WIDGET (win));
221 device = tny_account_store_get_device (account_store);
222 tny_device_force_online (device);
223 g_object_unref (G_OBJECT (device));
229 g_object_unref (modest_ui);
231 modest_icon_factory_uninit ();
239 #if MODEST_PLATFORM_ID==2
241 osso_context_t *osso_context =
242 osso_initialize(PACKAGE, PACKAGE_VERSION,
245 g_printerr ("modest: failed to acquire osso context\n");
249 #endif /* MODEST_PLATFORM_ID==2 */
257 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
258 const gchar* subject, const gchar *body)
260 ModestAccountMgr *acc_mgr = NULL;
261 TnyPlatformFactory *fact = NULL;
262 TnyAccountStore *acc_store = NULL;
263 ModestMailOperation *mail_operation = NULL;
265 TnyList *accounts = NULL;
266 TnyIterator *iter = NULL;
267 TnyTransportAccount *account = NULL;
270 fact = modest_tny_platform_factory_get_instance ();
271 acc_mgr = modest_tny_platform_factory_get_account_mgr_instance
272 (MODEST_TNY_PLATFORM_FACTORY(fact));
273 acc_store = tny_platform_factory_new_account_store (fact);
275 accounts = TNY_LIST(tny_simple_list_new ());
276 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
277 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
279 iter = tny_list_create_iterator(accounts);
280 tny_iterator_first (iter);
281 if (tny_iterator_is_done (iter)) {
282 g_printerr("modest: no transport accounts defined\n");
283 retval = MODEST_ERR_SEND;
287 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
289 mail_operation = modest_mail_operation_new ();
291 modest_mail_operation_send_new_mail (mail_operation,
292 account, "test@example.com",
294 subject, body, NULL);
297 if (modest_mail_operation_get_status (mail_operation) ==
298 MODEST_MAIL_OPERATION_STATUS_FAILED) {
299 retval = MODEST_ERR_SEND;
302 retval = MODEST_ERR_NONE; /* hurray! */
306 g_object_unref (G_OBJECT (iter));
308 g_object_unref (G_OBJECT (accounts));
310 g_object_unref (G_OBJECT (mail_operation));