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-debug.h"
38 #include "modest-search.h"
39 #include "widgets/modest-msg-edit-window.h"
40 #include "modest-tny-msg.h"
41 #include "modest-platform.h"
42 #include <libmodest-dbus-client/libmodest-dbus-client.h>
43 #include <libgnomevfs/gnome-vfs-utils.h>
46 #include <glib/gstdio.h>
47 #ifdef MODEST_HAVE_HILDON0_WIDGETS
48 #include <libgnomevfs/gnome-vfs-mime-utils.h>
50 #include <libgnomevfs/gnome-vfs-mime.h>
52 #include <tny-fs-stream.h>
55 #include <tny-iterator.h>
56 #include <tny-simple-list.h>
57 #include <tny-merge-folder.h>
58 #include <tny-account.h>
60 #include <modest-text-utils.h>
70 } ComposeMailIdleData;
73 static gboolean notify_error_in_dbus_callback (gpointer user_data);
74 static gboolean on_idle_compose_mail(gpointer user_data);
75 static gboolean on_idle_top_application (gpointer user_data);
78 * @uri An escaped URI. URIs should always be escaped.
79 * @len The length of the @uri string, or -1 if the string is null terminated.
81 * Decode a URI, or URI fragment, as per RFC 1738.
82 * http://www.ietf.org/rfc/rfc1738.txt
84 * Return value: An unescaped string. This should be freed with g_free().
87 uri_unescape(const gchar* uri, size_t len)
95 /* Allocate an extra string so we can be sure that it is null-terminated,
96 * so we can use gnome_vfs_unescape_string().
97 * This is not efficient. */
98 gchar * escaped_nullterminated = g_strndup (uri, len);
99 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
100 g_free (escaped_nullterminated);
105 /** uri_parse_mailto:
106 * @mailto A mailto URI, with the mailto: prefix.
107 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
108 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
109 * all the string items have been freed. This parameter may be NULL.
110 * Parse a mailto URI as per RFC2368.
111 * http://www.ietf.org/rfc/rfc2368.txt
113 * Return value: The to address, unescaped. This should be freed with g_free().
116 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
118 /* The URL must begin with mailto: */
119 if (strncmp (mailto, "mailto:", 7) != 0) {
122 const gchar* start_to = mailto + 7;
124 /* Look for ?, or the end of the string, marking the end of the to address: */
125 const size_t len_to = strcspn (start_to, "?");
126 gchar* result_to = uri_unescape (start_to, len_to);
127 printf("debug: result_to=%s\n", result_to);
129 if (list_items_and_values == NULL) {
133 /* Get any other items: */
134 const size_t len_mailto = strlen (start_to);
135 const gchar* p = start_to + len_to + 1; /* parsed so far. */
136 const gchar* end = start_to + len_mailto;
138 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
140 name_end = strchr (name_start, '='); /* Separator between name and value */
141 if (name_end == NULL) {
142 g_debug ("Malformed URI: %s\n", mailto);
145 value_start = name_end + 1;
146 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
148 name = g_strndup(name_start, name_end - name_start);
149 if (value_end != NULL) {
150 value = uri_unescape(value_start, value_end - value_start);
153 value = uri_unescape(value_start, -1);
156 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
157 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
164 check_and_offer_account_creation()
166 gboolean result = TRUE;
168 /* This is called from idle handlers, so lock gdk: */
169 gdk_threads_enter ();
171 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
172 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
174 g_debug ("modest: %s: no account exists even after offering, "
175 "or account setup was already underway.\n", __FUNCTION__);
180 gdk_threads_leave ();
186 on_idle_mail_to(gpointer user_data)
188 gchar *uri = (gchar*)user_data;
189 GSList *list_names_and_values = NULL;
191 const gchar *cc = NULL;
192 const gchar *bcc = NULL;
193 const gchar *subject = NULL;
194 const gchar *body = NULL;
196 if (!check_and_offer_account_creation ()) {
197 g_idle_add (notify_error_in_dbus_callback, NULL);
201 /* Get the relevant items from the list: */
202 to = uri_parse_mailto (uri, &list_names_and_values);
203 GSList *list = list_names_and_values;
205 GSList *list_value = g_slist_next (list);
206 const gchar * name = (const gchar*)list->data;
207 const gchar * value = (const gchar*)list_value->data;
209 if (strcmp (name, "cc") == 0) {
211 } else if (strcmp (name, "bcc") == 0) {
213 } else if (strcmp (name, "subject") == 0) {
215 } else if (strcmp (name, "body") == 0) {
219 list = g_slist_next (list_value);
222 gdk_threads_enter (); /* CHECKED */
223 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
224 gdk_threads_leave (); /* CHECKED */
227 /* Free the to: and the list, as required by uri_parse_mailto() */
229 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
230 g_slist_free (list_names_and_values);
234 return FALSE; /* Do not call this callback again. */
238 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
244 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
245 uri = g_strdup (val.value.s);
247 g_idle_add(on_idle_mail_to, (gpointer)uri);
249 /* Note that we cannot report failures during sending,
250 * because that would be asynchronous. */
256 on_idle_compose_mail(gpointer user_data)
258 GSList *attachments = NULL;
259 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
261 if (!check_and_offer_account_creation ()) {
262 g_idle_add (notify_error_in_dbus_callback, NULL);
266 /* it seems Sketch at least sends a leading ',' -- take that into account,
268 if (idle_data->attachments && idle_data->attachments[0]==',') {
269 gchar *tmp = g_strdup (idle_data->attachments + 1);
270 g_free(idle_data->attachments);
271 idle_data->attachments = tmp;
274 if (idle_data->attachments != NULL) {
275 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
277 for (i=0; list[i] != NULL; i++) {
278 attachments = g_slist_append(attachments, g_strdup(list[i]));
283 /* If the message has nothing then mark the buffers as not
284 modified. This happens in Maemo for example when opening a
285 new message from Contacts plugin, it sends "" instead of
287 gdk_threads_enter (); /* CHECKED */
288 if (!strncmp (idle_data->to, "", 1) &&
289 !strncmp (idle_data->to, "", 1) &&
290 !strncmp (idle_data->cc, "", 1) &&
291 !strncmp (idle_data->bcc, "", 1) &&
292 !strncmp (idle_data->subject, "", 1) &&
293 !strncmp (idle_data->body, "", 1) &&
294 attachments == NULL) {
295 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
297 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
298 idle_data->bcc, idle_data->subject,
299 idle_data->body, attachments, TRUE);
301 gdk_threads_leave (); /* CHECKED */
303 g_slist_foreach(attachments, (GFunc)g_free, NULL);
304 g_slist_free(attachments);
305 g_free (idle_data->to);
306 g_free (idle_data->cc);
307 g_free (idle_data->bcc);
308 g_free (idle_data->subject);
309 g_free (idle_data->body);
310 g_free (idle_data->attachments);
313 return FALSE; /* Do not call this callback again. */
317 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
319 ComposeMailIdleData *idle_data;
322 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
324 /* Get the arguments: */
325 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
326 idle_data->to = g_strdup (val.value.s);
328 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
329 idle_data->cc = g_strdup (val.value.s);
331 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
332 idle_data->bcc = g_strdup (val.value.s);
334 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
335 idle_data->subject = g_strdup (val.value.s);
337 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
338 idle_data->body = g_strdup (val.value.s);
340 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
341 idle_data->attachments = g_strdup (val.value.s);
343 /* Use g_idle to context-switch into the application's thread: */
344 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
350 find_message_by_url (const char *uri, TnyAccount **ac_out)
352 ModestTnyAccountStore *astore;
353 TnyAccount *account = NULL;
354 TnyFolder *folder = NULL;
357 astore = modest_runtime_get_account_store ();
362 if (uri && g_str_has_prefix (uri, "merge://")) {
363 /* we assume we're talking about outbox folder, as this
364 * is the only merge folder we work with in modest */
365 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
367 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
370 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
374 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
379 msg = tny_folder_find_msg (folder, uri, NULL);
382 if (account && !msg) {
383 g_object_unref (account);
387 g_object_unref (folder);
396 guint animation_timeout;
397 GtkWidget *animation;
398 } OpenMsgPerformerInfo;
401 on_show_opening_animation (gpointer userdata)
403 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
404 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
405 info->animation_timeout = 0;
411 on_find_msg_async_destroy (gpointer userdata)
413 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
415 if (info->animation_timeout>0) {
416 g_source_remove (info->animation_timeout);
417 info->animation_timeout = 0;
420 if (info->animation) {
421 gtk_widget_destroy (info->animation);
422 info->animation = NULL;
429 g_object_unref (info->account);
431 g_slice_free (OpenMsgPerformerInfo, info);
436 find_msg_async_cb (TnyFolder *folder,
444 ModestWindowMgr *win_mgr;
445 ModestWindow *msg_view = NULL;
446 gboolean is_draft = FALSE;
447 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
449 if (err || cancelled) {
450 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
451 g_idle_add (notify_error_in_dbus_callback, NULL);
455 header = tny_msg_get_header (msg);
456 if (header && (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
457 g_object_unref (header);
458 g_object_unref (msg);
459 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
460 g_idle_add (notify_error_in_dbus_callback, NULL);
464 msg_uid = modest_tny_folder_get_header_unique_id (header);
465 win_mgr = modest_runtime_get_window_mgr ();
467 if (modest_tny_folder_is_local_folder (folder) &&
468 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
472 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
473 gtk_window_present (GTK_WINDOW(msg_view));
475 const gchar *modest_account_name;
478 modest_window_mgr_register_header (win_mgr, header, NULL);
480 account = tny_folder_get_account (folder);
482 modest_account_name =
483 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
485 modest_account_name = NULL;
488 /* Drafts will be opened in the editor, and others will be opened in the viewer */
490 gchar *modest_account_name = NULL;
493 /* we cannot edit without a valid account... */
494 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
495 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
496 modest_window_mgr_unregister_header (win_mgr,
502 from_header = tny_header_dup_from (header);
504 GSList *accounts = modest_account_mgr_account_names (modest_runtime_get_account_mgr (), TRUE);
506 for (node = accounts; node != NULL; node = g_slist_next (node)) {
507 gchar *from = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr (), node->data);
509 if (from && (strcmp (from_header, from) == 0)) {
510 g_free (modest_account_name);
511 modest_account_name = g_strdup (node->data);
517 g_slist_foreach (accounts, (GFunc) g_free, NULL);
518 g_slist_free (accounts);
519 g_free (from_header);
522 if (modest_account_name == NULL) {
523 modest_account_name = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
525 msg_view = modest_msg_edit_window_new (msg, modest_account_name, 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, 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 modest_window_mgr_register_window (win_mgr, msg_view);
556 gtk_widget_show_all (GTK_WIDGET (msg_view));
561 g_object_unref (header);
562 g_object_unref (msg);
565 on_find_msg_async_destroy (info);
570 on_open_message_performer (gboolean canceled,
572 GtkWindow *parent_window,
576 OpenMsgPerformerInfo *info;
577 TnyFolder *folder = NULL;
579 info = (OpenMsgPerformerInfo *) user_data;
580 if (canceled || err) {
581 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
582 g_idle_add (notify_error_in_dbus_callback, NULL);
583 on_find_msg_async_destroy (info);
589 ModestTnyAccountStore *account_store;
590 ModestTnyLocalFoldersAccount *local_folders_account;
592 account_store = modest_runtime_get_account_store ();
593 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
594 modest_tny_account_store_get_local_folders_account (account_store));
595 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
596 g_object_unref (local_folders_account);
598 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
601 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
602 g_idle_add (notify_error_in_dbus_callback, NULL);
603 on_find_msg_async_destroy (info);
607 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
609 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
610 g_object_unref (folder);
614 on_idle_open_message_performer (gpointer user_data)
616 ModestWindow *main_win = NULL;
617 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
619 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
620 FALSE); /* don't create */
622 /* Lock before the call as we're in an idle handler */
623 gdk_threads_enter ();
625 modest_platform_connect_and_perform (GTK_WINDOW (main_win), TRUE, info->account,
626 on_open_message_performer, info);
628 on_open_message_performer (FALSE, NULL, GTK_WINDOW (main_win), info->account, info);
630 gdk_threads_leave ();
636 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
640 TnyAccount *account = NULL;
644 /* Get the arguments: */
645 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
646 uri = g_strdup (val.value.s);
648 is_merge = g_str_has_prefix (uri, "merge:");
650 /* Get the account */
652 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
656 if (is_merge || account) {
657 OpenMsgPerformerInfo *info;
658 TnyFolder *folder = NULL;
660 info = g_slice_new0 (OpenMsgPerformerInfo);
662 info->account = g_object_ref (account);
664 info->connect = TRUE;
665 info->animation = NULL;
666 info->animation_timeout = 0;
668 /* Try to get the message, if it's already downloaded
669 we don't need to connect */
671 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
673 ModestTnyAccountStore *account_store;
674 ModestTnyLocalFoldersAccount *local_folders_account;
676 account_store = modest_runtime_get_account_store ();
677 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
678 modest_tny_account_store_get_local_folders_account (account_store));
679 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
680 g_object_unref (local_folders_account);
684 gboolean device_online;
686 device = modest_runtime_get_device();
687 device_online = tny_device_is_online (device);
689 info->connect = TRUE;
691 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
693 info->connect = FALSE;
694 g_object_unref (msg);
696 info->connect = TRUE;
699 g_object_unref (folder);
702 /* We need to call it into an idle to get
703 modest_platform_connect_and_perform into the main
705 g_idle_add (on_idle_open_message_performer, info);
706 osso_retval = OSSO_OK;
709 osso_retval = OSSO_ERROR;
710 g_idle_add (notify_error_in_dbus_callback, NULL);
714 g_object_unref (account);
719 on_remove_msgs_finished (ModestMailOperation *mail_op,
723 ModestWindow *main_win = NULL, *msg_view = NULL;
724 ModestHeaderView *header_view;
726 header = (TnyHeader *) user_data;
728 /* Get the main window if exists */
729 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
730 FALSE); /* don't create */
732 g_object_unref (header);
736 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
737 header, &msg_view)) {
738 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
739 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
741 g_object_unref (header);
743 /* Refilter the header view explicitly */
744 header_view = (ModestHeaderView *)
745 modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(main_win),
746 MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW);
747 if (header_view && MODEST_IS_HEADER_VIEW (header_view))
748 modest_header_view_refilter (header_view);
752 on_idle_delete_message (gpointer user_data)
754 TnyList *headers = NULL, *tmp_headers = NULL;
755 TnyFolder *folder = NULL;
756 TnyIterator *iter = NULL;
757 TnyHeader *header = NULL, *msg_header = NULL;
759 TnyAccount *account = NULL;
760 const char *uri = NULL;
762 ModestMailOperation *mail_op = NULL;
763 ModestWindow *main_win = NULL;
765 uri = (char *) user_data;
767 msg = find_message_by_url (uri, &account);
769 g_object_unref (account);
772 g_warning ("%s: Could not find message '%s'", __FUNCTION__, uri);
773 g_idle_add (notify_error_in_dbus_callback, NULL);
777 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr(),
778 FALSE); /* don't create */
780 folder = tny_msg_get_folder (msg);
782 g_warning ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
783 g_object_unref (msg);
784 g_idle_add (notify_error_in_dbus_callback, NULL);
789 msg_header = tny_msg_get_header (msg);
790 uid = tny_header_dup_uid (msg_header);
791 g_object_unref (msg);
792 g_object_unref (msg_header);
794 headers = tny_simple_list_new ();
795 tny_folder_get_headers (folder, headers, TRUE, NULL);
796 iter = tny_list_create_iterator (headers);
798 while (!tny_iterator_is_done (iter)) {
799 gchar *cur_id = NULL;
801 header = TNY_HEADER (tny_iterator_get_current (iter));
803 cur_id = tny_header_dup_uid (header);
805 if (cur_id && uid && g_str_equal (cur_id, uid)) {
807 /* g_debug ("Found corresponding header from folder"); */
813 g_object_unref (header);
817 tny_iterator_next (iter);
820 g_object_unref (iter);
821 g_object_unref (headers);
823 if (header == NULL) {
825 g_object_unref (folder);
826 g_idle_add (notify_error_in_dbus_callback, NULL);
830 /* This is a GDK lock because we are an idle callback and
831 * the code below is or does Gtk+ code */
832 gdk_threads_enter (); /* CHECKED */
834 mail_op = modest_mail_operation_new (main_win ? G_OBJECT(main_win) : NULL);
835 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
837 g_signal_connect (G_OBJECT (mail_op),
838 "operation-finished",
839 G_CALLBACK (on_remove_msgs_finished),
840 g_object_ref (header));
842 tmp_headers = tny_simple_list_new ();
843 tny_list_append (tmp_headers, (GObject *) header);
845 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
847 g_object_unref (tmp_headers);
848 g_object_unref (G_OBJECT (mail_op));
849 gdk_threads_leave (); /* CHECKED */
853 g_object_unref (header);
862 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
864 /* Get the arguments: */
865 osso_rpc_t val = g_array_index (arguments,
867 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
868 gchar *uri = g_strdup (val.value.s);
870 /* Use g_idle to context-switch into the application's thread: */
871 g_idle_add(on_idle_delete_message, (gpointer)uri);
877 on_idle_send_receive(gpointer user_data)
879 gboolean auto_update;
880 ModestWindow *main_win = NULL;
883 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
884 FALSE); /* don't create */
886 gdk_threads_enter (); /* CHECKED */
888 /* Check if the autoupdate feature is on */
889 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
890 MODEST_CONF_AUTO_UPDATE, NULL);
893 /* Do send receive */
894 modest_ui_actions_do_send_receive_all (main_win, FALSE, FALSE, FALSE);
896 /* Disable auto update */
897 modest_platform_set_update_interval (0);
899 gdk_threads_leave (); /* CHECKED */
907 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
912 dbus_uint32_t serial = 0;
914 GSList *account_names, *cursor;
916 str = g_strdup("\nsend queues\n"
919 cursor = account_names = modest_account_mgr_account_names
920 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
924 gchar *tmp, *accname = (gchar*)cursor->data;
926 tmp = g_strdup_printf ("%s", str);
931 acc = modest_tny_account_store_get_server_account (
932 modest_runtime_get_account_store(), accname,
933 TNY_ACCOUNT_TYPE_TRANSPORT);
934 if (TNY_IS_ACCOUNT(acc)) {
935 gchar *tmp, *url = tny_account_get_url_string (acc);
936 ModestTnySendQueue *sendqueue =
937 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
938 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
940 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
941 str, accname, tny_account_get_id (acc), url,
948 g_object_unref (acc);
951 cursor = g_slist_next (cursor);
953 modest_account_mgr_free_account_names (account_names);
957 reply = dbus_message_new_method_return (message);
959 dbus_message_append_args (reply,
960 DBUS_TYPE_STRING, &str,
962 dbus_connection_send (con, reply, &serial);
963 dbus_connection_flush (con);
964 dbus_message_unref (reply);
969 g_idle_add (notify_error_in_dbus_callback, NULL);
976 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
982 dbus_uint32_t serial = 0;
984 /* operations queue; */
985 op_queue_str = modest_mail_operation_queue_to_string
986 (modest_runtime_get_mail_operation_queue ());
988 str = g_strdup_printf ("\noperation queue\n"
992 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
994 g_free (op_queue_str);
998 reply = dbus_message_new_method_return (message);
1000 dbus_message_append_args (reply,
1001 DBUS_TYPE_STRING, &str,
1003 dbus_connection_send (con, reply, &serial);
1004 dbus_connection_flush (con);
1005 dbus_message_unref (reply);
1009 /* Let modest die */
1010 g_idle_add (notify_error_in_dbus_callback, NULL);
1018 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1023 dbus_uint32_t serial = 0;
1025 GSList *account_names, *cursor;
1027 str = g_strdup ("\naccounts\n========\n");
1029 cursor = account_names = modest_account_mgr_account_names
1030 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1034 gchar *tmp, *accname = (gchar*)cursor->data;
1036 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1041 acc = modest_tny_account_store_get_server_account (
1042 modest_runtime_get_account_store(), accname,
1043 TNY_ACCOUNT_TYPE_STORE);
1044 if (TNY_IS_ACCOUNT(acc)) {
1045 gchar *tmp, *url = tny_account_get_url_string (acc);
1046 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1047 str, tny_account_get_id (acc), url,
1048 ((GObject*)acc)->ref_count-1);
1052 g_object_unref (acc);
1056 acc = modest_tny_account_store_get_server_account (
1057 modest_runtime_get_account_store(), accname,
1058 TNY_ACCOUNT_TYPE_TRANSPORT);
1059 if (TNY_IS_ACCOUNT(acc)) {
1060 gchar *tmp, *url = tny_account_get_url_string (acc);
1061 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1062 str, tny_account_get_id (acc), url,
1063 ((GObject*)acc)->ref_count-1);
1067 g_object_unref (acc);
1070 cursor = g_slist_next (cursor);
1073 modest_account_mgr_free_account_names (account_names);
1077 reply = dbus_message_new_method_return (message);
1079 dbus_message_append_args (reply,
1080 DBUS_TYPE_STRING, &str,
1082 dbus_connection_send (con, reply, &serial);
1083 dbus_connection_flush (con);
1084 dbus_message_unref (reply);
1088 /* Let modest die */
1089 g_idle_add (notify_error_in_dbus_callback, NULL);
1095 on_send_receive_performer(gboolean canceled,
1097 GtkWindow *parent_window,
1098 TnyAccount *account,
1101 ModestConnectedVia connect_when;
1103 if (err || canceled) {
1104 g_idle_add (notify_error_in_dbus_callback, NULL);
1108 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
1109 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
1111 /* Perform a send and receive if the user selected to connect
1112 via any mean or if the current connection method is the
1113 same as the one specified by the user */
1114 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
1115 connect_when == modest_platform_get_current_connection ()) {
1116 g_idle_add (on_idle_send_receive, NULL);
1118 /* We need this to allow modest to finish */
1119 g_idle_add (notify_error_in_dbus_callback, NULL);
1125 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1127 TnyDevice *device = modest_runtime_get_device ();
1129 if (!tny_device_is_online (device))
1130 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, NULL);
1132 on_send_receive_performer (FALSE, NULL, NULL, NULL, NULL);
1138 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1140 g_idle_add(on_idle_top_application, NULL);
1147 on_idle_top_application (gpointer user_data)
1149 ModestWindow *main_win;
1151 /* This is a GDK lock because we are an idle callback and
1152 * the code below is or does Gtk+ code */
1154 gdk_threads_enter (); /* CHECKED */
1156 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1157 TRUE); /* create if non-existent */
1159 /* Ideally, we would just use gtk_widget_show(),
1160 * but this widget is not coded correctly to support that: */
1161 gtk_widget_show_all (GTK_WIDGET (main_win));
1162 gtk_window_present (GTK_WINDOW (main_win));
1164 g_warning ("%s: BUG: no main window", __FUNCTION__);
1166 gdk_threads_leave (); /* CHECKED */
1168 return FALSE; /* Do not call this callback again. */
1172 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1174 /* Use g_idle to context-switch into the application's thread: */
1175 g_idle_add(on_idle_top_application, NULL);
1181 on_idle_show_memory_low (gpointer user_data)
1183 ModestWindow *main_win = NULL;
1185 gdk_threads_enter ();
1186 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (), FALSE);
1187 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1188 dgettext("ke-recv","memr_ib_operation_disabled"),
1190 gdk_threads_leave ();
1195 /* Callback for normal D-BUS messages */
1197 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1198 GArray * arguments, gpointer data,
1199 osso_rpc_t * retval)
1201 /* Check memory low conditions */
1202 if (modest_platform_check_memory_low (NULL, FALSE)) {
1203 g_idle_add (on_idle_show_memory_low, NULL);
1207 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1208 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1210 return on_mail_to (arguments, data, retval);
1211 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1212 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1214 return on_open_message (arguments, data, retval);
1215 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1216 if (arguments->len != 0)
1218 return on_send_receive (arguments, data, retval);
1219 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1220 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1222 return on_compose_mail (arguments, data, retval);
1223 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1224 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1226 return on_delete_message (arguments,data, retval);
1227 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1228 if (arguments->len != 0)
1230 return on_open_default_inbox (arguments, data, retval);
1231 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1232 if (arguments->len != 0)
1234 return on_top_application (arguments, data, retval);
1236 /* We need to return INVALID here so
1237 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1238 * so that our modest_dbus_req_filter will then be tried instead.
1240 return OSSO_INVALID;
1243 /* Notify error in D-Bus method */
1244 g_idle_add (notify_error_in_dbus_callback, NULL);
1248 /* A complex D-Bus type (like a struct),
1249 * used to return various information about a search hit.
1251 #define SEARCH_HIT_DBUS_TYPE \
1252 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1253 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1254 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1255 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1256 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1257 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1258 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1259 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1260 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1261 DBUS_STRUCT_END_CHAR_AS_STRING
1263 static DBusMessage *
1264 search_result_to_message (DBusMessage *reply,
1267 DBusMessageIter iter;
1268 DBusMessageIter array_iter;
1271 dbus_message_iter_init_append (reply, &iter);
1272 dbus_message_iter_open_container (&iter,
1274 SEARCH_HIT_DBUS_TYPE,
1277 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1278 DBusMessageIter struct_iter;
1279 ModestSearchResultHit *hit;
1281 const char *subject;
1285 gboolean has_attachment;
1289 hit = (ModestSearchResultHit *) hit_iter->data;
1291 msg_url = hit->msgid;
1292 subject = hit->subject;
1293 folder = hit->folder;
1294 sender = hit->sender;
1296 has_attachment = hit->has_attachment;
1297 is_unread = hit->is_unread;
1298 ts = hit->timestamp;
1300 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1302 dbus_message_iter_open_container (&array_iter,
1307 dbus_message_iter_append_basic (&struct_iter,
1311 dbus_message_iter_append_basic (&struct_iter,
1315 dbus_message_iter_append_basic (&struct_iter,
1319 dbus_message_iter_append_basic (&struct_iter,
1323 dbus_message_iter_append_basic (&struct_iter,
1327 dbus_message_iter_append_basic (&struct_iter,
1331 dbus_message_iter_append_basic (&struct_iter,
1335 dbus_message_iter_append_basic (&struct_iter,
1339 dbus_message_iter_close_container (&array_iter,
1342 g_free (hit->msgid);
1343 g_free (hit->subject);
1344 g_free (hit->folder);
1345 g_free (hit->sender);
1347 g_slice_free (ModestSearchResultHit, hit);
1350 dbus_message_iter_close_container (&iter, &array_iter);
1357 DBusConnection *con;
1358 DBusMessage *message;
1359 ModestSearch *search;
1363 search_all_cb (GList *hits, gpointer user_data)
1366 SearchHelper *helper = (SearchHelper *) user_data;
1368 reply = dbus_message_new_method_return (helper->message);
1371 dbus_uint32_t serial = 0;
1373 search_result_to_message (reply, hits);
1375 dbus_connection_send (helper->con, reply, &serial);
1376 dbus_connection_flush (helper->con);
1377 dbus_message_unref (reply);
1380 /* Free the helper */
1381 dbus_message_unref (helper->message);
1382 modest_search_free (helper->search);
1383 g_slice_free (ModestSearch, helper->search);
1384 g_slice_free (SearchHelper, helper);
1388 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1390 ModestDBusSearchFlags dbus_flags;
1394 dbus_int32_t flags_v;
1395 dbus_uint32_t size_v;
1400 ModestSearch *search;
1403 dbus_error_init (&error);
1408 res = dbus_message_get_args (message,
1410 DBUS_TYPE_STRING, &query,
1411 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1412 DBUS_TYPE_INT64, &sd_v,
1413 DBUS_TYPE_INT64, &ed_v,
1414 DBUS_TYPE_INT32, &flags_v,
1415 DBUS_TYPE_UINT32, &size_v,
1418 dbus_flags = (ModestDBusSearchFlags) flags_v;
1419 start_date = (time_t) sd_v;
1420 end_date = (time_t) ed_v;
1422 search = g_slice_new0 (ModestSearch);
1424 if (folder && g_str_has_prefix (folder, "MAND:")) {
1425 search->folder = g_strdup (folder + strlen ("MAND:"));
1426 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1427 search->folder = g_strdup (folder + strlen ("USER:"));
1428 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1429 search->folder = g_strdup (folder + strlen ("MY:"));
1431 search->folder = g_strdup (folder);
1434 /* Remember the text to search for: */
1435 #ifdef MODEST_HAVE_OGS
1436 search->query = g_strdup (query);
1439 /* Other criteria: */
1440 search->start_date = start_date;
1441 search->end_date = end_date;
1444 /* Text to serach for in various parts of the message: */
1445 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1446 search->flags |= MODEST_SEARCH_SUBJECT;
1447 search->subject = g_strdup (query);
1450 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1451 search->flags |= MODEST_SEARCH_SENDER;
1452 search->from = g_strdup (query);
1455 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1456 search->flags |= MODEST_SEARCH_RECIPIENT;
1457 search->recipient = g_strdup (query);
1460 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1461 search->flags |= MODEST_SEARCH_BODY;
1462 search->body = g_strdup (query);
1466 search->flags |= MODEST_SEARCH_BEFORE;
1467 search->start_date = start_date;
1471 search->flags |= MODEST_SEARCH_AFTER;
1472 search->end_date = end_date;
1476 search->flags |= MODEST_SEARCH_SIZE;
1477 search->minsize = size_v;
1480 #ifdef MODEST_HAVE_OGS
1481 search->flags |= MODEST_SEARCH_USE_OGS;
1482 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1485 SearchHelper *helper = g_slice_new (SearchHelper);
1486 helper->search = search;
1487 dbus_message_ref (message);
1488 helper->message = message;
1491 /* Search asynchronously */
1492 modest_search_all_accounts (search, search_all_cb, helper);
1496 /* A complex D-Bus type (like a struct),
1497 * used to return various information about a folder.
1499 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1500 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1501 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1502 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1503 DBUS_STRUCT_END_CHAR_AS_STRING
1505 static DBusMessage *
1506 get_folders_result_to_message (DBusMessage *reply,
1509 DBusMessageIter iter;
1510 dbus_message_iter_init_append (reply, &iter);
1512 DBusMessageIter array_iter;
1513 dbus_message_iter_open_container (&iter,
1515 GET_FOLDERS_RESULT_DBUS_TYPE,
1518 GList *list_iter = folder_ids;
1519 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1521 const gchar *folder_name = (const gchar*)list_iter->data;
1523 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1525 DBusMessageIter struct_iter;
1526 dbus_message_iter_open_container (&array_iter,
1532 dbus_message_iter_append_basic (&struct_iter,
1534 &folder_name); /* The string will be copied. */
1536 /* URI: This is maybe not needed by osso-global-search: */
1537 const gchar *folder_uri = "TODO:unimplemented";
1538 dbus_message_iter_append_basic (&struct_iter,
1540 &folder_uri); /* The string will be copied. */
1542 dbus_message_iter_close_container (&array_iter,
1547 dbus_message_iter_close_container (&iter, &array_iter);
1553 add_single_folder_to_list (TnyFolder *folder, GList** list)
1558 if (TNY_IS_MERGE_FOLDER (folder)) {
1559 const gchar * folder_name;
1560 /* Ignore these because their IDs ares
1561 * a) not always unique or sensible.
1562 * b) not human-readable, and currently need a human-readable
1563 * ID here, because the osso-email-interface API does not allow
1564 * us to return both an ID and a display name.
1566 * This is actually the merged outbox folder.
1567 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1568 * but that seems unwise. murrayc.
1570 folder_name = tny_folder_get_name (folder);
1571 if (folder_name && !strcmp (folder_name, "Outbox")) {
1572 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1577 /* Add this folder to the list: */
1579 const gchar * folder_name = tny_folder_get_name (folder);
1581 *list = g_list_append(*list, g_strdup (folder_name));
1584 /* osso-global-search only uses one string,
1585 * so ID is the only thing that could possibly identify a folder.
1586 * TODO: osso-global search should probably be changed to
1587 * take an ID and a Name.
1589 const gchar * id = tny_folder_get_id (folder);
1590 if (id && strlen(id)) {
1591 const gchar *prefix = NULL;
1592 TnyFolderType folder_type;
1594 /* dbus global search api expects a prefix identifying the type of
1595 * folder here. Mandatory folders should have MAND: prefix, and
1596 * other user created folders should have USER: prefix
1598 folder_type = modest_tny_folder_guess_folder_type (folder);
1599 switch (folder_type) {
1600 case TNY_FOLDER_TYPE_INBOX:
1603 case TNY_FOLDER_TYPE_OUTBOX:
1604 case TNY_FOLDER_TYPE_DRAFTS:
1605 case TNY_FOLDER_TYPE_SENT:
1606 case TNY_FOLDER_TYPE_ARCHIVE:
1609 case TNY_FOLDER_TYPE_INVALID:
1610 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1611 return; /* don't add it */
1618 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1623 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1628 /* Add this folder to the list: */
1629 if (TNY_IS_FOLDER (folder_store)) {
1630 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1633 /* Recurse into child folders: */
1635 /* Get the folders list: */
1637 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1638 tny_folder_store_query_add_item (query, NULL,
1639 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1641 TnyList *all_folders = tny_simple_list_new ();
1642 tny_folder_store_get_folders (folder_store,
1647 TnyIterator *iter = tny_list_create_iterator (all_folders);
1648 while (!tny_iterator_is_done (iter)) {
1650 /* Do not recurse, because the osso-global-search UI specification
1651 * does not seem to want the sub-folders, though that spec seems to
1652 * be generally unsuitable for Modest.
1654 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1656 add_single_folder_to_list (TNY_FOLDER (folder), list);
1659 if (TNY_IS_FOLDER_STORE (folder))
1660 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1662 add_single_folder_to_list (TNY_FOLDER (folder), list);
1666 /* tny_iterator_get_current() gave us a reference. */
1667 g_object_unref (folder);
1670 tny_iterator_next (iter);
1672 g_object_unref (G_OBJECT (iter));
1676 /* return >1 for a special folder, 0 for a user-folder */
1678 get_rank (const gchar *folder)
1680 if (strcmp (folder, "INBOX") == 0)
1682 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1684 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1686 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1692 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1694 gint r1 = get_rank (folder1);
1695 gint r2 = get_rank (folder2);
1697 if (r1 > 0 && r2 > 0)
1699 if (r1 > 0 && r2 == 0)
1701 if (r1 == 0 && r2 > 0)
1704 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1708 /* - we're still missing the outbox */
1709 /* - we need to take care of localization (urgh) */
1710 /* - what about 'All mail folders'? */
1712 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1714 DBusMessage *reply = NULL;
1715 ModestAccountMgr *account_mgr = NULL;
1716 gchar *account_name = NULL;
1717 GList *folder_names = NULL;
1718 TnyAccount *account_local = NULL;
1719 TnyAccount *account_mmc = NULL;
1721 /* Get the TnyStoreAccount so we can get the folders: */
1722 account_mgr = modest_runtime_get_account_mgr();
1723 account_name = modest_account_mgr_get_default_account (account_mgr);
1724 if (!account_name) {
1725 g_printerr ("modest: no account found\n");
1729 TnyAccount *account = NULL;
1731 account = modest_tny_account_store_get_server_account (
1732 modest_runtime_get_account_store(), account_name,
1733 TNY_ACCOUNT_TYPE_STORE);
1737 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1740 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1741 g_free (account_name);
1742 account_name = NULL;
1744 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1746 g_object_unref (account);
1750 /* Also add the folders from the local folders account,
1751 * because they are (currently) used with all accounts:
1752 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1755 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1756 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1758 g_object_unref (account_local);
1759 account_local = NULL;
1761 /* Obtain the mmc account */
1763 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1765 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1766 g_object_unref (account_mmc);
1770 /* specs require us to sort the folder names, although
1771 * this is really not the place to do that...
1773 folder_names = g_list_sort (folder_names,
1774 (GCompareFunc)folder_name_compare_func);
1776 /* Put the result in a DBus reply: */
1777 reply = dbus_message_new_method_return (message);
1779 get_folders_result_to_message (reply, folder_names);
1781 if (reply == NULL) {
1782 g_warning ("%s: Could not create reply.", __FUNCTION__);
1786 dbus_uint32_t serial = 0;
1787 dbus_connection_send (con, reply, &serial);
1788 dbus_connection_flush (con);
1789 dbus_message_unref (reply);
1792 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1793 g_list_free (folder_names);
1798 reply_empty_results (DBusConnection *con, DBusMessage *msg)
1800 DBusMessage *reply = dbus_message_new_method_return (msg);
1802 dbus_uint32_t serial = 0;
1803 /* we simply return an empty list, otherwise
1804 global-search gets confused */
1805 search_result_to_message (reply, NULL);
1807 dbus_connection_send (con, reply, &serial);
1808 dbus_connection_flush (con);
1809 dbus_message_unref (reply);
1811 g_warning ("%s: failed to send reply",
1816 /** This D-Bus handler is used when the main osso-rpc
1817 * D-Bus handler has not handled something.
1818 * We use this for D-Bus methods that need to use more complex types
1819 * than osso-rpc supports.
1822 modest_dbus_req_filter (DBusConnection *con,
1823 DBusMessage *message,
1826 gboolean handled = FALSE;
1828 if (dbus_message_is_method_call (message,
1830 MODEST_DBUS_METHOD_SEARCH)) {
1832 /* don't try to search when there not enough mem */
1833 if (modest_platform_check_memory_low (NULL, TRUE)) {
1834 g_warning ("%s: not enough memory for searching",
1836 reply_empty_results (con, message);
1840 on_dbus_method_search (con, message);
1844 } else if (dbus_message_is_method_call (message,
1846 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1847 on_dbus_method_get_folders (con, message);
1849 } else if (dbus_message_is_method_call (message,
1851 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
1852 on_dbus_method_dump_operation_queue (con, message);
1854 } else if (dbus_message_is_method_call (message,
1856 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
1857 on_dbus_method_dump_accounts (con, message);
1859 } else if (dbus_message_is_method_call (message,
1861 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
1862 on_dbus_method_dump_send_queues (con, message);
1865 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
1867 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
1868 __FUNCTION__, dbus_message_get_interface (message),
1869 dbus_message_get_member(message));
1874 DBUS_HANDLER_RESULT_HANDLED :
1875 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1879 notify_error_in_dbus_callback (gpointer user_data)
1881 ModestMailOperation *mail_op;
1882 ModestMailOperationQueue *mail_op_queue;
1884 mail_op = modest_mail_operation_new (NULL);
1885 mail_op_queue = modest_runtime_get_mail_operation_queue ();
1887 /* Issues a noop operation in order to force the queue to emit
1888 the "queue-empty" signal to allow modest to quit */
1889 modest_mail_operation_queue_add (mail_op_queue, mail_op);
1890 modest_mail_operation_noop (mail_op);
1891 g_object_unref (mail_op);