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>
59 } ComposeMailIdleData;
62 on_idle_send_mail(gpointer user_data)
64 SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
66 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
67 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
68 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
70 g_printerr ("modest: no account found\n");
73 TnyTransportAccount *transport_account = NULL;
75 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
76 (modest_runtime_get_account_store(),
80 if (!transport_account) {
81 g_printerr ("modest: no transport account found for '%s'\n", account_name);
84 /* Create the mail operation: */
85 if (transport_account) {
86 /* Use the mail operation: */
87 gchar * from = modest_account_mgr_get_from_string (account_mgr,
90 g_printerr ("modest: no from address for account '%s'\n", account_name);
92 ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
93 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
95 modest_mail_operation_send_new_mail (mail_operation,
98 idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject,
99 idle_data->body, /* plain_body */
100 NULL, /* html_body */
101 NULL, /* attachments_list, GSList of TnyMimePart. */
105 g_object_unref (G_OBJECT (mail_operation));
108 g_object_unref (G_OBJECT (transport_account));
111 g_free (account_name);
113 /* Free the idle data: */
114 g_free (idle_data->to);
115 g_free (idle_data->cc);
116 g_free (idle_data->bcc);
117 g_free (idle_data->subject);
118 g_free (idle_data->body);
121 return FALSE; /* Do not call this callback again. */
124 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
126 if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
129 /* Use g_idle to context-switch into the application's thread: */
130 SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
132 /* Get the arguments: */
133 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
134 idle_data->to = g_strdup (val.value.s);
136 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
137 idle_data->cc = g_strdup (val.value.s);
139 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
140 idle_data->bcc = g_strdup (val.value.s);
142 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
143 idle_data->subject = g_strdup (val.value.s);
145 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
146 idle_data->body = g_strdup (val.value.s);
148 /* printf(" debug: to=%s\n", idle_data->to); */
149 g_idle_add(on_idle_send_mail, (gpointer)idle_data);
151 /* Note that we cannot report failures during sending,
152 * because that would be asynchronous. */
157 * @uri An escaped URI. URIs should always be escaped.
158 * @len The length of the @uri string, or -1 if the string is null terminated.
160 * Decode a URI, or URI fragment, as per RFC 1738.
161 * http://www.ietf.org/rfc/rfc1738.txt
163 * Return value: An unescaped string. This should be freed with g_free().
165 static gchar* uri_unescape(const gchar* uri, size_t len)
173 /* Allocate an extra string so we can be sure that it is null-terminated,
174 * so we can use gnome_vfs_unescape_string().
175 * This is not efficient. */
176 gchar * escaped_nullterminated = g_strndup (uri, len);
177 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
178 g_free (escaped_nullterminated);
183 /** uri_parse_mailto:
184 * @mailto A mailto URI, with the mailto: prefix.
185 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
186 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
187 * all the string items have been freed. This parameter may be NULL.
188 * Parse a mailto URI as per RFC2368.
189 * http://www.ietf.org/rfc/rfc2368.txt
191 * Return value: The to address, unescaped. This should be freed with g_free().
193 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
195 const gchar* start_to = NULL;
196 /* Remove the mailto: prefix:
197 * 7 is the length of "mailto:": */
198 if (strncmp (mailto, "mailto:", 7) == 0) {
199 start_to = mailto + 7;
205 /* Look for ?, or the end of the string, marking the end of the to address: */
206 const size_t len_to = strcspn (start_to, "?");
207 gchar* result_to = uri_unescape (start_to, len_to);
208 printf("debug: result_to=%s\n", result_to);
210 /* Get any other items: */
211 const size_t len_mailto = strlen (start_to);
212 const gchar* p = start_to + len_to + 1; /* parsed so far. */
213 const gchar* end = start_to + len_mailto;
214 /* GSList *items = NULL; */
215 const gchar* start_item_name = p;
216 size_t len_item_name = 0;
217 const gchar* start_item_value = NULL;
220 /* Looking for the end of a name; */
221 if (start_item_name) {
222 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
224 /* This marks the end of a name and the start of the value: */
227 /* Skip over the name and mark the start of the value: */
228 p += (len + 1); /* Skip over the = */
229 start_item_value = p;
233 /* Looking for the end of a value: */
234 if (start_item_value) {
235 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
236 /* ? marks the start of a new item: */
238 if (start_item_name && len_item_name) {
239 /* Finish the previously-started item: */
240 gchar *item_value = uri_unescape (start_item_value, len);
241 gchar *item_name = g_strndup (start_item_name, len_item_name);
242 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
244 /* Append the items to the list */
245 if(list_items_and_values) {
246 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
247 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
251 /* Skip over the value and mark the start of a possible new name/value pair: */
252 p += (len + 1); /* Skip over the ? */
255 start_item_value = NULL;
266 on_idle_mail_to(gpointer user_data)
268 /* This is based on the implemenation of main.c:start_uil(): */
270 gchar *uri = (gchar*)user_data;
271 GSList *list_names_and_values = NULL;
272 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
274 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
275 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
276 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
278 g_printerr ("modest: no account found\n");
281 TnyAccount *account = NULL;
283 account = modest_tny_account_store_get_transport_account_for_open_connection (
284 modest_runtime_get_account_store(), account_name);
288 g_printerr ("modest: failed to get tny account folder'\n", account_name);
290 gchar * from = modest_account_mgr_get_from_string (account_mgr,
293 g_printerr ("modest: no from address for account '%s'\n", account_name);
295 const gchar *cc = NULL;
296 const gchar *bcc = NULL;
297 const gchar *subject = NULL;
298 const gchar *body = NULL;
300 /* Get the relevant items from the list: */
301 GSList *list = list_names_and_values;
303 const gchar * name = (const gchar*)list->data;
304 GSList *list_value = g_slist_next (list);
305 const gchar * value = (const gchar*)list_value->data;
307 if (strcmp (name, "cc") == 0) {
309 } else if (strcmp (name, "bcc") == 0) {
311 } else if (strcmp (name, "subject") == 0) {
313 } else if (strcmp (name, "body") == 0) {
317 /* Go to the next pair: */
319 list = g_slist_next (list_value);
324 /* Create the message: */
325 TnyMsg *msg = modest_tny_msg_new (to, from,
326 cc, bcc, subject, body,
327 NULL /* attachments */);
330 g_printerr ("modest: failed to create message\n");
333 /* Add the message to a folder and show its UI for editing: */
334 TnyFolder *folder = modest_tny_account_get_special_folder (account,
335 TNY_FOLDER_TYPE_DRAFTS);
337 g_printerr ("modest: failed to find Drafts folder\n");
340 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
342 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
343 gtk_widget_show_all (GTK_WIDGET (win));
345 g_object_unref (G_OBJECT(folder));
348 g_object_unref (G_OBJECT(msg));
351 g_object_unref (G_OBJECT(account));
355 g_free (account_name);
357 /* Free the list, as required by the uri_parse_mailto() documentation: */
358 if (list_names_and_values)
359 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
360 g_slist_free (list_names_and_values);
366 return FALSE; /* Do not call this callback again. */
369 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
371 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
374 /* Use g_idle to context-switch into the application's thread: */
376 /* Get the arguments: */
377 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
378 gchar *uri = g_strdup (val.value.s);
380 /* printf(" debug: to=%s\n", idle_data->to); */
381 g_idle_add(on_idle_mail_to, (gpointer)uri);
383 /* Note that we cannot report failures during sending,
384 * because that would be asynchronous. */
390 on_idle_compose_mail(gpointer user_data)
392 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
394 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
395 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
396 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
398 g_printerr ("modest: no account found\n");
401 TnyAccount *account = NULL;
403 account = modest_tny_account_store_get_transport_account_for_open_connection (
404 modest_runtime_get_account_store(), account_name);
408 g_printerr ("modest: failed to get tny account folder'\n", account_name);
410 gchar * from = modest_account_mgr_get_from_string (account_mgr,
413 g_printerr ("modest: no from address for account '%s'\n", account_name);
416 /* Create the message: */
417 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
418 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
419 idle_data->attachments);
422 g_printerr ("modest: failed to create message\n");
425 /* Add the message to a folder and show its UI for editing: */
426 TnyFolder *folder = modest_tny_account_get_special_folder (account,
427 TNY_FOLDER_TYPE_DRAFTS);
429 g_printerr ("modest: failed to find Drafts folder\n");
432 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
434 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
435 gtk_widget_show_all (GTK_WIDGET (win));
437 g_object_unref (G_OBJECT(folder));
440 g_object_unref (G_OBJECT(msg));
443 g_object_unref (G_OBJECT(account));
447 /* Free the idle data: */
448 g_free (idle_data->to);
449 g_free (idle_data->cc);
450 g_free (idle_data->bcc);
451 g_free (idle_data->subject);
452 g_free (idle_data->body);
453 g_free (idle_data->attachments);
456 g_free (account_name);
457 return FALSE; /* Do not call this callback again. */
460 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
465 if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
468 /* Use g_idle to context-switch into the application's thread: */
469 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
471 /* Get the arguments: */
472 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
473 idle_data->to = g_strdup (val.value.s);
475 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
476 idle_data->cc = g_strdup (val.value.s);
478 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
479 idle_data->bcc = g_strdup (val.value.s);
481 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
482 idle_data->subject = g_strdup (val.value.s);
484 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
485 idle_data->body = g_strdup (val.value.s);
487 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
488 gchar *attachments_str = g_strdup (val.value.s);
490 list = g_strsplit(attachments_str, ",", 0);
491 for (i=0; list[i] != NULL; i++) {
492 idle_data->attachments = g_slist_append(idle_data->attachments, g_strdup(list[i]));
497 /* printf(" debug: to=%s\n", idle_data->to); */
498 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
500 /* Note that we cannot report failures during sending,
501 * because that would be asynchronous. */
507 on_idle_open_message(gpointer user_data)
509 gchar *uri = (gchar*)user_data;
511 g_message ("not implemented yet %s", __FUNCTION__);
514 return FALSE; /* Do not call this callback again. */
517 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
519 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
522 /* Use g_idle to context-switch into the application's thread: */
524 /* Get the arguments: */
525 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
526 gchar *uri = g_strdup (val.value.s);
528 /* printf(" debug: to=%s\n", idle_data->to); */
529 g_idle_add(on_idle_open_message, (gpointer)uri);
531 /* Note that we cannot report failures during sending,
532 * because that would be asynchronous. */
538 on_idle_send_receive(gpointer user_data)
542 /* Pick the main window if it exists */
543 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
545 /* Send & receive all if "Update automatically" is set */
546 /* TODO: check the auto-update parameter in the configuration */
547 modest_ui_actions_do_send_receive_all (win);
549 return FALSE; /* Do not call this callback again. */
552 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
554 /* Use g_idle to context-switch into the application's thread: */
556 /* This method has no arguments. */
558 /* printf(" debug: to=%s\n", idle_data->to); */
559 g_idle_add(on_idle_send_receive, NULL);
561 /* Note that we cannot report failures during send/receive,
562 * because that would be asynchronous. */
566 /* Callback for normal D-BUS messages */
567 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
568 GArray * arguments, gpointer data,
572 printf("debug: modest_dbus_req_handler()\n");
573 printf("debug: method received: %s\n", method);
575 if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
576 return on_send_mail (arguments, data, retval);
577 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
578 return on_mail_to (arguments, data, retval);
579 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
580 return on_open_message (arguments, data, retval);
581 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
582 return on_send_receive (arguments, data, retval);
583 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
584 return on_compose_mail (arguments, data, retval);
591 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
594 /* printf("%s()\n", __PRETTY_FUNCTION__); */
596 if(state->system_inactivity_ind)
599 else if(state->save_unsaved_data_ind)
607 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */