1 /* Copyright (c) 2007, 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.
30 #include "modest-dbus-callbacks.h"
31 #include "modest-runtime.h"
32 #include "modest-account-mgr.h"
33 #include "modest-account-mgr-helpers.h"
34 #include "modest-tny-account.h"
35 #include "modest-ui-actions.h"
36 #include "widgets/modest-msg-edit-window.h"
37 #include "modest-tny-msg.h"
38 #include <libgnomevfs/gnome-vfs-utils.h>
52 on_idle_send_mail(gpointer user_data)
54 SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
56 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
57 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
58 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
60 g_printerr ("modest: no account found\n");
63 TnyTransportAccount *transport_account = NULL;
65 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
66 (modest_runtime_get_account_store(),
70 if (!transport_account) {
71 g_printerr ("modest: no transport account found for '%s'\n", account_name);
74 /* Create the mail operation: */
75 if (transport_account) {
76 /* Use the mail operation: */
77 gchar * from = modest_account_mgr_get_from_string (account_mgr,
80 g_printerr ("modest: no from address for account '%s'\n", account_name);
82 ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND, NULL);
83 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
85 modest_mail_operation_send_new_mail (mail_operation,
88 idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject,
89 idle_data->body, /* plain_body */
91 NULL, /* attachments_list, GSList of TnyMimePart. */
95 g_object_unref (G_OBJECT (mail_operation));
98 g_object_unref (G_OBJECT (transport_account));
101 g_free (account_name);
103 /* Free the idle data: */
104 g_free (idle_data->to);
105 g_free (idle_data->cc);
106 g_free (idle_data->bcc);
107 g_free (idle_data->subject);
108 g_free (idle_data->body);
111 return FALSE; /* Do not call this callback again. */
114 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
116 if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
119 /* Use g_idle to context-switch into the application's thread: */
120 SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
122 /* Get the arguments: */
123 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
124 idle_data->to = g_strdup (val.value.s);
126 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
127 idle_data->cc = g_strdup (val.value.s);
129 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
130 idle_data->bcc = g_strdup (val.value.s);
132 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
133 idle_data->subject = g_strdup (val.value.s);
135 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
136 idle_data->body = g_strdup (val.value.s);
138 /* printf(" debug: to=%s\n", idle_data->to); */
139 g_idle_add(on_idle_send_mail, (gpointer)idle_data);
141 /* Note that we cannot report failures during sending,
142 * because that would be asynchronous. */
147 * @uri An escaped URI. URIs should always be escaped.
148 * @len The length of the @uri string, or -1 if the string is null terminated.
150 * Decode a URI, or URI fragment, as per RFC 1738.
151 * http://www.ietf.org/rfc/rfc1738.txt
153 * Return value: An unescaped string. This should be freed with g_free().
155 static gchar* uri_unescape(const gchar* uri, size_t len)
163 /* Allocate an extra string so we can be sure that it is null-terminated,
164 * so we can use gnome_vfs_unescape_string().
165 * This is not efficient. */
166 gchar * escaped_nullterminated = g_strndup (uri, len);
167 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
168 g_free (escaped_nullterminated);
173 /** uri_parse_mailto:
174 * @mailto A mailto URI, with the mailto: prefix.
175 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
176 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
177 * all the string items have been freed. This parameter may be NULL.
178 * Parse a mailto URI as per RFC2368.
179 * http://www.ietf.org/rfc/rfc2368.txt
181 * Return value: The to address, unescaped. This should be freed with g_free().
183 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
185 const gchar* start_to = NULL;
186 /* Remove the mailto: prefix:
187 * 7 is the length of "mailto:": */
188 if (strncmp (mailto, "mailto:", 7) == 0) {
189 start_to = mailto + 7;
195 /* Look for ?, or the end of the string, marking the end of the to address: */
196 const size_t len_to = strcspn (start_to, "?");
197 gchar* result_to = uri_unescape (start_to, len_to);
198 printf("debug: result_to=%s\n", result_to);
200 /* Get any other items: */
201 const size_t len_mailto = strlen (start_to);
202 const gchar* p = start_to + len_to + 1; /* parsed so far. */
203 const gchar* end = start_to + len_mailto;
204 /* GSList *items = NULL; */
205 const gchar* start_item_name = p;
206 size_t len_item_name = 0;
207 const gchar* start_item_value = NULL;
210 /* Looking for the end of a name; */
211 if (start_item_name) {
212 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
214 /* This marks the end of a name and the start of the value: */
217 /* Skip over the name and mark the start of the value: */
218 p += (len + 1); /* Skip over the = */
219 start_item_value = p;
223 /* Looking for the end of a value: */
224 if (start_item_value) {
225 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
226 /* ? marks the start of a new item: */
228 if (start_item_name && len_item_name) {
229 /* Finish the previously-started item: */
230 gchar *item_value = uri_unescape (start_item_value, len);
231 gchar *item_name = g_strndup (start_item_name, len_item_name);
232 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
234 /* Append the items to the list */
235 if(list_items_and_values) {
236 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
237 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
241 /* Skip over the value and mark the start of a possible new name/value pair: */
242 p += (len + 1); /* Skip over the ? */
245 start_item_value = NULL;
256 on_idle_mail_to(gpointer user_data)
258 /* This is based on the implemenation of main.c:start_uil(): */
260 gchar *uri = (gchar*)user_data;
261 GSList *list_names_and_values = NULL;
262 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
264 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
265 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
266 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
268 g_printerr ("modest: no account found\n");
271 TnyAccount *account = NULL;
273 account = modest_tny_account_store_get_transport_account_for_open_connection (
274 modest_runtime_get_account_store(), account_name);
278 g_printerr ("modest: failed to get tny account folder'\n", account_name);
280 gchar * from = modest_account_mgr_get_from_string (account_mgr,
283 g_printerr ("modest: no from address for account '%s'\n", account_name);
285 const gchar *cc = NULL;
286 const gchar *bcc = NULL;
287 const gchar *subject = NULL;
288 const gchar *body = NULL;
290 /* Get the relevant items from the list: */
291 GSList *list = list_names_and_values;
293 const gchar * name = (const gchar*)list->data;
294 GSList *list_value = g_slist_next (list);
295 const gchar * value = (const gchar*)list_value->data;
297 if (strcmp (name, "cc") == 0) {
299 } else if (strcmp (name, "bcc") == 0) {
301 } else if (strcmp (name, "subject") == 0) {
303 } else if (strcmp (name, "body") == 0) {
307 /* Go to the next pair: */
309 list = g_slist_next (list_value);
314 /* Create the message: */
315 TnyMsg *msg = modest_tny_msg_new (to, from,
316 cc, bcc, subject, body,
317 NULL /* attachments */);
320 g_printerr ("modest: failed to create message\n");
323 /* Add the message to a folder and show its UI for editing: */
324 TnyFolder *folder = modest_tny_account_get_special_folder (account,
325 TNY_FOLDER_TYPE_DRAFTS);
327 g_printerr ("modest: failed to find Drafts folder\n");
330 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
332 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
333 gtk_widget_show_all (GTK_WIDGET (win));
335 g_object_unref (G_OBJECT(folder));
338 g_object_unref (G_OBJECT(msg));
341 g_object_unref (G_OBJECT(account));
345 g_free (account_name);
347 /* Free the list, as required by the uri_parse_mailto() documentation: */
348 if (list_names_and_values)
349 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
350 g_slist_free (list_names_and_values);
356 return FALSE; /* Do not call this callback again. */
359 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
361 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
364 /* Use g_idle to context-switch into the application's thread: */
366 /* Get the arguments: */
367 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
368 gchar *uri = g_strdup (val.value.s);
370 /* printf(" debug: to=%s\n", idle_data->to); */
371 g_idle_add(on_idle_mail_to, (gpointer)uri);
373 /* Note that we cannot report failures during sending,
374 * because that would be asynchronous. */
380 on_idle_open_message(gpointer user_data)
382 gchar *uri = (gchar*)user_data;
384 g_message ("not implemented yet %s", __FUNCTION__);
387 return FALSE; /* Do not call this callback again. */
390 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
392 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
395 /* Use g_idle to context-switch into the application's thread: */
397 /* Get the arguments: */
398 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
399 gchar *uri = g_strdup (val.value.s);
401 /* printf(" debug: to=%s\n", idle_data->to); */
402 g_idle_add(on_idle_open_message, (gpointer)uri);
404 /* Note that we cannot report failures during sending,
405 * because that would be asynchronous. */
410 on_idle_send_receive(gpointer user_data)
414 /* Pick the main window if it exists */
415 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
417 /* Send & receive all if "Update automatically" is set */
418 /* TODO: check the auto-update parametter in the configuration */
419 modest_ui_actions_do_send_receive_all (win);
421 return FALSE; /* Do not call this callback again. */
424 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
426 /* Use g_idle to context-switch into the application's thread: */
428 /* This method has no arguments. */
430 /* printf(" debug: to=%s\n", idle_data->to); */
431 g_idle_add(on_idle_send_receive, NULL);
433 /* Note that we cannot report failures during send/receive,
434 * because that would be asynchronous. */
438 /* Callback for normal D-BUS messages */
439 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
440 GArray * arguments, gpointer data,
444 printf("debug: modest_dbus_req_handler()\n");
445 printf("debug: method received: %s\n", method);
447 if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
448 return on_send_mail (arguments, data, retval);
449 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
450 return on_mail_to (arguments, data, retval);
451 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
452 return on_open_message (arguments, data, retval);
453 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
454 return on_send_receive (arguments, data, retval);
461 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
463 printf("%s()\n", __PRETTY_FUNCTION__);
465 if(state->system_inactivity_ind)
468 else if(state->save_unsaved_data_ind)
476 printf("debug: %s(): return\n", __PRETTY_FUNCTION__);