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-tny-folder.h"
36 #include "modest-ui-actions.h"
37 #include "modest-utils.h"
38 #include "modest-debug.h"
39 #include "modest-search.h"
40 #include "widgets/modest-msg-edit-window.h"
41 #include "widgets/modest-window-mgr.h"
42 #include "modest-tny-msg.h"
43 #include "modest-platform.h"
44 #include "modest-defs.h"
45 #include <libmodest-dbus-client/libmodest-dbus-client.h>
46 #include <libgnomevfs/gnome-vfs-utils.h>
49 #include <glib/gstdio.h>
50 #ifdef MODEST_HAVE_HILDON0_WIDGETS
51 #include <hildon-widgets/hildon-program.h>
52 #include <libgnomevfs/gnome-vfs-mime-utils.h>
54 #include <hildon/hildon-program.h>
55 #include <libgnomevfs/gnome-vfs-mime.h>
57 #include <tny-fs-stream.h>
59 #ifdef MODEST_TOOLKIT_HILDON2
60 #include <hildon/hildon.h>
61 #include <modest-accounts-window.h>
62 #include <modest-folder-window.h>
63 #include <modest-mailboxes-window.h>
64 #include <modest-maemo-utils.h>
68 #include <tny-iterator.h>
69 #include <tny-simple-list.h>
70 #include <tny-merge-folder.h>
71 #include <tny-account.h>
73 #include <modest-text-utils.h>
83 } ComposeMailIdleData;
86 static gboolean notify_error_in_dbus_callback (gpointer user_data);
87 static gboolean on_idle_compose_mail(gpointer user_data);
88 static gboolean on_idle_top_application (gpointer user_data);
91 * @uri An escaped URI. URIs should always be escaped.
92 * @len The length of the @uri string, or -1 if the string is null terminated.
94 * Decode a URI, or URI fragment, as per RFC 1738.
95 * http://www.ietf.org/rfc/rfc1738.txt
97 * Return value: An unescaped string. This should be freed with g_free().
100 uri_unescape(const gchar* uri, size_t len)
108 /* Allocate an extra string so we can be sure that it is null-terminated,
109 * so we can use gnome_vfs_unescape_string().
110 * This is not efficient. */
111 gchar * escaped_nullterminated = g_strndup (uri, len);
112 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
113 g_free (escaped_nullterminated);
118 /** uri_parse_mailto:
119 * @mailto A mailto URI, with the mailto: prefix.
120 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
121 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
122 * all the string items have been freed. This parameter may be NULL.
123 * Parse a mailto URI as per RFC2368.
124 * http://www.ietf.org/rfc/rfc2368.txt
126 * Return value: The to address, unescaped. This should be freed with g_free().
129 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
131 /* The URL must begin with mailto: */
132 if (strncmp (mailto, "mailto:", 7) != 0) {
135 const gchar* start_to = mailto + 7;
137 /* Look for ?, or the end of the string, marking the end of the to address: */
138 const size_t len_to = strcspn (start_to, "?");
139 gchar* result_to = uri_unescape (start_to, len_to);
140 printf("debug: result_to=%s\n", result_to);
142 if (list_items_and_values == NULL) {
146 /* Get any other items: */
147 const size_t len_mailto = strlen (start_to);
148 const gchar* p = start_to + len_to + 1; /* parsed so far. */
149 const gchar* end = start_to + len_mailto;
151 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
153 name_end = strchr (name_start, '='); /* Separator between name and value */
154 if (name_end == NULL) {
155 g_debug ("Malformed URI: %s\n", mailto);
158 value_start = name_end + 1;
159 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
161 name = g_strndup(name_start, name_end - name_start);
162 if (value_end != NULL) {
163 value = uri_unescape(value_start, value_end - value_start);
166 value = uri_unescape(value_start, -1);
169 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
170 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
177 check_and_offer_account_creation()
179 gboolean result = TRUE;
181 /* This is called from idle handlers, so lock gdk: */
182 gdk_threads_enter ();
184 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
185 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
187 g_debug ("modest: %s: no account exists even after offering, "
188 "or account setup was already underway.\n", __FUNCTION__);
193 gdk_threads_leave ();
199 on_idle_mail_to(gpointer user_data)
201 gchar *uri = (gchar*)user_data;
202 GSList *list_names_and_values = NULL;
204 const gchar *cc = NULL;
205 const gchar *bcc = NULL;
206 const gchar *subject = NULL;
207 const gchar *body = NULL;
209 if (!check_and_offer_account_creation ()) {
210 g_idle_add (notify_error_in_dbus_callback, NULL);
214 /* Get the relevant items from the list: */
215 to = uri_parse_mailto (uri, &list_names_and_values);
216 GSList *list = list_names_and_values;
218 GSList *list_value = g_slist_next (list);
219 const gchar * name = (const gchar*)list->data;
220 const gchar * value = (const gchar*)list_value->data;
222 if (strcmp (name, "cc") == 0) {
224 } else if (strcmp (name, "bcc") == 0) {
226 } else if (strcmp (name, "subject") == 0) {
228 } else if (strcmp (name, "body") == 0) {
232 list = g_slist_next (list_value);
235 gdk_threads_enter (); /* CHECKED */
236 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
237 gdk_threads_leave (); /* CHECKED */
240 /* Free the to: and the list, as required by uri_parse_mailto() */
242 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
243 g_slist_free (list_names_and_values);
247 return FALSE; /* Do not call this callback again. */
251 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
257 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
258 uri = g_strdup (val.value.s);
260 g_idle_add(on_idle_mail_to, (gpointer)uri);
262 /* Note that we cannot report failures during sending,
263 * because that would be asynchronous. */
269 on_idle_compose_mail(gpointer user_data)
271 GSList *attachments = NULL;
272 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
274 if (!check_and_offer_account_creation ()) {
275 g_idle_add (notify_error_in_dbus_callback, NULL);
279 /* it seems Sketch at least sends a leading ',' -- take that into account,
281 if (idle_data->attachments && idle_data->attachments[0]==',') {
282 gchar *tmp = g_strdup (idle_data->attachments + 1);
283 g_free(idle_data->attachments);
284 idle_data->attachments = tmp;
287 if (idle_data->attachments != NULL) {
288 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
290 for (i=0; list[i] != NULL; i++) {
291 attachments = g_slist_append(attachments, g_uri_unescape_string (list[i], NULL));
296 /* If the message has nothing then mark the buffers as not
297 modified. This happens in Maemo for example when opening a
298 new message from Contacts plugin, it sends "" instead of
300 gdk_threads_enter (); /* CHECKED */
301 if (!strncmp (idle_data->to, "", 1) &&
302 !strncmp (idle_data->to, "", 1) &&
303 !strncmp (idle_data->cc, "", 1) &&
304 !strncmp (idle_data->bcc, "", 1) &&
305 !strncmp (idle_data->subject, "", 1) &&
306 !strncmp (idle_data->body, "", 1) &&
307 attachments == NULL) {
308 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
310 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
311 idle_data->bcc, idle_data->subject,
312 idle_data->body, attachments, TRUE);
314 gdk_threads_leave (); /* CHECKED */
316 g_slist_foreach(attachments, (GFunc)g_free, NULL);
317 g_slist_free(attachments);
318 g_free (idle_data->to);
319 g_free (idle_data->cc);
320 g_free (idle_data->bcc);
321 g_free (idle_data->subject);
322 g_free (idle_data->body);
323 g_free (idle_data->attachments);
326 return FALSE; /* Do not call this callback again. */
330 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
332 ComposeMailIdleData *idle_data;
335 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
337 /* Get the arguments: */
338 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
339 idle_data->to = g_strdup (val.value.s);
341 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
342 idle_data->cc = g_strdup (val.value.s);
344 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
345 idle_data->bcc = g_strdup (val.value.s);
347 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
348 idle_data->subject = g_strdup (val.value.s);
350 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
351 idle_data->body = g_strdup (val.value.s);
353 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
354 idle_data->attachments = g_strdup (val.value.s);
356 /* Use g_idle to context-switch into the application's thread: */
357 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
363 find_message_by_url (const char *uri, TnyAccount **ac_out)
365 ModestTnyAccountStore *astore;
366 TnyAccount *account = NULL;
367 TnyFolder *folder = NULL;
370 astore = modest_runtime_get_account_store ();
375 if (uri && g_str_has_prefix (uri, "merge://")) {
376 /* we assume we're talking about outbox folder, as this
377 * is the only merge folder we work with in modest */
378 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
380 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
383 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
387 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
392 msg = tny_folder_find_msg (folder, uri, NULL);
395 if (account && !msg) {
396 g_object_unref (account);
400 g_object_unref (folder);
409 guint animation_timeout;
410 GtkWidget *animation;
411 } OpenMsgPerformerInfo;
413 #ifndef MODEST_TOOLKIT_HILDON2
415 on_show_opening_animation (gpointer userdata)
417 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
418 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
419 info->animation_timeout = 0;
426 on_find_msg_async_destroy (gpointer userdata)
428 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
430 if (info->animation_timeout>0) {
431 g_source_remove (info->animation_timeout);
432 info->animation_timeout = 0;
435 if (info->animation) {
436 gtk_widget_destroy (info->animation);
437 info->animation = NULL;
444 g_object_unref (info->account);
446 g_slice_free (OpenMsgPerformerInfo, info);
451 find_msg_async_cb (TnyFolder *folder,
457 TnyHeader *header = NULL;
458 gchar *msg_uid = NULL;
459 ModestWindowMgr *win_mgr;
460 ModestWindow *msg_view = NULL;
461 gboolean is_draft = FALSE;
462 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
464 if (err || cancelled) {
465 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
466 g_idle_add (notify_error_in_dbus_callback, NULL);
470 header = tny_msg_get_header (msg);
471 if (!header || (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
472 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
473 g_idle_add (notify_error_in_dbus_callback, NULL);
477 msg_uid = modest_tny_folder_get_header_unique_id (header);
478 win_mgr = modest_runtime_get_window_mgr ();
480 if (modest_tny_folder_is_local_folder (folder) &&
481 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
485 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
486 gtk_window_present (GTK_WINDOW(msg_view));
488 const gchar *modest_account_name;
491 modest_window_mgr_register_header (win_mgr, header, NULL);
493 account = tny_folder_get_account (folder);
495 modest_account_name =
496 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
498 modest_account_name = NULL;
501 /* Drafts will be opened in the editor, and others will be opened in the viewer */
503 gchar *modest_account_name = NULL;
504 gchar *mailbox = NULL;
507 /* we cannot edit without a valid account... */
508 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
509 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
510 modest_window_mgr_unregister_header (win_mgr,
516 from_header = tny_header_dup_from (header);
517 modest_account_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
518 g_free (from_header);
520 if (modest_account_name == NULL) {
521 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
522 modest_account_name = modest_account_mgr_get_default_account (mgr);
524 msg_view = modest_msg_edit_window_new (msg, modest_account_name, mailbox, TRUE);
527 g_free (modest_account_name);
530 const gchar *modest_account_name;
533 modest_account_name =
534 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
536 modest_account_name = NULL;
539 header = tny_msg_get_header (msg);
540 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name, NULL, msg_uid);
541 if (! (tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
542 ModestMailOperation *mail_op;
544 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
545 /* Sync folder, we need this to save the seen flag */
546 mail_op = modest_mail_operation_new (NULL);
547 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
549 modest_mail_operation_sync_folder (mail_op, folder, FALSE);
550 g_object_unref (mail_op);
552 g_object_unref (header);
555 if (msg_view != NULL) {
556 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
557 gtk_widget_destroy (GTK_WIDGET (msg_view));
559 gtk_widget_show_all (GTK_WIDGET (msg_view));
566 g_object_unref (header);
569 on_find_msg_async_destroy (info);
574 on_open_message_performer (gboolean canceled,
576 GtkWindow *parent_window,
580 OpenMsgPerformerInfo *info;
581 TnyFolder *folder = NULL;
582 ModestTnyAccountStore *account_store;
583 ModestTnyLocalFoldersAccount *local_folders_account;
585 account_store = modest_runtime_get_account_store ();
586 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
587 modest_tny_account_store_get_local_folders_account (account_store));
589 info = (OpenMsgPerformerInfo *) user_data;
590 if (canceled || err) {
592 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
593 g_idle_add (notify_error_in_dbus_callback, NULL);
594 on_find_msg_async_destroy (info);
600 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
601 g_object_unref (local_folders_account);
602 } else if (account == TNY_ACCOUNT (local_folders_account)) {
603 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
607 if (!(folder && modest_tny_folder_is_local_folder (folder) &&
608 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS))) {
609 ModestWindowMgr *win_mgr;
610 ModestWindow *msg_view;
612 win_mgr = modest_runtime_get_window_mgr ();
613 if (modest_window_mgr_find_registered_message_uid (win_mgr, info->uri, &msg_view)) {
614 gtk_window_present (GTK_WINDOW(msg_view));
616 const gchar *modest_account_name;
618 modest_account_name =
619 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
621 modest_account_name = NULL;
624 msg_view = modest_msg_view_window_new_from_uid (modest_account_name, NULL, info->uri);
625 if (msg_view != NULL) {
626 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
627 gtk_widget_destroy (GTK_WIDGET (msg_view));
629 gtk_widget_show_all (GTK_WIDGET (msg_view));
633 g_object_unref (account);
636 on_find_msg_async_destroy (info);
640 #ifndef MODEST_TOOLKIT_HILDON2
641 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
644 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
645 g_object_unref (folder);
649 on_idle_open_message_performer (gpointer user_data)
651 ModestWindow *top_win = NULL;
652 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
654 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
656 /* Lock before the call as we're in an idle handler */
657 gdk_threads_enter ();
659 modest_platform_connect_and_perform (GTK_WINDOW (top_win), TRUE,
661 on_open_message_performer, info);
663 on_open_message_performer (FALSE, NULL, GTK_WINDOW (top_win),
664 info->account, info);
666 gdk_threads_leave ();
672 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
676 TnyAccount *account = NULL;
680 /* Get the arguments: */
681 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
682 uri = g_strdup (val.value.s);
684 is_merge = g_str_has_prefix (uri, "merge:");
686 /* Get the account */
688 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
692 if (is_merge || account) {
693 OpenMsgPerformerInfo *info;
694 TnyFolder *folder = NULL;
695 ModestTnyAccountStore *account_store;
696 ModestTnyLocalFoldersAccount *local_folders_account;
698 info = g_slice_new0 (OpenMsgPerformerInfo);
700 info->account = g_object_ref (account);
702 info->connect = TRUE;
703 info->animation = NULL;
704 info->animation_timeout = 0;
706 account_store = modest_runtime_get_account_store ();
707 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT
708 (modest_tny_account_store_get_local_folders_account (account_store));
710 /* Try to get the message, if it's already downloaded
711 we don't need to connect */
714 gboolean device_online;
716 device = modest_runtime_get_device ();
717 device_online = tny_device_is_online (device);
718 if (!device_online || TNY_ACCOUNT (local_folders_account) == account) {
719 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
724 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
725 g_object_unref (local_folders_account);
726 info->connect = FALSE;
729 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
731 info->connect = FALSE;
732 g_object_unref (msg);
734 info->connect = TRUE;
736 g_object_unref (folder);
739 /* We need to call it into an idle to get
740 modest_platform_connect_and_perform into the main
742 g_idle_add (on_idle_open_message_performer, info);
743 osso_retval = OSSO_OK;
746 osso_retval = OSSO_ERROR;
747 g_idle_add (notify_error_in_dbus_callback, NULL);
751 g_object_unref (account);
756 on_remove_msgs_finished (ModestMailOperation *mail_op,
760 ModestWindow *top_win = NULL, *msg_view = NULL;
762 header = (TnyHeader *) user_data;
764 /* Get the main window if exists */
765 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
767 g_object_unref (header);
771 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
772 header, &msg_view)) {
773 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
774 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
776 g_object_unref (header);
778 /* Refilter the header views explicitely */
780 /* TODO: call modest_window_mgr_refilter_header_views */
781 /* this call will go through all the windows, get the header views and refilter them */
785 thread_prepare_delete_message (gpointer userdata)
787 TnyList *headers = NULL, *tmp_headers = NULL;
788 TnyFolder *folder = NULL;
789 TnyIterator *iter = NULL;
790 TnyHeader *header = NULL, *msg_header = NULL;
792 TnyAccount *account = NULL;
795 ModestMailOperation *mail_op = NULL;
796 ModestWindow *top_win = NULL;
798 uri = (char *) userdata;
800 msg = find_message_by_url (uri, &account);
802 g_object_unref (account);
805 g_debug ("%s: Could not find message '%s'", __FUNCTION__, uri);
806 g_idle_add (notify_error_in_dbus_callback, NULL);
811 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
813 folder = tny_msg_get_folder (msg);
815 g_debug ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
816 g_object_unref (msg);
817 g_idle_add (notify_error_in_dbus_callback, NULL);
823 msg_header = tny_msg_get_header (msg);
824 uid = tny_header_dup_uid (msg_header);
825 g_object_unref (msg);
826 g_object_unref (msg_header);
828 headers = tny_simple_list_new ();
829 tny_folder_get_headers (folder, headers, TRUE, NULL);
830 iter = tny_list_create_iterator (headers);
832 while (!tny_iterator_is_done (iter)) {
833 gchar *cur_id = NULL;
835 header = TNY_HEADER (tny_iterator_get_current (iter));
837 cur_id = tny_header_dup_uid (header);
839 if (cur_id && uid && g_str_equal (cur_id, uid)) {
841 /* g_debug ("Found corresponding header from folder"); */
847 g_object_unref (header);
851 tny_iterator_next (iter);
854 g_object_unref (iter);
855 g_object_unref (headers);
857 if (header == NULL) {
859 g_object_unref (folder);
860 g_idle_add (notify_error_in_dbus_callback, NULL);
865 /* This is a GDK lock because we are an idle callback and
866 * the code below is or does Gtk+ code */
867 gdk_threads_enter (); /* CHECKED */
869 mail_op = modest_mail_operation_new (top_win ? G_OBJECT(top_win) : NULL);
870 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
872 g_signal_connect (G_OBJECT (mail_op),
873 "operation-finished",
874 G_CALLBACK (on_remove_msgs_finished),
875 g_object_ref (header));
877 tmp_headers = tny_simple_list_new ();
878 tny_list_append (tmp_headers, (GObject *) header);
880 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
882 g_object_unref (tmp_headers);
883 g_object_unref (G_OBJECT (mail_op));
884 gdk_threads_leave (); /* CHECKED */
888 g_object_unref (header);
895 on_idle_delete_message (gpointer user_data)
897 const char *uri = NULL;
899 uri = (char *) user_data;
901 g_thread_create (thread_prepare_delete_message, g_strdup (uri), FALSE, NULL);
911 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
913 /* Get the arguments: */
914 osso_rpc_t val = g_array_index (arguments,
916 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
917 gchar *uri = g_strdup (val.value.s);
919 /* Use g_idle to context-switch into the application's thread: */
920 g_idle_add(on_idle_delete_message, (gpointer)uri);
926 on_idle_send_receive(gpointer user_data)
928 gboolean auto_update;
929 ModestMailOperation *mail_op = (ModestMailOperation *) user_data;
930 ModestConnectedVia connect_when;
933 gdk_threads_enter (); /* CHECKED */
935 /* Check if the autoupdate feature is on */
936 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
937 MODEST_CONF_AUTO_UPDATE, NULL);
940 /* Do send receive. Never set the current top window
941 as we always assume that DBus send/receive requests
942 are not user driven */
944 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
945 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
946 /* Perform a send and receive if the user selected to connect
947 via any mean or if the current connection method is the
948 same as the one specified by the user */
949 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
950 connect_when == modest_platform_get_current_connection ()) {
951 modest_ui_actions_do_send_receive_all (NULL, FALSE, FALSE, FALSE);
954 /* Disable auto update */
955 modest_platform_set_update_interval (0);
958 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
961 g_object_unref (mail_op);
963 gdk_threads_leave (); /* CHECKED */
971 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
976 dbus_uint32_t serial = 0;
978 GSList *account_names, *cursor;
980 str = g_strdup("\nsend queues\n"
983 cursor = account_names = modest_account_mgr_account_names
984 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
988 gchar *tmp, *accname = (gchar*)cursor->data;
990 tmp = g_strdup_printf ("%s", str);
995 acc = modest_tny_account_store_get_server_account (
996 modest_runtime_get_account_store(), accname,
997 TNY_ACCOUNT_TYPE_TRANSPORT);
998 if (TNY_IS_ACCOUNT(acc)) {
999 gchar *tmp = NULL, *url = tny_account_get_url_string (acc);
1000 ModestTnySendQueue *sendqueue =
1001 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
1003 if (TNY_IS_SEND_QUEUE (sendqueue)) {
1004 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
1006 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
1007 str, accname, tny_account_get_id (acc), url,
1015 g_object_unref (acc);
1018 cursor = g_slist_next (cursor);
1020 modest_account_mgr_free_account_names (account_names);
1024 reply = dbus_message_new_method_return (message);
1026 dbus_message_append_args (reply,
1027 DBUS_TYPE_STRING, &str,
1029 dbus_connection_send (con, reply, &serial);
1030 dbus_connection_flush (con);
1031 dbus_message_unref (reply);
1035 /* Let modest die */
1036 g_idle_add (notify_error_in_dbus_callback, NULL);
1043 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
1046 gchar *op_queue_str;
1049 dbus_uint32_t serial = 0;
1051 /* operations queue; */
1052 op_queue_str = modest_mail_operation_queue_to_string
1053 (modest_runtime_get_mail_operation_queue ());
1055 str = g_strdup_printf ("\noperation queue\n"
1059 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
1061 g_free (op_queue_str);
1065 reply = dbus_message_new_method_return (message);
1067 dbus_message_append_args (reply,
1068 DBUS_TYPE_STRING, &str,
1070 dbus_connection_send (con, reply, &serial);
1071 dbus_connection_flush (con);
1072 dbus_message_unref (reply);
1076 /* Let modest die */
1077 g_idle_add (notify_error_in_dbus_callback, NULL);
1085 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1090 dbus_uint32_t serial = 0;
1092 GSList *account_names, *cursor;
1094 str = g_strdup ("\naccounts\n========\n");
1096 cursor = account_names = modest_account_mgr_account_names
1097 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1101 gchar *tmp, *accname = (gchar*)cursor->data;
1103 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1108 acc = modest_tny_account_store_get_server_account (
1109 modest_runtime_get_account_store(), accname,
1110 TNY_ACCOUNT_TYPE_STORE);
1111 if (TNY_IS_ACCOUNT(acc)) {
1112 gchar *tmp, *url = tny_account_get_url_string (acc);
1113 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1114 str, tny_account_get_id (acc), url,
1115 ((GObject*)acc)->ref_count-1);
1119 g_object_unref (acc);
1123 acc = modest_tny_account_store_get_server_account (
1124 modest_runtime_get_account_store(), accname,
1125 TNY_ACCOUNT_TYPE_TRANSPORT);
1126 if (TNY_IS_ACCOUNT(acc)) {
1127 gchar *tmp, *url = tny_account_get_url_string (acc);
1128 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1129 str, tny_account_get_id (acc), url,
1130 ((GObject*)acc)->ref_count-1);
1134 g_object_unref (acc);
1137 cursor = g_slist_next (cursor);
1140 modest_account_mgr_free_account_names (account_names);
1144 reply = dbus_message_new_method_return (message);
1146 dbus_message_append_args (reply,
1147 DBUS_TYPE_STRING, &str,
1149 dbus_connection_send (con, reply, &serial);
1150 dbus_connection_flush (con);
1151 dbus_message_unref (reply);
1155 /* Let modest die */
1156 g_idle_add (notify_error_in_dbus_callback, NULL);
1162 on_send_receive_performer(gboolean canceled,
1164 GtkWindow *parent_window,
1165 TnyAccount *account,
1168 ModestMailOperation *mail_op;
1170 if (err || canceled) {
1171 g_idle_add (notify_error_in_dbus_callback, NULL);
1175 mail_op = modest_mail_operation_new (NULL);
1176 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1178 modest_heartbeat_add (on_idle_send_receive, mail_op);
1183 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1185 TnyDevice *device = modest_runtime_get_device ();
1187 if (!tny_device_is_online (device))
1188 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, NULL);
1190 on_send_receive_performer (FALSE, NULL, NULL, NULL, NULL);
1196 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1198 g_idle_add(on_idle_top_application, NULL);
1205 on_idle_open_account (gpointer user_data)
1208 ModestWindowMgr *mgr;
1211 gdk_threads_enter ();
1213 acc_name = (gchar *) user_data;
1214 mgr = modest_runtime_get_window_mgr ();
1216 top = modest_window_mgr_get_current_top (mgr);
1218 top = modest_window_mgr_show_initial_window (mgr);
1220 #ifdef MODEST_TOOLKIT_HILDON2
1221 GtkWidget *new_window;
1222 ModestProtocolType store_protocol;
1223 gboolean mailboxes_protocol;
1225 store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
1227 mailboxes_protocol =
1228 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
1230 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
1232 if (mailboxes_protocol) {
1233 new_window = GTK_WIDGET (modest_mailboxes_window_new (acc_name));
1235 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
1236 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window),
1240 if (modest_window_mgr_register_window (mgr, MODEST_WINDOW (new_window), NULL)) {
1241 gtk_widget_show (new_window);
1243 gtk_widget_destroy (new_window);
1247 if (MODEST_IS_MAIN_WINDOW (top)) {
1249 GtkWidget *folder_view;
1251 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (top),
1252 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1253 server_name = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
1254 acc_name, TNY_ACCOUNT_TYPE_STORE);
1256 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (folder_view),
1258 g_free (server_name);
1263 gdk_threads_leave ();
1269 on_open_account (GArray *arguments, gpointer data, osso_rpc_t *retval)
1274 /* Get the arguments: */
1275 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
1276 account_id = g_strdup (val.value.s);
1278 g_idle_add (on_idle_open_account, account_id);
1283 #ifdef MODEST_TOOLKIT_HILDON2
1285 on_idle_top_application (gpointer user_data)
1287 HildonWindowStack *stack;
1290 /* This is a GDK lock because we are an idle callback and
1291 * the code below is or does Gtk+ code */
1293 gdk_threads_enter (); /* CHECKED */
1295 stack = hildon_window_stack_get_default ();
1296 window = GTK_WIDGET (hildon_window_stack_peek (stack));
1299 gtk_window_present (GTK_WINDOW (window));
1301 ModestWindowMgr *mgr;
1303 mgr = modest_runtime_get_window_mgr ();
1304 window = (GtkWidget *) modest_window_mgr_show_initial_window (mgr);
1306 modest_platform_remove_new_mail_notifications (FALSE);
1308 g_printerr ("modest: failed to get main window instance\n");
1312 gdk_threads_leave (); /* CHECKED */
1314 return FALSE; /* Do not call this callback again. */
1318 on_idle_top_application (gpointer user_data)
1320 ModestWindow *main_win;
1321 gboolean new_window = FALSE;
1323 /* This is a GDK lock because we are an idle callback and
1324 * the code below is or does Gtk+ code */
1326 gdk_threads_enter (); /* CHECKED */
1328 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1332 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1338 /* If we're showing an already existing window then
1339 reselect the INBOX */
1341 GtkWidget *folder_view;
1342 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_win),
1343 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1344 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
1349 gtk_widget_show_all (GTK_WIDGET (main_win));
1350 gtk_window_present (GTK_WINDOW (main_win));
1353 gdk_threads_leave (); /* CHECKED */
1355 return FALSE; /* Do not call this callback again. */
1360 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1362 /* Use g_idle to context-switch into the application's thread: */
1363 g_idle_add(on_idle_top_application, NULL);
1369 on_idle_show_memory_low (gpointer user_data)
1371 ModestWindow *main_win = NULL;
1373 gdk_threads_enter ();
1374 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
1375 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1376 dgettext("ke-recv","memr_ib_operation_disabled"),
1378 gdk_threads_leave ();
1384 on_idle_present_modal (gpointer user_data)
1386 gdk_threads_enter ();
1387 gtk_window_present (user_data);
1388 gdk_threads_leave ();
1394 /* Callback for normal D-BUS messages */
1396 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1397 GArray * arguments, gpointer data,
1398 osso_rpc_t * retval)
1402 /* Check memory low conditions */
1403 if (modest_platform_check_memory_low (NULL, FALSE)) {
1404 g_idle_add (on_idle_show_memory_low, NULL);
1408 /* Check if there is already a dialog or note open */
1409 dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
1411 g_idle_add (on_idle_present_modal, dialog);
1415 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1416 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1418 modest_runtime_set_allow_shutdown (TRUE);
1419 return on_mail_to (arguments, data, retval);
1420 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1421 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1423 modest_runtime_set_allow_shutdown (TRUE);
1424 return on_open_message (arguments, data, retval);
1425 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_ACCOUNT) == 0) {
1426 if (arguments->len != MODEST_DBUS_OPEN_ACCOUNT_ARGS_COUNT)
1428 modest_runtime_set_allow_shutdown (TRUE);
1429 return on_open_account (arguments, data, retval);
1430 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1431 if (arguments->len != 0)
1433 return on_send_receive (arguments, data, retval);
1434 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1435 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1437 modest_runtime_set_allow_shutdown (TRUE);
1438 return on_compose_mail (arguments, data, retval);
1439 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1440 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1442 return on_delete_message (arguments,data, retval);
1443 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1444 if (arguments->len != 0)
1446 modest_runtime_set_allow_shutdown (TRUE);
1447 return on_open_default_inbox (arguments, data, retval);
1448 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1449 if (arguments->len != 0)
1451 modest_runtime_set_allow_shutdown (TRUE);
1452 return on_top_application (arguments, data, retval);
1454 /* We need to return INVALID here so
1455 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1456 * so that our modest_dbus_req_filter will then be tried instead.
1458 return OSSO_INVALID;
1461 /* Notify error in D-Bus method */
1462 g_idle_add (notify_error_in_dbus_callback, NULL);
1466 /* A complex D-Bus type (like a struct),
1467 * used to return various information about a search hit.
1469 #define SEARCH_HIT_DBUS_TYPE \
1470 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1471 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1472 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1473 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1474 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1475 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1476 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1477 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1478 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1479 DBUS_STRUCT_END_CHAR_AS_STRING
1481 static DBusMessage *
1482 search_result_to_message (DBusMessage *reply,
1485 DBusMessageIter iter;
1486 DBusMessageIter array_iter;
1489 dbus_message_iter_init_append (reply, &iter);
1490 dbus_message_iter_open_container (&iter,
1492 SEARCH_HIT_DBUS_TYPE,
1495 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1496 DBusMessageIter struct_iter;
1497 ModestSearchResultHit *hit;
1499 const char *subject;
1503 gboolean has_attachment;
1507 hit = (ModestSearchResultHit *) hit_iter->data;
1509 msg_url = hit->msgid;
1510 subject = hit->subject;
1511 folder = hit->folder;
1512 sender = hit->sender;
1514 has_attachment = hit->has_attachment;
1515 is_unread = hit->is_unread;
1516 ts = hit->timestamp;
1518 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1520 dbus_message_iter_open_container (&array_iter,
1525 dbus_message_iter_append_basic (&struct_iter,
1529 dbus_message_iter_append_basic (&struct_iter,
1533 dbus_message_iter_append_basic (&struct_iter,
1537 dbus_message_iter_append_basic (&struct_iter,
1541 dbus_message_iter_append_basic (&struct_iter,
1545 dbus_message_iter_append_basic (&struct_iter,
1549 dbus_message_iter_append_basic (&struct_iter,
1553 dbus_message_iter_append_basic (&struct_iter,
1557 dbus_message_iter_close_container (&array_iter,
1560 g_free (hit->msgid);
1561 g_free (hit->subject);
1562 g_free (hit->folder);
1563 g_free (hit->sender);
1565 g_slice_free (ModestSearchResultHit, hit);
1568 dbus_message_iter_close_container (&iter, &array_iter);
1575 DBusConnection *con;
1576 DBusMessage *message;
1577 ModestSearch *search;
1581 search_all_cb (GList *hits, gpointer user_data)
1584 SearchHelper *helper = (SearchHelper *) user_data;
1586 reply = dbus_message_new_method_return (helper->message);
1589 dbus_uint32_t serial = 0;
1591 search_result_to_message (reply, hits);
1593 dbus_connection_send (helper->con, reply, &serial);
1594 dbus_connection_flush (helper->con);
1595 dbus_message_unref (reply);
1598 /* Free the helper */
1599 dbus_message_unref (helper->message);
1600 modest_search_free (helper->search);
1601 g_slice_free (ModestSearch, helper->search);
1602 g_slice_free (SearchHelper, helper);
1606 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1608 ModestDBusSearchFlags dbus_flags;
1612 dbus_int32_t flags_v;
1613 dbus_uint32_t size_v;
1618 ModestSearch *search;
1621 dbus_error_init (&error);
1626 res = dbus_message_get_args (message,
1628 DBUS_TYPE_STRING, &query,
1629 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1630 DBUS_TYPE_INT64, &sd_v,
1631 DBUS_TYPE_INT64, &ed_v,
1632 DBUS_TYPE_INT32, &flags_v,
1633 DBUS_TYPE_UINT32, &size_v,
1636 dbus_flags = (ModestDBusSearchFlags) flags_v;
1637 start_date = (time_t) sd_v;
1638 end_date = (time_t) ed_v;
1640 search = g_slice_new0 (ModestSearch);
1642 if (folder && g_str_has_prefix (folder, "MAND:")) {
1643 search->folder = g_strdup (folder + strlen ("MAND:"));
1644 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1645 search->folder = g_strdup (folder + strlen ("USER:"));
1646 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1647 search->folder = g_strdup (folder + strlen ("MY:"));
1649 search->folder = g_strdup (folder);
1652 /* Remember the text to search for: */
1653 #ifdef MODEST_HAVE_OGS
1654 search->query = g_strdup (query);
1657 /* Other criteria: */
1658 search->start_date = start_date;
1659 search->end_date = end_date;
1662 /* Text to serach for in various parts of the message: */
1663 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1664 search->flags |= MODEST_SEARCH_SUBJECT;
1665 search->subject = g_strdup (query);
1668 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1669 search->flags |= MODEST_SEARCH_SENDER;
1670 search->from = g_strdup (query);
1673 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1674 search->flags |= MODEST_SEARCH_RECIPIENT;
1675 search->recipient = g_strdup (query);
1678 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1679 search->flags |= MODEST_SEARCH_BODY;
1680 search->body = g_strdup (query);
1684 search->flags |= MODEST_SEARCH_AFTER;
1685 search->start_date = start_date;
1689 search->flags |= MODEST_SEARCH_BEFORE;
1690 search->end_date = end_date;
1694 search->flags |= MODEST_SEARCH_SIZE;
1695 search->minsize = size_v;
1698 #ifdef MODEST_HAVE_OGS
1699 search->flags |= MODEST_SEARCH_USE_OGS;
1700 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1703 SearchHelper *helper = g_slice_new (SearchHelper);
1704 helper->search = search;
1705 dbus_message_ref (message);
1706 helper->message = message;
1709 /* Search asynchronously */
1710 modest_search_all_accounts (search, search_all_cb, helper);
1714 /* A complex D-Bus type (like a struct),
1715 * used to return various information about a folder.
1717 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1718 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1719 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1720 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1721 DBUS_STRUCT_END_CHAR_AS_STRING
1723 static DBusMessage *
1724 get_folders_result_to_message (DBusMessage *reply,
1727 DBusMessageIter iter;
1728 dbus_message_iter_init_append (reply, &iter);
1730 DBusMessageIter array_iter;
1731 dbus_message_iter_open_container (&iter,
1733 GET_FOLDERS_RESULT_DBUS_TYPE,
1736 GList *list_iter = folder_ids;
1737 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1739 const gchar *folder_name = (const gchar*)list_iter->data;
1741 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1743 DBusMessageIter struct_iter;
1744 dbus_message_iter_open_container (&array_iter,
1750 dbus_message_iter_append_basic (&struct_iter,
1752 &folder_name); /* The string will be copied. */
1754 /* URI: This is maybe not needed by osso-global-search: */
1755 const gchar *folder_uri = "TODO:unimplemented";
1756 dbus_message_iter_append_basic (&struct_iter,
1758 &folder_uri); /* The string will be copied. */
1760 dbus_message_iter_close_container (&array_iter,
1765 dbus_message_iter_close_container (&iter, &array_iter);
1771 add_single_folder_to_list (TnyFolder *folder, GList** list)
1776 if (TNY_IS_MERGE_FOLDER (folder)) {
1777 const gchar * folder_name;
1778 /* Ignore these because their IDs ares
1779 * a) not always unique or sensible.
1780 * b) not human-readable, and currently need a human-readable
1781 * ID here, because the osso-email-interface API does not allow
1782 * us to return both an ID and a display name.
1784 * This is actually the merged outbox folder.
1785 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1786 * but that seems unwise. murrayc.
1788 folder_name = tny_folder_get_name (folder);
1789 if (folder_name && !strcmp (folder_name, "Outbox")) {
1790 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1795 /* Add this folder to the list: */
1797 const gchar * folder_name = tny_folder_get_name (folder);
1799 *list = g_list_append(*list, g_strdup (folder_name));
1802 /* osso-global-search only uses one string,
1803 * so ID is the only thing that could possibly identify a folder.
1804 * TODO: osso-global search should probably be changed to
1805 * take an ID and a Name.
1807 const gchar * id = tny_folder_get_id (folder);
1808 if (id && strlen(id)) {
1809 const gchar *prefix = NULL;
1810 TnyFolderType folder_type;
1812 /* dbus global search api expects a prefix identifying the type of
1813 * folder here. Mandatory folders should have MAND: prefix, and
1814 * other user created folders should have USER: prefix
1816 folder_type = modest_tny_folder_guess_folder_type (folder);
1817 switch (folder_type) {
1818 case TNY_FOLDER_TYPE_INBOX:
1821 case TNY_FOLDER_TYPE_OUTBOX:
1822 case TNY_FOLDER_TYPE_DRAFTS:
1823 case TNY_FOLDER_TYPE_SENT:
1824 case TNY_FOLDER_TYPE_ARCHIVE:
1827 case TNY_FOLDER_TYPE_INVALID:
1828 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1829 return; /* don't add it */
1836 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1841 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1846 /* Add this folder to the list: */
1847 if (TNY_IS_FOLDER (folder_store)) {
1848 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1851 /* Recurse into child folders: */
1853 /* Get the folders list: */
1855 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1856 tny_folder_store_query_add_item (query, NULL,
1857 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1859 TnyList *all_folders = tny_simple_list_new ();
1860 tny_folder_store_get_folders (folder_store,
1866 TnyIterator *iter = tny_list_create_iterator (all_folders);
1867 while (!tny_iterator_is_done (iter)) {
1869 /* Do not recurse, because the osso-global-search UI specification
1870 * does not seem to want the sub-folders, though that spec seems to
1871 * be generally unsuitable for Modest.
1873 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1875 add_single_folder_to_list (TNY_FOLDER (folder), list);
1878 if (TNY_IS_FOLDER_STORE (folder))
1879 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1881 add_single_folder_to_list (TNY_FOLDER (folder), list);
1885 /* tny_iterator_get_current() gave us a reference. */
1886 g_object_unref (folder);
1889 tny_iterator_next (iter);
1891 g_object_unref (G_OBJECT (iter));
1895 /* return >1 for a special folder, 0 for a user-folder */
1897 get_rank (const gchar *folder)
1899 if (strcmp (folder, "INBOX") == 0)
1901 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1903 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1905 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1911 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1913 gint r1 = get_rank (folder1);
1914 gint r2 = get_rank (folder2);
1916 if (r1 > 0 && r2 > 0)
1918 if (r1 > 0 && r2 == 0)
1920 if (r1 == 0 && r2 > 0)
1923 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1927 /* - we're still missing the outbox */
1928 /* - we need to take care of localization (urgh) */
1929 /* - what about 'All mail folders'? */
1931 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1933 DBusMessage *reply = NULL;
1934 ModestAccountMgr *account_mgr = NULL;
1935 gchar *account_name = NULL;
1936 GList *folder_names = NULL;
1937 TnyAccount *account_local = NULL;
1938 TnyAccount *account_mmc = NULL;
1940 /* Get the TnyStoreAccount so we can get the folders: */
1941 account_mgr = modest_runtime_get_account_mgr();
1942 account_name = modest_account_mgr_get_default_account (account_mgr);
1943 if (!account_name) {
1944 g_printerr ("modest: no account found\n");
1948 TnyAccount *account = NULL;
1950 account = modest_tny_account_store_get_server_account (
1951 modest_runtime_get_account_store(), account_name,
1952 TNY_ACCOUNT_TYPE_STORE);
1956 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1959 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1960 g_free (account_name);
1961 account_name = NULL;
1963 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1965 g_object_unref (account);
1969 /* Also add the folders from the local folders account,
1970 * because they are (currently) used with all accounts:
1971 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1974 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1975 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1977 g_object_unref (account_local);
1978 account_local = NULL;
1980 /* Obtain the mmc account */
1982 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1984 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1985 g_object_unref (account_mmc);
1989 /* specs require us to sort the folder names, although
1990 * this is really not the place to do that...
1992 folder_names = g_list_sort (folder_names,
1993 (GCompareFunc)folder_name_compare_func);
1995 /* Put the result in a DBus reply: */
1996 reply = dbus_message_new_method_return (message);
1998 get_folders_result_to_message (reply, folder_names);
2000 if (reply == NULL) {
2001 g_warning ("%s: Could not create reply.", __FUNCTION__);
2005 dbus_uint32_t serial = 0;
2006 dbus_connection_send (con, reply, &serial);
2007 dbus_connection_flush (con);
2008 dbus_message_unref (reply);
2011 g_list_foreach (folder_names, (GFunc)g_free, NULL);
2012 g_list_free (folder_names);
2017 reply_empty_results (DBusConnection *con, DBusMessage *msg)
2019 DBusMessage *reply = dbus_message_new_method_return (msg);
2021 dbus_uint32_t serial = 0;
2022 /* we simply return an empty list, otherwise
2023 global-search gets confused */
2024 search_result_to_message (reply, NULL);
2026 dbus_connection_send (con, reply, &serial);
2027 dbus_connection_flush (con);
2028 dbus_message_unref (reply);
2030 g_warning ("%s: failed to send reply",
2035 /** This D-Bus handler is used when the main osso-rpc
2036 * D-Bus handler has not handled something.
2037 * We use this for D-Bus methods that need to use more complex types
2038 * than osso-rpc supports.
2041 modest_dbus_req_filter (DBusConnection *con,
2042 DBusMessage *message,
2045 gboolean handled = FALSE;
2047 if (dbus_message_is_method_call (message,
2049 MODEST_DBUS_METHOD_SEARCH)) {
2051 /* don't try to search when there not enough mem */
2052 if (modest_platform_check_memory_low (NULL, TRUE)) {
2053 g_warning ("%s: not enough memory for searching",
2055 reply_empty_results (con, message);
2059 on_dbus_method_search (con, message);
2063 } else if (dbus_message_is_method_call (message,
2065 MODEST_DBUS_METHOD_GET_FOLDERS)) {
2066 on_dbus_method_get_folders (con, message);
2068 } else if (dbus_message_is_method_call (message,
2070 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
2071 on_dbus_method_dump_operation_queue (con, message);
2073 } else if (dbus_message_is_method_call (message,
2075 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
2076 on_dbus_method_dump_accounts (con, message);
2078 } else if (dbus_message_is_method_call (message,
2080 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
2081 on_dbus_method_dump_send_queues (con, message);
2084 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
2086 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
2087 __FUNCTION__, dbus_message_get_interface (message),
2088 dbus_message_get_member(message));
2093 DBUS_HANDLER_RESULT_HANDLED :
2094 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
2098 notify_error_in_dbus_callback (gpointer user_data)
2100 ModestMailOperation *mail_op;
2101 ModestMailOperationQueue *mail_op_queue;
2103 mail_op = modest_mail_operation_new (NULL);
2104 mail_op_queue = modest_runtime_get_mail_operation_queue ();
2106 /* Issues a noop operation in order to force the queue to emit
2107 the "queue-empty" signal to allow modest to quit */
2108 modest_mail_operation_queue_add (mail_op_queue, mail_op);
2109 modest_mail_operation_noop (mail_op);
2110 g_object_unref (mail_op);