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 #ifdef MODEST_HAVE_HILDON0_WIDGETS
46 #include <libgnomevfs/gnome-vfs-mime-utils.h>
48 #include <libgnomevfs/gnome-vfs-mime.h>
50 #include <tny-fs-stream.h>
53 #include <tny-iterator.h>
54 #include <tny-simple-list.h>
74 } ComposeMailIdleData;
77 * @uri An escaped URI. URIs should always be escaped.
78 * @len The length of the @uri string, or -1 if the string is null terminated.
80 * Decode a URI, or URI fragment, as per RFC 1738.
81 * http://www.ietf.org/rfc/rfc1738.txt
83 * Return value: An unescaped string. This should be freed with g_free().
85 static gchar* uri_unescape(const gchar* uri, size_t len)
93 /* Allocate an extra string so we can be sure that it is null-terminated,
94 * so we can use gnome_vfs_unescape_string().
95 * This is not efficient. */
96 gchar * escaped_nullterminated = g_strndup (uri, len);
97 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
98 g_free (escaped_nullterminated);
103 /** uri_parse_mailto:
104 * @mailto A mailto URI, with the mailto: prefix.
105 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
106 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
107 * all the string items have been freed. This parameter may be NULL.
108 * Parse a mailto URI as per RFC2368.
109 * http://www.ietf.org/rfc/rfc2368.txt
111 * Return value: The to address, unescaped. This should be freed with g_free().
113 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
115 const gchar* start_to = NULL;
116 /* Remove the mailto: prefix:
117 * 7 is the length of "mailto:": */
118 if (strncmp (mailto, "mailto:", 7) == 0) {
119 start_to = mailto + 7;
125 /* Look for ?, or the end of the string, marking the end of the to address: */
126 const size_t len_to = strcspn (start_to, "?");
127 gchar* result_to = uri_unescape (start_to, len_to);
128 printf("debug: result_to=%s\n", result_to);
130 /* Get any other items: */
131 const size_t len_mailto = strlen (start_to);
132 const gchar* p = start_to + len_to + 1; /* parsed so far. */
133 const gchar* end = start_to + len_mailto;
134 /* GSList *items = NULL; */
135 const gchar* start_item_name = p;
136 size_t len_item_name = 0;
137 const gchar* start_item_value = NULL;
140 /* Looking for the end of a name; */
141 if (start_item_name) {
142 const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
144 /* This marks the end of a name and the start of the value: */
147 /* Skip over the name and mark the start of the value: */
148 p += (len + 1); /* Skip over the = */
149 start_item_value = p;
153 /* Looking for the end of a value: */
154 if (start_item_value) {
155 const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
156 /* ? marks the start of a new item: */
158 if (start_item_name && len_item_name) {
159 /* Finish the previously-started item: */
160 gchar *item_value = uri_unescape (start_item_value, len);
161 gchar *item_name = g_strndup (start_item_name, len_item_name);
162 /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
164 /* Append the items to the list */
165 if(list_items_and_values) {
166 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
167 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
171 /* Skip over the value and mark the start of a possible new name/value pair: */
172 p += (len + 1); /* Skip over the ? */
175 start_item_value = NULL;
186 on_idle_mail_to(gpointer user_data)
190 /* This is based on the implemenation of main.c:start_uil(): */
192 gchar *uri = (gchar*)user_data;
193 GSList *list_names_and_values = NULL;
194 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
196 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
197 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
198 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
200 g_printerr ("modest: no account found\n");
203 TnyAccount *account = NULL;
205 account = modest_tny_account_store_get_transport_account_for_open_connection (
206 modest_runtime_get_account_store(), account_name);
210 g_printerr ("modest: failed to get tny account folder'\n", account_name);
212 gchar * from = modest_account_mgr_get_from_string (account_mgr,
215 g_printerr ("modest: no from address for account '%s'\n", account_name);
217 const gchar *cc = NULL;
218 const gchar *bcc = NULL;
219 const gchar *subject = NULL;
220 const gchar *body = NULL;
222 /* Get the relevant items from the list: */
223 GSList *list = list_names_and_values;
225 const gchar * name = (const gchar*)list->data;
226 GSList *list_value = g_slist_next (list);
227 const gchar * value = (const gchar*)list_value->data;
229 if (strcmp (name, "cc") == 0) {
231 } else if (strcmp (name, "bcc") == 0) {
233 } else if (strcmp (name, "subject") == 0) {
235 } else if (strcmp (name, "body") == 0) {
239 /* Go to the next pair: */
241 list = g_slist_next (list_value);
246 /* Create the message: */
247 TnyMsg *msg = modest_tny_msg_new (to, from,
248 cc, bcc, subject, body,
249 NULL /* attachments */);
252 g_printerr ("modest: failed to create message\n");
255 /* Add the message to a folder and show its UI for editing: */
256 TnyFolder *folder = modest_tny_account_get_special_folder (account,
257 TNY_FOLDER_TYPE_DRAFTS);
259 g_printerr ("modest: failed to find Drafts folder\n");
262 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
264 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
265 gtk_widget_show_all (GTK_WIDGET (win));
267 g_object_unref (G_OBJECT(folder));
270 g_object_unref (G_OBJECT(msg));
273 g_object_unref (G_OBJECT(account));
277 g_free (account_name);
279 /* Free the list, as required by the uri_parse_mailto() documentation: */
280 if (list_names_and_values)
281 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
282 g_slist_free (list_names_and_values);
290 return FALSE; /* Do not call this callback again. */
293 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
295 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
298 /* Use g_idle to context-switch into the application's thread: */
300 /* Get the arguments: */
301 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
302 gchar *uri = g_strdup (val.value.s);
304 /* printf(" debug: to=%s\n", idle_data->to); */
305 g_idle_add(on_idle_mail_to, (gpointer)uri);
307 /* Note that we cannot report failures during sending,
308 * because that would be asynchronous. */
317 on_idle_compose_mail(gpointer user_data)
321 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
325 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
326 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
327 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
329 g_printerr ("modest: no account found\n");
332 TnyAccount *account = NULL;
334 account = modest_tny_account_store_get_transport_account_for_open_connection (
335 modest_runtime_get_account_store(), account_name);
339 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
341 gchar * from = modest_account_mgr_get_from_string (account_mgr,
344 g_printerr ("modest: no from address for account '%s'\n", account_name);
347 /* Create the message: */
348 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
349 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body,
350 NULL); /* NULL because m_t_m_n doesn't use it */
353 g_printerr ("modest: failed to create message\n");
356 /* Add the message to a folder and show its UI for editing: */
357 TnyFolder *folder = modest_tny_account_get_special_folder (account,
358 TNY_FOLDER_TYPE_DRAFTS);
360 g_printerr ("modest: failed to find Drafts folder\n");
363 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
365 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
367 list = g_strsplit(idle_data->attachments, ",", 0);
368 for (i=0; list[i] != NULL; i++) {
369 modest_msg_edit_window_attach_file_noninteractive(
370 (ModestMsgEditWindow *)win, list[i]);
374 gtk_widget_show_all (GTK_WIDGET (win));
376 g_object_unref (G_OBJECT(folder));
379 g_object_unref (G_OBJECT(msg));
382 g_object_unref (G_OBJECT(account));
386 /* Free the idle data: */
387 g_free (idle_data->to);
388 g_free (idle_data->cc);
389 g_free (idle_data->bcc);
390 g_free (idle_data->subject);
391 g_free (idle_data->body);
392 g_free (idle_data->attachments);
395 g_free (account_name);
399 return FALSE; /* Do not call this callback again. */
402 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
404 if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
407 /* Use g_idle to context-switch into the application's thread: */
408 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
410 /* Get the arguments: */
411 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
412 idle_data->to = g_strdup (val.value.s);
414 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
415 idle_data->cc = g_strdup (val.value.s);
417 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
418 idle_data->bcc = g_strdup (val.value.s);
420 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
421 idle_data->subject = g_strdup (val.value.s);
423 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
424 idle_data->body = g_strdup (val.value.s);
426 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
427 idle_data->attachments = g_strdup (val.value.s);
429 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
431 /* Note that we cannot report failures during sending,
432 * because that would be asynchronous. */
438 find_message_by_url (const char *uri, TnyAccount **ac_out)
440 ModestTnyAccountStore *astore;
449 astore = modest_runtime_get_account_store ();
451 if (astore == NULL) {
455 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
458 if (account == NULL) {
459 g_debug ("%s: tny_account_store_find_account() failed.\n", __FUNCTION__);
463 g_debug ("%s: Found account.\n", __FUNCTION__);
465 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
469 g_debug ("%s: Account is store account.\n", __FUNCTION__);
473 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
477 if (folder == NULL) {
478 g_debug ("%s: tny_store_account_find_folder() failed\naccount=%s, uri=%s.\n", __FUNCTION__,
479 tny_account_get_id (TNY_ACCOUNT(account)), uri);
482 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
485 msg = tny_folder_find_msg (folder, uri, &err);
488 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
489 __FUNCTION__, tny_folder_get_id (folder), err->message);
496 if (account && !msg) {
497 g_object_unref (account);
502 g_object_unref (folder);
509 on_idle_open_message (gpointer user_data)
513 ModestWindow *msg_view;
518 const char *account_name;
521 uri = (char *) user_data;
523 g_debug ("%s: Trying to find msg by url: %s", __FUNCTION__, uri);
524 msg = find_message_by_url (uri, &account);
528 g_debug (" %s: message not found.", __FUNCTION__);
532 g_debug (" %s: Found message.", __FUNCTION__);
534 header = tny_msg_get_header (msg);
535 account_name = tny_account_get_name (account);
536 msg_uid = tny_header_get_uid (header);
538 msg_view = modest_msg_view_window_new (msg,
541 /* TODO: does that leak the msg_view ?! */
543 gtk_widget_show_all (GTK_WIDGET (msg_view));
545 g_object_unref (header);
546 g_object_unref (account);
550 return FALSE; /* Do not call this callback again. */
553 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
555 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
558 /* Use g_idle to context-switch into the application's thread: */
560 /* Get the arguments: */
561 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
562 gchar *uri = g_strdup (val.value.s);
564 /* printf(" debug: to=%s\n", idle_data->to); */
565 g_idle_add(on_idle_open_message, (gpointer)uri);
567 /* Note that we cannot report failures during sending,
568 * because that would be asynchronous. */
574 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
580 TnyHeader *msg_header;
589 if (arguments->len != MODEST_DEBUS_DELETE_MESSAGE_ARGS_COUNT) {
593 val = g_array_index (arguments,
595 MODEST_DEBUS_DELETE_MESSAGE_ARG_URI);
597 uri = (const char *) val.value.s;
599 g_debug ("Searching message (delete message)");
601 msg = find_message_by_url (uri, &account);
607 g_debug ("Found message");
609 msg_header = tny_msg_get_header (msg);
610 uid = tny_header_get_uid (msg_header);
611 folder = tny_msg_get_folder (msg);
614 /* tny_msg_get_header () flaw:
615 * From tinythingy doc: You can't use the returned instance with the
616 * TnyFolder operations
618 * To get a header instance that will work with these folder methods,
619 * you can use tny_folder_get_headers.
621 * Ok, we will do so then. Sigh.
623 headers = tny_simple_list_new ();
625 tny_folder_get_headers (folder, headers, TRUE, NULL);
626 iter = tny_list_create_iterator (headers);
629 g_debug ("Searching header for msg in folder");
630 while (!tny_iterator_is_done (iter)) {
633 header = TNY_HEADER (tny_iterator_get_current (iter));
634 cur_id = tny_header_get_uid (header);
636 if (cur_id && uid && g_str_equal (cur_id, uid)) {
637 g_debug ("Found correspoding header from folder");
642 g_object_unref (header);
643 tny_iterator_next (iter);
646 g_object_unref (iter);
647 g_object_unref (headers);
649 g_object_unref (msg_header);
650 g_object_unref (msg);
652 if (header == NULL) {
653 g_object_unref (folder);
660 tny_folder_remove_msg (folder, header, &error);
664 g_error_free (error);
667 g_object_unref (folder);
672 on_idle_send_receive(gpointer user_data)
677 /* Pick the main window if it exists */
678 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
680 /* Send & receive all if "Update automatically" is set */
681 /* TODO: check the auto-update parameter in the configuration */
682 modest_ui_actions_do_send_receive_all (win);
686 return FALSE; /* Do not call this callback again. */
689 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
691 /* Use g_idle to context-switch into the application's thread: */
693 /* This method has no arguments. */
695 /* printf(" debug: to=%s\n", idle_data->to); */
696 g_idle_add(on_idle_send_receive, NULL);
698 /* Note that we cannot report failures during send/receive,
699 * because that would be asynchronous. */
704 on_idle_open_default_inbox(gpointer user_data)
709 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
711 /* Get the folder view */
712 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
713 MODEST_WIDGET_TYPE_FOLDER_VIEW);
714 modest_folder_view_select_first_inbox_or_local (
715 MODEST_FOLDER_VIEW (folder_view));
719 return FALSE; /* Do not call this callback again. */
722 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
724 /* Use g_idle to context-switch into the application's thread: */
726 /* This method has no arguments. */
728 g_idle_add(on_idle_open_default_inbox, NULL);
730 /* Note that we cannot report failures during send/receive,
731 * because that would be asynchronous. */
735 /* Callback for normal D-BUS messages */
736 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
737 GArray * arguments, gpointer data,
741 g_debug ("debug: %s\n", __FUNCTION__);
742 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
744 if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
745 return on_mail_to (arguments, data, retval);
746 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
747 return on_open_message (arguments, data, retval);
748 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
749 return on_send_receive (arguments, data, retval);
750 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
751 return on_compose_mail (arguments, data, retval);
752 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
753 return on_delete_message (arguments,data, retval);
754 } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
755 return on_open_default_inbox (arguments, data, retval);
758 /* We need to return INVALID here so
759 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
760 * so that our modest_dbus_req_filter will then be tried instead.
766 /* A complex D-Bus type (like a struct),
767 * used to return various information about a search hit.
769 #define SEARCH_HIT_DBUS_TYPE \
770 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
771 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
772 DBUS_TYPE_STRING_AS_STRING /* subject */ \
773 DBUS_TYPE_STRING_AS_STRING /* folder */ \
774 DBUS_TYPE_STRING_AS_STRING /* sender */ \
775 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
776 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
777 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
778 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
779 DBUS_STRUCT_END_CHAR_AS_STRING
782 search_result_to_message (DBusMessage *reply,
785 DBusMessageIter iter;
786 DBusMessageIter array_iter;
789 dbus_message_iter_init_append (reply, &iter);
790 dbus_message_iter_open_container (&iter,
792 SEARCH_HIT_DBUS_TYPE,
795 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
796 DBusMessageIter struct_iter;
797 ModestSearchHit *hit;
803 gboolean has_attachment;
807 hit = (ModestSearchHit *) hit_iter->data;
809 msg_url = hit->msgid;
810 subject = hit->subject;
811 folder = hit->folder;
812 sender = hit->sender;
814 has_attachment = hit->has_attachment;
815 is_unread = hit->is_unread;
818 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
820 dbus_message_iter_open_container (&array_iter,
825 dbus_message_iter_append_basic (&struct_iter,
829 dbus_message_iter_append_basic (&struct_iter,
833 dbus_message_iter_append_basic (&struct_iter,
837 dbus_message_iter_append_basic (&struct_iter,
841 dbus_message_iter_append_basic (&struct_iter,
845 dbus_message_iter_append_basic (&struct_iter,
849 dbus_message_iter_append_basic (&struct_iter,
853 dbus_message_iter_append_basic (&struct_iter,
857 dbus_message_iter_close_container (&array_iter,
861 g_free (hit->subject);
862 g_free (hit->folder);
863 g_free (hit->sender);
865 g_slice_free (ModestSearchHit, hit);
868 dbus_message_iter_close_container (&iter, &array_iter);
875 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
877 ModestDBusSearchFlags dbus_flags;
878 DBusMessage *reply = NULL;
882 dbus_int32_t flags_v;
883 dbus_uint32_t size_v;
891 dbus_error_init (&error);
896 res = dbus_message_get_args (message,
898 DBUS_TYPE_STRING, &query,
899 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
900 DBUS_TYPE_INT64, &sd_v,
901 DBUS_TYPE_INT64, &ed_v,
902 DBUS_TYPE_INT32, &flags_v,
903 DBUS_TYPE_UINT32, &size_v,
906 dbus_flags = (ModestDBusSearchFlags) flags_v;
907 start_date = (time_t) sd_v;
908 end_date = (time_t) ed_v;
911 memset (&search, 0, sizeof (search));
913 /* Remember what folder we are searching in:
915 * Note that we don't copy the strings,
916 * because this struct will only be used for the lifetime of this function.
918 search.folder = folder;
920 /* Remember the text to search for: */
921 #ifdef MODEST_HAVE_OGS
922 search.query = query;
925 /* Other criteria: */
926 search.before = start_date;
927 search.after = end_date;
930 /* Text to serach for in various parts of the message: */
931 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
932 search.flags |= MODEST_SEARCH_SUBJECT;
933 search.subject = query;
936 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
937 search.flags |= MODEST_SEARCH_SENDER;
941 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
942 search.flags |= MODEST_SEARCH_RECIPIENT;
943 search.recipient = query;
946 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
947 search.flags |= MODEST_SEARCH_BODY;
952 search.flags |= MODEST_SEARCH_BEFORE;
953 search.before = start_date;
957 search.flags |= MODEST_SEARCH_AFTER;
958 search.after = end_date;
962 search.flags |= MODEST_SEARCH_SIZE;
963 search.minsize = size_v;
966 #ifdef MODEST_HAVE_OGS
967 search.flags |= MODEST_SEARCH_USE_OGS;
968 g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
971 /* Note that this currently gets folders and messages from the servers,
972 * which can take a long time. libmodest_dbus_client_search() can timeout,
973 * reporting no results, if this takes a long time: */
974 hits = modest_search_all_accounts (&search);
976 reply = dbus_message_new_method_return (message);
978 search_result_to_message (reply, hits);
981 g_warning ("%s: Could not create reply.", __FUNCTION__);
985 dbus_uint32_t serial = 0;
986 dbus_connection_send (con, reply, &serial);
987 dbus_connection_flush (con);
988 dbus_message_unref (reply);
995 /* A complex D-Bus type (like a struct),
996 * used to return various information about a folder.
998 #define GET_FOLDERS_RESULT_DBUS_TYPE \
999 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1000 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1001 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1002 DBUS_STRUCT_END_CHAR_AS_STRING
1004 static DBusMessage *
1005 get_folders_result_to_message (DBusMessage *reply,
1008 DBusMessageIter iter;
1009 dbus_message_iter_init_append (reply, &iter);
1011 DBusMessageIter array_iter;
1012 dbus_message_iter_open_container (&iter,
1014 GET_FOLDERS_RESULT_DBUS_TYPE,
1017 GList *list_iter = folder_ids;
1018 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1020 const gchar *folder_name = (const gchar*)list_iter->data;
1022 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1024 DBusMessageIter struct_iter;
1025 dbus_message_iter_open_container (&array_iter,
1031 dbus_message_iter_append_basic (&struct_iter,
1033 &folder_name); /* The string will be copied. */
1035 /* URI: This is maybe not needed by osso-global-search: */
1036 const gchar *folder_uri = "TODO:unimplemented";
1037 dbus_message_iter_append_basic (&struct_iter,
1039 &folder_uri); /* The string will be copied. */
1041 dbus_message_iter_close_container (&array_iter,
1046 dbus_message_iter_close_container (&iter, &array_iter);
1052 add_single_folder_to_list (TnyFolder *folder, GList** list)
1057 /* Add this folder to the list: */
1059 const gchar * folder_name = tny_folder_get_name (folder);
1061 *list = g_list_append(*list, g_strdup (folder_name));
1064 /* osso-global-search only uses one string,
1065 * so ID is the only thing that could possibly identify a folder.
1066 * TODO: osso-global search should probably be changed to
1067 * take an ID and a Name.
1069 const gchar * id = tny_folder_get_id (folder);
1070 if (id && strlen(id))
1071 *list = g_list_append(*list, g_strdup (id));
1074 g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);
1082 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1087 /* Add this folder to the list: */
1088 if (TNY_IS_FOLDER (folder_store)) {
1089 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1093 /* Recurse into child folders: */
1095 /* Get the folders list: */
1097 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1098 tny_folder_store_query_add_item (query, NULL,
1099 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1101 TnyList *all_folders = tny_simple_list_new ();
1102 tny_folder_store_get_folders (folder_store,
1107 TnyIterator *iter = tny_list_create_iterator (all_folders);
1108 while (!tny_iterator_is_done (iter)) {
1109 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1110 if (TNY_IS_FOLDER_STORE (folder))
1111 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1113 add_single_folder_to_list (TNY_FOLDER (folder), list);
1116 tny_iterator_next (iter);
1118 g_object_unref (G_OBJECT (iter));
1122 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1124 DBusMessage *reply = NULL;
1127 /* Get the TnyStoreAccount so we can get the folders: */
1128 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1129 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
1130 if (!account_name) {
1131 g_printerr ("modest: no account found\n");
1134 TnyAccount *account = NULL;
1136 account = modest_tny_account_store_get_server_account (
1137 modest_runtime_get_account_store(), account_name,
1138 TNY_ACCOUNT_TYPE_STORE);
1142 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1145 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1146 g_free (account_name);
1147 account_name = NULL;
1149 GList *folder_names = NULL;
1150 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1152 g_object_unref (account);
1156 /* Also add the folders from the local folders account,
1157 * because they are (currently) used with all accounts:
1158 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1160 TnyAccount *account_local =
1161 modest_tny_account_store_get_local_folders_account (
1162 TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
1163 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1165 g_object_unref (account_local);
1166 account_local = NULL;
1169 /* Put the result in a DBus reply: */
1170 reply = dbus_message_new_method_return (message);
1172 get_folders_result_to_message (reply, folder_names);
1174 if (reply == NULL) {
1175 g_warning ("%s: Could not create reply.", __FUNCTION__);
1179 dbus_uint32_t serial = 0;
1180 dbus_connection_send (con, reply, &serial);
1181 dbus_connection_flush (con);
1182 dbus_message_unref (reply);
1185 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1186 g_list_free (folder_names);
1190 /** This D-Bus handler is used when the main osso-rpc
1191 * D-Bus handler has not handled something.
1192 * We use this for D-Bus methods that need to use more complex types
1193 * than osso-rpc supports.
1196 modest_dbus_req_filter (DBusConnection *con,
1197 DBusMessage *message,
1200 gboolean handled = FALSE;
1202 if (dbus_message_is_method_call (message,
1204 MODEST_DBUS_METHOD_SEARCH)) {
1205 on_dbus_method_search (con, message);
1207 } else if (dbus_message_is_method_call (message,
1209 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1210 on_dbus_method_get_folders (con, message);
1215 DBUS_HANDLER_RESULT_HANDLED :
1216 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1221 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1224 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1226 if(state->system_inactivity_ind)
1229 else if(state->save_unsaved_data_ind)
1237 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */