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 "modest-tny-msg.h"
42 #include "modest-platform.h"
43 #include <libmodest-dbus-client/libmodest-dbus-client.h>
44 #include <libgnomevfs/gnome-vfs-utils.h>
47 #include <glib/gstdio.h>
48 #ifdef MODEST_HAVE_HILDON0_WIDGETS
49 #include <libgnomevfs/gnome-vfs-mime-utils.h>
51 #include <libgnomevfs/gnome-vfs-mime.h>
53 #include <tny-fs-stream.h>
56 #include <tny-iterator.h>
57 #include <tny-simple-list.h>
58 #include <tny-merge-folder.h>
59 #include <tny-account.h>
61 #include <modest-text-utils.h>
71 } ComposeMailIdleData;
74 static gboolean notify_error_in_dbus_callback (gpointer user_data);
75 static gboolean notify_msg_not_found_in_idle (gpointer user_data);
76 static gboolean on_idle_compose_mail(gpointer user_data);
77 static gboolean on_idle_top_application (gpointer user_data);
80 * @uri An escaped URI. URIs should always be escaped.
81 * @len The length of the @uri string, or -1 if the string is null terminated.
83 * Decode a URI, or URI fragment, as per RFC 1738.
84 * http://www.ietf.org/rfc/rfc1738.txt
86 * Return value: An unescaped string. This should be freed with g_free().
89 uri_unescape(const gchar* uri, size_t len)
97 /* Allocate an extra string so we can be sure that it is null-terminated,
98 * so we can use gnome_vfs_unescape_string().
99 * This is not efficient. */
100 gchar * escaped_nullterminated = g_strndup (uri, len);
101 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
102 g_free (escaped_nullterminated);
107 /** uri_parse_mailto:
108 * @mailto A mailto URI, with the mailto: prefix.
109 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
110 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
111 * all the string items have been freed. This parameter may be NULL.
112 * Parse a mailto URI as per RFC2368.
113 * http://www.ietf.org/rfc/rfc2368.txt
115 * Return value: The to address, unescaped. This should be freed with g_free().
118 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
120 /* The URL must begin with mailto: */
121 if (strncmp (mailto, "mailto:", 7) != 0) {
124 const gchar* start_to = mailto + 7;
126 /* Look for ?, or the end of the string, marking the end of the to address: */
127 const size_t len_to = strcspn (start_to, "?");
128 gchar* result_to = uri_unescape (start_to, len_to);
129 printf("debug: result_to=%s\n", result_to);
131 if (list_items_and_values == NULL) {
135 /* Get any other items: */
136 const size_t len_mailto = strlen (start_to);
137 const gchar* p = start_to + len_to + 1; /* parsed so far. */
138 const gchar* end = start_to + len_mailto;
140 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
142 name_end = strchr (name_start, '='); /* Separator between name and value */
143 if (name_end == NULL) {
144 g_debug ("Malformed URI: %s\n", mailto);
147 value_start = name_end + 1;
148 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
150 name = g_strndup(name_start, name_end - name_start);
151 if (value_end != NULL) {
152 value = uri_unescape(value_start, value_end - value_start);
155 value = uri_unescape(value_start, -1);
158 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
159 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
166 check_and_offer_account_creation()
168 gboolean result = TRUE;
170 /* This is called from idle handlers, so lock gdk: */
171 gdk_threads_enter ();
173 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
174 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
176 g_debug ("modest: %s: no account exists even after offering, "
177 "or account setup was already underway.\n", __FUNCTION__);
182 gdk_threads_leave ();
188 on_idle_mail_to(gpointer user_data)
190 gchar *uri = (gchar*)user_data;
191 GSList *list_names_and_values = NULL;
193 const gchar *cc = NULL;
194 const gchar *bcc = NULL;
195 const gchar *subject = NULL;
196 const gchar *body = NULL;
198 if (!check_and_offer_account_creation ()) {
199 g_idle_add (notify_error_in_dbus_callback, NULL);
203 /* Get the relevant items from the list: */
204 to = uri_parse_mailto (uri, &list_names_and_values);
205 GSList *list = list_names_and_values;
207 GSList *list_value = g_slist_next (list);
208 const gchar * name = (const gchar*)list->data;
209 const gchar * value = (const gchar*)list_value->data;
211 if (strcmp (name, "cc") == 0) {
213 } else if (strcmp (name, "bcc") == 0) {
215 } else if (strcmp (name, "subject") == 0) {
217 } else if (strcmp (name, "body") == 0) {
221 list = g_slist_next (list_value);
224 gdk_threads_enter (); /* CHECKED */
225 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
226 gdk_threads_leave (); /* CHECKED */
229 /* Free the to: and the list, as required by uri_parse_mailto() */
231 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
232 g_slist_free (list_names_and_values);
236 return FALSE; /* Do not call this callback again. */
240 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
246 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
247 uri = g_strdup (val.value.s);
249 g_idle_add(on_idle_mail_to, (gpointer)uri);
251 /* Note that we cannot report failures during sending,
252 * because that would be asynchronous. */
258 on_idle_compose_mail(gpointer user_data)
260 GSList *attachments = NULL;
261 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
263 if (!check_and_offer_account_creation ()) {
264 g_idle_add (notify_error_in_dbus_callback, NULL);
268 /* it seems Sketch at least sends a leading ',' -- take that into account,
270 if (idle_data->attachments && idle_data->attachments[0]==',') {
271 gchar *tmp = g_strdup (idle_data->attachments + 1);
272 g_free(idle_data->attachments);
273 idle_data->attachments = tmp;
276 if (idle_data->attachments != NULL) {
277 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
279 for (i=0; list[i] != NULL; i++) {
280 attachments = g_slist_append(attachments, g_strdup(list[i]));
285 /* If the message has nothing then mark the buffers as not
286 modified. This happens in Maemo for example when opening a
287 new message from Contacts plugin, it sends "" instead of
289 gdk_threads_enter (); /* CHECKED */
290 if (!strncmp (idle_data->to, "", 1) &&
291 !strncmp (idle_data->to, "", 1) &&
292 !strncmp (idle_data->cc, "", 1) &&
293 !strncmp (idle_data->bcc, "", 1) &&
294 !strncmp (idle_data->subject, "", 1) &&
295 !strncmp (idle_data->body, "", 1) &&
296 attachments == NULL) {
297 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
299 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
300 idle_data->bcc, idle_data->subject,
301 idle_data->body, attachments, TRUE);
303 gdk_threads_leave (); /* CHECKED */
305 g_slist_foreach(attachments, (GFunc)g_free, NULL);
306 g_slist_free(attachments);
307 g_free (idle_data->to);
308 g_free (idle_data->cc);
309 g_free (idle_data->bcc);
310 g_free (idle_data->subject);
311 g_free (idle_data->body);
312 g_free (idle_data->attachments);
315 return FALSE; /* Do not call this callback again. */
319 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
321 ComposeMailIdleData *idle_data;
324 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
326 /* Get the arguments: */
327 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
328 idle_data->to = g_strdup (val.value.s);
330 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
331 idle_data->cc = g_strdup (val.value.s);
333 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
334 idle_data->bcc = g_strdup (val.value.s);
336 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
337 idle_data->subject = g_strdup (val.value.s);
339 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
340 idle_data->body = g_strdup (val.value.s);
342 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
343 idle_data->attachments = g_strdup (val.value.s);
345 /* Use g_idle to context-switch into the application's thread: */
346 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
352 find_message_by_url (const char *uri, TnyAccount **ac_out)
354 ModestTnyAccountStore *astore;
355 TnyAccount *account = NULL;
356 TnyFolder *folder = NULL;
359 astore = modest_runtime_get_account_store ();
364 if (uri && g_str_has_prefix (uri, "merge://")) {
365 /* we assume we're talking about outbox folder, as this
366 * is the only merge folder we work with in modest */
367 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
369 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
372 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
376 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
381 msg = tny_folder_find_msg (folder, uri, NULL);
384 if (account && !msg) {
385 g_object_unref (account);
389 g_object_unref (folder);
398 guint animation_timeout;
399 GtkWidget *animation;
400 } OpenMsgPerformerInfo;
403 on_show_opening_animation (gpointer userdata)
405 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
406 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
407 info->animation_timeout = 0;
413 on_find_msg_async_destroy (gpointer userdata)
415 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
417 if (info->animation_timeout>0) {
418 g_source_remove (info->animation_timeout);
419 info->animation_timeout = 0;
422 if (info->animation) {
423 gtk_widget_destroy (info->animation);
424 info->animation = NULL;
431 g_object_unref (info->account);
433 g_slice_free (OpenMsgPerformerInfo, info);
438 find_msg_async_cb (TnyFolder *folder,
446 ModestWindowMgr *win_mgr;
447 ModestWindow *msg_view = NULL;
448 gboolean is_draft = FALSE;
449 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
451 if (err || cancelled) {
452 g_idle_add (notify_msg_not_found_in_idle, NULL);
456 header = tny_msg_get_header (msg);
457 if (header && (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
458 g_object_unref (header);
459 g_idle_add (notify_msg_not_found_in_idle, NULL);
463 msg_uid = modest_tny_folder_get_header_unique_id (header);
464 win_mgr = modest_runtime_get_window_mgr ();
466 if (modest_tny_folder_is_local_folder (folder) &&
467 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
471 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
472 gtk_window_present (GTK_WINDOW(msg_view));
476 modest_window_mgr_register_header (win_mgr, header, NULL);
478 account = tny_folder_get_account (folder);
480 /* Drafts will be opened in the editor, and others will be opened in the viewer */
482 gchar *modest_account_name = NULL;
485 /* we cannot edit without a valid account... */
486 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
487 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
488 modest_window_mgr_unregister_header (win_mgr,
494 from_header = tny_header_dup_from (header);
495 modest_account_name = modest_utils_get_account_name_from_recipient (from_header);
496 g_free (from_header);
498 if (modest_account_name == NULL) {
499 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
500 modest_account_name = modest_account_mgr_get_default_account (mgr);
502 msg_view = modest_msg_edit_window_new (msg, modest_account_name, TRUE);
503 g_free (modest_account_name);
506 const gchar *modest_account_name;
509 modest_account_name =
510 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
512 modest_account_name = NULL;
515 header = tny_msg_get_header (msg);
516 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name, msg_uid);
518 if (!(tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
519 ModestMailOperation *mail_op;
521 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
522 /* Sync folder, we need this to save the seen flag */
523 mail_op = modest_mail_operation_new (NULL);
524 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
526 modest_mail_operation_sync_folder (mail_op, folder, FALSE);
527 g_object_unref (mail_op);
529 g_object_unref (header);
531 if (msg_view != NULL) {
532 modest_window_mgr_register_window (win_mgr, msg_view);
533 gtk_widget_show_all (GTK_WIDGET (msg_view));
538 g_object_unref (header);
541 on_find_msg_async_destroy (info);
546 on_open_message_performer (gboolean canceled,
548 GtkWindow *parent_window,
552 OpenMsgPerformerInfo *info;
553 TnyFolder *folder = NULL;
555 info = (OpenMsgPerformerInfo *) user_data;
556 if (canceled || err) {
557 g_idle_add (notify_msg_not_found_in_idle, NULL);
558 on_find_msg_async_destroy (info);
564 ModestTnyAccountStore *account_store;
565 ModestTnyLocalFoldersAccount *local_folders_account;
567 account_store = modest_runtime_get_account_store ();
568 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
569 modest_tny_account_store_get_local_folders_account (account_store));
570 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
571 g_object_unref (local_folders_account);
573 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
576 g_idle_add (notify_msg_not_found_in_idle, NULL);
577 on_find_msg_async_destroy (info);
581 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
583 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
584 g_object_unref (folder);
588 on_idle_open_message_performer (gpointer user_data)
590 ModestWindow *main_win = NULL;
591 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
593 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
594 FALSE); /* don't create */
596 /* Lock before the call as we're in an idle handler */
597 gdk_threads_enter ();
599 modest_platform_connect_and_perform (GTK_WINDOW (main_win), TRUE, info->account,
600 on_open_message_performer, info);
602 on_open_message_performer (FALSE, NULL, GTK_WINDOW (main_win), info->account, info);
604 gdk_threads_leave ();
610 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
614 TnyAccount *account = NULL;
618 /* Get the arguments: */
619 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
620 uri = g_strdup (val.value.s);
622 is_merge = g_str_has_prefix (uri, "merge:");
624 /* Get the account */
626 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
630 if (is_merge || account) {
631 OpenMsgPerformerInfo *info;
632 TnyFolder *folder = NULL;
634 info = g_slice_new0 (OpenMsgPerformerInfo);
636 info->account = g_object_ref (account);
638 info->connect = TRUE;
639 info->animation = NULL;
640 info->animation_timeout = 0;
642 /* Try to get the message, if it's already downloaded
643 we don't need to connect */
645 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
647 ModestTnyAccountStore *account_store;
648 ModestTnyLocalFoldersAccount *local_folders_account;
650 account_store = modest_runtime_get_account_store ();
651 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
652 modest_tny_account_store_get_local_folders_account (account_store));
653 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
654 g_object_unref (local_folders_account);
658 gboolean device_online;
660 device = modest_runtime_get_device();
661 device_online = tny_device_is_online (device);
663 info->connect = TRUE;
665 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
667 info->connect = FALSE;
668 g_object_unref (msg);
670 info->connect = TRUE;
673 g_object_unref (folder);
676 /* We need to call it into an idle to get
677 modest_platform_connect_and_perform into the main
679 g_idle_add (on_idle_open_message_performer, info);
680 osso_retval = OSSO_OK;
683 osso_retval = OSSO_ERROR;
684 g_idle_add (notify_error_in_dbus_callback, NULL);
688 g_object_unref (account);
693 on_remove_msgs_finished (ModestMailOperation *mail_op,
697 ModestWindow *main_win = NULL, *msg_view = NULL;
698 ModestHeaderView *header_view;
700 header = (TnyHeader *) user_data;
702 /* Get the main window if exists */
703 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
704 FALSE); /* don't create */
706 g_object_unref (header);
710 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
711 header, &msg_view)) {
712 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
713 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
715 g_object_unref (header);
717 /* Refilter the header view explicitly */
718 header_view = (ModestHeaderView *)
719 modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(main_win),
720 MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
721 if (header_view && MODEST_IS_HEADER_VIEW (header_view))
722 modest_header_view_refilter (header_view);
726 on_idle_delete_message (gpointer user_data)
728 TnyList *headers = NULL, *tmp_headers = NULL;
729 TnyFolder *folder = NULL;
730 TnyIterator *iter = NULL;
731 TnyHeader *header = NULL, *msg_header = NULL;
733 TnyAccount *account = NULL;
734 const char *uri = NULL;
736 ModestMailOperation *mail_op = NULL;
737 ModestWindow *main_win = NULL;
739 uri = (char *) user_data;
741 msg = find_message_by_url (uri, &account);
743 g_object_unref (account);
746 g_warning ("%s: Could not find message '%s'", __FUNCTION__, uri);
747 g_idle_add (notify_error_in_dbus_callback, NULL);
751 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
752 FALSE); /* don't create */
754 folder = tny_msg_get_folder (msg);
756 g_warning ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
757 g_object_unref (msg);
758 g_idle_add (notify_error_in_dbus_callback, NULL);
763 msg_header = tny_msg_get_header (msg);
764 uid = tny_header_dup_uid (msg_header);
765 g_object_unref (msg);
766 g_object_unref (msg_header);
768 headers = tny_simple_list_new ();
769 tny_folder_get_headers (folder, headers, TRUE, NULL);
770 iter = tny_list_create_iterator (headers);
772 while (!tny_iterator_is_done (iter)) {
773 gchar *cur_id = NULL;
775 header = TNY_HEADER (tny_iterator_get_current (iter));
777 cur_id = tny_header_dup_uid (header);
779 if (cur_id && uid && g_str_equal (cur_id, uid)) {
781 /* g_debug ("Found corresponding header from folder"); */
787 g_object_unref (header);
791 tny_iterator_next (iter);
794 g_object_unref (iter);
795 g_object_unref (headers);
797 if (header == NULL) {
799 g_object_unref (folder);
800 g_idle_add (notify_error_in_dbus_callback, NULL);
804 /* This is a GDK lock because we are an idle callback and
805 * the code below is or does Gtk+ code */
806 gdk_threads_enter (); /* CHECKED */
808 mail_op = modest_mail_operation_new (main_win ? G_OBJECT(main_win) : NULL);
809 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
811 g_signal_connect (G_OBJECT (mail_op),
812 "operation-finished",
813 G_CALLBACK (on_remove_msgs_finished),
814 g_object_ref (header));
816 tmp_headers = tny_simple_list_new ();
817 tny_list_append (tmp_headers, (GObject *) header);
819 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
821 g_object_unref (tmp_headers);
822 g_object_unref (G_OBJECT (mail_op));
823 gdk_threads_leave (); /* CHECKED */
827 g_object_unref (header);
836 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
838 /* Get the arguments: */
839 osso_rpc_t val = g_array_index (arguments,
841 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
842 gchar *uri = g_strdup (val.value.s);
844 /* Use g_idle to context-switch into the application's thread: */
845 g_idle_add(on_idle_delete_message, (gpointer)uri);
851 on_idle_send_receive(gpointer user_data)
853 gboolean auto_update;
854 ModestWindow *main_win = NULL;
857 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
858 FALSE); /* don't create */
860 gdk_threads_enter (); /* CHECKED */
862 /* Check if the autoupdate feature is on */
863 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
864 MODEST_CONF_AUTO_UPDATE, NULL);
867 /* Do send receive */
868 modest_ui_actions_do_send_receive_all (main_win, FALSE, FALSE, FALSE);
870 /* Disable auto update */
871 modest_platform_set_update_interval (0);
873 gdk_threads_leave (); /* CHECKED */
881 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
886 dbus_uint32_t serial = 0;
888 GSList *account_names, *cursor;
890 str = g_strdup("\nsend queues\n"
893 cursor = account_names = modest_account_mgr_account_names
894 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
898 gchar *tmp, *accname = (gchar*)cursor->data;
900 tmp = g_strdup_printf ("%s", str);
905 acc = modest_tny_account_store_get_server_account (
906 modest_runtime_get_account_store(), accname,
907 TNY_ACCOUNT_TYPE_TRANSPORT);
908 if (TNY_IS_ACCOUNT(acc)) {
909 gchar *tmp = NULL, *url = tny_account_get_url_string (acc);
910 ModestTnySendQueue *sendqueue =
911 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
913 if (TNY_IS_SEND_QUEUE (sendqueue)) {
914 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
916 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
917 str, accname, tny_account_get_id (acc), url,
925 g_object_unref (acc);
928 cursor = g_slist_next (cursor);
930 modest_account_mgr_free_account_names (account_names);
934 reply = dbus_message_new_method_return (message);
936 dbus_message_append_args (reply,
937 DBUS_TYPE_STRING, &str,
939 dbus_connection_send (con, reply, &serial);
940 dbus_connection_flush (con);
941 dbus_message_unref (reply);
946 g_idle_add (notify_error_in_dbus_callback, NULL);
953 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
959 dbus_uint32_t serial = 0;
961 /* operations queue; */
962 op_queue_str = modest_mail_operation_queue_to_string
963 (modest_runtime_get_mail_operation_queue ());
965 str = g_strdup_printf ("\noperation queue\n"
969 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
971 g_free (op_queue_str);
975 reply = dbus_message_new_method_return (message);
977 dbus_message_append_args (reply,
978 DBUS_TYPE_STRING, &str,
980 dbus_connection_send (con, reply, &serial);
981 dbus_connection_flush (con);
982 dbus_message_unref (reply);
987 g_idle_add (notify_error_in_dbus_callback, NULL);
995 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1000 dbus_uint32_t serial = 0;
1002 GSList *account_names, *cursor;
1004 str = g_strdup ("\naccounts\n========\n");
1006 cursor = account_names = modest_account_mgr_account_names
1007 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1011 gchar *tmp, *accname = (gchar*)cursor->data;
1013 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1018 acc = modest_tny_account_store_get_server_account (
1019 modest_runtime_get_account_store(), accname,
1020 TNY_ACCOUNT_TYPE_STORE);
1021 if (TNY_IS_ACCOUNT(acc)) {
1022 gchar *tmp, *url = tny_account_get_url_string (acc);
1023 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1024 str, tny_account_get_id (acc), url,
1025 ((GObject*)acc)->ref_count-1);
1029 g_object_unref (acc);
1033 acc = modest_tny_account_store_get_server_account (
1034 modest_runtime_get_account_store(), accname,
1035 TNY_ACCOUNT_TYPE_TRANSPORT);
1036 if (TNY_IS_ACCOUNT(acc)) {
1037 gchar *tmp, *url = tny_account_get_url_string (acc);
1038 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1039 str, tny_account_get_id (acc), url,
1040 ((GObject*)acc)->ref_count-1);
1044 g_object_unref (acc);
1047 cursor = g_slist_next (cursor);
1050 modest_account_mgr_free_account_names (account_names);
1054 reply = dbus_message_new_method_return (message);
1056 dbus_message_append_args (reply,
1057 DBUS_TYPE_STRING, &str,
1059 dbus_connection_send (con, reply, &serial);
1060 dbus_connection_flush (con);
1061 dbus_message_unref (reply);
1065 /* Let modest die */
1066 g_idle_add (notify_error_in_dbus_callback, NULL);
1072 on_send_receive_performer(gboolean canceled,
1074 GtkWindow *parent_window,
1075 TnyAccount *account,
1078 ModestConnectedVia connect_when;
1080 if (err || canceled) {
1081 g_idle_add (notify_error_in_dbus_callback, NULL);
1085 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
1086 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
1088 /* Perform a send and receive if the user selected to connect
1089 via any mean or if the current connection method is the
1090 same as the one specified by the user */
1091 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
1092 connect_when == modest_platform_get_current_connection ()) {
1093 g_idle_add (on_idle_send_receive, NULL);
1095 /* We need this to allow modest to finish */
1096 g_idle_add (notify_error_in_dbus_callback, NULL);
1102 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1104 TnyDevice *device = modest_runtime_get_device ();
1106 if (!tny_device_is_online (device))
1107 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, NULL);
1109 on_send_receive_performer (FALSE, NULL, NULL, NULL, NULL);
1115 on_idle_open_default_inbox(gpointer user_data)
1117 ModestWindow *main_win;
1118 GtkWidget *folder_view;
1120 if (!check_and_offer_account_creation ()) /* this has it's only lock already */
1123 /* This is a GDK lock because we are an idle callback and
1124 * the code below is or does Gtk+ code */
1125 gdk_threads_enter (); /* CHECKED */
1127 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1128 TRUE); /* create if non-existent */
1130 g_warning ("%s: BUG: no main window", __FUNCTION__);
1131 gdk_threads_leave (); /* CHECKED */
1132 return FALSE; /* don't call me again */
1135 /* Get the folder view */
1136 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_win),
1137 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1138 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
1140 gdk_threads_leave (); /* CHECKED */
1142 /* This D-Bus method is obviously meant to result in the UI being visible,
1143 * so show it, by calling this idle handler directly: */
1144 on_idle_top_application(user_data);
1146 return FALSE; /* Do not call this callback again. */
1150 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1152 /* Use g_idle to context-switch into the application's thread: */
1153 g_idle_add(on_idle_open_default_inbox, NULL);
1160 on_idle_top_application (gpointer user_data)
1162 ModestWindow *main_win;
1164 /* This is a GDK lock because we are an idle callback and
1165 * the code below is or does Gtk+ code */
1167 gdk_threads_enter (); /* CHECKED */
1169 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1170 TRUE); /* create if non-existent */
1172 /* Ideally, we would just use gtk_widget_show(),
1173 * but this widget is not coded correctly to support that: */
1174 gtk_widget_show_all (GTK_WIDGET (main_win));
1175 gtk_window_present (GTK_WINDOW (main_win));
1177 g_warning ("%s: BUG: no main window", __FUNCTION__);
1179 gdk_threads_leave (); /* CHECKED */
1181 return FALSE; /* Do not call this callback again. */
1185 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1187 /* Use g_idle to context-switch into the application's thread: */
1188 g_idle_add(on_idle_top_application, NULL);
1194 on_idle_show_memory_low (gpointer user_data)
1196 ModestWindow *main_win = NULL;
1198 gdk_threads_enter ();
1199 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
1200 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1201 dgettext("ke-recv","memr_ib_operation_disabled"),
1203 gdk_threads_leave ();
1208 /* Callback for normal D-BUS messages */
1210 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1211 GArray * arguments, gpointer data,
1212 osso_rpc_t * retval)
1214 /* Check memory low conditions */
1215 if (modest_platform_check_memory_low (NULL, FALSE)) {
1216 g_idle_add (on_idle_show_memory_low, NULL);
1220 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1221 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1223 return on_mail_to (arguments, data, retval);
1224 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1225 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1227 return on_open_message (arguments, data, retval);
1228 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1229 if (arguments->len != 0)
1231 return on_send_receive (arguments, data, retval);
1232 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1233 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1235 return on_compose_mail (arguments, data, retval);
1236 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1237 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1239 return on_delete_message (arguments,data, retval);
1240 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1241 if (arguments->len != 0)
1243 return on_open_default_inbox (arguments, data, retval);
1244 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1245 if (arguments->len != 0)
1247 return on_top_application (arguments, data, retval);
1249 /* We need to return INVALID here so
1250 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1251 * so that our modest_dbus_req_filter will then be tried instead.
1253 return OSSO_INVALID;
1256 /* Notify error in D-Bus method */
1257 g_idle_add (notify_error_in_dbus_callback, NULL);
1261 /* A complex D-Bus type (like a struct),
1262 * used to return various information about a search hit.
1264 #define SEARCH_HIT_DBUS_TYPE \
1265 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1266 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1267 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1268 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1269 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1270 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1271 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1272 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1273 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1274 DBUS_STRUCT_END_CHAR_AS_STRING
1276 static DBusMessage *
1277 search_result_to_message (DBusMessage *reply,
1280 DBusMessageIter iter;
1281 DBusMessageIter array_iter;
1284 dbus_message_iter_init_append (reply, &iter);
1285 dbus_message_iter_open_container (&iter,
1287 SEARCH_HIT_DBUS_TYPE,
1290 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1291 DBusMessageIter struct_iter;
1292 ModestSearchResultHit *hit;
1294 const char *subject;
1298 gboolean has_attachment;
1302 hit = (ModestSearchResultHit *) hit_iter->data;
1304 msg_url = hit->msgid;
1305 subject = hit->subject;
1306 folder = hit->folder;
1307 sender = hit->sender;
1309 has_attachment = hit->has_attachment;
1310 is_unread = hit->is_unread;
1311 ts = hit->timestamp;
1313 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1315 dbus_message_iter_open_container (&array_iter,
1320 dbus_message_iter_append_basic (&struct_iter,
1324 dbus_message_iter_append_basic (&struct_iter,
1328 dbus_message_iter_append_basic (&struct_iter,
1332 dbus_message_iter_append_basic (&struct_iter,
1336 dbus_message_iter_append_basic (&struct_iter,
1340 dbus_message_iter_append_basic (&struct_iter,
1344 dbus_message_iter_append_basic (&struct_iter,
1348 dbus_message_iter_append_basic (&struct_iter,
1352 dbus_message_iter_close_container (&array_iter,
1355 g_free (hit->msgid);
1356 g_free (hit->subject);
1357 g_free (hit->folder);
1358 g_free (hit->sender);
1360 g_slice_free (ModestSearchResultHit, hit);
1363 dbus_message_iter_close_container (&iter, &array_iter);
1370 DBusConnection *con;
1371 DBusMessage *message;
1372 ModestSearch *search;
1376 search_all_cb (GList *hits, gpointer user_data)
1379 SearchHelper *helper = (SearchHelper *) user_data;
1381 reply = dbus_message_new_method_return (helper->message);
1384 dbus_uint32_t serial = 0;
1386 search_result_to_message (reply, hits);
1388 dbus_connection_send (helper->con, reply, &serial);
1389 dbus_connection_flush (helper->con);
1390 dbus_message_unref (reply);
1393 /* Free the helper */
1394 dbus_message_unref (helper->message);
1395 modest_search_free (helper->search);
1396 g_slice_free (ModestSearch, helper->search);
1397 g_slice_free (SearchHelper, helper);
1401 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1403 ModestDBusSearchFlags dbus_flags;
1407 dbus_int32_t flags_v;
1408 dbus_uint32_t size_v;
1413 ModestSearch *search;
1416 dbus_error_init (&error);
1421 res = dbus_message_get_args (message,
1423 DBUS_TYPE_STRING, &query,
1424 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1425 DBUS_TYPE_INT64, &sd_v,
1426 DBUS_TYPE_INT64, &ed_v,
1427 DBUS_TYPE_INT32, &flags_v,
1428 DBUS_TYPE_UINT32, &size_v,
1431 dbus_flags = (ModestDBusSearchFlags) flags_v;
1432 start_date = (time_t) sd_v;
1433 end_date = (time_t) ed_v;
1435 search = g_slice_new0 (ModestSearch);
1437 if (folder && g_str_has_prefix (folder, "MAND:")) {
1438 search->folder = g_strdup (folder + strlen ("MAND:"));
1439 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1440 search->folder = g_strdup (folder + strlen ("USER:"));
1441 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1442 search->folder = g_strdup (folder + strlen ("MY:"));
1444 search->folder = g_strdup (folder);
1447 /* Remember the text to search for: */
1448 #ifdef MODEST_HAVE_OGS
1449 search->query = g_strdup (query);
1452 /* Other criteria: */
1453 search->start_date = start_date;
1454 search->end_date = end_date;
1457 /* Text to serach for in various parts of the message: */
1458 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1459 search->flags |= MODEST_SEARCH_SUBJECT;
1460 search->subject = g_strdup (query);
1463 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1464 search->flags |= MODEST_SEARCH_SENDER;
1465 search->from = g_strdup (query);
1468 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1469 search->flags |= MODEST_SEARCH_RECIPIENT;
1470 search->recipient = g_strdup (query);
1473 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1474 search->flags |= MODEST_SEARCH_BODY;
1475 search->body = g_strdup (query);
1479 search->flags |= MODEST_SEARCH_BEFORE;
1480 search->start_date = start_date;
1484 search->flags |= MODEST_SEARCH_AFTER;
1485 search->end_date = end_date;
1489 search->flags |= MODEST_SEARCH_SIZE;
1490 search->minsize = size_v;
1493 #ifdef MODEST_HAVE_OGS
1494 search->flags |= MODEST_SEARCH_USE_OGS;
1495 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1498 SearchHelper *helper = g_slice_new (SearchHelper);
1499 helper->search = search;
1500 dbus_message_ref (message);
1501 helper->message = message;
1504 /* Search asynchronously */
1505 modest_search_all_accounts (search, search_all_cb, helper);
1509 /* A complex D-Bus type (like a struct),
1510 * used to return various information about a folder.
1512 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1513 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1514 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1515 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1516 DBUS_STRUCT_END_CHAR_AS_STRING
1518 static DBusMessage *
1519 get_folders_result_to_message (DBusMessage *reply,
1522 DBusMessageIter iter;
1523 dbus_message_iter_init_append (reply, &iter);
1525 DBusMessageIter array_iter;
1526 dbus_message_iter_open_container (&iter,
1528 GET_FOLDERS_RESULT_DBUS_TYPE,
1531 GList *list_iter = folder_ids;
1532 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1534 const gchar *folder_name = (const gchar*)list_iter->data;
1536 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1538 DBusMessageIter struct_iter;
1539 dbus_message_iter_open_container (&array_iter,
1545 dbus_message_iter_append_basic (&struct_iter,
1547 &folder_name); /* The string will be copied. */
1549 /* URI: This is maybe not needed by osso-global-search: */
1550 const gchar *folder_uri = "TODO:unimplemented";
1551 dbus_message_iter_append_basic (&struct_iter,
1553 &folder_uri); /* The string will be copied. */
1555 dbus_message_iter_close_container (&array_iter,
1560 dbus_message_iter_close_container (&iter, &array_iter);
1566 add_single_folder_to_list (TnyFolder *folder, GList** list)
1571 if (TNY_IS_MERGE_FOLDER (folder)) {
1572 const gchar * folder_name;
1573 /* Ignore these because their IDs ares
1574 * a) not always unique or sensible.
1575 * b) not human-readable, and currently need a human-readable
1576 * ID here, because the osso-email-interface API does not allow
1577 * us to return both an ID and a display name.
1579 * This is actually the merged outbox folder.
1580 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1581 * but that seems unwise. murrayc.
1583 folder_name = tny_folder_get_name (folder);
1584 if (folder_name && !strcmp (folder_name, "Outbox")) {
1585 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1590 /* Add this folder to the list: */
1592 const gchar * folder_name = tny_folder_get_name (folder);
1594 *list = g_list_append(*list, g_strdup (folder_name));
1597 /* osso-global-search only uses one string,
1598 * so ID is the only thing that could possibly identify a folder.
1599 * TODO: osso-global search should probably be changed to
1600 * take an ID and a Name.
1602 const gchar * id = tny_folder_get_id (folder);
1603 if (id && strlen(id)) {
1604 const gchar *prefix = NULL;
1605 TnyFolderType folder_type;
1607 /* dbus global search api expects a prefix identifying the type of
1608 * folder here. Mandatory folders should have MAND: prefix, and
1609 * other user created folders should have USER: prefix
1611 folder_type = modest_tny_folder_guess_folder_type (folder);
1612 switch (folder_type) {
1613 case TNY_FOLDER_TYPE_INBOX:
1616 case TNY_FOLDER_TYPE_OUTBOX:
1617 case TNY_FOLDER_TYPE_DRAFTS:
1618 case TNY_FOLDER_TYPE_SENT:
1619 case TNY_FOLDER_TYPE_ARCHIVE:
1622 case TNY_FOLDER_TYPE_INVALID:
1623 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1624 return; /* don't add it */
1631 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1636 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1641 /* Add this folder to the list: */
1642 if (TNY_IS_FOLDER (folder_store)) {
1643 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1646 /* Recurse into child folders: */
1648 /* Get the folders list: */
1650 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1651 tny_folder_store_query_add_item (query, NULL,
1652 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1654 TnyList *all_folders = tny_simple_list_new ();
1655 tny_folder_store_get_folders (folder_store,
1660 TnyIterator *iter = tny_list_create_iterator (all_folders);
1661 while (!tny_iterator_is_done (iter)) {
1663 /* Do not recurse, because the osso-global-search UI specification
1664 * does not seem to want the sub-folders, though that spec seems to
1665 * be generally unsuitable for Modest.
1667 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1669 add_single_folder_to_list (TNY_FOLDER (folder), list);
1672 if (TNY_IS_FOLDER_STORE (folder))
1673 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1675 add_single_folder_to_list (TNY_FOLDER (folder), list);
1679 /* tny_iterator_get_current() gave us a reference. */
1680 g_object_unref (folder);
1683 tny_iterator_next (iter);
1685 g_object_unref (G_OBJECT (iter));
1689 /* return >1 for a special folder, 0 for a user-folder */
1691 get_rank (const gchar *folder)
1693 if (strcmp (folder, "INBOX") == 0)
1695 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1697 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1699 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1705 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1707 gint r1 = get_rank (folder1);
1708 gint r2 = get_rank (folder2);
1710 if (r1 > 0 && r2 > 0)
1712 if (r1 > 0 && r2 == 0)
1714 if (r1 == 0 && r2 > 0)
1717 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1721 /* - we're still missing the outbox */
1722 /* - we need to take care of localization (urgh) */
1723 /* - what about 'All mail folders'? */
1725 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1727 DBusMessage *reply = NULL;
1728 ModestAccountMgr *account_mgr = NULL;
1729 gchar *account_name = NULL;
1730 GList *folder_names = NULL;
1731 TnyAccount *account_local = NULL;
1732 TnyAccount *account_mmc = NULL;
1734 /* Get the TnyStoreAccount so we can get the folders: */
1735 account_mgr = modest_runtime_get_account_mgr();
1736 account_name = modest_account_mgr_get_default_account (account_mgr);
1737 if (!account_name) {
1738 g_printerr ("modest: no account found\n");
1742 TnyAccount *account = NULL;
1744 account = modest_tny_account_store_get_server_account (
1745 modest_runtime_get_account_store(), account_name,
1746 TNY_ACCOUNT_TYPE_STORE);
1750 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1753 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1754 g_free (account_name);
1755 account_name = NULL;
1757 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1759 g_object_unref (account);
1763 /* Also add the folders from the local folders account,
1764 * because they are (currently) used with all accounts:
1765 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1768 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1769 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1771 g_object_unref (account_local);
1772 account_local = NULL;
1774 /* Obtain the mmc account */
1776 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1778 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1779 g_object_unref (account_mmc);
1783 /* specs require us to sort the folder names, although
1784 * this is really not the place to do that...
1786 folder_names = g_list_sort (folder_names,
1787 (GCompareFunc)folder_name_compare_func);
1789 /* Put the result in a DBus reply: */
1790 reply = dbus_message_new_method_return (message);
1792 get_folders_result_to_message (reply, folder_names);
1794 if (reply == NULL) {
1795 g_warning ("%s: Could not create reply.", __FUNCTION__);
1799 dbus_uint32_t serial = 0;
1800 dbus_connection_send (con, reply, &serial);
1801 dbus_connection_flush (con);
1802 dbus_message_unref (reply);
1805 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1806 g_list_free (folder_names);
1811 reply_empty_results (DBusConnection *con, DBusMessage *msg)
1813 DBusMessage *reply = dbus_message_new_method_return (msg);
1815 dbus_uint32_t serial = 0;
1816 /* we simply return an empty list, otherwise
1817 global-search gets confused */
1818 search_result_to_message (reply, NULL);
1820 dbus_connection_send (con, reply, &serial);
1821 dbus_connection_flush (con);
1822 dbus_message_unref (reply);
1824 g_warning ("%s: failed to send reply",
1829 /** This D-Bus handler is used when the main osso-rpc
1830 * D-Bus handler has not handled something.
1831 * We use this for D-Bus methods that need to use more complex types
1832 * than osso-rpc supports.
1835 modest_dbus_req_filter (DBusConnection *con,
1836 DBusMessage *message,
1839 gboolean handled = FALSE;
1841 if (dbus_message_is_method_call (message,
1843 MODEST_DBUS_METHOD_SEARCH)) {
1845 /* don't try to search when there not enough mem */
1846 if (modest_platform_check_memory_low (NULL, TRUE)) {
1847 g_warning ("%s: not enough memory for searching",
1849 reply_empty_results (con, message);
1853 on_dbus_method_search (con, message);
1857 } else if (dbus_message_is_method_call (message,
1859 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1860 on_dbus_method_get_folders (con, message);
1862 } else if (dbus_message_is_method_call (message,
1864 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
1865 on_dbus_method_dump_operation_queue (con, message);
1867 } else if (dbus_message_is_method_call (message,
1869 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
1870 on_dbus_method_dump_accounts (con, message);
1872 } else if (dbus_message_is_method_call (message,
1874 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
1875 on_dbus_method_dump_send_queues (con, message);
1878 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
1880 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
1881 __FUNCTION__, dbus_message_get_interface (message),
1882 dbus_message_get_member(message));
1887 DBUS_HANDLER_RESULT_HANDLED :
1888 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1892 notify_error_in_dbus_callback (gpointer user_data)
1894 ModestMailOperation *mail_op;
1895 ModestMailOperationQueue *mail_op_queue;
1897 mail_op = modest_mail_operation_new (NULL);
1898 mail_op_queue = modest_runtime_get_mail_operation_queue ();
1900 /* Issues a noop operation in order to force the queue to emit
1901 the "queue-empty" signal to allow modest to quit */
1902 modest_mail_operation_queue_add (mail_op_queue, mail_op);
1903 modest_mail_operation_noop (mail_op);
1904 g_object_unref (mail_op);
1910 notify_msg_not_found_in_idle (gpointer user_data)
1912 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), FALSE);
1914 g_idle_add (notify_error_in_dbus_callback, NULL);