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"
37 #include "modest-search.h"
38 #include "widgets/modest-msg-edit-window.h"
39 #include "modest-tny-msg.h"
40 #include <libmodest-dbus-client/libmodest-dbus-client.h>
41 #include <libgnomevfs/gnome-vfs-utils.h>
44 #include <glib/gstdio.h>
45 #include <libgnomevfs/gnome-vfs-mime.h>
46 #include <tny-fs-stream.h>
49 #include <tny-iterator.h>
50 #include <tny-simple-list.h>
69 } ComposeMailIdleData;
72 on_idle_send_mail(gpointer user_data)
74 SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
76 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
77 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
78 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
80 g_printerr ("modest: no account found\n");
83 TnyTransportAccount *transport_account = NULL;
85 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
86 (modest_runtime_get_account_store(),
90 if (!transport_account) {
91 g_printerr ("modest: no transport account found for '%s'\n", account_name);
94 /* Create the mail operation: */
95 if (transport_account) {
96 /* Use the mail operation: */
97 gchar * from = modest_account_mgr_get_from_string (account_mgr,
100 g_printerr ("modest: no from address for account '%s'\n", account_name);
102 ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
103 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
105 modest_mail_operation_send_new_mail (mail_operation,
109 idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject,
110 idle_data->body, /* plain_body */
111 NULL, /* html_body */
112 NULL, /* attachments_list, GSList of TnyMimePart. */
116 g_object_unref (G_OBJECT (mail_operation));
119 g_object_unref (G_OBJECT (transport_account));
122 g_free (account_name);
124 /* Free the idle data: */
125 g_free (idle_data->to);
126 g_free (idle_data->cc);
127 g_free (idle_data->bcc);
128 g_free (idle_data->subject);
129 g_free (idle_data->body);
132 return FALSE; /* Do not call this callback again. */
135 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
137 if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
140 /* Use g_idle to context-switch into the application's thread: */
141 SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
143 /* Get the arguments: */
144 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
145 idle_data->to = g_strdup (val.value.s);
147 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
148 idle_data->cc = g_strdup (val.value.s);
150 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
151 idle_data->bcc = g_strdup (val.value.s);
153 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
154 idle_data->subject = g_strdup (val.value.s);
156 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
157 idle_data->body = g_strdup (val.value.s);
159 /* printf(" debug: to=%s\n", idle_data->to); */
160 g_idle_add(on_idle_send_mail, (gpointer)idle_data);
162 /* Note that we cannot report failures during sending,
163 * because that would be asynchronous. */
168 * @uri An escaped URI. URIs should always be escaped.
169 * @len The length of the @uri string, or -1 if the string is null terminated.
171 * Decode a URI, or URI fragment, as per RFC 1738.
172 * http://www.ietf.org/rfc/rfc1738.txt
174 * Return value: An unescaped string. This should be freed with g_free().
176 static gchar* uri_unescape(const gchar* uri, size_t len)
184 /* Allocate an extra string so we can be sure that it is null-terminated,
185 * so we can use gnome_vfs_unescape_string().
186 * This is not efficient. */
187 gchar * escaped_nullterminated = g_strndup (uri, len);
188 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
189 g_free (escaped_nullterminated);
194 /** uri_parse_mailto:
195 * @mailto A mailto URI, with the mailto: prefix.
196 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
197 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
198 * all the string items have been freed. This parameter may be NULL.
199 * Parse a mailto URI as per RFC2368.
200 * http://www.ietf.org/rfc/rfc2368.txt
202 * Return value: The to address, unescaped. This should be freed with g_free().
204 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
206 const gchar* start_to = NULL;
207 /* Remove the mailto: prefix:
208 * 7 is the length of "mailto:": */
209 if (strncmp (mailto, "mailto:", 7) == 0) {
210 start_to = mailto + 7;
216 /* Look for ?, or the end of the string, marking the end of the to address: */
217 const size_t len_to = strcspn (start_to, "?");
218 gchar* result_to = uri_unescape (start_to, len_to);
219 printf("debug: result_to=%s\n", result_to);
221 /* Get any other items: */
222 const size_t len_mailto = strlen (start_to);
223 const gchar* p = start_to + len_to + 1; /* parsed so far. */
224 const gchar* end = start_to + len_mailto;
225 /* GSList *items = NULL; */
226 const gchar* start_item_name = p;
227 size_t len_item_name = 0;
228 const gchar* start_item_value = NULL;
231 /* Looking for the end of a name; */
232 if (start_item_name) {
233 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
235 /* This marks the end of a name and the start of the value: */
238 /* Skip over the name and mark the start of the value: */
239 p += (len + 1); /* Skip over the = */
240 start_item_value = p;
244 /* Looking for the end of a value: */
245 if (start_item_value) {
246 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
247 /* ? marks the start of a new item: */
249 if (start_item_name && len_item_name) {
250 /* Finish the previously-started item: */
251 gchar *item_value = uri_unescape (start_item_value, len);
252 gchar *item_name = g_strndup (start_item_name, len_item_name);
253 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
255 /* Append the items to the list */
256 if(list_items_and_values) {
257 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
258 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
262 /* Skip over the value and mark the start of a possible new name/value pair: */
263 p += (len + 1); /* Skip over the ? */
266 start_item_value = NULL;
277 on_idle_mail_to(gpointer user_data)
279 /* This is based on the implemenation of main.c:start_uil(): */
281 gchar *uri = (gchar*)user_data;
282 GSList *list_names_and_values = NULL;
283 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
285 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
286 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
287 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
289 g_printerr ("modest: no account found\n");
292 TnyAccount *account = NULL;
294 account = modest_tny_account_store_get_transport_account_for_open_connection (
295 modest_runtime_get_account_store(), account_name);
299 g_printerr ("modest: failed to get tny account folder'\n", account_name);
301 gchar * from = modest_account_mgr_get_from_string (account_mgr,
304 g_printerr ("modest: no from address for account '%s'\n", account_name);
306 const gchar *cc = NULL;
307 const gchar *bcc = NULL;
308 const gchar *subject = NULL;
309 const gchar *body = NULL;
311 /* Get the relevant items from the list: */
312 GSList *list = list_names_and_values;
314 const gchar * name = (const gchar*)list->data;
315 GSList *list_value = g_slist_next (list);
316 const gchar * value = (const gchar*)list_value->data;
318 if (strcmp (name, "cc") == 0) {
320 } else if (strcmp (name, "bcc") == 0) {
322 } else if (strcmp (name, "subject") == 0) {
324 } else if (strcmp (name, "body") == 0) {
328 /* Go to the next pair: */
330 list = g_slist_next (list_value);
335 /* Create the message: */
336 TnyMsg *msg = modest_tny_msg_new (to, from,
337 cc, bcc, subject, body,
338 NULL /* attachments */);
341 g_printerr ("modest: failed to create message\n");
344 /* Add the message to a folder and show its UI for editing: */
345 TnyFolder *folder = modest_tny_account_get_special_folder (account,
346 TNY_FOLDER_TYPE_DRAFTS);
348 g_printerr ("modest: failed to find Drafts folder\n");
351 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
353 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
354 gtk_widget_show_all (GTK_WIDGET (win));
356 g_object_unref (G_OBJECT(folder));
359 g_object_unref (G_OBJECT(msg));
362 g_object_unref (G_OBJECT(account));
366 g_free (account_name);
368 /* Free the list, as required by the uri_parse_mailto() documentation: */
369 if (list_names_and_values)
370 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
371 g_slist_free (list_names_and_values);
377 return FALSE; /* Do not call this callback again. */
380 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
382 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
385 /* Use g_idle to context-switch into the application's thread: */
387 /* Get the arguments: */
388 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
389 gchar *uri = g_strdup (val.value.s);
391 /* printf(" debug: to=%s\n", idle_data->to); */
392 g_idle_add(on_idle_mail_to, (gpointer)uri);
394 /* Note that we cannot report failures during sending,
395 * because that would be asynchronous. */
404 on_idle_compose_mail(gpointer user_data)
406 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
410 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
411 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
412 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
414 g_printerr ("modest: no account found\n");
417 TnyAccount *account = NULL;
419 account = modest_tny_account_store_get_transport_account_for_open_connection (
420 modest_runtime_get_account_store(), account_name);
424 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
426 gchar * from = modest_account_mgr_get_from_string (account_mgr,
429 g_printerr ("modest: no from address for account '%s'\n", account_name);
432 /* Create the message: */
433 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
434 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
435 NULL); /* NULL because m_t_m_n doesn't use it */
438 g_printerr ("modest: failed to create message\n");
441 /* Add the message to a folder and show its UI for editing: */
442 TnyFolder *folder = modest_tny_account_get_special_folder (account,
443 TNY_FOLDER_TYPE_DRAFTS);
445 g_printerr ("modest: failed to find Drafts folder\n");
448 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
450 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
452 list = g_strsplit(idle_data->attachments, ",", 0);
453 for (i=0; list[i] != NULL; i++) {
454 modest_msg_edit_window_attach_file_noninteractive(
455 (ModestMsgEditWindow *)win, list[i]);
459 gtk_widget_show_all (GTK_WIDGET (win));
461 g_object_unref (G_OBJECT(folder));
464 g_object_unref (G_OBJECT(msg));
467 g_object_unref (G_OBJECT(account));
471 /* Free the idle data: */
472 g_free (idle_data->to);
473 g_free (idle_data->cc);
474 g_free (idle_data->bcc);
475 g_free (idle_data->subject);
476 g_free (idle_data->body);
477 g_free (idle_data->attachments);
480 g_free (account_name);
481 return FALSE; /* Do not call this callback again. */
484 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
488 if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
491 /* Use g_idle to context-switch into the application's thread: */
492 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
494 /* Get the arguments: */
495 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
496 idle_data->to = g_strdup (val.value.s);
498 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
499 idle_data->cc = g_strdup (val.value.s);
501 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
502 idle_data->bcc = g_strdup (val.value.s);
504 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
505 idle_data->subject = g_strdup (val.value.s);
507 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
508 idle_data->body = g_strdup (val.value.s);
510 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
511 idle_data->attachments = g_strdup (val.value.s);
513 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
515 /* Note that we cannot report failures during sending,
516 * because that would be asynchronous. */
522 find_message_by_url (const char *uri, TnyAccount **ac_out)
525 ModestTnyAccountStore *astore;
534 astore = modest_runtime_get_account_store ();
536 if (astore == NULL) {
540 g_debug ("Got AccountStore, lets go");
542 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
545 if (account == NULL) {
549 g_debug ("Found account");
551 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
555 g_debug ("Account is store account");
559 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
563 if (folder == NULL) {
566 g_debug ("Found folder");
569 msg = tny_folder_find_msg (folder, uri, NULL);
572 if (account && !msg) {
573 g_object_unref (account);
578 g_object_unref (folder);
585 on_idle_open_message (gpointer user_data)
587 ModestWindow *msg_view;
592 const char *account_name;
595 uri = (char *) user_data;
597 g_debug ("Trying to find msg by url: %s", uri);
598 msg = find_message_by_url (uri, &account);
604 g_debug ("Found message");
606 header = tny_msg_get_header (msg);
607 account_name = tny_account_get_name (account);
608 msg_uid = tny_header_get_uid (header);
610 msg_view = modest_msg_view_window_new (msg,
613 /* TODO: does that leak the msg_view ?! */
615 gtk_widget_show_all (GTK_WIDGET (msg_view));
617 g_object_unref (header);
618 g_object_unref (account);
620 return FALSE; /* Do not call this callback again. */
623 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
625 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
628 /* Use g_idle to context-switch into the application's thread: */
630 /* Get the arguments: */
631 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
632 gchar *uri = g_strdup (val.value.s);
634 /* printf(" debug: to=%s\n", idle_data->to); */
635 g_idle_add(on_idle_open_message, (gpointer)uri);
637 /* Note that we cannot report failures during sending,
638 * because that would be asynchronous. */
644 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
650 TnyHeader *msg_header;
659 if (arguments->len != MODEST_DEBUS_DELETE_MESSAGE_ARGS_COUNT) {
663 val = g_array_index (arguments,
665 MODEST_DEBUS_DELETE_MESSAGE_ARG_URI);
667 uri = (const char *) val.value.s;
669 g_debug ("Searching message (delete message)");
671 msg = find_message_by_url (uri, &account);
677 g_debug ("Found message");
679 msg_header = tny_msg_get_header (msg);
680 uid = tny_header_get_uid (msg_header);
681 folder = tny_msg_get_folder (msg);
684 /* tny_msg_get_header () flaw:
685 * From tinythingy doc: You can't use the returned instance with the
686 * TnyFolder operations
688 * To get a header instance that will work with these folder methods,
689 * you can use tny_folder_get_headers.
691 * Ok, we will do so then. Sigh.
693 headers = tny_simple_list_new ();
695 tny_folder_get_headers (folder, headers, TRUE, NULL);
696 iter = tny_list_create_iterator (headers);
699 g_debug ("Searching header for msg in folder");
700 while (!tny_iterator_is_done (iter)) {
703 header = TNY_HEADER (tny_iterator_get_current (iter));
704 cur_id = tny_header_get_uid (header);
706 if (cur_id && uid && g_str_equal (cur_id, uid)) {
707 g_debug ("Found correspoding header from folder");
712 g_object_unref (header);
713 tny_iterator_next (iter);
716 g_object_unref (iter);
717 g_object_unref (headers);
719 g_object_unref (msg_header);
720 g_object_unref (msg);
722 if (header == NULL) {
723 g_object_unref (folder);
730 tny_folder_remove_msg (folder, header, &error);
734 g_error_free (error);
737 g_object_unref (folder);
742 on_idle_send_receive(gpointer user_data)
746 /* Pick the main window if it exists */
747 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
749 /* Send & receive all if "Update automatically" is set */
750 /* TODO: check the auto-update parameter in the configuration */
751 modest_ui_actions_do_send_receive_all (win);
753 return FALSE; /* Do not call this callback again. */
756 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
758 /* Use g_idle to context-switch into the application's thread: */
760 /* This method has no arguments. */
762 /* printf(" debug: to=%s\n", idle_data->to); */
763 g_idle_add(on_idle_send_receive, NULL);
765 /* Note that we cannot report failures during send/receive,
766 * because that would be asynchronous. */
771 on_idle_open_default_inbox(gpointer user_data)
774 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
776 /* Get the folder view */
777 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
778 MODEST_WIDGET_TYPE_FOLDER_VIEW);
779 modest_folder_view_select_first_inbox_or_local (
780 MODEST_FOLDER_VIEW (folder_view));
782 return FALSE; /* Do not call this callback again. */
785 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
787 /* Use g_idle to context-switch into the application's thread: */
789 /* This method has no arguments. */
791 g_idle_add(on_idle_open_default_inbox, NULL);
793 /* Note that we cannot report failures during send/receive,
794 * because that would be asynchronous. */
798 /* Callback for normal D-BUS messages */
799 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
800 GArray * arguments, gpointer data,
804 g_debug ("modest_dbus_req_handler()\n");
805 g_debug ("debug: method received: %s\n", method);
807 if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
808 return on_send_mail (arguments, data, retval);
809 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
810 return on_mail_to (arguments, data, retval);
811 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
812 return on_open_message (arguments, data, retval);
813 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
814 return on_send_receive (arguments, data, retval);
815 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
816 return on_compose_mail (arguments, data, retval);
817 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
818 return on_delete_message (arguments,data, retval);
819 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
820 return on_open_default_inbox (arguments, data, retval);
823 /* We need to return INVALID here so
824 * osso is returning DBUS_HANDLER_RESULT_NOT_YET_HANDLED
825 * so our modest_dbus_req_filter can kick in!
831 #define SEARCH_HIT_DBUS_TYPE \
832 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
833 DBUS_TYPE_STRING_AS_STRING \
834 DBUS_TYPE_STRING_AS_STRING \
835 DBUS_TYPE_STRING_AS_STRING \
836 DBUS_TYPE_STRING_AS_STRING \
837 DBUS_TYPE_UINT64_AS_STRING \
838 DBUS_TYPE_BOOLEAN_AS_STRING \
839 DBUS_TYPE_BOOLEAN_AS_STRING \
840 DBUS_TYPE_INT64_AS_STRING \
841 DBUS_STRUCT_END_CHAR_AS_STRING
844 search_result_to_messsage (DBusMessage *reply,
847 DBusMessageIter iter;
848 DBusMessageIter array_iter;
851 dbus_message_iter_init_append (reply, &iter);
852 dbus_message_iter_open_container (&iter,
854 SEARCH_HIT_DBUS_TYPE,
857 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
858 DBusMessageIter struct_iter;
859 ModestSearchHit *hit;
865 gboolean has_attachment;
869 hit = (ModestSearchHit *) hit_iter->data;
871 msg_url = hit->msgid;
872 subject = hit->subject;
873 folder = hit->folder;
874 sender = hit->sender;
876 has_attachment = hit->has_attachment;
877 is_unread = hit->is_unread;
880 g_debug ("Adding hit: %s", msg_url);
882 dbus_message_iter_open_container (&array_iter,
887 dbus_message_iter_append_basic (&struct_iter,
891 dbus_message_iter_append_basic (&struct_iter,
895 dbus_message_iter_append_basic (&struct_iter,
899 dbus_message_iter_append_basic (&struct_iter,
903 dbus_message_iter_append_basic (&struct_iter,
907 dbus_message_iter_append_basic (&struct_iter,
911 dbus_message_iter_append_basic (&struct_iter,
915 dbus_message_iter_append_basic (&struct_iter,
919 dbus_message_iter_close_container (&array_iter,
923 g_free (hit->subject);
924 g_free (hit->folder);
925 g_free (hit->sender);
927 g_slice_free (ModestSearchHit, hit);
930 dbus_message_iter_close_container (&iter, &array_iter);
936 modest_dbus_req_filter (DBusConnection *con,
937 DBusMessage *message,
940 gboolean handled = FALSE;
943 if (dbus_message_is_method_call (message,
945 MODEST_DBUS_METHOD_SEARCH)) {
946 ModestDBusSearchFlags dbus_flags;
948 DBusMessage *reply = NULL;
952 dbus_int32_t flags_v;
953 dbus_uint32_t serial;
954 dbus_uint32_t size_v;
963 dbus_error_init (&error);
968 res = dbus_message_get_args (message,
970 DBUS_TYPE_STRING, &query,
971 DBUS_TYPE_STRING, &folder,
972 DBUS_TYPE_INT64, &sd_v,
973 DBUS_TYPE_INT64, &ed_v,
974 DBUS_TYPE_INT32, &flags_v,
975 DBUS_TYPE_UINT32, &size_v,
978 dbus_flags = (ModestDBusSearchFlags) flags_v;
979 start_date = (time_t) sd_v;
980 end_date = (time_t) ed_v;
982 memset (&search, 0, sizeof (search));
983 #ifdef MODEST_HAVE_OGS
984 search.query = query;
986 search.before = start_date;
987 search.after = end_date;
990 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
991 search.flags |= MODEST_SEARCH_SUBJECT;
992 search.subject = query;
995 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
996 search.flags |= MODEST_SEARCH_SENDER;
1000 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1001 search.flags |= MODEST_SEARCH_RECIPIENT;
1002 search.recipient = query;
1005 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1006 search.flags |= MODEST_SEARCH_BODY;
1007 search.subject = query;
1011 search.flags |= MODEST_SEARCH_BEFORE;
1012 search.before = start_date;
1016 search.flags |= MODEST_SEARCH_AFTER;
1017 search.after = end_date;
1021 search.flags |= MODEST_SEARCH_SIZE;
1022 search.minsize = size_v;
1025 #ifdef MODEST_HAVE_OGS
1026 search.flags |= MODEST_SEARCH_USE_OGS;
1027 g_debug ("Starting search for %s", search.query);
1029 hits = modest_search_all_accounts (&search);
1031 reply = dbus_message_new_method_return (message);
1033 search_result_to_messsage (reply, hits);
1035 if (reply == NULL) {
1036 g_warning ("Could not create reply");
1040 dbus_connection_send (con, reply, &serial);
1041 dbus_connection_flush (con);
1042 dbus_message_unref (reply);
1051 DBUS_HANDLER_RESULT_HANDLED :
1052 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1057 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1060 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1062 if(state->system_inactivity_ind)
1065 else if(state->save_unsaved_data_ind)
1073 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */