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 #include <hildon/hildon-program.h>
51 #include <libgnomevfs/gnome-vfs-mime.h>
52 #include <tny-fs-stream.h>
54 #ifdef MODEST_TOOLKIT_HILDON2
55 #include <hildon/hildon.h>
56 #include <modest-accounts-window.h>
57 #include <modest-folder-window.h>
58 #include <modest-mailboxes-window.h>
59 #include <modest-maemo-utils.h>
63 #include <tny-iterator.h>
64 #include <tny-simple-list.h>
65 #include <tny-merge-folder.h>
66 #include <tny-account.h>
68 #include <modest-text-utils.h>
70 #define DISABLE_GET_UNREAD_MSGS_FOR_MULTI_MAILBOX 1
80 } ComposeMailIdleData;
83 static gboolean notify_error_in_dbus_callback (gpointer user_data);
84 static gboolean on_idle_compose_mail(gpointer user_data);
85 static gboolean on_idle_top_application (gpointer user_data);
88 * @uri An escaped URI. URIs should always be escaped.
89 * @len The length of the @uri string, or -1 if the string is null terminated.
91 * Decode a URI, or URI fragment, as per RFC 1738.
92 * http://www.ietf.org/rfc/rfc1738.txt
94 * Return value: An unescaped string. This should be freed with g_free().
97 uri_unescape(const gchar* uri, size_t len)
105 /* Allocate an extra string so we can be sure that it is null-terminated,
106 * so we can use gnome_vfs_unescape_string().
107 * This is not efficient. */
108 gchar * escaped_nullterminated = g_strndup (uri, len);
109 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
110 g_free (escaped_nullterminated);
115 /** uri_parse_mailto:
116 * @mailto A mailto URI, with the mailto: prefix.
117 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
118 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
119 * all the string items have been freed. This parameter may be NULL.
120 * Parse a mailto URI as per RFC2368.
121 * http://www.ietf.org/rfc/rfc2368.txt
123 * Return value: The to address, unescaped. This should be freed with g_free().
126 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
128 /* The URL must begin with mailto: */
129 if (strncmp (mailto, "mailto:", 7) != 0) {
132 const gchar* start_to = mailto + 7;
134 /* Look for ?, or the end of the string, marking the end of the to address: */
135 const size_t len_to = strcspn (start_to, "?");
136 gchar* result_to = uri_unescape (start_to, len_to);
137 printf("debug: result_to=%s\n", result_to);
139 if (list_items_and_values == NULL) {
143 /* Get any other items: */
144 const size_t len_mailto = strlen (start_to);
145 const gchar* p = start_to + len_to + 1; /* parsed so far. */
146 const gchar* end = start_to + len_mailto;
148 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
150 name_end = strchr (name_start, '='); /* Separator between name and value */
151 if (name_end == NULL) {
152 g_debug ("Malformed URI: %s\n", mailto);
155 value_start = name_end + 1;
156 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
158 name = g_strndup(name_start, name_end - name_start);
159 if (value_end != NULL) {
160 value = uri_unescape(value_start, value_end - value_start);
163 value = uri_unescape(value_start, -1);
166 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
167 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
174 check_and_offer_account_creation()
176 gboolean result = TRUE;
178 /* This is called from idle handlers, so lock gdk: */
179 gdk_threads_enter ();
181 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
182 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
184 g_debug ("modest: %s: no account exists even after offering, "
185 "or account setup was already underway.\n", __FUNCTION__);
190 gdk_threads_leave ();
196 on_idle_mail_to(gpointer user_data)
198 gchar *uri = (gchar*)user_data;
199 GSList *list_names_and_values = NULL;
201 const gchar *cc = NULL;
202 const gchar *bcc = NULL;
203 const gchar *subject = NULL;
204 const gchar *body = NULL;
206 if (!check_and_offer_account_creation ()) {
207 g_idle_add (notify_error_in_dbus_callback, NULL);
211 /* Get the relevant items from the list: */
212 to = uri_parse_mailto (uri, &list_names_and_values);
213 GSList *list = list_names_and_values;
215 GSList *list_value = g_slist_next (list);
216 const gchar * name = (const gchar*)list->data;
217 const gchar * value = (const gchar*)list_value->data;
219 if (strcmp (name, "cc") == 0) {
221 } else if (strcmp (name, "bcc") == 0) {
223 } else if (strcmp (name, "subject") == 0) {
225 } else if (strcmp (name, "body") == 0) {
229 list = g_slist_next (list_value);
232 gdk_threads_enter (); /* CHECKED */
233 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
234 gdk_threads_leave (); /* CHECKED */
237 /* Free the to: and the list, as required by uri_parse_mailto() */
239 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
240 g_slist_free (list_names_and_values);
244 return FALSE; /* Do not call this callback again. */
248 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
254 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
255 uri = g_strdup (val.value.s);
257 g_idle_add(on_idle_mail_to, (gpointer)uri);
259 /* Note that we cannot report failures during sending,
260 * because that would be asynchronous. */
266 on_idle_compose_mail(gpointer user_data)
268 GSList *attachments = NULL;
269 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
271 if (!check_and_offer_account_creation ()) {
272 g_idle_add (notify_error_in_dbus_callback, NULL);
276 /* it seems Sketch at least sends a leading ',' -- take that into account,
278 if (idle_data->attachments && idle_data->attachments[0]==',') {
279 gchar *tmp = g_strdup (idle_data->attachments + 1);
280 g_free(idle_data->attachments);
281 idle_data->attachments = tmp;
284 if (idle_data->attachments != NULL) {
285 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
287 for (i=0; list[i] != NULL; i++) {
288 attachments = g_slist_append(attachments, g_uri_unescape_string (list[i], NULL));
293 /* If the message has nothing then mark the buffers as not
294 modified. This happens in Maemo for example when opening a
295 new message from Contacts plugin, it sends "" instead of
297 gdk_threads_enter (); /* CHECKED */
298 if (!strncmp (idle_data->to, "", 1) &&
299 !strncmp (idle_data->to, "", 1) &&
300 !strncmp (idle_data->cc, "", 1) &&
301 !strncmp (idle_data->bcc, "", 1) &&
302 !strncmp (idle_data->subject, "", 1) &&
303 !strncmp (idle_data->body, "", 1) &&
304 attachments == NULL) {
305 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
307 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
308 idle_data->bcc, idle_data->subject,
309 idle_data->body, attachments, TRUE);
311 gdk_threads_leave (); /* CHECKED */
313 g_slist_foreach(attachments, (GFunc)g_free, NULL);
314 g_slist_free(attachments);
315 g_free (idle_data->to);
316 g_free (idle_data->cc);
317 g_free (idle_data->bcc);
318 g_free (idle_data->subject);
319 g_free (idle_data->body);
320 g_free (idle_data->attachments);
323 return FALSE; /* Do not call this callback again. */
327 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
329 ComposeMailIdleData *idle_data;
332 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
334 /* Get the arguments: */
335 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
336 idle_data->to = g_strdup (val.value.s);
338 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
339 idle_data->cc = g_strdup (val.value.s);
341 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
342 idle_data->bcc = g_strdup (val.value.s);
344 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
345 idle_data->subject = g_strdup (val.value.s);
347 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
348 idle_data->body = g_strdup (val.value.s);
350 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
351 idle_data->attachments = g_strdup (val.value.s);
353 /* Use g_idle to context-switch into the application's thread: */
354 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
360 find_message_by_url (const char *uri, TnyAccount **ac_out)
362 ModestTnyAccountStore *astore;
363 TnyAccount *account = NULL;
364 TnyFolder *folder = NULL;
367 astore = modest_runtime_get_account_store ();
372 if (uri && g_str_has_prefix (uri, "merge://")) {
373 /* we assume we're talking about outbox folder, as this
374 * is the only merge folder we work with in modest */
375 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
377 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
380 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
384 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
389 msg = tny_folder_find_msg (folder, uri, NULL);
392 if (account && !msg) {
393 g_object_unref (account);
397 g_object_unref (folder);
406 guint animation_timeout;
407 GtkWidget *animation;
408 } OpenMsgPerformerInfo;
410 #ifndef MODEST_TOOLKIT_HILDON2
412 on_show_opening_animation (gpointer userdata)
414 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
415 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
416 info->animation_timeout = 0;
423 on_find_msg_async_destroy (gpointer userdata)
425 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
427 if (info->animation_timeout>0) {
428 g_source_remove (info->animation_timeout);
429 info->animation_timeout = 0;
432 if (info->animation) {
433 gtk_widget_destroy (info->animation);
434 info->animation = NULL;
441 g_object_unref (info->account);
443 g_slice_free (OpenMsgPerformerInfo, info);
448 find_msg_async_cb (TnyFolder *folder,
454 TnyHeader *header = NULL;
455 gchar *msg_uid = NULL;
456 ModestWindowMgr *win_mgr;
457 ModestWindow *msg_view = NULL;
458 gboolean is_draft = FALSE;
459 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
461 if (err || cancelled) {
462 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
463 g_idle_add (notify_error_in_dbus_callback, NULL);
467 header = tny_msg_get_header (msg);
468 if (!header || (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
469 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
470 g_idle_add (notify_error_in_dbus_callback, NULL);
474 msg_uid = modest_tny_folder_get_header_unique_id (header);
475 win_mgr = modest_runtime_get_window_mgr ();
477 if (modest_tny_folder_is_local_folder (folder) &&
478 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
482 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
483 gtk_window_present (GTK_WINDOW(msg_view));
485 const gchar *modest_account_name;
488 modest_window_mgr_register_header (win_mgr, header, NULL);
490 account = tny_folder_get_account (folder);
492 modest_account_name =
493 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
495 modest_account_name = NULL;
498 /* Drafts will be opened in the editor, and others will be opened in the viewer */
500 gchar *modest_account_name = NULL;
501 gchar *mailbox = NULL;
504 /* we cannot edit without a valid account... */
505 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
506 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
507 modest_window_mgr_unregister_header (win_mgr,
513 from_header = tny_header_dup_from (header);
514 modest_account_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
515 g_free (from_header);
517 if (modest_account_name == NULL) {
518 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
519 modest_account_name = modest_account_mgr_get_default_account (mgr);
521 msg_view = modest_msg_edit_window_new (msg, modest_account_name, mailbox, TRUE);
524 g_free (modest_account_name);
527 const gchar *modest_account_name;
530 modest_account_name =
531 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
533 modest_account_name = NULL;
536 header = tny_msg_get_header (msg);
537 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name, NULL, msg_uid);
538 if (! (tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
539 ModestMailOperation *mail_op;
541 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
542 modest_platform_emit_msg_read_changed_signal (msg_uid, TRUE);
543 /* Sync folder, we need this to save the seen flag */
544 mail_op = modest_mail_operation_new (NULL);
545 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
547 modest_mail_operation_sync_folder (mail_op, folder, FALSE, NULL, NULL);
548 g_object_unref (mail_op);
550 g_object_unref (header);
553 if (msg_view != NULL) {
554 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
555 gtk_widget_destroy (GTK_WIDGET (msg_view));
557 gtk_widget_show_all (GTK_WIDGET (msg_view));
564 g_object_unref (header);
567 on_find_msg_async_destroy (info);
572 on_open_message_performer (gboolean canceled,
574 ModestWindow *parent_window,
578 OpenMsgPerformerInfo *info;
579 TnyFolder *folder = NULL;
580 ModestTnyAccountStore *account_store;
581 ModestTnyLocalFoldersAccount *local_folders_account;
583 account_store = modest_runtime_get_account_store ();
584 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
585 modest_tny_account_store_get_local_folders_account (account_store));
587 info = (OpenMsgPerformerInfo *) user_data;
588 if (canceled || err) {
590 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
591 g_idle_add (notify_error_in_dbus_callback, NULL);
592 on_find_msg_async_destroy (info);
598 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
599 g_object_unref (local_folders_account);
600 } else if (account == TNY_ACCOUNT (local_folders_account)) {
601 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
605 if (!(folder && modest_tny_folder_is_local_folder (folder) &&
606 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS))) {
607 ModestWindowMgr *win_mgr;
608 ModestWindow *msg_view;
610 win_mgr = modest_runtime_get_window_mgr ();
611 if (modest_window_mgr_find_registered_message_uid (win_mgr, info->uri, &msg_view)) {
612 gtk_window_present (GTK_WINDOW(msg_view));
614 const gchar *modest_account_name;
616 modest_account_name =
617 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
619 modest_account_name = NULL;
622 msg_view = modest_msg_view_window_new_from_uid (modest_account_name, NULL, info->uri);
623 if (msg_view != NULL) {
624 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
625 gtk_widget_destroy (GTK_WIDGET (msg_view));
627 gtk_widget_show_all (GTK_WIDGET (msg_view));
631 g_object_unref (account);
634 on_find_msg_async_destroy (info);
638 #ifndef MODEST_TOOLKIT_HILDON2
639 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
642 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
643 g_object_unref (folder);
647 on_idle_open_message_performer (gpointer user_data)
649 ModestWindow *top_win = NULL;
650 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
652 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
654 /* Lock before the call as we're in an idle handler */
655 gdk_threads_enter ();
657 modest_platform_connect_and_perform (top_win, TRUE,
659 on_open_message_performer, info);
661 on_open_message_performer (FALSE, NULL, top_win,
662 info->account, info);
664 gdk_threads_leave ();
670 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
674 TnyAccount *account = NULL;
678 /* Get the arguments: */
679 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
680 uri = g_strdup (val.value.s);
682 is_merge = g_str_has_prefix (uri, "merge:");
684 /* Get the account */
686 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
690 if (is_merge || account) {
691 OpenMsgPerformerInfo *info;
692 TnyFolder *folder = NULL;
693 ModestTnyAccountStore *account_store;
694 ModestTnyLocalFoldersAccount *local_folders_account;
696 info = g_slice_new0 (OpenMsgPerformerInfo);
698 info->account = g_object_ref (account);
700 info->connect = TRUE;
701 info->animation = NULL;
702 info->animation_timeout = 0;
704 account_store = modest_runtime_get_account_store ();
705 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT
706 (modest_tny_account_store_get_local_folders_account (account_store));
708 /* Try to get the message, if it's already downloaded
709 we don't need to connect */
712 gboolean device_online;
714 device = modest_runtime_get_device ();
715 device_online = tny_device_is_online (device);
716 if (!device_online || TNY_ACCOUNT (local_folders_account) == account) {
717 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
722 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
723 g_object_unref (local_folders_account);
724 info->connect = FALSE;
727 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
729 info->connect = FALSE;
730 g_object_unref (msg);
732 info->connect = TRUE;
734 g_object_unref (folder);
737 /* We need to call it into an idle to get
738 modest_platform_connect_and_perform into the main
740 g_idle_add (on_idle_open_message_performer, info);
741 osso_retval = OSSO_OK;
744 osso_retval = OSSO_ERROR;
745 g_idle_add (notify_error_in_dbus_callback, NULL);
749 g_object_unref (account);
754 on_remove_msgs_finished (ModestMailOperation *mail_op,
758 ModestWindow *top_win = NULL, *msg_view = NULL;
760 header = (TnyHeader *) user_data;
762 /* Get the main window if exists */
763 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
765 g_object_unref (header);
769 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
770 header, &msg_view)) {
771 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
772 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
774 g_object_unref (header);
776 /* Refilter the header views explicitely */
778 /* TODO: call modest_window_mgr_refilter_header_views */
779 /* this call will go through all the windows, get the header views and refilter them */
783 thread_prepare_delete_message (gpointer userdata)
785 TnyList *headers = NULL, *tmp_headers = NULL;
786 TnyFolder *folder = NULL;
787 TnyIterator *iter = NULL;
788 TnyHeader *header = NULL, *msg_header = NULL;
790 TnyAccount *account = NULL;
793 ModestMailOperation *mail_op = NULL;
794 ModestWindow *top_win = NULL;
796 uri = (char *) userdata;
798 msg = find_message_by_url (uri, &account);
800 g_object_unref (account);
803 g_debug ("%s: Could not find message '%s'", __FUNCTION__, uri);
804 g_idle_add (notify_error_in_dbus_callback, NULL);
809 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
811 folder = tny_msg_get_folder (msg);
813 g_debug ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
814 g_object_unref (msg);
815 g_idle_add (notify_error_in_dbus_callback, NULL);
821 msg_header = tny_msg_get_header (msg);
822 uid = tny_header_dup_uid (msg_header);
823 g_object_unref (msg);
824 g_object_unref (msg_header);
826 headers = tny_simple_list_new ();
827 tny_folder_get_headers (folder, headers, TRUE, NULL);
828 iter = tny_list_create_iterator (headers);
830 while (!tny_iterator_is_done (iter)) {
831 gchar *cur_id = NULL;
833 header = TNY_HEADER (tny_iterator_get_current (iter));
835 cur_id = tny_header_dup_uid (header);
837 if (cur_id && uid && g_str_equal (cur_id, uid)) {
839 /* g_debug ("Found corresponding header from folder"); */
845 g_object_unref (header);
849 tny_iterator_next (iter);
852 g_object_unref (iter);
853 g_object_unref (headers);
855 if (header == NULL) {
857 g_object_unref (folder);
858 g_idle_add (notify_error_in_dbus_callback, NULL);
863 /* This is a GDK lock because we are an idle callback and
864 * the code below is or does Gtk+ code */
865 gdk_threads_enter (); /* CHECKED */
867 mail_op = modest_mail_operation_new (top_win ? G_OBJECT(top_win) : NULL);
868 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
870 g_signal_connect (G_OBJECT (mail_op),
871 "operation-finished",
872 G_CALLBACK (on_remove_msgs_finished),
873 g_object_ref (header));
875 tmp_headers = tny_simple_list_new ();
876 tny_list_append (tmp_headers, (GObject *) header);
878 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
880 g_object_unref (tmp_headers);
881 g_object_unref (G_OBJECT (mail_op));
882 gdk_threads_leave (); /* CHECKED */
886 g_object_unref (header);
893 on_idle_delete_message (gpointer user_data)
895 const char *uri = NULL;
897 uri = (char *) user_data;
899 g_thread_create (thread_prepare_delete_message, g_strdup (uri), FALSE, NULL);
909 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
911 /* Get the arguments: */
912 osso_rpc_t val = g_array_index (arguments,
914 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
915 gchar *uri = g_strdup (val.value.s);
917 /* Use g_idle to context-switch into the application's thread: */
918 g_idle_add(on_idle_delete_message, (gpointer)uri);
923 typedef struct _SendReceivePerformerData {
926 ModestMailOperation *mail_op;
927 } SendReceivePerformerData;
929 static void send_receive_performer_data_free (SendReceivePerformerData *data)
932 g_object_unref (data->mail_op);
934 g_free (data->account_id);
935 g_slice_free (SendReceivePerformerData, data);
939 on_idle_send_receive(gpointer user_data)
941 gboolean auto_update;
942 SendReceivePerformerData *data = (SendReceivePerformerData *) user_data;
943 ModestConnectedVia connect_when;
944 gboolean right_connection = FALSE;
946 gdk_threads_enter (); /* CHECKED */
948 /* Check if the autoupdate feature is on */
949 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
950 MODEST_CONF_AUTO_UPDATE, NULL);
953 /* Do send receive. Never set the current top window
954 as we always assume that DBus send/receive requests
955 are not user driven */
957 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
958 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
959 /* Perform a send and receive if the user selected to connect
960 via any mean or if the current connection method is the
961 same as the one specified by the user */
962 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
963 connect_when == modest_platform_get_current_connection ()) {
964 right_connection = TRUE;
967 /* Disable auto update */
968 modest_platform_set_update_interval (0);
971 if ((auto_update && right_connection) || data->manual) {
972 if (data->account_id) {
973 modest_ui_actions_do_send_receive (data->account_id, data->manual, FALSE, data->manual, NULL);
975 modest_ui_actions_do_send_receive_all (NULL, data->manual, FALSE, data->manual);
979 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
982 send_receive_performer_data_free (data);
984 gdk_threads_leave (); /* CHECKED */
990 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
995 dbus_uint32_t serial = 0;
997 GSList *account_names, *cursor;
999 str = g_strdup("\nsend queues\n"
1002 cursor = account_names = modest_account_mgr_account_names
1003 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1007 gchar *tmp, *accname = (gchar*)cursor->data;
1009 tmp = g_strdup_printf ("%s", str);
1014 acc = modest_tny_account_store_get_server_account (
1015 modest_runtime_get_account_store(), accname,
1016 TNY_ACCOUNT_TYPE_TRANSPORT);
1017 if (TNY_IS_ACCOUNT(acc)) {
1018 gchar *tmp = NULL, *url = tny_account_get_url_string (acc);
1019 ModestTnySendQueue *sendqueue =
1020 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
1022 if (TNY_IS_SEND_QUEUE (sendqueue)) {
1023 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
1025 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
1026 str, accname, tny_account_get_id (acc), url,
1034 g_object_unref (acc);
1037 cursor = g_slist_next (cursor);
1039 modest_account_mgr_free_account_names (account_names);
1043 reply = dbus_message_new_method_return (message);
1045 dbus_message_append_args (reply,
1046 DBUS_TYPE_STRING, &str,
1048 dbus_connection_send (con, reply, &serial);
1049 dbus_connection_flush (con);
1050 dbus_message_unref (reply);
1054 /* Let modest die */
1055 g_idle_add (notify_error_in_dbus_callback, NULL);
1062 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
1065 gchar *op_queue_str;
1068 dbus_uint32_t serial = 0;
1070 /* operations queue; */
1071 op_queue_str = modest_mail_operation_queue_to_string
1072 (modest_runtime_get_mail_operation_queue ());
1074 str = g_strdup_printf ("\noperation queue\n"
1078 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
1080 g_free (op_queue_str);
1084 reply = dbus_message_new_method_return (message);
1086 dbus_message_append_args (reply,
1087 DBUS_TYPE_STRING, &str,
1089 dbus_connection_send (con, reply, &serial);
1090 dbus_connection_flush (con);
1091 dbus_message_unref (reply);
1095 /* Let modest die */
1096 g_idle_add (notify_error_in_dbus_callback, NULL);
1104 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1109 dbus_uint32_t serial = 0;
1111 GSList *account_names, *cursor;
1113 str = g_strdup ("\naccounts\n========\n");
1115 cursor = account_names = modest_account_mgr_account_names
1116 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1120 gchar *tmp, *accname = (gchar*)cursor->data;
1122 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1127 acc = modest_tny_account_store_get_server_account (
1128 modest_runtime_get_account_store(), accname,
1129 TNY_ACCOUNT_TYPE_STORE);
1130 if (TNY_IS_ACCOUNT(acc)) {
1131 gchar *tmp, *url = tny_account_get_url_string (acc);
1132 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1133 str, tny_account_get_id (acc), url,
1134 ((GObject*)acc)->ref_count-1);
1138 g_object_unref (acc);
1142 acc = modest_tny_account_store_get_server_account (
1143 modest_runtime_get_account_store(), accname,
1144 TNY_ACCOUNT_TYPE_TRANSPORT);
1145 if (TNY_IS_ACCOUNT(acc)) {
1146 gchar *tmp, *url = tny_account_get_url_string (acc);
1147 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1148 str, tny_account_get_id (acc), url,
1149 ((GObject*)acc)->ref_count-1);
1153 g_object_unref (acc);
1156 cursor = g_slist_next (cursor);
1159 modest_account_mgr_free_account_names (account_names);
1163 reply = dbus_message_new_method_return (message);
1165 dbus_message_append_args (reply,
1166 DBUS_TYPE_STRING, &str,
1168 dbus_connection_send (con, reply, &serial);
1169 dbus_connection_flush (con);
1170 dbus_message_unref (reply);
1174 /* Let modest die */
1175 g_idle_add (notify_error_in_dbus_callback, NULL);
1181 on_send_receive_performer(gboolean canceled,
1183 ModestWindow *parent_window,
1184 TnyAccount *account,
1187 SendReceivePerformerData *data = (SendReceivePerformerData *) user_data;
1189 if (err || canceled || data == NULL) {
1190 g_idle_add (notify_error_in_dbus_callback, NULL);
1192 send_receive_performer_data_free (data);
1197 data->mail_op = modest_mail_operation_new (NULL);
1198 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1201 g_idle_add (on_idle_send_receive, data);
1203 modest_heartbeat_add (on_idle_send_receive, data);
1209 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1211 TnyDevice *device = modest_runtime_get_device ();
1212 SendReceivePerformerData *srp_data;
1214 srp_data = g_slice_new0 (SendReceivePerformerData);
1215 srp_data->account_id = NULL;
1216 srp_data->manual = FALSE;
1217 srp_data->mail_op = NULL;
1219 if (!tny_device_is_online (device))
1220 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, srp_data);
1222 on_send_receive_performer (FALSE, NULL, NULL, NULL, srp_data);
1228 on_send_receive_full (GArray *arguments, gpointer data, osso_rpc_t * retval)
1234 SendReceivePerformerData *srp_data;
1236 val = g_array_index (arguments, osso_rpc_t, MODEST_DBUS_SEND_RECEIVE_FULL_ARG_ACCOUNT_ID);
1237 account_id = g_strdup (val.value.s);
1238 val = g_array_index (arguments, osso_rpc_t, MODEST_DBUS_SEND_RECEIVE_FULL_ARG_MANUAL);
1239 manual = val.value.b;
1241 srp_data = g_slice_new0 (SendReceivePerformerData);
1242 srp_data->manual = manual;
1243 if (account_id && account_id[0] != '\0') {
1244 srp_data->account_id = account_id;
1246 srp_data->account_id = NULL;
1247 g_free (account_id);
1249 srp_data->mail_op = NULL;
1250 device = modest_runtime_get_device ();
1251 if (!tny_device_is_online (device))
1252 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, srp_data);
1254 on_send_receive_performer (FALSE, NULL, NULL, NULL, srp_data);
1260 on_idle_update_folder_counts (gpointer userdata)
1262 ModestMailOperation *mail_op;
1263 gchar *account_id = (gchar *) userdata;
1265 mail_op = modest_mail_operation_new (NULL);
1266 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1268 modest_mail_operation_update_folder_counts (mail_op, account_id);
1269 g_object_unref (mail_op);
1270 g_free (account_id);
1277 on_update_folder_counts (GArray *arguments, gpointer data, osso_rpc_t * retval)
1282 val = g_array_index (arguments, osso_rpc_t, MODEST_DBUS_SEND_RECEIVE_FULL_ARG_ACCOUNT_ID);
1283 account_id = g_strdup (val.value.s);
1285 if (account_id != NULL) {
1286 g_idle_add (on_idle_update_folder_counts, g_strdup (account_id));
1289 g_free (account_id);
1295 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1297 g_idle_add(on_idle_top_application, NULL);
1304 on_idle_open_account (gpointer user_data)
1307 ModestWindowMgr *mgr;
1310 gdk_threads_enter ();
1312 acc_name = (gchar *) user_data;
1313 mgr = modest_runtime_get_window_mgr ();
1315 top = modest_window_mgr_get_current_top (mgr);
1317 top = modest_window_mgr_show_initial_window (mgr);
1319 #ifdef MODEST_TOOLKIT_HILDON2
1320 GtkWidget *new_window;
1321 ModestProtocolType store_protocol;
1322 gboolean mailboxes_protocol;
1324 store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
1326 mailboxes_protocol =
1327 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
1329 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
1331 if (mailboxes_protocol) {
1332 new_window = GTK_WIDGET (modest_mailboxes_window_new (acc_name));
1334 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
1335 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window),
1339 if (modest_window_mgr_register_window (mgr, MODEST_WINDOW (new_window), NULL)) {
1340 gtk_widget_show (new_window);
1342 gtk_widget_destroy (new_window);
1346 if (MODEST_IS_MAIN_WINDOW (top)) {
1348 GtkWidget *folder_view;
1350 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (top),
1351 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1352 server_name = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
1353 acc_name, TNY_ACCOUNT_TYPE_STORE);
1355 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (folder_view),
1357 g_free (server_name);
1362 gdk_threads_leave ();
1368 on_open_account (GArray *arguments, gpointer data, osso_rpc_t *retval)
1373 /* Get the arguments: */
1374 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
1375 account_id = g_strdup (val.value.s);
1377 g_idle_add (on_idle_open_account, account_id);
1382 #ifdef MODEST_TOOLKIT_HILDON2
1384 on_idle_top_application (gpointer user_data)
1386 HildonWindowStack *stack;
1389 /* This is a GDK lock because we are an idle callback and
1390 * the code below is or does Gtk+ code */
1392 gdk_threads_enter (); /* CHECKED */
1394 stack = hildon_window_stack_get_default ();
1395 window = GTK_WIDGET (hildon_window_stack_peek (stack));
1398 gtk_window_present (GTK_WINDOW (window));
1400 ModestWindowMgr *mgr;
1402 mgr = modest_runtime_get_window_mgr ();
1403 window = (GtkWidget *) modest_window_mgr_show_initial_window (mgr);
1406 gdk_threads_leave (); /* CHECKED */
1408 return FALSE; /* Do not call this callback again. */
1412 on_idle_top_application (gpointer user_data)
1414 ModestWindow *main_win;
1415 gboolean new_window = FALSE;
1417 /* This is a GDK lock because we are an idle callback and
1418 * the code below is or does Gtk+ code */
1420 gdk_threads_enter (); /* CHECKED */
1422 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1426 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1432 /* If we're showing an already existing window then
1433 reselect the INBOX */
1435 GtkWidget *folder_view;
1436 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_win),
1437 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1438 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
1443 gtk_widget_show_all (GTK_WIDGET (main_win));
1444 gtk_window_present (GTK_WINDOW (main_win));
1447 gdk_threads_leave (); /* CHECKED */
1449 return FALSE; /* Do not call this callback again. */
1454 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1456 /* Use g_idle to context-switch into the application's thread: */
1457 g_idle_add(on_idle_top_application, NULL);
1463 on_idle_open_edit_accounts_dialog (gpointer user_data)
1466 ModestWindowMgr *mgr;
1469 /* This is a GDK lock because we are an idle callback and
1470 * the code below is or does Gtk+ code */
1472 gdk_threads_enter (); /* CHECKED */
1474 mgr = modest_runtime_get_window_mgr ();
1475 closed = modest_window_mgr_close_all_but_initial (mgr);
1476 top = modest_window_mgr_get_current_top (mgr);
1479 /* Show edit accounts dialog */
1480 modest_ui_actions_on_accounts (NULL, top);
1482 gboolean has_accounts;
1484 has_accounts = modest_account_mgr_has_accounts (modest_runtime_get_account_mgr (),
1487 /* Present the current top window */
1488 gdk_threads_leave ();
1489 on_idle_top_application (NULL);
1490 gdk_threads_enter ();
1492 /* Show edit accounts dialog if we're launching the
1493 program and there is no account (if there is at
1494 least one account then on_idle_top_application will
1495 show the accounts wizard */
1496 if (!top && has_accounts) {
1497 top = modest_window_mgr_get_current_top (mgr);
1498 modest_ui_actions_on_accounts (NULL, top);
1502 gdk_threads_leave (); /* CHECKED */
1504 return FALSE; /* Do not call this callback again. */
1508 on_open_edit_accounts_dialog (GArray * arguments, gpointer data, osso_rpc_t * retval)
1510 /* Use g_idle to context-switch into the application's thread: */
1511 g_idle_add (on_idle_open_edit_accounts_dialog, NULL);
1517 on_idle_show_memory_low (gpointer user_data)
1519 ModestWindow *main_win = NULL;
1521 gdk_threads_enter ();
1522 main_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr ());
1523 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1524 dgettext("ke-recv","memr_ib_operation_disabled"),
1526 gdk_threads_leave ();
1532 on_idle_present_modal (gpointer user_data)
1534 GtkWindow *current, *transient;
1535 gdk_threads_enter ();
1536 current = (GtkWindow *) user_data;
1537 while (GTK_IS_DIALOG (current)) {
1538 transient = gtk_window_get_transient_for (GTK_WINDOW (current));
1539 if (transient == NULL)
1542 current = transient;
1544 gtk_window_present (current);
1545 gdk_threads_leave ();
1551 /* Callback for normal D-BUS messages */
1553 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1554 GArray * arguments, gpointer data,
1555 osso_rpc_t * retval)
1559 /* Check memory low conditions */
1560 if (modest_platform_check_memory_low (NULL, FALSE)) {
1561 g_idle_add (on_idle_show_memory_low, NULL);
1565 /* Check if there is already a dialog or note open */
1566 dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
1568 g_idle_add (on_idle_present_modal, dialog);
1572 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1573 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1575 return on_mail_to (arguments, data, retval);
1576 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1577 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1579 return on_open_message (arguments, data, retval);
1580 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_ACCOUNT) == 0) {
1581 if (arguments->len != MODEST_DBUS_OPEN_ACCOUNT_ARGS_COUNT)
1583 return on_open_account (arguments, data, retval);
1584 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1585 if (arguments->len != 0)
1587 return on_send_receive (arguments, data, retval);
1588 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE_FULL) == 0) {
1589 if (arguments->len != MODEST_DBUS_SEND_RECEIVE_FULL_ARGS_COUNT)
1591 return on_send_receive_full (arguments, data, retval);
1592 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_UPDATE_FOLDER_COUNTS) == 0) {
1593 if (arguments->len != MODEST_DBUS_UPDATE_FOLDER_COUNTS_ARGS_COUNT)
1595 return on_update_folder_counts (arguments, data, retval);
1596 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1597 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1599 return on_compose_mail (arguments, data, retval);
1600 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1601 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1603 return on_delete_message (arguments,data, retval);
1604 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1605 if (arguments->len != 0)
1607 return on_open_default_inbox (arguments, data, retval);
1608 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1609 if (arguments->len != 0)
1611 return on_top_application (arguments, data, retval);
1612 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_EDIT_ACCOUNTS_DIALOG) == 0) {
1613 if (arguments->len != 0)
1615 return on_open_edit_accounts_dialog (arguments, data, retval);
1617 /* We need to return INVALID here so
1618 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1619 * so that our modest_dbus_req_filter will then be tried instead.
1621 return OSSO_INVALID;
1624 /* Notify error in D-Bus method */
1625 g_idle_add (notify_error_in_dbus_callback, NULL);
1629 /* A complex D-Bus type (like a struct),
1630 * used to return various information about a search hit.
1632 #define SEARCH_HIT_DBUS_TYPE \
1633 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1634 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1635 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1636 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1637 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1638 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1639 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1640 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1641 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1642 DBUS_STRUCT_END_CHAR_AS_STRING
1644 #define ACCOUNT_HIT_DBUS_TYPE \
1645 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1646 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1647 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1648 DBUS_STRUCT_END_CHAR_AS_STRING
1651 #define ACCOUNTS_HIT_DBUS_TYPE \
1652 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1653 DBUS_TYPE_STRING_AS_STRING \
1654 DBUS_TYPE_STRING_AS_STRING \
1655 DBUS_TYPE_STRING_AS_STRING \
1656 DBUS_TYPE_INT64_AS_STRING \
1657 DBUS_TYPE_ARRAY_AS_STRING \
1658 ACCOUNT_HIT_DBUS_TYPE \
1659 DBUS_STRUCT_END_CHAR_AS_STRING
1661 static DBusMessage *
1662 search_result_to_message (DBusMessage *reply,
1665 DBusMessageIter iter;
1666 DBusMessageIter array_iter;
1669 dbus_message_iter_init_append (reply, &iter);
1670 dbus_message_iter_open_container (&iter,
1672 SEARCH_HIT_DBUS_TYPE,
1675 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1676 DBusMessageIter struct_iter;
1677 ModestSearchResultHit *hit;
1679 const char *subject;
1683 gboolean has_attachment;
1687 hit = (ModestSearchResultHit *) hit_iter->data;
1689 msg_url = hit->msgid;
1690 subject = hit->subject;
1691 folder = hit->folder;
1692 sender = hit->sender;
1694 has_attachment = hit->has_attachment;
1695 is_unread = hit->is_unread;
1696 ts = hit->timestamp;
1698 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1700 dbus_message_iter_open_container (&array_iter,
1705 dbus_message_iter_append_basic (&struct_iter,
1709 dbus_message_iter_append_basic (&struct_iter,
1713 dbus_message_iter_append_basic (&struct_iter,
1717 dbus_message_iter_append_basic (&struct_iter,
1721 dbus_message_iter_append_basic (&struct_iter,
1725 dbus_message_iter_append_basic (&struct_iter,
1729 dbus_message_iter_append_basic (&struct_iter,
1733 dbus_message_iter_append_basic (&struct_iter,
1737 dbus_message_iter_close_container (&array_iter,
1740 g_free (hit->msgid);
1741 g_free (hit->subject);
1742 g_free (hit->folder);
1743 g_free (hit->sender);
1745 g_slice_free (ModestSearchResultHit, hit);
1748 dbus_message_iter_close_container (&iter, &array_iter);
1755 DBusConnection *con;
1756 DBusMessage *message;
1757 ModestSearch *search;
1761 search_all_cb (GList *hits, gpointer user_data)
1764 SearchHelper *helper = (SearchHelper *) user_data;
1766 reply = dbus_message_new_method_return (helper->message);
1769 dbus_uint32_t serial = 0;
1771 search_result_to_message (reply, hits);
1773 dbus_connection_send (helper->con, reply, &serial);
1774 dbus_connection_flush (helper->con);
1775 dbus_message_unref (reply);
1778 /* Free the helper */
1779 dbus_message_unref (helper->message);
1780 modest_search_free (helper->search);
1781 g_slice_free (ModestSearch, helper->search);
1782 g_slice_free (SearchHelper, helper);
1786 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1788 ModestDBusSearchFlags dbus_flags;
1792 dbus_int32_t flags_v;
1793 dbus_uint32_t size_v;
1798 ModestSearch *search;
1801 dbus_error_init (&error);
1806 res = dbus_message_get_args (message,
1808 DBUS_TYPE_STRING, &query,
1809 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1810 DBUS_TYPE_INT64, &sd_v,
1811 DBUS_TYPE_INT64, &ed_v,
1812 DBUS_TYPE_INT32, &flags_v,
1813 DBUS_TYPE_UINT32, &size_v,
1816 dbus_flags = (ModestDBusSearchFlags) flags_v;
1817 start_date = (time_t) sd_v;
1818 end_date = (time_t) ed_v;
1820 search = g_slice_new0 (ModestSearch);
1822 if (folder && g_str_has_prefix (folder, "MAND:")) {
1823 search->folder = g_strdup (folder + strlen ("MAND:"));
1824 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1825 search->folder = g_strdup (folder + strlen ("USER:"));
1826 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1827 search->folder = g_strdup (folder + strlen ("MY:"));
1829 search->folder = g_strdup (folder);
1832 /* Remember the text to search for: */
1833 #ifdef MODEST_HAVE_OGS
1834 search->query = g_strdup (query);
1837 /* Other criteria: */
1838 search->start_date = start_date;
1839 search->end_date = end_date;
1842 /* Text to serach for in various parts of the message: */
1843 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1844 search->flags |= MODEST_SEARCH_SUBJECT;
1845 search->subject = g_strdup (query);
1848 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1849 search->flags |= MODEST_SEARCH_SENDER;
1850 search->from = g_strdup (query);
1853 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1854 search->flags |= MODEST_SEARCH_RECIPIENT;
1855 search->recipient = g_strdup (query);
1858 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1859 search->flags |= MODEST_SEARCH_BODY;
1860 search->body = g_strdup (query);
1864 search->flags |= MODEST_SEARCH_AFTER;
1865 search->start_date = start_date;
1869 search->flags |= MODEST_SEARCH_BEFORE;
1870 search->end_date = end_date;
1874 search->flags |= MODEST_SEARCH_SIZE;
1875 search->minsize = size_v;
1878 #ifdef MODEST_HAVE_OGS
1879 search->flags |= MODEST_SEARCH_USE_OGS;
1880 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1883 SearchHelper *helper = g_slice_new (SearchHelper);
1884 helper->search = search;
1885 dbus_message_ref (message);
1886 helper->message = message;
1889 /* Search asynchronously */
1890 modest_search_all_accounts (search, search_all_cb, helper);
1894 headers_cmp (TnyHeader *a, TnyHeader *b)
1896 time_t date_a, date_b;
1897 date_a = tny_header_get_date_received (a);
1898 date_b = tny_header_get_date_received (b);
1900 return date_a - date_b;
1904 * The helper structure for handling GetUnreadMessages DBus request
1907 TnyList *accounts_list;
1908 TnyList *inboxes_list;
1909 gint unread_msgs_count;
1910 DBusConnection *con;
1911 DBusMessage *message;
1912 GList *account_hits_list;
1913 ModestMailOperation *mail_op;
1914 guint folder_requests_total; /**< Total get-folder requests number for multi-mailbox accounts */
1915 guint folder_requests_done; /**< Done get-folder requests number for multi-mailbox accounts */
1916 } GetUnreadMessagesHelper;
1920 gchar *account_name;
1921 gchar *store_protocol;
1927 static void return_results (GetUnreadMessagesHelper *helper)
1931 reply = dbus_message_new_method_return (helper->message);
1933 dbus_uint32_t serial = 0;
1935 DBusMessageIter iter;
1936 DBusMessageIter array_iter;
1938 dbus_message_iter_init_append (reply, &iter);
1939 dbus_message_iter_open_container (&iter,
1941 ACCOUNTS_HIT_DBUS_TYPE,
1943 for (node = helper->account_hits_list; node != NULL; node = g_list_next (node)) {
1944 AccountHits *ah = (AccountHits *) node->data;
1945 const char *account_id;
1946 const char *account_name;
1947 const char *store_protocol;
1948 gint64 unread_count;
1949 DBusMessageIter ah_struct_iter;
1950 DBusMessageIter sh_array_iter;
1953 dbus_message_iter_open_container (&array_iter,
1957 account_id = ah->account_id;
1958 account_name = ah->account_name;
1959 store_protocol = ah->store_protocol;
1960 unread_count = ah->unread_count;
1961 dbus_message_iter_append_basic (&ah_struct_iter,
1964 dbus_message_iter_append_basic (&ah_struct_iter,
1967 dbus_message_iter_append_basic (&ah_struct_iter,
1970 dbus_message_iter_append_basic (&ah_struct_iter,
1974 dbus_message_iter_open_container (&ah_struct_iter,
1976 ACCOUNT_HIT_DBUS_TYPE,
1978 for (result_node = ah->header_list; result_node != NULL; result_node = g_list_next (result_node)) {
1979 TnyHeader *header = (TnyHeader *) result_node->data;
1980 DBusMessageIter sh_struct_iter;
1981 gint64 ts = tny_header_get_date_received (header);
1982 gchar *subject = tny_header_dup_subject (header);
1984 dbus_message_iter_open_container (&sh_array_iter,
1988 dbus_message_iter_append_basic (&sh_struct_iter,
1991 dbus_message_iter_append_basic (&sh_struct_iter,
1993 (const gchar **) &subject);
1995 dbus_message_iter_close_container (&sh_array_iter,
1997 g_object_unref (header);
1999 dbus_message_iter_close_container (&ah_struct_iter,
2002 dbus_message_iter_close_container (&array_iter,
2004 g_free (ah->account_id);
2005 g_free (ah->account_name);
2006 g_free (ah->store_protocol);
2007 g_list_free (ah->header_list);
2010 dbus_message_iter_close_container (&iter,
2012 dbus_connection_send (helper->con, reply, &serial);
2013 dbus_connection_flush (helper->con);
2014 dbus_message_unref (reply);
2017 g_list_free (helper->account_hits_list);
2018 dbus_message_unref (helper->message);
2019 g_object_unref (helper->accounts_list);
2020 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
2022 g_object_unref (helper->mail_op);
2023 g_slice_free (GetUnreadMessagesHelper, helper);
2026 static void get_unread_messages_get_account (GetUnreadMessagesHelper *helper);
2027 static void get_unread_messages_get_headers (GetUnreadMessagesHelper *helper);
2030 static void get_unread_messages_get_headers_cb (TnyFolder *self,
2034 GetUnreadMessagesHelper *helper)
2036 TnyIterator *acc_iterator;
2037 TnyAccount *account;
2038 TnyIterator *headers_iterator;
2039 GList *result_list = NULL;
2040 gint members_count = 0;
2041 AccountHits *account_hits;
2042 const gchar *folder_id;
2044 ModestProtocolType store_protocol_type;
2045 ModestProtocol *store_protocol;
2047 ModestProtocolRegistry *registry;
2049 acc_iterator = tny_list_create_iterator (helper->accounts_list);
2050 account = TNY_ACCOUNT (tny_iterator_get_current (acc_iterator));
2052 headers_iterator = tny_list_create_iterator (headers);
2054 while (!tny_iterator_is_done (headers_iterator)) {
2056 TnyHeaderFlags flags;
2058 header = TNY_HEADER (tny_iterator_get_current (headers_iterator));
2059 flags = tny_header_get_flags (header);
2060 if (!(flags & TNY_HEADER_FLAG_SEEN)) {
2062 result_list = g_list_insert_sorted (result_list, g_object_ref (header), (GCompareFunc) headers_cmp);
2063 if (members_count == helper->unread_msgs_count) {
2064 g_object_unref (result_list->data);
2065 result_list = g_list_delete_link (result_list, result_list);
2071 g_object_unref (header);
2072 tny_iterator_next (headers_iterator);
2074 g_object_unref (headers_iterator);
2076 registry = modest_runtime_get_protocol_registry ();
2077 store_protocol_type = modest_tny_account_get_protocol_type (account);
2079 /* Get the number of unread messages for plug-in based accounts */
2080 if (modest_protocol_registry_protocol_type_is_provider (registry, store_protocol_type)) {
2081 unread_count = tny_folder_get_unread_count (self);
2084 account_hits = g_slice_new (AccountHits);
2085 account_hits->account_id = g_strdup (modest_tny_account_get_parent_modest_account_name_for_server_account (account));
2086 account_hits->account_name = g_strdup (tny_account_get_name (account));
2087 store_protocol = modest_protocol_registry_get_protocol_by_type (registry, store_protocol_type);
2088 account_hits->store_protocol = g_strdup (modest_protocol_get_name (store_protocol));
2089 account_hits->header_list = result_list;
2090 account_hits->mailbox_id = NULL;
2091 account_hits->unread_count = unread_count;
2093 folder_id = tny_folder_get_id (self);
2094 bar = g_strstr_len (folder_id, -1, "/");
2097 prefix = g_strndup (folder_id, bar - folder_id);
2098 if (g_strstr_len (prefix, -1, "@")) {
2099 account_hits->mailbox_id = g_strdup (prefix);
2104 helper->account_hits_list = g_list_prepend (helper->account_hits_list, account_hits);
2106 get_unread_messages_get_headers (helper);
2111 get_unread_messages_get_headers (GetUnreadMessagesHelper *helper)
2113 TnyIterator *iterator;
2115 iterator = tny_list_create_iterator (helper->inboxes_list);
2116 if (tny_iterator_is_done (iterator)) {
2117 TnyIterator *accounts_iter;
2118 TnyAccount *account;
2119 g_object_unref (helper->inboxes_list);
2120 helper->inboxes_list = NULL;
2122 accounts_iter = tny_list_create_iterator (helper->accounts_list);
2123 account = TNY_ACCOUNT (tny_iterator_get_current (accounts_iter));
2124 g_object_unref (accounts_iter);
2126 tny_list_remove (helper->accounts_list, (GObject *) account);
2127 g_object_unref (account);
2128 get_unread_messages_get_account (helper);
2132 folder = TNY_FOLDER (tny_iterator_get_current (iterator));
2134 TnyList *headers_list;
2136 headers_list = TNY_LIST (tny_simple_list_new ());
2137 tny_folder_get_headers_async (folder, headers_list, FALSE, (TnyGetHeadersCallback) get_unread_messages_get_headers_cb, NULL, helper);
2138 g_object_unref (headers_list);
2139 tny_list_remove (helper->inboxes_list, G_OBJECT (folder));
2140 g_object_unref (folder);
2143 g_object_unref (iterator);
2146 static void get_account_folders_cb (TnyFolderStore *self, gboolean cancelled, TnyList *list, GError *err, gpointer user_data)
2148 GetUnreadMessagesHelper *helper = (GetUnreadMessagesHelper *) user_data;
2149 TnyIterator *iterator;
2151 iterator = tny_list_create_iterator (list);
2152 while (!tny_iterator_is_done (iterator)) {
2155 folder = TNY_FOLDER (tny_iterator_get_current (iterator));
2156 if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX) {
2157 tny_list_prepend (helper->inboxes_list, G_OBJECT (folder));
2158 g_object_unref (folder);
2161 g_object_unref (folder);
2162 tny_iterator_next (iterator);
2164 g_object_unref (iterator);
2166 get_unread_messages_get_headers (helper);
2169 static void get_multi_mailbox_account_folders_cb (TnyFolderStore *self, gboolean cancelled, TnyList *list, GError *err, gpointer user_data)
2171 GetUnreadMessagesHelper *helper = (GetUnreadMessagesHelper *) user_data;
2172 TnyIterator *iterator;
2174 /* another request has been finished */
2175 helper->folder_requests_done += 1;
2177 /* analize the folders we got */
2178 iterator = tny_list_create_iterator (list);
2179 while (!tny_iterator_is_done (iterator)) {
2182 folder = TNY_FOLDER (tny_iterator_get_current (iterator));
2183 if (tny_folder_get_folder_type (folder) == TNY_FOLDER_TYPE_INBOX) {
2184 tny_list_prepend (helper->inboxes_list, G_OBJECT (folder));
2187 /* Try to check if we have inbox sub-folders in this folder */
2188 if (TNY_IS_FOLDER_STORE (folder)) {
2189 TnyFolderStore *folder_store;
2191 folder_store = TNY_FOLDER_STORE (folder);
2193 TnyList *folders_list;
2195 helper->folder_requests_total += 1;
2196 folders_list = tny_simple_list_new ();
2197 tny_folder_store_get_folders_async (folder_store, folders_list,
2198 NULL, FALSE, get_multi_mailbox_account_folders_cb, NULL, helper);
2199 g_object_unref (folders_list);
2202 g_object_unref (folder);
2203 tny_iterator_next (iterator);
2205 g_object_unref (iterator);
2207 /* Check if we have handled all the inbox folders for multi-mailbox accounts */
2208 if (helper->folder_requests_done == helper->folder_requests_total) {
2209 TnyIterator *inboxes_it;
2210 TnyIterator *accounts_it;
2211 TnyAccount *account;
2212 guint unread_messages;
2213 AccountHits* account_hits;
2214 ModestProtocol *store_protocol;
2215 ModestProtocolType store_protocol_type;
2217 /* Store the number of unread messages for the handled account */
2218 unread_messages = 0;
2219 accounts_it = tny_list_create_iterator (helper->accounts_list);
2220 account = TNY_ACCOUNT (tny_iterator_get_current (accounts_it));
2221 g_object_unref (accounts_it);
2222 inboxes_it = tny_list_create_iterator (helper->inboxes_list);
2223 while (!tny_iterator_is_done (inboxes_it)) {
2226 folder = TNY_FOLDER (tny_iterator_get_current (inboxes_it));
2228 unread_messages += tny_folder_get_unread_count (folder);
2230 tny_iterator_next (inboxes_it);
2232 g_object_unref (inboxes_it);
2233 g_object_unref (helper->inboxes_list);
2234 helper->inboxes_list = NULL;
2235 account_hits = g_slice_new (AccountHits);
2236 account_hits->account_id = g_strdup (
2237 modest_tny_account_get_parent_modest_account_name_for_server_account (account));
2238 account_hits->account_name = g_strdup (tny_account_get_name (account));
2239 store_protocol_type = modest_tny_account_get_protocol_type (account);
2240 store_protocol = modest_protocol_registry_get_protocol_by_type (
2241 modest_runtime_get_protocol_registry (), store_protocol_type);
2242 account_hits->store_protocol = g_strdup (modest_protocol_get_name (store_protocol));
2243 account_hits->mailbox_id = NULL;
2244 account_hits->header_list = NULL;
2245 account_hits->unread_count = unread_messages;
2246 helper->account_hits_list = g_list_prepend (helper->account_hits_list, account_hits);
2248 /* remove the handled account from the accounts_list */
2249 tny_list_remove (helper->accounts_list, G_OBJECT (account));
2250 g_object_unref (account);
2252 get_unread_messages_get_account (helper);
2257 get_unread_messages_get_account (GetUnreadMessagesHelper *helper)
2259 TnyIterator *iterator;
2261 /* Search through all accounts */
2262 iterator = tny_list_create_iterator (helper->accounts_list);
2263 if (tny_iterator_is_done (iterator)) {
2264 /* all results, then finish */
2265 return_results (helper);
2267 TnyAccount *account;
2268 TnyList *folders_list;
2269 ModestProtocolType protocol_type;
2271 account = TNY_ACCOUNT (tny_iterator_get_current (iterator));
2272 protocol_type = modest_tny_account_get_protocol_type (account);
2274 folders_list = tny_simple_list_new ();
2275 helper->inboxes_list = tny_simple_list_new ();
2276 if (MODEST_PROTOCOL_REGISTRY_TYPE_INVALID != protocol_type &&
2277 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
2278 protocol_type, MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS)) {
2279 /* For multi-mailbox protocols we only can get the number of unread messages,
2280 we will not even try to get their email headers */
2281 helper->folder_requests_done = 0;
2282 helper->folder_requests_total = 1;
2283 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account),
2284 folders_list, NULL, FALSE, get_multi_mailbox_account_folders_cb, NULL, helper);
2287 /* For non-multi-mailbox protocols we will get their email headers */
2288 tny_folder_store_get_folders_async (TNY_FOLDER_STORE (account),
2289 folders_list, NULL, FALSE, get_account_folders_cb, NULL, helper);
2291 g_object_unref (folders_list);
2292 g_object_unref (account);
2295 g_object_unref (iterator);
2299 on_idle_get_unread_messages (GetUnreadMessagesHelper *helper)
2301 ModestTnyAccountStore *astore;
2302 astore = modest_runtime_get_account_store ();
2304 tny_account_store_get_accounts (TNY_ACCOUNT_STORE (astore),
2305 helper->accounts_list,
2306 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
2308 get_unread_messages_get_account (helper);
2314 on_dbus_method_get_unread_messages (DBusConnection *con, DBusMessage *message)
2317 dbus_int64_t unread_msgs_count_v;
2319 GetUnreadMessagesHelper *helper;
2321 dbus_error_init (&error);
2323 res = dbus_message_get_args (message,
2325 DBUS_TYPE_INT32, &unread_msgs_count_v,
2328 helper = g_slice_new (GetUnreadMessagesHelper);
2329 helper->unread_msgs_count = unread_msgs_count_v;
2330 dbus_message_ref (message);
2331 helper->message = message;
2333 helper->account_hits_list = NULL;
2334 helper->inboxes_list = NULL;
2335 helper->accounts_list = TNY_LIST (tny_simple_list_new ());
2336 helper->mail_op = modest_mail_operation_new (NULL);
2337 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
2340 g_idle_add ((GSourceFunc) on_idle_get_unread_messages, helper);
2344 /* A complex D-Bus type (like a struct),
2345 * used to return various information about a folder.
2347 #define GET_FOLDERS_RESULT_DBUS_TYPE \
2348 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
2349 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
2350 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
2351 DBUS_STRUCT_END_CHAR_AS_STRING
2353 static DBusMessage *
2354 get_folders_result_to_message (DBusMessage *reply,
2357 DBusMessageIter iter;
2358 dbus_message_iter_init_append (reply, &iter);
2360 DBusMessageIter array_iter;
2361 dbus_message_iter_open_container (&iter,
2363 GET_FOLDERS_RESULT_DBUS_TYPE,
2366 GList *list_iter = folder_ids;
2367 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
2369 const gchar *folder_name = (const gchar*)list_iter->data;
2371 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
2373 DBusMessageIter struct_iter;
2374 dbus_message_iter_open_container (&array_iter,
2380 dbus_message_iter_append_basic (&struct_iter,
2382 &folder_name); /* The string will be copied. */
2384 /* URI: This is maybe not needed by osso-global-search: */
2385 const gchar *folder_uri = "TODO:unimplemented";
2386 dbus_message_iter_append_basic (&struct_iter,
2388 &folder_uri); /* The string will be copied. */
2390 dbus_message_iter_close_container (&array_iter,
2395 dbus_message_iter_close_container (&iter, &array_iter);
2401 add_single_folder_to_list (TnyFolder *folder, GList** list)
2406 if (TNY_IS_MERGE_FOLDER (folder)) {
2407 const gchar * folder_name;
2408 /* Ignore these because their IDs ares
2409 * a) not always unique or sensible.
2410 * b) not human-readable, and currently need a human-readable
2411 * ID here, because the osso-email-interface API does not allow
2412 * us to return both an ID and a display name.
2414 * This is actually the merged outbox folder.
2415 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
2416 * but that seems unwise. murrayc.
2418 folder_name = tny_folder_get_name (folder);
2419 if (folder_name && !strcmp (folder_name, "Outbox")) {
2420 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
2425 /* Add this folder to the list: */
2427 const gchar * folder_name = tny_folder_get_name (folder);
2429 *list = g_list_append(*list, g_strdup (folder_name));
2432 /* osso-global-search only uses one string,
2433 * so ID is the only thing that could possibly identify a folder.
2434 * TODO: osso-global search should probably be changed to
2435 * take an ID and a Name.
2437 const gchar * id = tny_folder_get_id (folder);
2438 if (id && strlen(id)) {
2439 const gchar *prefix = NULL;
2440 TnyFolderType folder_type;
2442 /* dbus global search api expects a prefix identifying the type of
2443 * folder here. Mandatory folders should have MAND: prefix, and
2444 * other user created folders should have USER: prefix
2446 folder_type = modest_tny_folder_guess_folder_type (folder);
2447 switch (folder_type) {
2448 case TNY_FOLDER_TYPE_INBOX:
2451 case TNY_FOLDER_TYPE_OUTBOX:
2452 case TNY_FOLDER_TYPE_DRAFTS:
2453 case TNY_FOLDER_TYPE_SENT:
2454 case TNY_FOLDER_TYPE_ARCHIVE:
2457 case TNY_FOLDER_TYPE_INVALID:
2458 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
2459 return; /* don't add it */
2466 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
2471 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
2476 /* Add this folder to the list: */
2477 if (TNY_IS_FOLDER (folder_store)) {
2478 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
2481 /* Recurse into child folders: */
2483 /* Get the folders list: */
2485 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
2486 tny_folder_store_query_add_item (query, NULL,
2487 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
2489 TnyList *all_folders = tny_simple_list_new ();
2490 tny_folder_store_get_folders (folder_store,
2496 TnyIterator *iter = tny_list_create_iterator (all_folders);
2497 while (!tny_iterator_is_done (iter)) {
2499 /* Do not recurse, because the osso-global-search UI specification
2500 * does not seem to want the sub-folders, though that spec seems to
2501 * be generally unsuitable for Modest.
2503 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
2505 add_single_folder_to_list (TNY_FOLDER (folder), list);
2508 if (TNY_IS_FOLDER_STORE (folder))
2509 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
2511 add_single_folder_to_list (TNY_FOLDER (folder), list);
2515 /* tny_iterator_get_current() gave us a reference. */
2516 g_object_unref (folder);
2519 tny_iterator_next (iter);
2521 g_object_unref (G_OBJECT (iter));
2525 /* return >1 for a special folder, 0 for a user-folder */
2527 get_rank (const gchar *folder)
2529 if (strcmp (folder, "INBOX") == 0)
2531 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
2533 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
2535 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
2541 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
2543 gint r1 = get_rank (folder1);
2544 gint r2 = get_rank (folder2);
2546 if (r1 > 0 && r2 > 0)
2548 if (r1 > 0 && r2 == 0)
2550 if (r1 == 0 && r2 > 0)
2553 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
2557 /* - we're still missing the outbox */
2558 /* - we need to take care of localization (urgh) */
2559 /* - what about 'All mail folders'? */
2561 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
2563 DBusMessage *reply = NULL;
2564 ModestAccountMgr *account_mgr = NULL;
2565 gchar *account_name = NULL;
2566 GList *folder_names = NULL;
2567 TnyAccount *account_local = NULL;
2568 TnyAccount *account_mmc = NULL;
2570 /* Get the TnyStoreAccount so we can get the folders: */
2571 account_mgr = modest_runtime_get_account_mgr();
2572 account_name = modest_account_mgr_get_default_account (account_mgr);
2573 if (!account_name) {
2574 g_printerr ("modest: no account found\n");
2578 TnyAccount *account = NULL;
2580 account = modest_tny_account_store_get_server_account (
2581 modest_runtime_get_account_store(), account_name,
2582 TNY_ACCOUNT_TYPE_STORE);
2586 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
2589 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
2590 g_free (account_name);
2591 account_name = NULL;
2593 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
2595 g_object_unref (account);
2599 /* Also add the folders from the local folders account,
2600 * because they are (currently) used with all accounts:
2601 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
2604 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
2605 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
2607 g_object_unref (account_local);
2608 account_local = NULL;
2610 /* Obtain the mmc account */
2612 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
2614 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
2615 g_object_unref (account_mmc);
2619 /* specs require us to sort the folder names, although
2620 * this is really not the place to do that...
2622 folder_names = g_list_sort (folder_names,
2623 (GCompareFunc)folder_name_compare_func);
2625 /* Put the result in a DBus reply: */
2626 reply = dbus_message_new_method_return (message);
2628 get_folders_result_to_message (reply, folder_names);
2630 if (reply == NULL) {
2631 g_warning ("%s: Could not create reply.", __FUNCTION__);
2635 dbus_uint32_t serial = 0;
2636 dbus_connection_send (con, reply, &serial);
2637 dbus_connection_flush (con);
2638 dbus_message_unref (reply);
2641 g_list_foreach (folder_names, (GFunc)g_free, NULL);
2642 g_list_free (folder_names);
2647 reply_empty_results (DBusConnection *con, DBusMessage *msg)
2649 DBusMessage *reply = dbus_message_new_method_return (msg);
2651 dbus_uint32_t serial = 0;
2652 /* we simply return an empty list, otherwise
2653 global-search gets confused */
2654 search_result_to_message (reply, NULL);
2656 dbus_connection_send (con, reply, &serial);
2657 dbus_connection_flush (con);
2658 dbus_message_unref (reply);
2660 g_warning ("%s: failed to send reply",
2665 /** This D-Bus handler is used when the main osso-rpc
2666 * D-Bus handler has not handled something.
2667 * We use this for D-Bus methods that need to use more complex types
2668 * than osso-rpc supports.
2671 modest_dbus_req_filter (DBusConnection *con,
2672 DBusMessage *message,
2675 gboolean handled = FALSE;
2677 if (dbus_message_is_method_call (message,
2679 MODEST_DBUS_METHOD_SEARCH)) {
2681 /* don't try to search when there not enough mem */
2682 if (modest_platform_check_memory_low (NULL, TRUE)) {
2683 g_warning ("%s: not enough memory for searching",
2685 reply_empty_results (con, message);
2689 on_dbus_method_search (con, message);
2693 } else if (dbus_message_is_method_call (message,
2695 MODEST_DBUS_METHOD_GET_UNREAD_MESSAGES)) {
2697 /* don't try to get unread messages when there not enough mem */
2698 if (modest_platform_check_memory_low (NULL, TRUE)) {
2699 g_warning ("%s: not enough memory for searching",
2701 reply_empty_results (con, message);
2705 on_dbus_method_get_unread_messages (con, message);
2709 } else if (dbus_message_is_method_call (message,
2711 MODEST_DBUS_METHOD_GET_FOLDERS)) {
2712 on_dbus_method_get_folders (con, message);
2714 } else if (dbus_message_is_method_call (message,
2716 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
2717 on_dbus_method_dump_operation_queue (con, message);
2719 } else if (dbus_message_is_method_call (message,
2721 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
2722 on_dbus_method_dump_accounts (con, message);
2724 } else if (dbus_message_is_method_call (message,
2726 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
2727 on_dbus_method_dump_send_queues (con, message);
2730 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
2732 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
2733 __FUNCTION__, dbus_message_get_interface (message),
2734 dbus_message_get_member(message));
2739 DBUS_HANDLER_RESULT_HANDLED :
2740 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
2744 notify_error_in_dbus_callback (gpointer user_data)
2746 ModestMailOperation *mail_op;
2747 ModestMailOperationQueue *mail_op_queue;
2749 mail_op = modest_mail_operation_new (NULL);
2750 mail_op_queue = modest_runtime_get_mail_operation_queue ();
2752 /* Issues a noop operation in order to force the queue to emit
2753 the "queue-empty" signal to allow modest to quit */
2754 modest_mail_operation_queue_add (mail_op_queue, mail_op);
2755 modest_mail_operation_noop (mail_op);
2756 g_object_unref (mail_op);
2762 modest_dbus_emit_account_created_signal (DBusConnection *con,
2763 const gchar *account_id)
2765 DBusMessage *signal;
2768 signal = dbus_message_new_signal (MODEST_DBUS_OBJECT, MODEST_DBUS_IFACE,
2769 MODEST_DBUS_SIGNAL_ACCOUNT_CREATED);
2774 if (!dbus_message_append_args (signal,
2775 DBUS_TYPE_STRING, &account_id,
2776 DBUS_TYPE_INVALID)) {
2777 dbus_message_unref (signal);
2781 result = dbus_connection_send (con, signal, NULL);
2783 dbus_message_unref (signal);
2785 return result?TRUE:FALSE;
2789 modest_dbus_emit_account_removed_signal (DBusConnection *con,
2790 const gchar *account_id)
2792 DBusMessage *signal;
2795 signal = dbus_message_new_signal (MODEST_DBUS_OBJECT, MODEST_DBUS_IFACE,
2796 MODEST_DBUS_SIGNAL_ACCOUNT_REMOVED);
2801 if (!dbus_message_append_args (signal,
2802 DBUS_TYPE_STRING, &account_id,
2803 DBUS_TYPE_INVALID)) {
2804 dbus_message_unref (signal);
2808 result = dbus_connection_send (con, signal, NULL);
2810 dbus_message_unref (signal);
2812 return result?TRUE:FALSE;
2816 modest_dbus_emit_folder_updated_signal (DBusConnection *con,
2817 const gchar *account_id,
2818 const gchar *folder_path)
2820 DBusMessage *signal;
2823 signal = dbus_message_new_signal (MODEST_DBUS_OBJECT, MODEST_DBUS_IFACE,
2824 MODEST_DBUS_SIGNAL_FOLDER_UPDATED);
2829 if (!dbus_message_append_args (signal,
2830 DBUS_TYPE_STRING, &account_id,
2831 DBUS_TYPE_STRING, &folder_path,
2832 DBUS_TYPE_INVALID)) {
2833 dbus_message_unref (signal);
2837 result = dbus_connection_send (con, signal, NULL);
2839 dbus_message_unref (signal);
2841 return result?TRUE:FALSE;
2845 modest_dbus_emit_msg_read_changed_signal (DBusConnection *con,
2849 DBusMessage *signal;
2852 signal = dbus_message_new_signal (MODEST_DBUS_OBJECT, MODEST_DBUS_IFACE,
2853 MODEST_DBUS_SIGNAL_MSG_READ_CHANGED);
2858 if (!dbus_message_append_args (signal,
2859 DBUS_TYPE_STRING, &uid,
2860 DBUS_TYPE_BOOLEAN, &is_read,
2861 DBUS_TYPE_INVALID)) {
2862 dbus_message_unref (signal);
2866 result = dbus_connection_send (con, signal, NULL);
2868 dbus_message_unref (signal);
2870 return result?TRUE:FALSE;