1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-dbus-callbacks.h"
31 #include "modest-runtime.h"
32 #include "modest-account-mgr.h"
33 #include "modest-account-mgr-helpers.h"
34 #include "modest-tny-account.h"
35 #include "modest-tny-folder.h"
36 #include "modest-ui-actions.h"
37 #include "modest-utils.h"
38 #include "modest-debug.h"
39 #include "modest-search.h"
40 #include "widgets/modest-msg-edit-window.h"
41 #include "widgets/modest-window-mgr.h"
42 #include "modest-tny-msg.h"
43 #include "modest-platform.h"
44 #include "modest-defs.h"
45 #include <libmodest-dbus-client/libmodest-dbus-client.h>
46 #include <libgnomevfs/gnome-vfs-utils.h>
49 #include <glib/gstdio.h>
50 #include <hildon/hildon-program.h>
51 #include <libgnomevfs/gnome-vfs-mime.h>
52 #include <tny-fs-stream.h>
54 #ifdef MODEST_TOOLKIT_HILDON2
55 #include <hildon/hildon.h>
56 #include <modest-accounts-window.h>
57 #include <modest-folder-window.h>
58 #include <modest-mailboxes-window.h>
59 #include <modest-maemo-utils.h>
63 #include <tny-iterator.h>
64 #include <tny-simple-list.h>
65 #include <tny-merge-folder.h>
66 #include <tny-account.h>
68 #include <modest-text-utils.h>
78 } ComposeMailIdleData;
81 static gboolean notify_error_in_dbus_callback (gpointer user_data);
82 static gboolean on_idle_compose_mail(gpointer user_data);
83 static gboolean on_idle_top_application (gpointer user_data);
86 * @uri An escaped URI. URIs should always be escaped.
87 * @len The length of the @uri string, or -1 if the string is null terminated.
89 * Decode a URI, or URI fragment, as per RFC 1738.
90 * http://www.ietf.org/rfc/rfc1738.txt
92 * Return value: An unescaped string. This should be freed with g_free().
95 uri_unescape(const gchar* uri, size_t len)
103 /* Allocate an extra string so we can be sure that it is null-terminated,
104 * so we can use gnome_vfs_unescape_string().
105 * This is not efficient. */
106 gchar * escaped_nullterminated = g_strndup (uri, len);
107 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
108 g_free (escaped_nullterminated);
113 /** uri_parse_mailto:
114 * @mailto A mailto URI, with the mailto: prefix.
115 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
116 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
117 * all the string items have been freed. This parameter may be NULL.
118 * Parse a mailto URI as per RFC2368.
119 * http://www.ietf.org/rfc/rfc2368.txt
121 * Return value: The to address, unescaped. This should be freed with g_free().
124 uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
126 /* The URL must begin with mailto: */
127 if (strncmp (mailto, "mailto:", 7) != 0) {
130 const gchar* start_to = mailto + 7;
132 /* Look for ?, or the end of the string, marking the end of the to address: */
133 const size_t len_to = strcspn (start_to, "?");
134 gchar* result_to = uri_unescape (start_to, len_to);
135 printf("debug: result_to=%s\n", result_to);
137 if (list_items_and_values == NULL) {
141 /* Get any other items: */
142 const size_t len_mailto = strlen (start_to);
143 const gchar* p = start_to + len_to + 1; /* parsed so far. */
144 const gchar* end = start_to + len_mailto;
146 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
148 name_end = strchr (name_start, '='); /* Separator between name and value */
149 if (name_end == NULL) {
150 g_debug ("Malformed URI: %s\n", mailto);
153 value_start = name_end + 1;
154 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
156 name = g_strndup(name_start, name_end - name_start);
157 if (value_end != NULL) {
158 value = uri_unescape(value_start, value_end - value_start);
161 value = uri_unescape(value_start, -1);
164 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
165 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
172 check_and_offer_account_creation()
174 gboolean result = TRUE;
176 /* This is called from idle handlers, so lock gdk: */
177 gdk_threads_enter ();
179 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
180 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
182 g_debug ("modest: %s: no account exists even after offering, "
183 "or account setup was already underway.\n", __FUNCTION__);
188 gdk_threads_leave ();
194 on_idle_mail_to(gpointer user_data)
196 gchar *uri = (gchar*)user_data;
197 GSList *list_names_and_values = NULL;
199 const gchar *cc = NULL;
200 const gchar *bcc = NULL;
201 const gchar *subject = NULL;
202 const gchar *body = NULL;
204 if (!check_and_offer_account_creation ()) {
205 g_idle_add (notify_error_in_dbus_callback, NULL);
209 /* Get the relevant items from the list: */
210 to = uri_parse_mailto (uri, &list_names_and_values);
211 GSList *list = list_names_and_values;
213 GSList *list_value = g_slist_next (list);
214 const gchar * name = (const gchar*)list->data;
215 const gchar * value = (const gchar*)list_value->data;
217 if (strcmp (name, "cc") == 0) {
219 } else if (strcmp (name, "bcc") == 0) {
221 } else if (strcmp (name, "subject") == 0) {
223 } else if (strcmp (name, "body") == 0) {
227 list = g_slist_next (list_value);
230 gdk_threads_enter (); /* CHECKED */
231 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL, FALSE);
232 gdk_threads_leave (); /* CHECKED */
235 /* Free the to: and the list, as required by uri_parse_mailto() */
237 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
238 g_slist_free (list_names_and_values);
242 return FALSE; /* Do not call this callback again. */
246 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
252 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
253 uri = g_strdup (val.value.s);
255 g_idle_add(on_idle_mail_to, (gpointer)uri);
257 /* Note that we cannot report failures during sending,
258 * because that would be asynchronous. */
264 on_idle_compose_mail(gpointer user_data)
266 GSList *attachments = NULL;
267 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
269 if (!check_and_offer_account_creation ()) {
270 g_idle_add (notify_error_in_dbus_callback, NULL);
274 /* it seems Sketch at least sends a leading ',' -- take that into account,
276 if (idle_data->attachments && idle_data->attachments[0]==',') {
277 gchar *tmp = g_strdup (idle_data->attachments + 1);
278 g_free(idle_data->attachments);
279 idle_data->attachments = tmp;
282 if (idle_data->attachments != NULL) {
283 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
285 for (i=0; list[i] != NULL; i++) {
286 attachments = g_slist_append(attachments, g_uri_unescape_string (list[i], NULL));
291 /* If the message has nothing then mark the buffers as not
292 modified. This happens in Maemo for example when opening a
293 new message from Contacts plugin, it sends "" instead of
295 gdk_threads_enter (); /* CHECKED */
296 if (!strncmp (idle_data->to, "", 1) &&
297 !strncmp (idle_data->to, "", 1) &&
298 !strncmp (idle_data->cc, "", 1) &&
299 !strncmp (idle_data->bcc, "", 1) &&
300 !strncmp (idle_data->subject, "", 1) &&
301 !strncmp (idle_data->body, "", 1) &&
302 attachments == NULL) {
303 modest_ui_actions_compose_msg(NULL, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
305 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
306 idle_data->bcc, idle_data->subject,
307 idle_data->body, attachments, TRUE);
309 gdk_threads_leave (); /* CHECKED */
311 g_slist_foreach(attachments, (GFunc)g_free, NULL);
312 g_slist_free(attachments);
313 g_free (idle_data->to);
314 g_free (idle_data->cc);
315 g_free (idle_data->bcc);
316 g_free (idle_data->subject);
317 g_free (idle_data->body);
318 g_free (idle_data->attachments);
321 return FALSE; /* Do not call this callback again. */
325 on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
327 ComposeMailIdleData *idle_data;
330 idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
332 /* Get the arguments: */
333 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
334 idle_data->to = g_strdup (val.value.s);
336 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
337 idle_data->cc = g_strdup (val.value.s);
339 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
340 idle_data->bcc = g_strdup (val.value.s);
342 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
343 idle_data->subject = g_strdup (val.value.s);
345 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
346 idle_data->body = g_strdup (val.value.s);
348 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
349 idle_data->attachments = g_strdup (val.value.s);
351 /* Use g_idle to context-switch into the application's thread: */
352 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
358 find_message_by_url (const char *uri, TnyAccount **ac_out)
360 ModestTnyAccountStore *astore;
361 TnyAccount *account = NULL;
362 TnyFolder *folder = NULL;
365 astore = modest_runtime_get_account_store ();
370 if (uri && g_str_has_prefix (uri, "merge://")) {
371 /* we assume we're talking about outbox folder, as this
372 * is the only merge folder we work with in modest */
373 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
375 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
378 if (account == NULL || !TNY_IS_STORE_ACCOUNT (account))
382 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
387 msg = tny_folder_find_msg (folder, uri, NULL);
390 if (account && !msg) {
391 g_object_unref (account);
395 g_object_unref (folder);
404 guint animation_timeout;
405 GtkWidget *animation;
406 } OpenMsgPerformerInfo;
408 #ifndef MODEST_TOOLKIT_HILDON2
410 on_show_opening_animation (gpointer userdata)
412 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
413 info->animation = modest_platform_animation_banner (NULL, NULL, _("mail_me_opening"));
414 info->animation_timeout = 0;
421 on_find_msg_async_destroy (gpointer userdata)
423 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) userdata;
425 if (info->animation_timeout>0) {
426 g_source_remove (info->animation_timeout);
427 info->animation_timeout = 0;
430 if (info->animation) {
431 gtk_widget_destroy (info->animation);
432 info->animation = NULL;
439 g_object_unref (info->account);
441 g_slice_free (OpenMsgPerformerInfo, info);
446 find_msg_async_cb (TnyFolder *folder,
452 TnyHeader *header = NULL;
453 gchar *msg_uid = NULL;
454 ModestWindowMgr *win_mgr;
455 ModestWindow *msg_view = NULL;
456 gboolean is_draft = FALSE;
457 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
459 if (err || cancelled) {
460 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
461 g_idle_add (notify_error_in_dbus_callback, NULL);
465 header = tny_msg_get_header (msg);
466 if (!header || (tny_header_get_flags (header) & TNY_HEADER_FLAG_DELETED)) {
467 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
468 g_idle_add (notify_error_in_dbus_callback, NULL);
472 msg_uid = modest_tny_folder_get_header_unique_id (header);
473 win_mgr = modest_runtime_get_window_mgr ();
475 if (modest_tny_folder_is_local_folder (folder) &&
476 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
480 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
481 gtk_window_present (GTK_WINDOW(msg_view));
483 const gchar *modest_account_name;
486 modest_window_mgr_register_header (win_mgr, header, NULL);
488 account = tny_folder_get_account (folder);
490 modest_account_name =
491 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
493 modest_account_name = NULL;
496 /* Drafts will be opened in the editor, and others will be opened in the viewer */
498 gchar *modest_account_name = NULL;
499 gchar *mailbox = NULL;
502 /* we cannot edit without a valid account... */
503 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr (), TRUE)) {
504 if (!modest_ui_actions_run_account_setup_wizard(NULL)) {
505 modest_window_mgr_unregister_header (win_mgr,
511 from_header = tny_header_dup_from (header);
512 modest_account_name = modest_utils_get_account_name_from_recipient (from_header, &mailbox);
513 g_free (from_header);
515 if (modest_account_name == NULL) {
516 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
517 modest_account_name = modest_account_mgr_get_default_account (mgr);
519 msg_view = modest_msg_edit_window_new (msg, modest_account_name, mailbox, TRUE);
522 g_free (modest_account_name);
525 const gchar *modest_account_name;
528 modest_account_name =
529 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
531 modest_account_name = NULL;
534 header = tny_msg_get_header (msg);
535 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name, NULL, msg_uid);
536 if (! (tny_header_get_flags (header) & TNY_HEADER_FLAG_SEEN)) {
537 ModestMailOperation *mail_op;
539 tny_header_set_flag (header, TNY_HEADER_FLAG_SEEN);
540 /* Sync folder, we need this to save the seen flag */
541 mail_op = modest_mail_operation_new (NULL);
542 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
544 modest_mail_operation_sync_folder (mail_op, folder, FALSE, NULL, NULL);
545 g_object_unref (mail_op);
547 g_object_unref (header);
550 if (msg_view != NULL) {
551 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
552 gtk_widget_destroy (GTK_WIDGET (msg_view));
554 gtk_widget_show_all (GTK_WIDGET (msg_view));
561 g_object_unref (header);
564 on_find_msg_async_destroy (info);
569 on_open_message_performer (gboolean canceled,
571 GtkWindow *parent_window,
575 OpenMsgPerformerInfo *info;
576 TnyFolder *folder = NULL;
577 ModestTnyAccountStore *account_store;
578 ModestTnyLocalFoldersAccount *local_folders_account;
580 account_store = modest_runtime_get_account_store ();
581 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT (
582 modest_tny_account_store_get_local_folders_account (account_store));
584 info = (OpenMsgPerformerInfo *) user_data;
585 if (canceled || err) {
587 modest_platform_run_information_dialog (NULL, _("mail_ni_ui_folder_get_msg_folder_error"), TRUE);
588 g_idle_add (notify_error_in_dbus_callback, NULL);
589 on_find_msg_async_destroy (info);
595 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
596 g_object_unref (local_folders_account);
597 } else if (account == TNY_ACCOUNT (local_folders_account)) {
598 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), info->uri, NULL);
602 if (!(folder && modest_tny_folder_is_local_folder (folder) &&
603 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS))) {
604 ModestWindowMgr *win_mgr;
605 ModestWindow *msg_view;
607 win_mgr = modest_runtime_get_window_mgr ();
608 if (modest_window_mgr_find_registered_message_uid (win_mgr, info->uri, &msg_view)) {
609 gtk_window_present (GTK_WINDOW(msg_view));
611 const gchar *modest_account_name;
613 modest_account_name =
614 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
616 modest_account_name = NULL;
619 msg_view = modest_msg_view_window_new_from_uid (modest_account_name, NULL, info->uri);
620 if (msg_view != NULL) {
621 if (!modest_window_mgr_register_window (win_mgr, msg_view, NULL)) {
622 gtk_widget_destroy (GTK_WIDGET (msg_view));
624 gtk_widget_show_all (GTK_WIDGET (msg_view));
628 g_object_unref (account);
631 on_find_msg_async_destroy (info);
635 #ifndef MODEST_TOOLKIT_HILDON2
636 info->animation_timeout = g_timeout_add (1000, on_show_opening_animation, info);
639 tny_folder_find_msg_async (folder, info->uri, find_msg_async_cb, NULL, info);
640 g_object_unref (folder);
644 on_idle_open_message_performer (gpointer user_data)
646 ModestWindow *top_win = NULL;
647 OpenMsgPerformerInfo *info = (OpenMsgPerformerInfo *) user_data;
649 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
651 /* Lock before the call as we're in an idle handler */
652 gdk_threads_enter ();
654 modest_platform_connect_and_perform (GTK_WINDOW (top_win), TRUE,
656 on_open_message_performer, info);
658 on_open_message_performer (FALSE, NULL, GTK_WINDOW (top_win),
659 info->account, info);
661 gdk_threads_leave ();
667 on_open_message (GArray * arguments, gpointer data, osso_rpc_t * retval)
671 TnyAccount *account = NULL;
675 /* Get the arguments: */
676 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
677 uri = g_strdup (val.value.s);
679 is_merge = g_str_has_prefix (uri, "merge:");
681 /* Get the account */
683 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
687 if (is_merge || account) {
688 OpenMsgPerformerInfo *info;
689 TnyFolder *folder = NULL;
690 ModestTnyAccountStore *account_store;
691 ModestTnyLocalFoldersAccount *local_folders_account;
693 info = g_slice_new0 (OpenMsgPerformerInfo);
695 info->account = g_object_ref (account);
697 info->connect = TRUE;
698 info->animation = NULL;
699 info->animation_timeout = 0;
701 account_store = modest_runtime_get_account_store ();
702 local_folders_account = MODEST_TNY_LOCAL_FOLDERS_ACCOUNT
703 (modest_tny_account_store_get_local_folders_account (account_store));
705 /* Try to get the message, if it's already downloaded
706 we don't need to connect */
709 gboolean device_online;
711 device = modest_runtime_get_device ();
712 device_online = tny_device_is_online (device);
713 if (!device_online || TNY_ACCOUNT (local_folders_account) == account) {
714 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account), uri, NULL);
719 folder = modest_tny_local_folders_account_get_merged_outbox (local_folders_account);
720 g_object_unref (local_folders_account);
721 info->connect = FALSE;
724 TnyMsg *msg = tny_folder_find_msg (folder, uri, NULL);
726 info->connect = FALSE;
727 g_object_unref (msg);
729 info->connect = TRUE;
731 g_object_unref (folder);
734 /* We need to call it into an idle to get
735 modest_platform_connect_and_perform into the main
737 g_idle_add (on_idle_open_message_performer, info);
738 osso_retval = OSSO_OK;
741 osso_retval = OSSO_ERROR;
742 g_idle_add (notify_error_in_dbus_callback, NULL);
746 g_object_unref (account);
751 on_remove_msgs_finished (ModestMailOperation *mail_op,
755 ModestWindow *top_win = NULL, *msg_view = NULL;
757 header = (TnyHeader *) user_data;
759 /* Get the main window if exists */
760 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
762 g_object_unref (header);
766 if (modest_window_mgr_find_registered_header (modest_runtime_get_window_mgr(),
767 header, &msg_view)) {
768 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
769 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
771 g_object_unref (header);
773 /* Refilter the header views explicitely */
775 /* TODO: call modest_window_mgr_refilter_header_views */
776 /* this call will go through all the windows, get the header views and refilter them */
780 thread_prepare_delete_message (gpointer userdata)
782 TnyList *headers = NULL, *tmp_headers = NULL;
783 TnyFolder *folder = NULL;
784 TnyIterator *iter = NULL;
785 TnyHeader *header = NULL, *msg_header = NULL;
787 TnyAccount *account = NULL;
790 ModestMailOperation *mail_op = NULL;
791 ModestWindow *top_win = NULL;
793 uri = (char *) userdata;
795 msg = find_message_by_url (uri, &account);
797 g_object_unref (account);
800 g_debug ("%s: Could not find message '%s'", __FUNCTION__, uri);
801 g_idle_add (notify_error_in_dbus_callback, NULL);
806 top_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr());
808 folder = tny_msg_get_folder (msg);
810 g_debug ("%s: Could not find folder (uri:'%s')", __FUNCTION__, uri);
811 g_object_unref (msg);
812 g_idle_add (notify_error_in_dbus_callback, NULL);
818 msg_header = tny_msg_get_header (msg);
819 uid = tny_header_dup_uid (msg_header);
820 g_object_unref (msg);
821 g_object_unref (msg_header);
823 headers = tny_simple_list_new ();
824 tny_folder_get_headers (folder, headers, TRUE, NULL);
825 iter = tny_list_create_iterator (headers);
827 while (!tny_iterator_is_done (iter)) {
828 gchar *cur_id = NULL;
830 header = TNY_HEADER (tny_iterator_get_current (iter));
832 cur_id = tny_header_dup_uid (header);
834 if (cur_id && uid && g_str_equal (cur_id, uid)) {
836 /* g_debug ("Found corresponding header from folder"); */
842 g_object_unref (header);
846 tny_iterator_next (iter);
849 g_object_unref (iter);
850 g_object_unref (headers);
852 if (header == NULL) {
854 g_object_unref (folder);
855 g_idle_add (notify_error_in_dbus_callback, NULL);
860 /* This is a GDK lock because we are an idle callback and
861 * the code below is or does Gtk+ code */
862 gdk_threads_enter (); /* CHECKED */
864 mail_op = modest_mail_operation_new (top_win ? G_OBJECT(top_win) : NULL);
865 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
867 g_signal_connect (G_OBJECT (mail_op),
868 "operation-finished",
869 G_CALLBACK (on_remove_msgs_finished),
870 g_object_ref (header));
872 tmp_headers = tny_simple_list_new ();
873 tny_list_append (tmp_headers, (GObject *) header);
875 modest_mail_operation_remove_msgs (mail_op, tmp_headers, FALSE);
877 g_object_unref (tmp_headers);
878 g_object_unref (G_OBJECT (mail_op));
879 gdk_threads_leave (); /* CHECKED */
883 g_object_unref (header);
890 on_idle_delete_message (gpointer user_data)
892 const char *uri = NULL;
894 uri = (char *) user_data;
896 g_thread_create (thread_prepare_delete_message, g_strdup (uri), FALSE, NULL);
906 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
908 /* Get the arguments: */
909 osso_rpc_t val = g_array_index (arguments,
911 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
912 gchar *uri = g_strdup (val.value.s);
914 /* Use g_idle to context-switch into the application's thread: */
915 g_idle_add(on_idle_delete_message, (gpointer)uri);
920 typedef struct _SendReceivePerformerData {
923 ModestMailOperation *mail_op;
924 } SendReceivePerformerData;
926 static void send_receive_performer_data_free (SendReceivePerformerData *data)
929 g_object_unref (data->mail_op);
931 g_free (data->account_id);
932 g_slice_free (SendReceivePerformerData, data);
936 on_idle_send_receive(gpointer user_data)
938 gboolean auto_update;
939 SendReceivePerformerData *data = (SendReceivePerformerData *) user_data;
940 ModestConnectedVia connect_when;
941 gboolean right_connection = FALSE;
943 gdk_threads_enter (); /* CHECKED */
945 /* Check if the autoupdate feature is on */
946 auto_update = modest_conf_get_bool (modest_runtime_get_conf (),
947 MODEST_CONF_AUTO_UPDATE, NULL);
950 /* Do send receive. Never set the current top window
951 as we always assume that DBus send/receive requests
952 are not user driven */
954 connect_when = modest_conf_get_int (modest_runtime_get_conf (),
955 MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, NULL);
956 /* Perform a send and receive if the user selected to connect
957 via any mean or if the current connection method is the
958 same as the one specified by the user */
959 if (connect_when == MODEST_CONNECTED_VIA_ANY ||
960 connect_when == modest_platform_get_current_connection ()) {
961 right_connection = TRUE;
964 /* Disable auto update */
965 modest_platform_set_update_interval (0);
968 if ((auto_update && right_connection) || data->manual) {
969 if (data->account_id) {
970 modest_ui_actions_do_send_receive (data->account_id, data->manual, FALSE, data->manual, NULL);
972 modest_ui_actions_do_send_receive_all (NULL, data->manual, FALSE, data->manual);
976 modest_mail_operation_queue_remove (modest_runtime_get_mail_operation_queue (),
979 send_receive_performer_data_free (data);
981 gdk_threads_leave (); /* CHECKED */
989 on_dbus_method_dump_send_queues (DBusConnection *con, DBusMessage *message)
994 dbus_uint32_t serial = 0;
996 GSList *account_names, *cursor;
998 str = g_strdup("\nsend queues\n"
1001 cursor = account_names = modest_account_mgr_account_names
1002 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1006 gchar *tmp, *accname = (gchar*)cursor->data;
1008 tmp = g_strdup_printf ("%s", str);
1013 acc = modest_tny_account_store_get_server_account (
1014 modest_runtime_get_account_store(), accname,
1015 TNY_ACCOUNT_TYPE_TRANSPORT);
1016 if (TNY_IS_ACCOUNT(acc)) {
1017 gchar *tmp = NULL, *url = tny_account_get_url_string (acc);
1018 ModestTnySendQueue *sendqueue =
1019 modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT(acc), TRUE);
1021 if (TNY_IS_SEND_QUEUE (sendqueue)) {
1022 gchar *queue_str = modest_tny_send_queue_to_string (sendqueue);
1024 tmp = g_strdup_printf ("%s[%s]: '%s': %s\n%s",
1025 str, accname, tny_account_get_id (acc), url,
1033 g_object_unref (acc);
1036 cursor = g_slist_next (cursor);
1038 modest_account_mgr_free_account_names (account_names);
1042 reply = dbus_message_new_method_return (message);
1044 dbus_message_append_args (reply,
1045 DBUS_TYPE_STRING, &str,
1047 dbus_connection_send (con, reply, &serial);
1048 dbus_connection_flush (con);
1049 dbus_message_unref (reply);
1053 /* Let modest die */
1054 g_idle_add (notify_error_in_dbus_callback, NULL);
1061 on_dbus_method_dump_operation_queue (DBusConnection *con, DBusMessage *message)
1064 gchar *op_queue_str;
1067 dbus_uint32_t serial = 0;
1069 /* operations queue; */
1070 op_queue_str = modest_mail_operation_queue_to_string
1071 (modest_runtime_get_mail_operation_queue ());
1073 str = g_strdup_printf ("\noperation queue\n"
1077 tny_device_is_online (modest_runtime_get_device ()) ? "online" : "offline",
1079 g_free (op_queue_str);
1083 reply = dbus_message_new_method_return (message);
1085 dbus_message_append_args (reply,
1086 DBUS_TYPE_STRING, &str,
1088 dbus_connection_send (con, reply, &serial);
1089 dbus_connection_flush (con);
1090 dbus_message_unref (reply);
1094 /* Let modest die */
1095 g_idle_add (notify_error_in_dbus_callback, NULL);
1103 on_dbus_method_dump_accounts (DBusConnection *con, DBusMessage *message)
1108 dbus_uint32_t serial = 0;
1110 GSList *account_names, *cursor;
1112 str = g_strdup ("\naccounts\n========\n");
1114 cursor = account_names = modest_account_mgr_account_names
1115 (modest_runtime_get_account_mgr(), TRUE); /* only enabled accounts */
1119 gchar *tmp, *accname = (gchar*)cursor->data;
1121 tmp = g_strdup_printf ("%s[%s]\n", str, accname);
1126 acc = modest_tny_account_store_get_server_account (
1127 modest_runtime_get_account_store(), accname,
1128 TNY_ACCOUNT_TYPE_STORE);
1129 if (TNY_IS_ACCOUNT(acc)) {
1130 gchar *tmp, *url = tny_account_get_url_string (acc);
1131 tmp = g_strdup_printf ("%sstore : '%s': %s (refs: %d)\n",
1132 str, tny_account_get_id (acc), url,
1133 ((GObject*)acc)->ref_count-1);
1137 g_object_unref (acc);
1141 acc = modest_tny_account_store_get_server_account (
1142 modest_runtime_get_account_store(), accname,
1143 TNY_ACCOUNT_TYPE_TRANSPORT);
1144 if (TNY_IS_ACCOUNT(acc)) {
1145 gchar *tmp, *url = tny_account_get_url_string (acc);
1146 tmp = g_strdup_printf ("%stransport: '%s': %s (refs: %d)\n",
1147 str, tny_account_get_id (acc), url,
1148 ((GObject*)acc)->ref_count-1);
1152 g_object_unref (acc);
1155 cursor = g_slist_next (cursor);
1158 modest_account_mgr_free_account_names (account_names);
1162 reply = dbus_message_new_method_return (message);
1164 dbus_message_append_args (reply,
1165 DBUS_TYPE_STRING, &str,
1167 dbus_connection_send (con, reply, &serial);
1168 dbus_connection_flush (con);
1169 dbus_message_unref (reply);
1173 /* Let modest die */
1174 g_idle_add (notify_error_in_dbus_callback, NULL);
1180 on_send_receive_performer(gboolean canceled,
1182 GtkWindow *parent_window,
1183 TnyAccount *account,
1186 SendReceivePerformerData *data = (SendReceivePerformerData *) user_data;
1188 if (err || canceled || data == NULL) {
1189 g_idle_add (notify_error_in_dbus_callback, NULL);
1191 send_receive_performer_data_free (data);
1196 data->mail_op = modest_mail_operation_new (NULL);
1197 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1200 g_idle_add (on_idle_send_receive, data);
1202 modest_heartbeat_add (on_idle_send_receive, data);
1208 on_send_receive(GArray *arguments, gpointer data, osso_rpc_t * retval)
1210 TnyDevice *device = modest_runtime_get_device ();
1211 SendReceivePerformerData *srp_data;
1213 srp_data = g_slice_new0 (SendReceivePerformerData);
1214 srp_data->account_id = NULL;
1215 srp_data->manual = FALSE;
1216 srp_data->mail_op = NULL;
1218 if (!tny_device_is_online (device))
1219 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, srp_data);
1221 on_send_receive_performer (FALSE, NULL, NULL, NULL, srp_data);
1227 on_send_receive_full (GArray *arguments, gpointer data, osso_rpc_t * retval)
1233 SendReceivePerformerData *srp_data;
1235 val = g_array_index (arguments, osso_rpc_t, MODEST_DBUS_SEND_RECEIVE_FULL_ARG_ACCOUNT_ID);
1236 account_id = g_strdup (val.value.s);
1237 val = g_array_index (arguments, osso_rpc_t, MODEST_DBUS_SEND_RECEIVE_FULL_ARG_MANUAL);
1238 manual = val.value.b;
1240 srp_data = g_slice_new0 (SendReceivePerformerData);
1241 srp_data->manual = manual;
1242 if (account_id && account_id[0] != '\0') {
1243 srp_data->account_id = account_id;
1245 srp_data->account_id = NULL;
1246 g_free (account_id);
1248 srp_data->mail_op = NULL;
1249 device = modest_runtime_get_device ();
1250 if (!tny_device_is_online (device))
1251 modest_platform_connect_and_perform (NULL, FALSE, NULL, on_send_receive_performer, srp_data);
1253 on_send_receive_performer (FALSE, NULL, NULL, NULL, srp_data);
1259 on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
1261 g_idle_add(on_idle_top_application, NULL);
1268 on_idle_open_account (gpointer user_data)
1271 ModestWindowMgr *mgr;
1274 gdk_threads_enter ();
1276 acc_name = (gchar *) user_data;
1277 mgr = modest_runtime_get_window_mgr ();
1279 top = modest_window_mgr_get_current_top (mgr);
1281 top = modest_window_mgr_show_initial_window (mgr);
1283 #ifdef MODEST_TOOLKIT_HILDON2
1284 GtkWidget *new_window;
1285 ModestProtocolType store_protocol;
1286 gboolean mailboxes_protocol;
1288 store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (),
1290 mailboxes_protocol =
1291 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
1293 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
1295 if (mailboxes_protocol) {
1296 new_window = GTK_WIDGET (modest_mailboxes_window_new (acc_name));
1298 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
1299 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window),
1303 if (modest_window_mgr_register_window (mgr, MODEST_WINDOW (new_window), NULL)) {
1304 gtk_widget_show (new_window);
1306 gtk_widget_destroy (new_window);
1310 if (MODEST_IS_MAIN_WINDOW (top)) {
1312 GtkWidget *folder_view;
1314 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (top),
1315 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1316 server_name = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
1317 acc_name, TNY_ACCOUNT_TYPE_STORE);
1319 modest_folder_view_set_account_id_of_visible_server_account (MODEST_FOLDER_VIEW (folder_view),
1321 g_free (server_name);
1326 gdk_threads_leave ();
1332 on_open_account (GArray *arguments, gpointer data, osso_rpc_t *retval)
1337 /* Get the arguments: */
1338 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
1339 account_id = g_strdup (val.value.s);
1341 g_idle_add (on_idle_open_account, account_id);
1346 #ifdef MODEST_TOOLKIT_HILDON2
1348 on_idle_top_application (gpointer user_data)
1350 HildonWindowStack *stack;
1353 /* This is a GDK lock because we are an idle callback and
1354 * the code below is or does Gtk+ code */
1356 gdk_threads_enter (); /* CHECKED */
1358 stack = hildon_window_stack_get_default ();
1359 window = GTK_WIDGET (hildon_window_stack_peek (stack));
1362 gtk_window_present (GTK_WINDOW (window));
1364 ModestWindowMgr *mgr;
1366 mgr = modest_runtime_get_window_mgr ();
1367 window = (GtkWidget *) modest_window_mgr_show_initial_window (mgr);
1369 modest_platform_remove_new_mail_notifications (FALSE);
1371 g_printerr ("modest: failed to get main window instance\n");
1375 gdk_threads_leave (); /* CHECKED */
1377 return FALSE; /* Do not call this callback again. */
1381 on_idle_top_application (gpointer user_data)
1383 ModestWindow *main_win;
1384 gboolean new_window = FALSE;
1386 /* This is a GDK lock because we are an idle callback and
1387 * the code below is or does Gtk+ code */
1389 gdk_threads_enter (); /* CHECKED */
1391 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1395 main_win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr (),
1401 /* If we're showing an already existing window then
1402 reselect the INBOX */
1404 GtkWidget *folder_view;
1405 folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (main_win),
1406 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
1407 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
1412 gtk_widget_show_all (GTK_WIDGET (main_win));
1413 gtk_window_present (GTK_WINDOW (main_win));
1416 gdk_threads_leave (); /* CHECKED */
1418 return FALSE; /* Do not call this callback again. */
1423 on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
1425 /* Use g_idle to context-switch into the application's thread: */
1426 g_idle_add(on_idle_top_application, NULL);
1432 on_idle_open_edit_accounts_dialog (gpointer user_data)
1435 ModestWindowMgr *mgr;
1438 /* This is a GDK lock because we are an idle callback and
1439 * the code below is or does Gtk+ code */
1441 gdk_threads_enter (); /* CHECKED */
1443 mgr = modest_runtime_get_window_mgr ();
1444 closed = modest_window_mgr_close_all_but_initial (mgr);
1445 top = modest_window_mgr_get_current_top (mgr);
1448 /* Show edit accounts dialog */
1449 modest_ui_actions_on_accounts (NULL, top);
1451 gboolean has_accounts;
1453 has_accounts = modest_account_mgr_has_accounts (modest_runtime_get_account_mgr (),
1456 /* Present the current top window */
1457 gdk_threads_leave ();
1458 on_idle_top_application (NULL);
1459 gdk_threads_enter ();
1461 /* Show edit accounts dialog if we're launching the
1462 program and there is no account (if there is at
1463 least one account then on_idle_top_application will
1464 show the accounts wizard */
1465 if (!top && has_accounts) {
1466 top = modest_window_mgr_get_current_top (mgr);
1467 modest_ui_actions_on_accounts (NULL, top);
1471 gdk_threads_leave (); /* CHECKED */
1473 return FALSE; /* Do not call this callback again. */
1477 on_open_edit_accounts_dialog (GArray * arguments, gpointer data, osso_rpc_t * retval)
1479 /* Use g_idle to context-switch into the application's thread: */
1480 g_idle_add (on_idle_open_edit_accounts_dialog, NULL);
1486 on_idle_show_memory_low (gpointer user_data)
1488 ModestWindow *main_win = NULL;
1490 gdk_threads_enter ();
1491 main_win = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr ());
1492 modest_platform_run_information_dialog (GTK_WINDOW (main_win),
1493 dgettext("ke-recv","memr_ib_operation_disabled"),
1495 gdk_threads_leave ();
1501 on_idle_present_modal (gpointer user_data)
1503 gdk_threads_enter ();
1504 gtk_window_present (user_data);
1505 gdk_threads_leave ();
1511 /* Callback for normal D-BUS messages */
1513 modest_dbus_req_handler(const gchar * interface, const gchar * method,
1514 GArray * arguments, gpointer data,
1515 osso_rpc_t * retval)
1519 /* Check memory low conditions */
1520 if (modest_platform_check_memory_low (NULL, FALSE)) {
1521 g_idle_add (on_idle_show_memory_low, NULL);
1525 /* Check if there is already a dialog or note open */
1526 dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
1528 g_idle_add (on_idle_present_modal, dialog);
1532 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
1533 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
1535 modest_runtime_set_allow_shutdown (TRUE);
1536 return on_mail_to (arguments, data, retval);
1537 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
1538 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
1540 modest_runtime_set_allow_shutdown (TRUE);
1541 return on_open_message (arguments, data, retval);
1542 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_ACCOUNT) == 0) {
1543 if (arguments->len != MODEST_DBUS_OPEN_ACCOUNT_ARGS_COUNT)
1545 modest_runtime_set_allow_shutdown (TRUE);
1546 return on_open_account (arguments, data, retval);
1547 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
1548 if (arguments->len != 0)
1550 return on_send_receive (arguments, data, retval);
1551 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE_FULL) == 0) {
1552 if (arguments->len != MODEST_DBUS_SEND_RECEIVE_FULL_ARGS_COUNT)
1554 return on_send_receive_full (arguments, data, retval);
1555 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
1556 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
1558 modest_runtime_set_allow_shutdown (TRUE);
1559 return on_compose_mail (arguments, data, retval);
1560 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
1561 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
1563 return on_delete_message (arguments,data, retval);
1564 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
1565 if (arguments->len != 0)
1567 modest_runtime_set_allow_shutdown (TRUE);
1568 return on_open_default_inbox (arguments, data, retval);
1569 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
1570 if (arguments->len != 0)
1572 modest_runtime_set_allow_shutdown (TRUE);
1573 return on_top_application (arguments, data, retval);
1574 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_EDIT_ACCOUNTS_DIALOG) == 0) {
1575 if (arguments->len != 0)
1577 modest_runtime_set_allow_shutdown (TRUE);
1578 return on_open_edit_accounts_dialog (arguments, data, retval);
1580 /* We need to return INVALID here so
1581 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
1582 * so that our modest_dbus_req_filter will then be tried instead.
1584 return OSSO_INVALID;
1587 /* Notify error in D-Bus method */
1588 g_idle_add (notify_error_in_dbus_callback, NULL);
1592 /* A complex D-Bus type (like a struct),
1593 * used to return various information about a search hit.
1595 #define SEARCH_HIT_DBUS_TYPE \
1596 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1597 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
1598 DBUS_TYPE_STRING_AS_STRING /* subject */ \
1599 DBUS_TYPE_STRING_AS_STRING /* folder */ \
1600 DBUS_TYPE_STRING_AS_STRING /* sender */ \
1601 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
1602 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
1603 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
1604 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
1605 DBUS_STRUCT_END_CHAR_AS_STRING
1607 static DBusMessage *
1608 search_result_to_message (DBusMessage *reply,
1611 DBusMessageIter iter;
1612 DBusMessageIter array_iter;
1615 dbus_message_iter_init_append (reply, &iter);
1616 dbus_message_iter_open_container (&iter,
1618 SEARCH_HIT_DBUS_TYPE,
1621 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
1622 DBusMessageIter struct_iter;
1623 ModestSearchResultHit *hit;
1625 const char *subject;
1629 gboolean has_attachment;
1633 hit = (ModestSearchResultHit *) hit_iter->data;
1635 msg_url = hit->msgid;
1636 subject = hit->subject;
1637 folder = hit->folder;
1638 sender = hit->sender;
1640 has_attachment = hit->has_attachment;
1641 is_unread = hit->is_unread;
1642 ts = hit->timestamp;
1644 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
1646 dbus_message_iter_open_container (&array_iter,
1651 dbus_message_iter_append_basic (&struct_iter,
1655 dbus_message_iter_append_basic (&struct_iter,
1659 dbus_message_iter_append_basic (&struct_iter,
1663 dbus_message_iter_append_basic (&struct_iter,
1667 dbus_message_iter_append_basic (&struct_iter,
1671 dbus_message_iter_append_basic (&struct_iter,
1675 dbus_message_iter_append_basic (&struct_iter,
1679 dbus_message_iter_append_basic (&struct_iter,
1683 dbus_message_iter_close_container (&array_iter,
1686 g_free (hit->msgid);
1687 g_free (hit->subject);
1688 g_free (hit->folder);
1689 g_free (hit->sender);
1691 g_slice_free (ModestSearchResultHit, hit);
1694 dbus_message_iter_close_container (&iter, &array_iter);
1701 DBusConnection *con;
1702 DBusMessage *message;
1703 ModestSearch *search;
1707 search_all_cb (GList *hits, gpointer user_data)
1710 SearchHelper *helper = (SearchHelper *) user_data;
1712 reply = dbus_message_new_method_return (helper->message);
1715 dbus_uint32_t serial = 0;
1717 search_result_to_message (reply, hits);
1719 dbus_connection_send (helper->con, reply, &serial);
1720 dbus_connection_flush (helper->con);
1721 dbus_message_unref (reply);
1724 /* Free the helper */
1725 dbus_message_unref (helper->message);
1726 modest_search_free (helper->search);
1727 g_slice_free (ModestSearch, helper->search);
1728 g_slice_free (SearchHelper, helper);
1732 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1734 ModestDBusSearchFlags dbus_flags;
1738 dbus_int32_t flags_v;
1739 dbus_uint32_t size_v;
1744 ModestSearch *search;
1747 dbus_error_init (&error);
1752 res = dbus_message_get_args (message,
1754 DBUS_TYPE_STRING, &query,
1755 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1756 DBUS_TYPE_INT64, &sd_v,
1757 DBUS_TYPE_INT64, &ed_v,
1758 DBUS_TYPE_INT32, &flags_v,
1759 DBUS_TYPE_UINT32, &size_v,
1762 dbus_flags = (ModestDBusSearchFlags) flags_v;
1763 start_date = (time_t) sd_v;
1764 end_date = (time_t) ed_v;
1766 search = g_slice_new0 (ModestSearch);
1768 if (folder && g_str_has_prefix (folder, "MAND:")) {
1769 search->folder = g_strdup (folder + strlen ("MAND:"));
1770 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1771 search->folder = g_strdup (folder + strlen ("USER:"));
1772 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1773 search->folder = g_strdup (folder + strlen ("MY:"));
1775 search->folder = g_strdup (folder);
1778 /* Remember the text to search for: */
1779 #ifdef MODEST_HAVE_OGS
1780 search->query = g_strdup (query);
1783 /* Other criteria: */
1784 search->start_date = start_date;
1785 search->end_date = end_date;
1788 /* Text to serach for in various parts of the message: */
1789 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1790 search->flags |= MODEST_SEARCH_SUBJECT;
1791 search->subject = g_strdup (query);
1794 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1795 search->flags |= MODEST_SEARCH_SENDER;
1796 search->from = g_strdup (query);
1799 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1800 search->flags |= MODEST_SEARCH_RECIPIENT;
1801 search->recipient = g_strdup (query);
1804 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1805 search->flags |= MODEST_SEARCH_BODY;
1806 search->body = g_strdup (query);
1810 search->flags |= MODEST_SEARCH_AFTER;
1811 search->start_date = start_date;
1815 search->flags |= MODEST_SEARCH_BEFORE;
1816 search->end_date = end_date;
1820 search->flags |= MODEST_SEARCH_SIZE;
1821 search->minsize = size_v;
1824 #ifdef MODEST_HAVE_OGS
1825 search->flags |= MODEST_SEARCH_USE_OGS;
1826 g_debug ("%s: Starting search for %s", __FUNCTION__, search->query);
1829 SearchHelper *helper = g_slice_new (SearchHelper);
1830 helper->search = search;
1831 dbus_message_ref (message);
1832 helper->message = message;
1835 /* Search asynchronously */
1836 modest_search_all_accounts (search, search_all_cb, helper);
1840 /* A complex D-Bus type (like a struct),
1841 * used to return various information about a folder.
1843 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1844 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1845 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1846 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1847 DBUS_STRUCT_END_CHAR_AS_STRING
1849 static DBusMessage *
1850 get_folders_result_to_message (DBusMessage *reply,
1853 DBusMessageIter iter;
1854 dbus_message_iter_init_append (reply, &iter);
1856 DBusMessageIter array_iter;
1857 dbus_message_iter_open_container (&iter,
1859 GET_FOLDERS_RESULT_DBUS_TYPE,
1862 GList *list_iter = folder_ids;
1863 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1865 const gchar *folder_name = (const gchar*)list_iter->data;
1867 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1869 DBusMessageIter struct_iter;
1870 dbus_message_iter_open_container (&array_iter,
1876 dbus_message_iter_append_basic (&struct_iter,
1878 &folder_name); /* The string will be copied. */
1880 /* URI: This is maybe not needed by osso-global-search: */
1881 const gchar *folder_uri = "TODO:unimplemented";
1882 dbus_message_iter_append_basic (&struct_iter,
1884 &folder_uri); /* The string will be copied. */
1886 dbus_message_iter_close_container (&array_iter,
1891 dbus_message_iter_close_container (&iter, &array_iter);
1897 add_single_folder_to_list (TnyFolder *folder, GList** list)
1902 if (TNY_IS_MERGE_FOLDER (folder)) {
1903 const gchar * folder_name;
1904 /* Ignore these because their IDs ares
1905 * a) not always unique or sensible.
1906 * b) not human-readable, and currently need a human-readable
1907 * ID here, because the osso-email-interface API does not allow
1908 * us to return both an ID and a display name.
1910 * This is actually the merged outbox folder.
1911 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1912 * but that seems unwise. murrayc.
1914 folder_name = tny_folder_get_name (folder);
1915 if (folder_name && !strcmp (folder_name, "Outbox")) {
1916 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1921 /* Add this folder to the list: */
1923 const gchar * folder_name = tny_folder_get_name (folder);
1925 *list = g_list_append(*list, g_strdup (folder_name));
1928 /* osso-global-search only uses one string,
1929 * so ID is the only thing that could possibly identify a folder.
1930 * TODO: osso-global search should probably be changed to
1931 * take an ID and a Name.
1933 const gchar * id = tny_folder_get_id (folder);
1934 if (id && strlen(id)) {
1935 const gchar *prefix = NULL;
1936 TnyFolderType folder_type;
1938 /* dbus global search api expects a prefix identifying the type of
1939 * folder here. Mandatory folders should have MAND: prefix, and
1940 * other user created folders should have USER: prefix
1942 folder_type = modest_tny_folder_guess_folder_type (folder);
1943 switch (folder_type) {
1944 case TNY_FOLDER_TYPE_INBOX:
1947 case TNY_FOLDER_TYPE_OUTBOX:
1948 case TNY_FOLDER_TYPE_DRAFTS:
1949 case TNY_FOLDER_TYPE_SENT:
1950 case TNY_FOLDER_TYPE_ARCHIVE:
1953 case TNY_FOLDER_TYPE_INVALID:
1954 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1955 return; /* don't add it */
1962 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1967 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1972 /* Add this folder to the list: */
1973 if (TNY_IS_FOLDER (folder_store)) {
1974 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1977 /* Recurse into child folders: */
1979 /* Get the folders list: */
1981 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1982 tny_folder_store_query_add_item (query, NULL,
1983 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1985 TnyList *all_folders = tny_simple_list_new ();
1986 tny_folder_store_get_folders (folder_store,
1992 TnyIterator *iter = tny_list_create_iterator (all_folders);
1993 while (!tny_iterator_is_done (iter)) {
1995 /* Do not recurse, because the osso-global-search UI specification
1996 * does not seem to want the sub-folders, though that spec seems to
1997 * be generally unsuitable for Modest.
1999 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
2001 add_single_folder_to_list (TNY_FOLDER (folder), list);
2004 if (TNY_IS_FOLDER_STORE (folder))
2005 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
2007 add_single_folder_to_list (TNY_FOLDER (folder), list);
2011 /* tny_iterator_get_current() gave us a reference. */
2012 g_object_unref (folder);
2015 tny_iterator_next (iter);
2017 g_object_unref (G_OBJECT (iter));
2021 /* return >1 for a special folder, 0 for a user-folder */
2023 get_rank (const gchar *folder)
2025 if (strcmp (folder, "INBOX") == 0)
2027 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
2029 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
2031 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
2037 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
2039 gint r1 = get_rank (folder1);
2040 gint r2 = get_rank (folder2);
2042 if (r1 > 0 && r2 > 0)
2044 if (r1 > 0 && r2 == 0)
2046 if (r1 == 0 && r2 > 0)
2049 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
2053 /* - we're still missing the outbox */
2054 /* - we need to take care of localization (urgh) */
2055 /* - what about 'All mail folders'? */
2057 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
2059 DBusMessage *reply = NULL;
2060 ModestAccountMgr *account_mgr = NULL;
2061 gchar *account_name = NULL;
2062 GList *folder_names = NULL;
2063 TnyAccount *account_local = NULL;
2064 TnyAccount *account_mmc = NULL;
2066 /* Get the TnyStoreAccount so we can get the folders: */
2067 account_mgr = modest_runtime_get_account_mgr();
2068 account_name = modest_account_mgr_get_default_account (account_mgr);
2069 if (!account_name) {
2070 g_printerr ("modest: no account found\n");
2074 TnyAccount *account = NULL;
2076 account = modest_tny_account_store_get_server_account (
2077 modest_runtime_get_account_store(), account_name,
2078 TNY_ACCOUNT_TYPE_STORE);
2082 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
2085 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
2086 g_free (account_name);
2087 account_name = NULL;
2089 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
2091 g_object_unref (account);
2095 /* Also add the folders from the local folders account,
2096 * because they are (currently) used with all accounts:
2097 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
2100 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
2101 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
2103 g_object_unref (account_local);
2104 account_local = NULL;
2106 /* Obtain the mmc account */
2108 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
2110 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
2111 g_object_unref (account_mmc);
2115 /* specs require us to sort the folder names, although
2116 * this is really not the place to do that...
2118 folder_names = g_list_sort (folder_names,
2119 (GCompareFunc)folder_name_compare_func);
2121 /* Put the result in a DBus reply: */
2122 reply = dbus_message_new_method_return (message);
2124 get_folders_result_to_message (reply, folder_names);
2126 if (reply == NULL) {
2127 g_warning ("%s: Could not create reply.", __FUNCTION__);
2131 dbus_uint32_t serial = 0;
2132 dbus_connection_send (con, reply, &serial);
2133 dbus_connection_flush (con);
2134 dbus_message_unref (reply);
2137 g_list_foreach (folder_names, (GFunc)g_free, NULL);
2138 g_list_free (folder_names);
2143 reply_empty_results (DBusConnection *con, DBusMessage *msg)
2145 DBusMessage *reply = dbus_message_new_method_return (msg);
2147 dbus_uint32_t serial = 0;
2148 /* we simply return an empty list, otherwise
2149 global-search gets confused */
2150 search_result_to_message (reply, NULL);
2152 dbus_connection_send (con, reply, &serial);
2153 dbus_connection_flush (con);
2154 dbus_message_unref (reply);
2156 g_warning ("%s: failed to send reply",
2161 /** This D-Bus handler is used when the main osso-rpc
2162 * D-Bus handler has not handled something.
2163 * We use this for D-Bus methods that need to use more complex types
2164 * than osso-rpc supports.
2167 modest_dbus_req_filter (DBusConnection *con,
2168 DBusMessage *message,
2171 gboolean handled = FALSE;
2173 if (dbus_message_is_method_call (message,
2175 MODEST_DBUS_METHOD_SEARCH)) {
2177 /* don't try to search when there not enough mem */
2178 if (modest_platform_check_memory_low (NULL, TRUE)) {
2179 g_warning ("%s: not enough memory for searching",
2181 reply_empty_results (con, message);
2185 on_dbus_method_search (con, message);
2189 } else if (dbus_message_is_method_call (message,
2191 MODEST_DBUS_METHOD_GET_FOLDERS)) {
2192 on_dbus_method_get_folders (con, message);
2194 } else if (dbus_message_is_method_call (message,
2196 MODEST_DBUS_METHOD_DUMP_OPERATION_QUEUE)) {
2197 on_dbus_method_dump_operation_queue (con, message);
2199 } else if (dbus_message_is_method_call (message,
2201 MODEST_DBUS_METHOD_DUMP_ACCOUNTS)) {
2202 on_dbus_method_dump_accounts (con, message);
2204 } else if (dbus_message_is_method_call (message,
2206 MODEST_DBUS_METHOD_DUMP_SEND_QUEUES)) {
2207 on_dbus_method_dump_send_queues (con, message);
2210 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
2212 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
2213 __FUNCTION__, dbus_message_get_interface (message),
2214 dbus_message_get_member(message));
2219 DBUS_HANDLER_RESULT_HANDLED :
2220 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
2224 notify_error_in_dbus_callback (gpointer user_data)
2226 ModestMailOperation *mail_op;
2227 ModestMailOperationQueue *mail_op_queue;
2229 mail_op = modest_mail_operation_new (NULL);
2230 mail_op_queue = modest_runtime_get_mail_operation_queue ();
2232 /* Issues a noop operation in order to force the queue to emit
2233 the "queue-empty" signal to allow modest to quit */
2234 modest_mail_operation_queue_add (mail_op_queue, mail_op);
2235 modest_mail_operation_noop (mail_op);
2236 g_object_unref (mail_op);