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>
67 #include <tny-iterator.h>
68 #include <tny-simple-list.h>
69 #include <tny-merge-folder.h>
70 #include <tny-account.h>
72 #include <modest-text-utils.h>
82 } ComposeMailIdleData;
85 static gboolean notify_error_in_dbus_callback (gpointer user_data);
86 static gboolean on_idle_compose_mail(gpointer user_data);
87 static gboolean on_idle_top_application (gpointer user_data);
90 * @uri An escaped URI. URIs should always be escaped.
91 * @len The length of the @uri string, or -1 if the string is null terminated.
93 * Decode a URI, or URI fragment, as per RFC 1738.
94 * http://www.ietf.org/rfc/rfc1738.txt
96 * Return value: An unescaped string. This should be freed with g_free().
99 uri_unescape(const gchar* uri, size_t len)
107 /* Allocate an extra string so we can be sure that it is null-terminated,
108 * so we can use gnome_vfs_unescape_string().
109 * This is not efficient. */
110 gchar * escaped_nullterminated = g_strndup (uri, len);
111 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
112 g_free (escaped_nullterminated);
117 /** uri_parse_mailto:
118 * @mailto A mailto URI, with the mailto: prefix.
119 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
120 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
121 * all the string items have been freed. This parameter may be NULL.
122 * Parse a mailto URI as per RFC2368.
123 * http://www.ietf.org/rfc/rfc2368.txt
125 * Return value: The to address, unescaped. This should be freed with g_free().
128 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
130 /* The URL must begin with mailto: */
131 if (strncmp (mailto, "mailto:", 7) != 0) {
134 const gchar* start_to = mailto + 7;
136 /* Look for ?, or the end of the string, marking the end of the to address: */
137 const size_t len_to = strcspn (start_to, "?");
138 gchar* result_to = uri_unescape (start_to, len_to);
139 printf("debug: result_to=%s\n", result_to);
141 if (list_items_and_values == NULL) {
145 /* Get any other items: */
146 const size_t len_mailto = strlen (start_to);
147 const gchar* p = start_to + len_to + 1; /* parsed so far. */
148 const gchar* end = start_to + len_mailto;
150 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
152 name_end = strchr (name_start, '='); /* Separator between name and value */
153 if (name_end == NULL) {
154 g_debug ("Malformed URI: %s\n", mailto);
157 value_start = name_end + 1;
158 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
160 name = g_strndup(name_start, name_end - name_start);
161 if (value_end != NULL) {
162 value = uri_unescape(value_start, value_end - value_start);
165 value = uri_unescape(value_start, -1);
168 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
169 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
176 check_and_offer_account_creation()
178 gboolean result = TRUE;
180 /* This is called from idle handlers, so lock gdk: */
181 gdk_threads_enter ();
183 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
184 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
186 g_debug ("modest: %s: no account exists even after offering, "
187 "or account setup was already underway.\n", __FUNCTION__);
192 gdk_threads_leave ();
198 on_idle_mail_to(gpointer user_data)
200 gchar *uri = (gchar*)user_data;
201 GSList *list_names_and_values = NULL;
203 const gchar *cc = NULL;
204 const gchar *bcc = NULL;
205 const gchar *subject = NULL;
206 const gchar *body = NULL;
208 if (!check_and_offer_account_creation ()) {
209 g_idle_add (notify_error_in_dbus_callback, NULL);
213 /* Get the relevant items from the list: */
214 to = uri_parse_mailto (uri, &list_names_and_values);
215 GSList *list = list_names_and_values;
217 GSList *list_value = g_slist_next (list);
218 const gchar * name = (const gchar*)list->data;
219 const gchar * value = (const gchar*)list_value->data;
221 if (strcmp (name, "cc") == 0) {
223 } else if (strcmp (name, "bcc") == 0) {
225 } else if (strcmp (name, "subject") == 0) {
227 } else if (strcmp (name, "body") == 0) {
231 list = g_slist_next (list_value);
234 gdk_threads_enter (); /* CHECKED */
235 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
236 gdk_threads_leave (); /* CHECKED */
239 /* Free the to: and the list, as required by uri_parse_mailto() */
241 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
242 g_slist_free (list_names_and_values);
246 return FALSE; /* Do not call this callback again. */
250 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
256 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
257 uri = g_strdup (val.value.s);
259 g_idle_add(on_idle_mail_to, (gpointer)uri);
261 /* Note that we cannot report failures during sending,
262 * because that would be asynchronous. */
268 on_idle_compose_mail(gpointer user_data)
270 GSList *attachments = NULL;
271 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
273 if (!check_and_offer_account_creation ()) {
274 g_idle_add (notify_error_in_dbus_callback, NULL);
278 /* it seems Sketch at least sends a leading ',' -- take that into account,
280 if (idle_data->attachments && idle_data->attachments[0]==',') {
281 gchar *tmp = g_strdup (idle_data->attachments + 1);
282 g_free(idle_data->attachments);
283 idle_data->attachments = tmp;
286 if (idle_data->attachments != NULL) {
287 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
289 for (i=0; list[i] != NULL; i++) {
290 attachments = g_slist_append(attachments, g_uri_unescape_string (list[i], NULL));
295 /* If the message has nothing then mark the buffers as not
296 modified. This happens in Maemo for example when opening a
297 new message from Contacts plugin, it sends "" instead of
299 gdk_threads_enter (); /* CHECKED */
300 if (!strncmp (idle_data->to, "", 1) &&
301 !strncmp (idle_data->to, "", 1) &&
302 !strncmp (idle_data->cc, "", 1) &&
303 !strncmp (idle_data->bcc, "", 1) &&
304 !strncmp (idle_data->subject, "", 1) &&
305 !strncmp (idle_data->body, "", 1) &&
306 attachments == NULL) {
307 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
309 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
310 idle_data->bcc, idle_data->subject,
311 idle_data->body, attachments, TRUE);
313 gdk_threads_leave (); /* CHECKED */
315 g_slist_foreach(attachments, (GFunc)g_free, NULL);
316 g_slist_free(attachments);
317 g_free (idle_data->to);
318 g_free (idle_data->cc);
319 g_free (idle_data->bcc);
320 g_free (idle_data->subject);
321 g_free (idle_data->body);
322 g_free (idle_data->attachments);
325 return FALSE; /* Do not call this callback again. */
329 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
331 ComposeMailIdleData *idle_data;
334 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
336 /* Get the arguments: */
337 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
338 idle_data->to = g_strdup (val.value.s);
340 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
341 idle_data->cc = g_strdup (val.value.s);
343 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
344 idle_data->bcc = g_strdup (val.value.s);
346 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
347 idle_data->subject = g_strdup (val.value.s);
349 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
350 idle_data->body = g_strdup (val.value.s);
352 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
353 idle_data->attachments = g_strdup (val.value.s);
355 /* Use g_idle to context-switch into the application's thread: */
356 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
362 find_message_by_url (const char *uri, TnyAccount **ac_out)
364 ModestTnyAccountStore *astore;
365 TnyAccount *account = NULL;
366 TnyFolder *folder = NULL;
369 astore = modest_runtime_get_account_store ();
374 if (uri && g_str_has_prefix (uri, "merge://")) {
375 /* we assume we're talking about outbox folder, as this
376 * is the only merge folder we work with in modest */
377 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
379 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
382 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
386 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
391 msg = tny_folder_find_msg (folder, uri, NULL);
394 if (account && !msg) {
395 g_object_unref (account);
399 g_object_unref (folder);
408 guint animation_timeout;
409 GtkWidget *animation;
410 } OpenMsgPerformerInfo;
412 #ifndef MODEST_TOOLKIT_HILDON2
414 on_show_opening_animation (gpointer userdata)
416 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
417 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
418 info->animation_timeout = 0;
425 on_find_msg_async_destroy (gpointer userdata)
427 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
429 if (info->animation_timeout>0) {
430 g_source_remove (info->animation_timeout);
431 info->animation_timeout = 0;
434 if (info->animation) {
435 gtk_widget_destroy (info->animation);
436 info->animation = NULL;
443 g_object_unref (info->account);
445 g_slice_free (OpenMsgPerformerInfo, info);
450 find_msg_async_cb (TnyFolder *folder,
456 TnyHeader *header = NULL;
457 gchar *msg_uid = NULL;
458 ModestWindowMgr *win_mgr;
459 ModestWindow *msg_view = NULL;
460 gboolean is_draft = FALSE;
461 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
463 if (err || cancelled) {
464 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
465 g_idle_add (notify_error_in_dbus_callback, NULL);
469 header = tny_msg_get_header (msg);
470 if (!header || (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
471 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
472 g_idle_add (notify_error_in_dbus_callback, NULL);
476 msg_uid = modest_tny_folder_get_header_unique_id (header);
477 win_mgr = modest_runtime_get_window_mgr ();
479 if (modest_tny_folder_is_local_folder (folder) &&
480 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
484 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
485 gtk_window_present (GTK_WINDOW(msg_view));
487 const gchar *modest_account_name;
490 modest_window_mgr_register_header (win_mgr, header, NULL);
492 account = tny_folder_get_account (folder);
494 modest_account_name =
495 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
497 modest_account_name = NULL;
500 /* Drafts will be opened in the editor, and others will be opened in the viewer */
502 gchar *modest_account_name = NULL;
503 gchar *mailbox = NULL;
506 /* we cannot edit without a valid account... */
507 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
508 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
509 modest_window_mgr_unregister_header (win_mgr,
515 from_header = tny_header_dup_from (header);
516 modest_account_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
517 g_free (from_header);
519 if (modest_account_name == NULL) {
520 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
521 modest_account_name = modest_account_mgr_get_default_account (mgr);
523 msg_view = modest_msg_edit_window_new (msg, modest_account_name, mailbox, TRUE);
526 g_free (modest_account_name);
529 const gchar *modest_account_name;
532 modest_account_name =
533 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
535 modest_account_name = NULL;
538 header = tny_msg_get_header (msg);
539 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name, NULL, msg_uid);
540 if (! (tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
541 ModestMailOperation *mail_op;
543 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
544 /* Sync folder, we need this to save the seen flag */
545 mail_op = modest_mail_operation_new (NULL);
546 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
548 modest_mail_operation_sync_folder (mail_op, folder, FALSE);
549 g_object_unref (mail_op);
551 g_object_unref (header);
554 if (msg_view != NULL) {
555 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
556 gtk_widget_destroy (GTK_WIDGET (msg_view));
558 gtk_widget_show_all (GTK_WIDGET (msg_view));
565 g_object_unref (header);
568 on_find_msg_async_destroy (info);
573 on_open_message_performer (gboolean canceled,
575 GtkWindow *parent_window,
579 OpenMsgPerformerInfo *info;
580 TnyFolder *folder = NULL;
581 ModestTnyAccountStore *account_store;
582 ModestTnyLocalFoldersAccount *local_folders_account;
584 account_store = modest_runtime_get_account_store ();
585 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
586 modest_tny_account_store_get_local_folders_account (account_store));
588 info = (OpenMsgPerformerInfo *) user_data;
589 if (canceled || err) {
591 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
592 g_idle_add (notify_error_in_dbus_callback, NULL);
593 on_find_msg_async_destroy (info);
599 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
600 g_object_unref (local_folders_account);
601 } else if (account == TNY_ACCOUNT (local_folders_account)) {
602 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
606 if (!(folder && modest_tny_folder_is_local_folder (folder) &&
607 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS))) {
608 ModestWindowMgr *win_mgr;
609 ModestWindow *msg_view;
611 win_mgr = modest_runtime_get_window_mgr ();
612 if (modest_window_mgr_find_registered_message_uid (win_mgr, info->uri, &msg_view)) {
613 gtk_window_present (GTK_WINDOW(msg_view));
615 const gchar *modest_account_name;
617 modest_account_name =
618 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
620 modest_account_name = NULL;
623 msg_view = modest_msg_view_window_new_from_uid (modest_account_name, NULL, info->uri);
624 if (msg_view != NULL) {
625 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
626 gtk_widget_destroy (GTK_WIDGET (msg_view));
628 gtk_widget_show_all (GTK_WIDGET (msg_view));
632 g_object_unref (account);
635 on_find_msg_async_destroy (info);
639 #ifndef MODEST_TOOLKIT_HILDON2
640 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
643 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
644 g_object_unref (folder);
648 on_idle_open_message_performer (gpointer user_data)
650 ModestWindow *top_win = NULL;
651 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
653 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
655 /* Lock before the call as we're in an idle handler */
656 gdk_threads_enter ();
658 modest_platform_connect_and_perform (GTK_WINDOW (top_win), TRUE,
660 on_open_message_performer, info);
662 on_open_message_performer (FALSE, NULL, GTK_WINDOW (top_win),
663 info->account, info);
665 gdk_threads_leave ();
671 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
675 TnyAccount *account = NULL;
679 /* Get the arguments: */
680 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
681 uri = g_strdup (val.value.s);
683 is_merge = g_str_has_prefix (uri, "merge:");
685 /* Get the account */
687 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
691 if (is_merge || account) {
692 OpenMsgPerformerInfo *info;
693 TnyFolder *folder = NULL;
694 ModestTnyAccountStore *account_store;
695 ModestTnyLocalFoldersAccount *local_folders_account;
697 info = g_slice_new0 (OpenMsgPerformerInfo);
699 info->account = g_object_ref (account);
701 info->connect = TRUE;
702 info->animation = NULL;
703 info->animation_timeout = 0;
705 account_store = modest_runtime_get_account_store ();
706 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT
707 (modest_tny_account_store_get_local_folders_account (account_store));
709 /* Try to get the message, if it's already downloaded
710 we don't need to connect */
713 gboolean device_online;
715 device = modest_runtime_get_device ();
716 device_online = tny_device_is_online (device);
717 if (!device_online || TNY_ACCOUNT (local_folders_account) == account) {
718 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
723 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
724 g_object_unref (local_folders_account);
725 info->connect = FALSE;
728 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
730 info->connect = FALSE;
731 g_object_unref (msg);
733 info->connect = TRUE;
735 g_object_unref (folder);
738 /* We need to call it into an idle to get
739 modest_platform_connect_and_perform into the main
741 g_idle_add (on_idle_open_message_performer, info);
742 osso_retval = OSSO_OK;
745 osso_retval = OSSO_ERROR;
746 g_idle_add (notify_error_in_dbus_callback, NULL);
750 g_object_unref (account);
755 on_remove_msgs_finished (ModestMailOperation *mail_op,
759 ModestWindow *top_win = NULL, *msg_view = NULL;
761 header = (TnyHeader *) user_data;
763 /* Get the main window if exists */
764 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
766 g_object_unref (header);
770 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
771 header, &msg_view)) {
772 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
773 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
775 g_object_unref (header);
777 /* Refilter the header views explicitely */
779 /* TODO: call modest_window_mgr_refilter_header_views */
780 /* this call will go through all the windows, get the header views and refilter them */
784 thread_prepare_delete_message (gpointer userdata)
786 TnyList *headers = NULL, *tmp_headers = NULL;
787 TnyFolder *folder = NULL;
788 TnyIterator *iter = NULL;
789 TnyHeader *header = NULL, *msg_header = NULL;
791 TnyAccount *account = NULL;
794 ModestMailOperation *mail_op = NULL;
795 ModestWindow *top_win = NULL;
797 uri = (char *) userdata;
799 msg = find_message_by_url (uri, &account);
801 g_object_unref (account);
804 g_debug ("%s: Could not find message '%s'", __FUNCTION__, uri);
805 g_idle_add (notify_error_in_dbus_callback, NULL);
810 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
812 folder = tny_msg_get_folder (msg);
814 g_debug ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
815 g_object_unref (msg);
816 g_idle_add (notify_error_in_dbus_callback, NULL);
822 msg_header = tny_msg_get_header (msg);
823 uid = tny_header_dup_uid (msg_header);
824 g_object_unref (msg);
825 g_object_unref (msg_header);
827 headers = tny_simple_list_new ();
828 tny_folder_get_headers (folder, headers, TRUE, NULL);
829 iter = tny_list_create_iterator (headers);
831 while (!tny_iterator_is_done (iter)) {
832 gchar *cur_id = NULL;
834 header = TNY_HEADER (tny_iterator_get_current (iter));
836 cur_id = tny_header_dup_uid (header);
838 if (cur_id && uid && g_str_equal (cur_id, uid)) {
840 /* g_debug ("Found corresponding header from folder"); */
846 g_object_unref (header);
850 tny_iterator_next (iter);
853 g_object_unref (iter);
854 g_object_unref (headers);
856 if (header == NULL) {
858 g_object_unref (folder);
859 g_idle_add (notify_error_in_dbus_callback, NULL);
864 /* This is a GDK lock because we are an idle callback and
865 * the code below is or does Gtk+ code */
866 gdk_threads_enter (); /* CHECKED */
868 mail_op = modest_mail_operation_new (top_win ? G_OBJECT(top_win) : NULL);
869 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
871 g_signal_connect (G_OBJECT (mail_op),
872 "operation-finished",
873 G_CALLBACK (on_remove_msgs_finished),
874 g_object_ref (header));
876 tmp_headers = tny_simple_list_new ();
877 tny_list_append (tmp_headers, (GObject *) header);
879 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
881 g_object_unref (tmp_headers);
882 g_object_unref (G_OBJECT (mail_op));
883 gdk_threads_leave (); /* CHECKED */
887 g_object_unref (header);
894 on_idle_delete_message (gpointer user_data)
896 const char *uri = NULL;
898 uri = (char *) user_data;
900 g_thread_create (thread_prepare_delete_message, g_strdup (uri), FALSE, NULL);
910 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
912 /* Get the arguments: */
913 osso_rpc_t val = g_array_index (arguments,
915 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
916 gchar *uri = g_strdup (val.value.s);
918 /* Use g_idle to context-switch into the application's thread: */
919 g_idle_add(on_idle_delete_message, (gpointer)uri);
925 on_idle_send_receive(gpointer user_data)
927 gboolean auto_update;
929 gdk_threads_enter (); /* CHECKED */
931 /* Check if the autoupdate feature is on */
932 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
933 MODEST_CONF_AUTO_UPDATE, NULL);
936 /* Do send receive. Never set the current top window
937 as we always assume that DBus send/receive requests
938 are not user driven */
939 modest_ui_actions_do_send_receive_all (NULL, FALSE, FALSE, FALSE);
941 /* Disable auto update */
942 modest_platform_set_update_interval (0);
944 gdk_threads_leave (); /* CHECKED */
952 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
957 dbus_uint32_t serial = 0;
959 GSList *account_names, *cursor;
961 str = g_strdup("\nsend queues\n"
964 cursor = account_names = modest_account_mgr_account_names
965 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
969 gchar *tmp, *accname = (gchar*)cursor->data;
971 tmp = g_strdup_printf ("%s", str);
976 acc = modest_tny_account_store_get_server_account (
977 modest_runtime_get_account_store(), accname,
978 TNY_ACCOUNT_TYPE_TRANSPORT);
979 if (TNY_IS_ACCOUNT(acc)) {
980 gchar *tmp = NULL, *url = tny_account_get_url_string (acc);
981 ModestTnySendQueue *sendqueue =
982 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
984 if (TNY_IS_SEND_QUEUE (sendqueue)) {
985 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
987 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
988 str, accname, tny_account_get_id (acc), url,
996 g_object_unref (acc);
999 cursor = g_slist_next (cursor);
1001 modest_account_mgr_free_account_names (account_names);
1005 reply = dbus_message_new_method_return (message);
1007 dbus_message_append_args (reply,
1008 DBUS_TYPE_STRING, &str,
1010 dbus_connection_send (con, reply, &serial);
1011 dbus_connection_flush (con);
1012 dbus_message_unref (reply);
1016 /* Let modest die */
1017 g_idle_add (notify_error_in_dbus_callback, NULL);
1024 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
1027 gchar *op_queue_str;
1030 dbus_uint32_t serial = 0;
1032 /* operations queue; */
1033 op_queue_str = modest_mail_operation_queue_to_string
1034 (modest_runtime_get_mail_operation_queue ());
1036 str = g_strdup_printf ("\noperation queue\n"
1040 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
1042 g_free (op_queue_str);
1046 reply = dbus_message_new_method_return (message);
1048 dbus_message_append_args (reply,
1049 DBUS_TYPE_STRING, &str,
1051 dbus_connection_send (con, reply, &serial);
1052 dbus_connection_flush (con);
1053 dbus_message_unref (reply);
1057 /* Let modest die */
1058 g_idle_add (notify_error_in_dbus_callback, NULL);
1066 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1071 dbus_uint32_t serial = 0;
1073 GSList *account_names, *cursor;
1075 str = g_strdup ("\naccounts\n========\n");
1077 cursor = account_names = modest_account_mgr_account_names
1078 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1082 gchar *tmp, *accname = (gchar*)cursor->data;
1084 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1089 acc = modest_tny_account_store_get_server_account (
1090 modest_runtime_get_account_store(), accname,
1091 TNY_ACCOUNT_TYPE_STORE);
1092 if (TNY_IS_ACCOUNT(acc)) {
1093 gchar *tmp, *url = tny_account_get_url_string (acc);
1094 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1095 str, tny_account_get_id (acc), url,
1096 ((GObject*)acc)->ref_count-1);
1100 g_object_unref (acc);
1104 acc = modest_tny_account_store_get_server_account (
1105 modest_runtime_get_account_store(), accname,
1106 TNY_ACCOUNT_TYPE_TRANSPORT);
1107 if (TNY_IS_ACCOUNT(acc)) {
1108 gchar *tmp, *url = tny_account_get_url_string (acc);
1109 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1110 str, tny_account_get_id (acc), url,
1111 ((GObject*)acc)->ref_count-1);
1115 g_object_unref (acc);
1118 cursor = g_slist_next (cursor);
1121 modest_account_mgr_free_account_names (account_names);
1125 reply = dbus_message_new_method_return (message);
1127 dbus_message_append_args (reply,
1128 DBUS_TYPE_STRING, &str,
1130 dbus_connection_send (con, reply, &serial);
1131 dbus_connection_flush (con);
1132 dbus_message_unref (reply);
1136 /* Let modest die */
1137 g_idle_add (notify_error_in_dbus_callback, NULL);
1143 on_send_receive_performer(gboolean canceled,
1145 GtkWindow *parent_window,
1146 TnyAccount *account,
1149 ModestConnectedVia connect_when;
1151 if (err || canceled) {
1152 g_idle_add (notify_error_in_dbus_callback, NULL);
1156 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
1157 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
1159 /* Perform a send and receive if the user selected to connect
1160 via any mean or if the current connection method is the
1161 same as the one specified by the user */
1162 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
1163 connect_when == modest_platform_get_current_connection ()) {
1164 g_idle_add (on_idle_send_receive, NULL);
1166 /* We need this to allow modest to finish */
1167 g_idle_add (notify_error_in_dbus_callback, NULL);
1173 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1175 TnyDevice *device = modest_runtime_get_device ();
1177 if (!tny_device_is_online (device))
1178 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, NULL);
1180 on_send_receive_performer (FALSE, NULL, NULL, NULL, NULL);
1186 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1188 g_idle_add(on_idle_top_application, NULL);
1195 on_idle_open_account (gpointer user_data)
1198 ModestWindowMgr *mgr;
1201 gdk_threads_enter ();
1203 acc_name = (gchar *) user_data;
1204 mgr = modest_runtime_get_window_mgr ();
1206 top = modest_window_mgr_get_current_top (mgr);
1208 top = modest_window_mgr_show_initial_window (mgr);
1210 #ifdef MODEST_TOOLKIT_HILDON2
1211 GtkWidget *new_window;
1212 ModestProtocolType store_protocol;
1213 gboolean mailboxes_protocol;
1215 store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
1217 mailboxes_protocol =
1218 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
1220 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
1222 if (mailboxes_protocol) {
1223 new_window = GTK_WIDGET (modest_mailboxes_window_new (acc_name));
1225 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
1226 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window),
1230 if (modest_window_mgr_register_window (mgr, MODEST_WINDOW (new_window), NULL)) {
1231 gtk_widget_show (new_window);
1233 gtk_widget_destroy (new_window);
1237 if (MODEST_IS_MAIN_WINDOW (top)) {
1239 GtkWidget *folder_view;
1241 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (top),
1242 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1243 server_name = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
1244 acc_name, TNY_ACCOUNT_TYPE_STORE);
1246 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (folder_view),
1248 g_free (server_name);
1253 gdk_threads_leave ();
1259 on_open_account (GArray *arguments, gpointer data, osso_rpc_t *retval)
1264 /* Get the arguments: */
1265 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
1266 account_id = g_strdup (val.value.s);
1268 g_idle_add (on_idle_open_account, account_id);
1273 #ifdef MODEST_TOOLKIT_HILDON2
1275 on_idle_top_application (gpointer user_data)
1277 HildonWindowStack *stack;
1280 /* This is a GDK lock because we are an idle callback and
1281 * the code below is or does Gtk+ code */
1283 gdk_threads_enter (); /* CHECKED */
1285 stack = hildon_window_stack_get_default ();
1286 window = GTK_WIDGET (hildon_window_stack_peek (stack));
1289 gtk_window_present (GTK_WINDOW (window));
1291 ModestWindowMgr *mgr;
1293 mgr = modest_runtime_get_window_mgr ();
1294 window = (GtkWidget *) modest_window_mgr_show_initial_window (mgr);
1296 modest_platform_remove_new_mail_notifications (FALSE);
1298 g_printerr ("modest: failed to get main window instance\n");
1302 gdk_threads_leave (); /* CHECKED */
1304 return FALSE; /* Do not call this callback again. */
1308 on_idle_top_application (gpointer user_data)
1310 ModestWindow *main_win;
1311 gboolean new_window = FALSE;
1313 /* This is a GDK lock because we are an idle callback and
1314 * the code below is or does Gtk+ code */
1316 gdk_threads_enter (); /* CHECKED */
1318 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1322 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1328 /* If we're showing an already existing window then
1329 reselect the INBOX */
1331 GtkWidget *folder_view;
1332 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_win),
1333 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1334 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
1339 gtk_widget_show_all (GTK_WIDGET (main_win));
1340 gtk_window_present (GTK_WINDOW (main_win));
1343 gdk_threads_leave (); /* CHECKED */
1345 return FALSE; /* Do not call this callback again. */
1350 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1352 /* Use g_idle to context-switch into the application's thread: */
1353 g_idle_add(on_idle_top_application, NULL);
1359 on_idle_show_memory_low (gpointer user_data)
1361 ModestWindow *main_win = NULL;
1363 gdk_threads_enter ();
1364 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
1365 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1366 dgettext("ke-recv","memr_ib_operation_disabled"),
1368 gdk_threads_leave ();
1374 on_idle_present_modal (gpointer user_data)
1376 gdk_threads_enter ();
1377 gtk_window_present (user_data);
1378 gdk_threads_leave ();
1384 /* Callback for normal D-BUS messages */
1386 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1387 GArray * arguments, gpointer data,
1388 osso_rpc_t * retval)
1392 /* Check memory low conditions */
1393 if (modest_platform_check_memory_low (NULL, FALSE)) {
1394 g_idle_add (on_idle_show_memory_low, NULL);
1398 /* Check if there is already a dialog or note open */
1399 dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
1401 g_idle_add (on_idle_present_modal, dialog);
1405 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1406 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1408 modest_runtime_set_allow_shutdown (TRUE);
1409 return on_mail_to (arguments, data, retval);
1410 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1411 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1413 modest_runtime_set_allow_shutdown (TRUE);
1414 return on_open_message (arguments, data, retval);
1415 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_ACCOUNT) == 0) {
1416 if (arguments->len != MODEST_DBUS_OPEN_ACCOUNT_ARGS_COUNT)
1418 modest_runtime_set_allow_shutdown (TRUE);
1419 return on_open_account (arguments, data, retval);
1420 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1421 if (arguments->len != 0)
1423 return on_send_receive (arguments, data, retval);
1424 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1425 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1427 modest_runtime_set_allow_shutdown (TRUE);
1428 return on_compose_mail (arguments, data, retval);
1429 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1430 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1432 return on_delete_message (arguments,data, retval);
1433 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1434 if (arguments->len != 0)
1436 modest_runtime_set_allow_shutdown (TRUE);
1437 return on_open_default_inbox (arguments, data, retval);
1438 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1439 if (arguments->len != 0)
1441 modest_runtime_set_allow_shutdown (TRUE);
1442 return on_top_application (arguments, data, retval);
1444 /* We need to return INVALID here so
1445 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1446 * so that our modest_dbus_req_filter will then be tried instead.
1448 return OSSO_INVALID;
1451 /* Notify error in D-Bus method */
1452 g_idle_add (notify_error_in_dbus_callback, NULL);
1456 /* A complex D-Bus type (like a struct),
1457 * used to return various information about a search hit.
1459 #define SEARCH_HIT_DBUS_TYPE \
1460 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1461 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1462 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1463 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1464 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1465 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1466 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1467 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1468 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1469 DBUS_STRUCT_END_CHAR_AS_STRING
1471 static DBusMessage *
1472 search_result_to_message (DBusMessage *reply,
1475 DBusMessageIter iter;
1476 DBusMessageIter array_iter;
1479 dbus_message_iter_init_append (reply, &iter);
1480 dbus_message_iter_open_container (&iter,
1482 SEARCH_HIT_DBUS_TYPE,
1485 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1486 DBusMessageIter struct_iter;
1487 ModestSearchResultHit *hit;
1489 const char *subject;
1493 gboolean has_attachment;
1497 hit = (ModestSearchResultHit *) hit_iter->data;
1499 msg_url = hit->msgid;
1500 subject = hit->subject;
1501 folder = hit->folder;
1502 sender = hit->sender;
1504 has_attachment = hit->has_attachment;
1505 is_unread = hit->is_unread;
1506 ts = hit->timestamp;
1508 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1510 dbus_message_iter_open_container (&array_iter,
1515 dbus_message_iter_append_basic (&struct_iter,
1519 dbus_message_iter_append_basic (&struct_iter,
1523 dbus_message_iter_append_basic (&struct_iter,
1527 dbus_message_iter_append_basic (&struct_iter,
1531 dbus_message_iter_append_basic (&struct_iter,
1535 dbus_message_iter_append_basic (&struct_iter,
1539 dbus_message_iter_append_basic (&struct_iter,
1543 dbus_message_iter_append_basic (&struct_iter,
1547 dbus_message_iter_close_container (&array_iter,
1550 g_free (hit->msgid);
1551 g_free (hit->subject);
1552 g_free (hit->folder);
1553 g_free (hit->sender);
1555 g_slice_free (ModestSearchResultHit, hit);
1558 dbus_message_iter_close_container (&iter, &array_iter);
1565 DBusConnection *con;
1566 DBusMessage *message;
1567 ModestSearch *search;
1571 search_all_cb (GList *hits, gpointer user_data)
1574 SearchHelper *helper = (SearchHelper *) user_data;
1576 reply = dbus_message_new_method_return (helper->message);
1579 dbus_uint32_t serial = 0;
1581 search_result_to_message (reply, hits);
1583 dbus_connection_send (helper->con, reply, &serial);
1584 dbus_connection_flush (helper->con);
1585 dbus_message_unref (reply);
1588 /* Free the helper */
1589 dbus_message_unref (helper->message);
1590 modest_search_free (helper->search);
1591 g_slice_free (ModestSearch, helper->search);
1592 g_slice_free (SearchHelper, helper);
1596 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1598 ModestDBusSearchFlags dbus_flags;
1602 dbus_int32_t flags_v;
1603 dbus_uint32_t size_v;
1608 ModestSearch *search;
1611 dbus_error_init (&error);
1616 res = dbus_message_get_args (message,
1618 DBUS_TYPE_STRING, &query,
1619 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1620 DBUS_TYPE_INT64, &sd_v,
1621 DBUS_TYPE_INT64, &ed_v,
1622 DBUS_TYPE_INT32, &flags_v,
1623 DBUS_TYPE_UINT32, &size_v,
1626 dbus_flags = (ModestDBusSearchFlags) flags_v;
1627 start_date = (time_t) sd_v;
1628 end_date = (time_t) ed_v;
1630 search = g_slice_new0 (ModestSearch);
1632 if (folder && g_str_has_prefix (folder, "MAND:")) {
1633 search->folder = g_strdup (folder + strlen ("MAND:"));
1634 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1635 search->folder = g_strdup (folder + strlen ("USER:"));
1636 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1637 search->folder = g_strdup (folder + strlen ("MY:"));
1639 search->folder = g_strdup (folder);
1642 /* Remember the text to search for: */
1643 #ifdef MODEST_HAVE_OGS
1644 search->query = g_strdup (query);
1647 /* Other criteria: */
1648 search->start_date = start_date;
1649 search->end_date = end_date;
1652 /* Text to serach for in various parts of the message: */
1653 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1654 search->flags |= MODEST_SEARCH_SUBJECT;
1655 search->subject = g_strdup (query);
1658 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1659 search->flags |= MODEST_SEARCH_SENDER;
1660 search->from = g_strdup (query);
1663 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1664 search->flags |= MODEST_SEARCH_RECIPIENT;
1665 search->recipient = g_strdup (query);
1668 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1669 search->flags |= MODEST_SEARCH_BODY;
1670 search->body = g_strdup (query);
1674 search->flags |= MODEST_SEARCH_AFTER;
1675 search->start_date = start_date;
1679 search->flags |= MODEST_SEARCH_BEFORE;
1680 search->end_date = end_date;
1684 search->flags |= MODEST_SEARCH_SIZE;
1685 search->minsize = size_v;
1688 #ifdef MODEST_HAVE_OGS
1689 search->flags |= MODEST_SEARCH_USE_OGS;
1690 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1693 SearchHelper *helper = g_slice_new (SearchHelper);
1694 helper->search = search;
1695 dbus_message_ref (message);
1696 helper->message = message;
1699 /* Search asynchronously */
1700 modest_search_all_accounts (search, search_all_cb, helper);
1704 /* A complex D-Bus type (like a struct),
1705 * used to return various information about a folder.
1707 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1708 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1709 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1710 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1711 DBUS_STRUCT_END_CHAR_AS_STRING
1713 static DBusMessage *
1714 get_folders_result_to_message (DBusMessage *reply,
1717 DBusMessageIter iter;
1718 dbus_message_iter_init_append (reply, &iter);
1720 DBusMessageIter array_iter;
1721 dbus_message_iter_open_container (&iter,
1723 GET_FOLDERS_RESULT_DBUS_TYPE,
1726 GList *list_iter = folder_ids;
1727 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1729 const gchar *folder_name = (const gchar*)list_iter->data;
1731 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1733 DBusMessageIter struct_iter;
1734 dbus_message_iter_open_container (&array_iter,
1740 dbus_message_iter_append_basic (&struct_iter,
1742 &folder_name); /* The string will be copied. */
1744 /* URI: This is maybe not needed by osso-global-search: */
1745 const gchar *folder_uri = "TODO:unimplemented";
1746 dbus_message_iter_append_basic (&struct_iter,
1748 &folder_uri); /* The string will be copied. */
1750 dbus_message_iter_close_container (&array_iter,
1755 dbus_message_iter_close_container (&iter, &array_iter);
1761 add_single_folder_to_list (TnyFolder *folder, GList** list)
1766 if (TNY_IS_MERGE_FOLDER (folder)) {
1767 const gchar * folder_name;
1768 /* Ignore these because their IDs ares
1769 * a) not always unique or sensible.
1770 * b) not human-readable, and currently need a human-readable
1771 * ID here, because the osso-email-interface API does not allow
1772 * us to return both an ID and a display name.
1774 * This is actually the merged outbox folder.
1775 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1776 * but that seems unwise. murrayc.
1778 folder_name = tny_folder_get_name (folder);
1779 if (folder_name && !strcmp (folder_name, "Outbox")) {
1780 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1785 /* Add this folder to the list: */
1787 const gchar * folder_name = tny_folder_get_name (folder);
1789 *list = g_list_append(*list, g_strdup (folder_name));
1792 /* osso-global-search only uses one string,
1793 * so ID is the only thing that could possibly identify a folder.
1794 * TODO: osso-global search should probably be changed to
1795 * take an ID and a Name.
1797 const gchar * id = tny_folder_get_id (folder);
1798 if (id && strlen(id)) {
1799 const gchar *prefix = NULL;
1800 TnyFolderType folder_type;
1802 /* dbus global search api expects a prefix identifying the type of
1803 * folder here. Mandatory folders should have MAND: prefix, and
1804 * other user created folders should have USER: prefix
1806 folder_type = modest_tny_folder_guess_folder_type (folder);
1807 switch (folder_type) {
1808 case TNY_FOLDER_TYPE_INBOX:
1811 case TNY_FOLDER_TYPE_OUTBOX:
1812 case TNY_FOLDER_TYPE_DRAFTS:
1813 case TNY_FOLDER_TYPE_SENT:
1814 case TNY_FOLDER_TYPE_ARCHIVE:
1817 case TNY_FOLDER_TYPE_INVALID:
1818 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1819 return; /* don't add it */
1826 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1831 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1836 /* Add this folder to the list: */
1837 if (TNY_IS_FOLDER (folder_store)) {
1838 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1841 /* Recurse into child folders: */
1843 /* Get the folders list: */
1845 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1846 tny_folder_store_query_add_item (query, NULL,
1847 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1849 TnyList *all_folders = tny_simple_list_new ();
1850 tny_folder_store_get_folders (folder_store,
1856 TnyIterator *iter = tny_list_create_iterator (all_folders);
1857 while (!tny_iterator_is_done (iter)) {
1859 /* Do not recurse, because the osso-global-search UI specification
1860 * does not seem to want the sub-folders, though that spec seems to
1861 * be generally unsuitable for Modest.
1863 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1865 add_single_folder_to_list (TNY_FOLDER (folder), list);
1868 if (TNY_IS_FOLDER_STORE (folder))
1869 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1871 add_single_folder_to_list (TNY_FOLDER (folder), list);
1875 /* tny_iterator_get_current() gave us a reference. */
1876 g_object_unref (folder);
1879 tny_iterator_next (iter);
1881 g_object_unref (G_OBJECT (iter));
1885 /* return >1 for a special folder, 0 for a user-folder */
1887 get_rank (const gchar *folder)
1889 if (strcmp (folder, "INBOX") == 0)
1891 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1893 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1895 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1901 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1903 gint r1 = get_rank (folder1);
1904 gint r2 = get_rank (folder2);
1906 if (r1 > 0 && r2 > 0)
1908 if (r1 > 0 && r2 == 0)
1910 if (r1 == 0 && r2 > 0)
1913 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1917 /* - we're still missing the outbox */
1918 /* - we need to take care of localization (urgh) */
1919 /* - what about 'All mail folders'? */
1921 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1923 DBusMessage *reply = NULL;
1924 ModestAccountMgr *account_mgr = NULL;
1925 gchar *account_name = NULL;
1926 GList *folder_names = NULL;
1927 TnyAccount *account_local = NULL;
1928 TnyAccount *account_mmc = NULL;
1930 /* Get the TnyStoreAccount so we can get the folders: */
1931 account_mgr = modest_runtime_get_account_mgr();
1932 account_name = modest_account_mgr_get_default_account (account_mgr);
1933 if (!account_name) {
1934 g_printerr ("modest: no account found\n");
1938 TnyAccount *account = NULL;
1940 account = modest_tny_account_store_get_server_account (
1941 modest_runtime_get_account_store(), account_name,
1942 TNY_ACCOUNT_TYPE_STORE);
1946 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1949 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1950 g_free (account_name);
1951 account_name = NULL;
1953 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1955 g_object_unref (account);
1959 /* Also add the folders from the local folders account,
1960 * because they are (currently) used with all accounts:
1961 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1964 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1965 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1967 g_object_unref (account_local);
1968 account_local = NULL;
1970 /* Obtain the mmc account */
1972 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1974 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1975 g_object_unref (account_mmc);
1979 /* specs require us to sort the folder names, although
1980 * this is really not the place to do that...
1982 folder_names = g_list_sort (folder_names,
1983 (GCompareFunc)folder_name_compare_func);
1985 /* Put the result in a DBus reply: */
1986 reply = dbus_message_new_method_return (message);
1988 get_folders_result_to_message (reply, folder_names);
1990 if (reply == NULL) {
1991 g_warning ("%s: Could not create reply.", __FUNCTION__);
1995 dbus_uint32_t serial = 0;
1996 dbus_connection_send (con, reply, &serial);
1997 dbus_connection_flush (con);
1998 dbus_message_unref (reply);
2001 g_list_foreach (folder_names, (GFunc)g_free, NULL);
2002 g_list_free (folder_names);
2007 reply_empty_results (DBusConnection *con, DBusMessage *msg)
2009 DBusMessage *reply = dbus_message_new_method_return (msg);
2011 dbus_uint32_t serial = 0;
2012 /* we simply return an empty list, otherwise
2013 global-search gets confused */
2014 search_result_to_message (reply, NULL);
2016 dbus_connection_send (con, reply, &serial);
2017 dbus_connection_flush (con);
2018 dbus_message_unref (reply);
2020 g_warning ("%s: failed to send reply",
2025 /** This D-Bus handler is used when the main osso-rpc
2026 * D-Bus handler has not handled something.
2027 * We use this for D-Bus methods that need to use more complex types
2028 * than osso-rpc supports.
2031 modest_dbus_req_filter (DBusConnection *con,
2032 DBusMessage *message,
2035 gboolean handled = FALSE;
2037 if (dbus_message_is_method_call (message,
2039 MODEST_DBUS_METHOD_SEARCH)) {
2041 /* don't try to search when there not enough mem */
2042 if (modest_platform_check_memory_low (NULL, TRUE)) {
2043 g_warning ("%s: not enough memory for searching",
2045 reply_empty_results (con, message);
2049 on_dbus_method_search (con, message);
2053 } else if (dbus_message_is_method_call (message,
2055 MODEST_DBUS_METHOD_GET_FOLDERS)) {
2056 on_dbus_method_get_folders (con, message);
2058 } else if (dbus_message_is_method_call (message,
2060 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
2061 on_dbus_method_dump_operation_queue (con, message);
2063 } else if (dbus_message_is_method_call (message,
2065 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
2066 on_dbus_method_dump_accounts (con, message);
2068 } else if (dbus_message_is_method_call (message,
2070 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
2071 on_dbus_method_dump_send_queues (con, message);
2074 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
2076 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
2077 __FUNCTION__, dbus_message_get_interface (message),
2078 dbus_message_get_member(message));
2083 DBUS_HANDLER_RESULT_HANDLED :
2084 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
2088 notify_error_in_dbus_callback (gpointer user_data)
2090 ModestMailOperation *mail_op;
2091 ModestMailOperationQueue *mail_op_queue;
2093 mail_op = modest_mail_operation_new (NULL);
2094 mail_op_queue = modest_runtime_get_mail_operation_queue ();
2096 /* Issues a noop operation in order to force the queue to emit
2097 the "queue-empty" signal to allow modest to quit */
2098 modest_mail_operation_queue_add (mail_op_queue, mail_op);
2099 modest_mail_operation_noop (mail_op);
2100 g_object_unref (mail_op);