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>
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-icon-factory.h"
45 #include "modest-tny-account-store.h"
46 #include "modest-tny-platform-factory.h"
47 #include "modest-mail-operation.h"
50 #if MODEST_PLATFORM_ID==2
52 #endif /* MODEST_PLATFORM==2 */
55 #define MODEST_ERR_NONE 0
56 #define MODEST_ERR_OPTIONS 1
57 #define MODEST_ERR_CONF 2
58 #define MODEST_ERR_UI 3
59 #define MODEST_ERR_HILDON 4
60 #define MODEST_ERR_RUN 5
61 #define MODEST_ERR_SEND 6
63 static gboolean hildon_init (); /* NOP if HILDON is not defined */
65 static int start_ui (const gchar* mailto, const gchar *cc,
66 const gchar *bcc, const gchar* subject, const gchar *body);
68 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
69 const gchar* subject, const gchar *body);
72 main (int argc, char *argv[])
74 GOptionContext *context = NULL;
75 TnyPlatformFactory *fact = NULL;
76 ModestConf *modest_conf = NULL;
79 int retval = MODEST_ERR_NONE;
81 static gboolean debug=FALSE, batch=FALSE;
82 static gchar *mailto, *subject, *bcc, *cc, *body;
84 static GOptionEntry options[] = {
85 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
86 "Run in debug mode", NULL},
87 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
88 "New email to <addresses> (comma-separated)", NULL},
89 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
90 "Subject for a new mail", NULL},
91 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
92 "Body for a new email", NULL},
93 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
94 "Cc: addresses for a new mail (comma-separated)", NULL},
95 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
96 "Bcc: addresses for a new mail (comma-separated)", NULL},
97 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
98 "Run in batch mode (don't show UI)", NULL},
99 { NULL, 0, 0, 0, NULL, NULL, NULL }
102 bindtextdomain (GETTEXT_PACKAGE, MODESTLOCALEDIR);
103 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
104 textdomain (GETTEXT_PACKAGE);
107 g_thread_init (NULL);
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 fact = modest_tny_platform_factory_get_instance ();
123 modest_conf = modest_tny_platform_factory_get_modest_conf_instance (fact);
125 g_printerr ("modest: failed to initialize config system, exiting\n");
126 retval = MODEST_ERR_CONF;
131 g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
133 if (!getenv("DISPLAY"))
137 gtk_init (&argc, &argv);
138 retval = start_ui (mailto, cc, bcc, subject, body);
140 retval = send_mail (mailto, cc, bcc, subject, body);
150 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
151 const gchar* subject, const gchar *body)
161 modest_ui = MODEST_UI(modest_ui_new ());
163 g_printerr ("modest: failed to initialize ui, exiting\n");
164 retval = MODEST_ERR_UI;
168 modest_icon_factory_init ();
170 if (!hildon_init ()) { /* NOP if hildon is not defined */
171 g_printerr ("modest: failed to initialize hildon, exiting\n");
172 retval = MODEST_ERR_HILDON;
176 if (mailto||cc||bcc||subject||body) {
178 /* ok = modest_ui_new_edit_window (modest_ui, */
179 /* mailto, /\* to *\/ */
181 /* bcc, /\* bcc *\/ */
182 /* subject, /\* subject *\/ */
183 /* body, /\* body *\/ */
184 /* NULL); /\* attachments *\/ */
186 win = modest_ui_main_window (modest_ui);
189 gtk_widget_show (win);
194 g_object_unref (modest_ui);
196 modest_icon_factory_uninit ();
204 #if MODEST_PLATFORM_ID==2
206 osso_context_t *osso_context =
207 osso_initialize(PACKAGE, PACKAGE_VERSION,
210 g_printerr ("modest: failed to acquire osso context\n");
214 #endif /* MODEST_PLATFORM_ID==2 */
222 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
223 const gchar* subject, const gchar *body)
225 ModestAccountMgr *acc_mgr = NULL;
226 TnyPlatformFactory *fact = NULL;
227 TnyAccountStore *acc_store = NULL;
228 ModestMailOperation *mail_operation;
230 TnyList *accounts = NULL;
231 TnyIterator *iter = NULL;
232 TnyTransportAccount *account = NULL;
235 fact = modest_tny_platform_factory_get_instance ();
236 acc_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
237 acc_store = tny_platform_factory_new_account_store (fact);
239 accounts = TNY_LIST(tny_simple_list_new ());
240 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
241 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
243 iter = tny_list_create_iterator(accounts);
244 tny_iterator_first (iter);
245 if (tny_iterator_is_done (iter)) {
246 g_printerr("modest: no transport accounts defined\n");
247 retval = MODEST_ERR_SEND;
251 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
253 mail_operation = modest_mail_operation_new ();
255 modest_mail_operation_send_new_mail (mail_operation,
257 "djcb@djcbsoftware.nl", mailto, cc, bcc,
258 subject, body, NULL);
261 if (modest_mail_operation_get_status (mail_operation) ==
262 MODEST_MAIL_OPERATION_STATUS_FAILED) {
263 retval = MODEST_ERR_SEND;
266 retval = MODEST_ERR_NONE; /* hurray! */
269 if (iter) g_object_unref (G_OBJECT (iter));
270 if (accounts) g_object_unref (G_OBJECT (accounts));
271 if (mail_operation) g_object_unref (G_OBJECT (mail_operation));