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>
40 #include "modest-conf.h"
41 #include "modest-account-mgr.h"
42 #include "modest-ui.h"
43 #include "modest-icon-factory.h"
44 #include "modest-tny-account-store.h"
45 #include "modest-tny-platform-factory.h"
48 #ifdef MODEST_ENABLE_HILDON /* Hildon includes */
50 #endif /* MODEST_ENABLE_HILDON */
53 #define MODEST_ERR_NONE 0
54 #define MODEST_ERR_OPTIONS 1
55 #define MODEST_ERR_CONF 2
56 #define MODEST_ERR_UI 3
57 #define MODEST_ERR_HILDON 4
58 #define MODEST_ERR_RUN 5
59 #define MODEST_ERR_SEND 6
61 static gboolean hildon_init (); /* NOP if HILDON is not defined */
63 static int start_ui (const gchar* mailto, const gchar *cc,
64 const gchar *bcc, const gchar* subject, const gchar *body);
66 static int send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
67 const gchar* subject, const gchar *body);
70 main (int argc, char *argv[])
72 GOptionContext *context = NULL;
73 TnyPlatformFactory *fact = NULL;
74 ModestConf *modest_conf = NULL;
77 int retval = MODEST_ERR_NONE;
79 static gboolean debug=FALSE, batch=FALSE;
80 static gchar *mailto, *subject, *bcc, *cc, *body;
82 static GOptionEntry options[] = {
83 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
84 "Run in debug mode", NULL},
85 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
86 "New email to <addresses> (comma-separated)", NULL},
87 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
88 "Subject for a new mail", NULL},
89 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
90 "Body for a new email", NULL},
91 { "cc", 'c', 0, G_OPTION_ARG_STRING, &cc,
92 "Cc: addresses for a new mail (comma-separated)", NULL},
93 { "bcc", 'x', 0, G_OPTION_ARG_STRING, &bcc,
94 "Bcc: addresses for a new mail (comma-separated)", NULL},
95 { "batch", 'y', 0, G_OPTION_ARG_NONE, &batch,
96 "Run in batch mode (don't show UI)", NULL},
97 { NULL, 0, 0, 0, NULL, NULL, NULL }
102 context = g_option_context_new (NULL);
103 g_option_context_add_main_entries (context, options, NULL);
105 if (!g_option_context_parse (context, &argc, &argv, &err)) {
106 g_printerr ("modest: error in command line parameter(s): '%s', exiting\n",
107 err ? err->message : "");
109 retval = MODEST_ERR_OPTIONS;
112 g_option_context_free (context);
114 fact = modest_tny_platform_factory_get_instance ();
115 modest_conf = modest_tny_platform_factory_get_modest_conf_instance (fact);
117 g_printerr ("modest: failed to initialize config system, exiting\n");
118 retval = MODEST_ERR_CONF;
123 g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
125 if (!getenv("DISPLAY"))
129 gtk_init (&argc, &argv);
130 retval = start_ui (mailto, cc, bcc, subject, body);
132 retval = send_mail (mailto, cc, bcc, subject, body);
142 start_ui (const gchar* mailto, const gchar *cc, const gchar *bcc,
143 const gchar* subject, const gchar *body)
153 modest_ui = MODEST_UI(modest_ui_new ());
155 g_printerr ("modest: failed to initialize ui, exiting\n");
156 retval = MODEST_ERR_UI;
160 modest_icon_factory_init ();
162 if (!hildon_init ()) { /* NOP if hildon is not defined */
163 g_printerr ("modest: failed to initialize hildon, exiting\n");
164 retval = MODEST_ERR_HILDON;
168 if (mailto||cc||bcc||subject||body) {
170 /* ok = modest_ui_new_edit_window (modest_ui, */
171 /* mailto, /\* to *\/ */
173 /* bcc, /\* bcc *\/ */
174 /* subject, /\* subject *\/ */
175 /* body, /\* body *\/ */
176 /* NULL); /\* attachments *\/ */
179 win = modest_ui_main_window (modest_ui);
180 gtk_widget_show (win);
182 modest_ui_show_main_window (modest_ui);
188 g_object_unref (modest_ui);
190 modest_icon_factory_uninit ();
198 #ifdef MODEST_ENABLE_HILDON
200 osso_context_t *osso_context =
201 osso_initialize(PACKAGE, PACKAGE_VERSION,
204 g_printerr ("modest: failed to aquire osso context, exiting\n");
209 #endif /* MODEST_ENABLE_HILDON */
217 send_mail (const gchar* mailto, const gchar *cc, const gchar *bcc,
218 const gchar* subject, const gchar *body)
220 ModestAccountMgr *acc_mgr = NULL;
221 TnyPlatformFactory *fact = NULL;
222 TnyAccountStore *acc_store = NULL;
224 TnyList *accounts = NULL;
225 TnyIterator *iter = NULL;
226 TnyTransportAccount *account = NULL;
229 fact = modest_tny_platform_factory_get_instance ();
230 acc_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
231 acc_store = tny_platform_factory_new_account_store (fact);
233 accounts = TNY_LIST(tny_simple_list_new ());
234 tny_account_store_get_accounts (TNY_ACCOUNT_STORE(acc_store), accounts,
235 TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
237 iter = tny_list_create_iterator(accounts);
238 tny_iterator_first (iter);
239 if (tny_iterator_is_done (iter)) {
240 g_printerr("modest: no transport accounts defined\n");
241 retval = MODEST_ERR_SEND;
245 account = TNY_TRANSPORT_ACCOUNT (tny_iterator_get_current(iter));
247 if (!modest_mail_operation_send_mail (account,
248 "djcb@djcbsoftware.nl", mailto, cc, bcc, subject, body,
250 retval = MODEST_ERR_SEND;
253 retval = MODEST_ERR_NONE; /* hurray! */
257 g_object_unref (G_OBJECT(iter));
259 g_object_unref (G_OBJECT(accounts));