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"
38 #include "modest-search.h"
39 #include "widgets/modest-msg-edit-window.h"
40 #include "modest-tny-msg.h"
41 #include <libmodest-dbus-client/libmodest-dbus-client.h>
42 #include <libgnomevfs/gnome-vfs-utils.h>
45 #include <glib/gstdio.h>
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <libgnomevfs/gnome-vfs-mime-utils.h>
49 #include <libgnomevfs/gnome-vfs-mime.h>
51 #include <tny-fs-stream.h>
54 #include <tny-iterator.h>
55 #include <tny-simple-list.h>
56 #include <tny-merge-folder.h>
58 #include <modest-text-utils.h>
68 } ComposeMailIdleData;
70 static gboolean on_idle_compose_mail(gpointer user_data);
73 * @uri An escaped URI. URIs should always be escaped.
74 * @len The length of the @uri string, or -1 if the string is null terminated.
76 * Decode a URI, or URI fragment, as per RFC 1738.
77 * http://www.ietf.org/rfc/rfc1738.txt
79 * Return value: An unescaped string. This should be freed with g_free().
81 static gchar* uri_unescape(const gchar* uri, size_t len)
89 /* Allocate an extra string so we can be sure that it is null-terminated,
90 * so we can use gnome_vfs_unescape_string().
91 * This is not efficient. */
92 gchar * escaped_nullterminated = g_strndup (uri, len);
93 gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
94 g_free (escaped_nullterminated);
100 * @mailto A mailto URI, with the mailto: prefix.
101 * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings,
102 * with each name item being followed by a value item. This list should be freed with g_slist_free) after
103 * all the string items have been freed. This parameter may be NULL.
104 * Parse a mailto URI as per RFC2368.
105 * http://www.ietf.org/rfc/rfc2368.txt
107 * Return value: The to address, unescaped. This should be freed with g_free().
109 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
111 /* The URL must begin with mailto: */
112 if (strncmp (mailto, "mailto:", 7) != 0) {
115 const gchar* start_to = mailto + 7;
117 /* Look for ?, or the end of the string, marking the end of the to address: */
118 const size_t len_to = strcspn (start_to, "?");
119 gchar* result_to = uri_unescape (start_to, len_to);
120 printf("debug: result_to=%s\n", result_to);
122 if (list_items_and_values == NULL) {
126 /* Get any other items: */
127 const size_t len_mailto = strlen (start_to);
128 const gchar* p = start_to + len_to + 1; /* parsed so far. */
129 const gchar* end = start_to + len_mailto;
131 const gchar *name, *value, *name_start, *name_end, *value_start, *value_end;
133 name_end = strchr (name_start, '='); /* Separator between name and value */
134 if (name_end == NULL) {
135 g_debug ("Malformed URI: %s\n", mailto);
138 value_start = name_end + 1;
139 value_end = strchr (value_start, '&'); /* Separator between value and next parameter */
141 name = g_strndup(name_start, name_end - name_start);
142 if (value_end != NULL) {
143 value = uri_unescape(value_start, value_end - value_start);
146 value = uri_unescape(value_start, -1);
149 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) name);
150 *list_items_and_values = g_slist_append (*list_items_and_values, (gpointer) value);
157 check_and_offer_account_creation()
159 gboolean result = TRUE;
161 /* This is called from idle handlers, so lock gdk: */
162 gdk_threads_enter ();
164 if (!modest_account_mgr_has_accounts(modest_runtime_get_account_mgr(), TRUE)) {
165 printf ("DEBUG1: %s\n", __FUNCTION__);
166 const gboolean created = modest_ui_actions_run_account_setup_wizard (NULL);
167 printf ("DEBUG1: %s\n", __FUNCTION__);
169 g_debug ("modest: %s: no account exists even after offering, "
170 "or account setup was already underway.\n", __FUNCTION__);
175 gdk_threads_leave ();
181 on_idle_mail_to(gpointer user_data)
183 gchar *uri = (gchar*)user_data;
184 GSList *list_names_and_values = NULL;
186 const gchar *cc = NULL;
187 const gchar *bcc = NULL;
188 const gchar *subject = NULL;
189 const gchar *body = NULL;
190 if (!check_and_offer_account_creation ()) {
194 /* Get the relevant items from the list: */
195 to = uri_parse_mailto (uri, &list_names_and_values);
196 GSList *list = list_names_and_values;
198 GSList *list_value = g_slist_next (list);
199 const gchar * name = (const gchar*)list->data;
200 const gchar * value = (const gchar*)list_value->data;
202 if (strcmp (name, "cc") == 0) {
204 } else if (strcmp (name, "bcc") == 0) {
206 } else if (strcmp (name, "subject") == 0) {
208 } else if (strcmp (name, "body") == 0) {
212 list = g_slist_next (list_value);
215 gdk_threads_enter (); /* CHECKED */
216 modest_ui_actions_compose_msg(NULL, to, cc, bcc, subject, body, NULL);
217 gdk_threads_leave (); /* CHECKED */
220 /* Free the to: and the list, as required by uri_parse_mailto() */
222 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
223 g_slist_free (list_names_and_values);
227 return FALSE; /* Do not call this callback again. */
231 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
233 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
236 /* Use g_idle to context-switch into the application's thread: */
238 /* Get the arguments: */
239 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
240 gchar *uri = g_strdup (val.value.s);
242 /* printf(" debug: to=%s\n", idle_data->to); */
243 g_idle_add(on_idle_mail_to, (gpointer)uri);
245 /* Note that we cannot report failures during sending,
246 * because that would be asynchronous. */
252 on_idle_compose_mail(gpointer user_data)
254 GSList *attachments = NULL;
255 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
256 if (!check_and_offer_account_creation ()) {
260 /* it seems Sketch at least sends a leading ',' -- take that into account,
262 if (idle_data->attachments && idle_data->attachments[0]==',') {
263 gchar *tmp = g_strdup (idle_data->attachments + 1);
264 g_free(idle_data->attachments);
265 idle_data->attachments = tmp;
267 if (idle_data->attachments != NULL) {
268 gchar **list = g_strsplit(idle_data->attachments, ",", 0);
270 for (i=0; list[i] != NULL; i++) {
271 attachments = g_slist_append(attachments, g_strdup(list[i]));
275 gdk_threads_enter (); /* CHECKED */
276 modest_ui_actions_compose_msg(NULL, idle_data->to, idle_data->cc,
277 idle_data->bcc, idle_data->subject,
278 idle_data->body, attachments);
279 gdk_threads_leave (); /* CHECKED */
281 g_slist_foreach(attachments, (GFunc)g_free, NULL);
282 g_slist_free(attachments);
283 g_free (idle_data->to);
284 g_free (idle_data->cc);
285 g_free (idle_data->bcc);
286 g_free (idle_data->subject);
287 g_free (idle_data->body);
288 g_free (idle_data->attachments);
291 return FALSE; /* Do not call this callback again. */
294 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
296 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
299 /* Use g_idle to context-switch into the application's thread: */
300 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
302 /* Get the arguments: */
303 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
304 idle_data->to = g_strdup (val.value.s);
306 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
307 idle_data->cc = g_strdup (val.value.s);
309 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
310 idle_data->bcc = g_strdup (val.value.s);
312 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
313 idle_data->subject = g_strdup (val.value.s);
315 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
316 idle_data->body = g_strdup (val.value.s);
318 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
319 idle_data->attachments = g_strdup (val.value.s);
321 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
323 /* Note that we cannot report failures during sending,
324 * because that would be asynchronous. */
329 find_message_by_url (const char *uri, TnyAccount **ac_out)
331 ModestTnyAccountStore *astore;
340 astore = modest_runtime_get_account_store ();
342 if (astore == NULL) {
346 if (uri && g_str_has_prefix (uri, "merge://")) {
347 /* we assume we're talking about outbox folder, as this
348 * is the only merge folder we work with in modest */
349 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
352 printf ("DEBUG: %s: uri=%s\n", __FUNCTION__, uri);
353 /* TODO: When tinymail is built with the extra DBC assertion checks,
354 * this will crash for local folders (such as drafts),
355 * because tny_folder_get_url_string() (in add_hit())
356 * returns mail:/home/murrayc/yaddayadda
357 * instead of mail://localhost/home/murrayc/yaddayadd,
358 * but I'm not sure where that folder URI is built. murrayc.
360 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
363 if (account == NULL) {
364 g_debug ("%s: tny_account_store_find_account() failed for\n uri=%s\n",
369 g_debug ("%s: Found account.\n", __FUNCTION__);
371 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
375 g_debug ("%s: Account is store account.\n", __FUNCTION__);
378 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
382 if (folder == NULL) {
383 g_debug ("%s: tny_store_account_find_folder() failed for\n account=%s, uri=%s.\n", __FUNCTION__,
384 tny_account_get_id (TNY_ACCOUNT(account)), uri);
388 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
390 msg = tny_folder_find_msg (folder, uri, &err);
393 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
394 __FUNCTION__, tny_folder_get_id (folder), err->message);
401 if (account && !msg) {
402 g_object_unref (account);
407 g_object_unref (folder);
413 on_idle_open_message (gpointer user_data)
416 TnyAccount *account = NULL;
417 TnyHeader *header = NULL;
418 const char *msg_uid = NULL;
420 ModestWindowMgr *win_mgr = NULL;
421 TnyFolder *folder = NULL;
423 uri = (char *) user_data;
425 /* g_debug ("modest: %s: Trying to find msg by url: %s", __FUNCTION__, uri); */
426 msg = find_message_by_url (uri, &account);
430 g_debug ("modest: %s: message not found.", __FUNCTION__);
433 g_debug ("modest: %s: Found message.", __FUNCTION__);
436 folder = tny_msg_get_folder (msg);
438 /* Drafts will be opened in the editor, instead of the viewer, as per the UI spec: */
439 /* FIXME: same should happen for Outbox; not enabling that, as the handling
440 * of edited messages is not clear in that case */
441 gboolean is_draft = FALSE;
442 if (folder && modest_tny_folder_is_local_folder (folder) &&
443 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
447 header = tny_msg_get_header (msg);
449 /* TODO: The modest_tny_folder_get_header_unique_id() documentation warns against
450 * using it with tny_msg_get_header(), and there is a
451 * " camel_folder_get_full_name: assertion `CAMEL_IS_FOLDER (folder)' failed" runtime warning,
452 * but it seems to work.
454 msg_uid = modest_tny_folder_get_header_unique_id(header);
456 win_mgr = modest_runtime_get_window_mgr ();
458 /* This is a GDK lock because we are an idle callback and
459 * the code below is or does Gtk+ code */
461 gdk_threads_enter (); /* CHECKED */
463 gboolean already_opened = FALSE;
464 ModestWindow *msg_view = NULL;
465 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
467 g_debug ("modest: %s: A window for this message is open already: type=%s",
468 __FUNCTION__, G_OBJECT_TYPE_NAME (msg_view));
472 g_debug ("modest_window_mgr_find_registered_header(): Returned TRUE, but msg_view is NULL");
473 else if (!MODEST_IS_MSG_VIEW_WINDOW (msg_view) && !MODEST_IS_MSG_EDIT_WINDOW (msg_view))
474 g_debug (" DEBUG: But the window is not a msg view or edit window.");
476 gtk_window_present (GTK_WINDOW(msg_view));
477 already_opened = TRUE;
481 if (!already_opened) {
482 /* g_debug ("creating new window for this msg"); */
483 modest_window_mgr_register_header (win_mgr, header, NULL);
485 const gchar *modest_account_name =
486 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
488 /* Drafts will be opened in the editor, and others will be opened in the viewer,
489 * as per the UI spec: */
491 /* TODO: Maybe the msg_uid should be registered for edit windows too,
492 * so we can open the same window again next time: */
493 msg_view = modest_msg_edit_window_new (msg, modest_account_name, TRUE);
495 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name,
499 modest_window_mgr_register_window (win_mgr, msg_view);
500 gtk_widget_show_all (GTK_WIDGET (msg_view));
503 gdk_threads_leave (); /* CHECKED */
505 g_object_unref (header);
506 g_object_unref (account);
507 g_object_unref (folder);
509 return FALSE; /* Do not call this callback again. */
512 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
514 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
517 /* Use g_idle to context-switch into the application's thread: */
519 /* Get the arguments: */
520 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
521 gchar *uri = g_strdup (val.value.s);
523 /* printf(" debug: to=%s\n", idle_data->to); */
524 g_idle_add(on_idle_open_message, (gpointer)uri);
526 /* Note that we cannot report failures during sending,
527 * because that would be asynchronous. */
532 on_idle_delete_message (gpointer user_data)
534 TnyList *headers = NULL;
535 TnyFolder *folder = NULL;
536 TnyIterator *iter = NULL;
537 TnyHeader *header = NULL, *msg_header = NULL;
539 TnyAccount *account = NULL;
540 const char *uri = NULL, *uid = NULL;
542 ModestMailOperation *mail_op = NULL;
543 ModestWindow *win = NULL, *msg_view = NULL;
544 ModestWindowMgr *win_mgr = NULL;
546 uri = (char *) user_data;
548 /* g_debug ("modest: %s Searching for message (delete message)"); */
550 msg = find_message_by_url (uri, &account);
556 g_debug ("modest: %s: Found message", __FUNCTION__);
558 msg_header = tny_msg_get_header (msg);
559 uid = tny_header_get_uid (msg_header);
560 folder = tny_msg_get_folder (msg);
563 /* tny_msg_get_header () flaw:
564 * From tinythingy doc: You can't use the returned instance with the
565 * TnyFolder operations
567 * To get a header instance that will work with these folder methods,
568 * you can use tny_folder_get_headers.
570 * Ok, we will do so then. Sigh.
572 headers = tny_simple_list_new ();
574 tny_folder_get_headers (folder, headers, TRUE, NULL);
575 iter = tny_list_create_iterator (headers);
578 /* g_debug ("Searching header for msg in folder"); */
579 while (!tny_iterator_is_done (iter)) {
580 const char *cur_id = NULL;
582 header = TNY_HEADER (tny_iterator_get_current (iter));
584 cur_id = tny_header_get_uid (header);
586 if (cur_id && uid && g_str_equal (cur_id, uid)) {
587 /* g_debug ("Found corresponding header from folder"); */
592 g_object_unref (header);
596 tny_iterator_next (iter);
599 g_object_unref (iter);
601 g_object_unref (headers);
604 g_object_unref (msg_header);
606 g_object_unref (msg);
609 if (header == NULL) {
611 g_object_unref (folder);
618 /* This is a GDK lock because we are an idle callback and
619 * the code below is or does Gtk+ code */
621 gdk_threads_enter (); /* CHECKED */
622 win_mgr = modest_runtime_get_window_mgr ();
623 win = modest_window_mgr_get_main_window (win_mgr);
625 mail_op = modest_mail_operation_new (win ? G_OBJECT(win) : NULL);
626 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op);
627 modest_mail_operation_remove_msg (mail_op, header, FALSE);
628 g_object_unref (G_OBJECT (mail_op));
630 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
631 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
632 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
635 gdk_threads_leave (); /* CHECKED */
638 g_object_unref (header);
641 /* Trick: do a poke status in order to speed up the signaling
643 A delete via the menu does this, in do_headers_action(),
644 though I don't know why.
646 tny_folder_poke_status (folder);
648 g_object_unref (folder);
652 g_object_unref (account);
654 /* Refilter the header view explicitly, to make sure that
655 * deleted emails are really removed from view.
656 * (They are not really deleted until contact is made with the server,
657 * so they would appear with a strike-through until then):
659 ModestHeaderView *header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (
660 MODEST_MAIN_WINDOW(win), MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
661 if (header_view && MODEST_IS_HEADER_VIEW (header_view))
662 modest_header_view_refilter (header_view);
671 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
673 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
676 /* Use g_idle to context-switch into the application's thread: */
678 /* Get the arguments: */
679 osso_rpc_t val = g_array_index (arguments,
681 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
682 gchar *uri = g_strdup (val.value.s);
684 /* printf(" debug: to=%s\n", idle_data->to); */
685 g_idle_add(on_idle_delete_message, (gpointer)uri);
687 /* Note that we cannot report failures during sending,
688 * because that would be asynchronous. */
693 on_idle_send_receive(gpointer user_data)
697 /* This is a GDK lock because we are an idle callback and
698 * the code below is or does Gtk+ code */
700 gdk_threads_enter (); /* CHECKED */
702 /* Pick the main window if it exists */
703 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
705 /* Send & receive all if "Update automatically" is set */
706 /* TODO: check the auto-update parameter in the configuration */
707 modest_ui_actions_do_send_receive_all (win);
709 gdk_threads_leave (); /* CHECKED */
711 return FALSE; /* Do not call this callback again. */
714 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
716 printf("DEBUG: modest: %s\n", __FUNCTION__);
717 /* Use g_idle to context-switch into the application's thread: */
719 /* This method has no arguments. */
721 /* printf(" debug: to=%s\n", idle_data->to); */
722 g_idle_add(on_idle_send_receive, NULL);
724 /* Note that we cannot report failures during send/receive,
725 * because that would be asynchronous. */
729 static gboolean on_idle_top_application (gpointer user_data);
732 on_idle_open_default_inbox(gpointer user_data)
734 if (!check_and_offer_account_creation ())
737 /* This is a GDK lock because we are an idle callback and
738 * the code below is or does Gtk+ code */
740 gdk_threads_enter (); /* CHECKED */
743 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
745 /* Get the folder view */
746 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
747 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
748 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
750 gdk_threads_leave (); /* CHECKED */
752 /* This D-Bus method is obviously meant to result in the UI being visible,
753 * so show it, by calling this idle handler directly: */
754 on_idle_top_application(user_data);
756 return FALSE; /* Do not call this callback again. */
759 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
761 /* Use g_idle to context-switch into the application's thread: */
763 /* This method has no arguments. */
765 g_idle_add(on_idle_open_default_inbox, NULL);
767 /* Note that we cannot report failures during send/receive,
768 * because that would be asynchronous. */
773 static gboolean on_idle_top_application (gpointer user_data)
776 /* This is a GDK lock because we are an idle callback and
777 * the code below is or does Gtk+ code */
779 gdk_threads_enter (); /* CHECKED */
782 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
784 /* Ideally, we would just use gtk_widget_show(),
785 * but this widget is not coded correctly to support that: */
786 gtk_widget_show_all (GTK_WIDGET (win));
787 gtk_window_present (GTK_WINDOW (win));
790 gdk_threads_leave (); /* CHECKED */
792 return FALSE; /* Do not call this callback again. */
795 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
797 /* Use g_idle to context-switch into the application's thread: */
799 /* This method has no arguments. */
801 g_idle_add(on_idle_top_application, NULL);
806 /* Callback for normal D-BUS messages */
807 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
808 GArray * arguments, gpointer data,
812 /* g_debug ("debug: %s\n", __FUNCTION__); */
813 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
815 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
816 return on_mail_to (arguments, data, retval);
817 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
818 return on_open_message (arguments, data, retval);
819 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
820 return on_send_receive (arguments, data, retval);
821 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
822 return on_compose_mail (arguments, data, retval);
823 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
824 return on_delete_message (arguments,data, retval);
825 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
826 return on_open_default_inbox (arguments, data, retval);
827 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
828 return on_top_application (arguments, data, retval);
831 /* We need to return INVALID here so
832 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
833 * so that our modest_dbus_req_filter will then be tried instead.
839 /* A complex D-Bus type (like a struct),
840 * used to return various information about a search hit.
842 #define SEARCH_HIT_DBUS_TYPE \
843 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
844 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
845 DBUS_TYPE_STRING_AS_STRING /* subject */ \
846 DBUS_TYPE_STRING_AS_STRING /* folder */ \
847 DBUS_TYPE_STRING_AS_STRING /* sender */ \
848 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
849 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
850 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
851 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
852 DBUS_STRUCT_END_CHAR_AS_STRING
855 search_result_to_message (DBusMessage *reply,
858 DBusMessageIter iter;
859 DBusMessageIter array_iter;
862 dbus_message_iter_init_append (reply, &iter);
863 dbus_message_iter_open_container (&iter,
865 SEARCH_HIT_DBUS_TYPE,
868 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
869 DBusMessageIter struct_iter;
870 ModestSearchHit *hit;
876 gboolean has_attachment;
880 hit = (ModestSearchHit *) hit_iter->data;
882 msg_url = hit->msgid;
883 subject = hit->subject;
884 folder = hit->folder;
885 sender = hit->sender;
887 has_attachment = hit->has_attachment;
888 is_unread = hit->is_unread;
891 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
893 dbus_message_iter_open_container (&array_iter,
898 dbus_message_iter_append_basic (&struct_iter,
902 dbus_message_iter_append_basic (&struct_iter,
906 dbus_message_iter_append_basic (&struct_iter,
910 dbus_message_iter_append_basic (&struct_iter,
914 dbus_message_iter_append_basic (&struct_iter,
918 dbus_message_iter_append_basic (&struct_iter,
922 dbus_message_iter_append_basic (&struct_iter,
926 dbus_message_iter_append_basic (&struct_iter,
930 dbus_message_iter_close_container (&array_iter,
934 g_free (hit->subject);
935 g_free (hit->folder);
936 g_free (hit->sender);
938 g_slice_free (ModestSearchHit, hit);
941 dbus_message_iter_close_container (&iter, &array_iter);
948 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
950 ModestDBusSearchFlags dbus_flags;
951 DBusMessage *reply = NULL;
955 dbus_int32_t flags_v;
956 dbus_uint32_t size_v;
964 dbus_error_init (&error);
969 res = dbus_message_get_args (message,
971 DBUS_TYPE_STRING, &query,
972 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
973 DBUS_TYPE_INT64, &sd_v,
974 DBUS_TYPE_INT64, &ed_v,
975 DBUS_TYPE_INT32, &flags_v,
976 DBUS_TYPE_UINT32, &size_v,
979 dbus_flags = (ModestDBusSearchFlags) flags_v;
980 start_date = (time_t) sd_v;
981 end_date = (time_t) ed_v;
984 memset (&search, 0, sizeof (search));
986 /* Remember what folder we are searching in:
988 * Note that we don't copy the strings,
989 * because this struct will only be used for the lifetime of this function.
991 if (folder && g_str_has_prefix (folder, "MAND:")) {
992 search.folder = folder + strlen ("MAND:");
993 } else if (folder && g_str_has_prefix (folder, "USER:")) {
994 search.folder = folder + strlen ("USER:");
995 } else if (folder && g_str_has_prefix (folder, "MY:")) {
996 search.folder = folder + strlen ("MY:");
998 search.folder = folder;
1001 /* Remember the text to search for: */
1002 #ifdef MODEST_HAVE_OGS
1003 search.query = query;
1006 /* Other criteria: */
1007 search.start_date = start_date;
1008 search.end_date = end_date;
1011 /* Text to serach for in various parts of the message: */
1012 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1013 search.flags |= MODEST_SEARCH_SUBJECT;
1014 search.subject = query;
1017 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1018 search.flags |= MODEST_SEARCH_SENDER;
1019 search.from = query;
1022 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1023 search.flags |= MODEST_SEARCH_RECIPIENT;
1024 search.recipient = query;
1027 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1028 search.flags |= MODEST_SEARCH_BODY;
1029 search.body = query;
1033 search.flags |= MODEST_SEARCH_BEFORE;
1034 search.start_date = start_date;
1038 search.flags |= MODEST_SEARCH_AFTER;
1039 search.end_date = end_date;
1043 search.flags |= MODEST_SEARCH_SIZE;
1044 search.minsize = size_v;
1047 #ifdef MODEST_HAVE_OGS
1048 search.flags |= MODEST_SEARCH_USE_OGS;
1049 g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1052 /* Note that this currently gets folders and messages from the servers,
1053 * which can take a long time. libmodest_dbus_client_search() can timeout,
1054 * reporting no results, if this takes a long time: */
1055 hits = modest_search_all_accounts (&search);
1057 reply = dbus_message_new_method_return (message);
1059 search_result_to_message (reply, hits);
1061 if (reply == NULL) {
1062 g_warning ("%s: Could not create reply.", __FUNCTION__);
1066 dbus_uint32_t serial = 0;
1067 dbus_connection_send (con, reply, &serial);
1068 dbus_connection_flush (con);
1069 dbus_message_unref (reply);
1076 /* A complex D-Bus type (like a struct),
1077 * used to return various information about a folder.
1079 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1080 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1081 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1082 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1083 DBUS_STRUCT_END_CHAR_AS_STRING
1085 static DBusMessage *
1086 get_folders_result_to_message (DBusMessage *reply,
1089 DBusMessageIter iter;
1090 dbus_message_iter_init_append (reply, &iter);
1092 DBusMessageIter array_iter;
1093 dbus_message_iter_open_container (&iter,
1095 GET_FOLDERS_RESULT_DBUS_TYPE,
1098 GList *list_iter = folder_ids;
1099 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1101 const gchar *folder_name = (const gchar*)list_iter->data;
1103 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1105 DBusMessageIter struct_iter;
1106 dbus_message_iter_open_container (&array_iter,
1112 dbus_message_iter_append_basic (&struct_iter,
1114 &folder_name); /* The string will be copied. */
1116 /* URI: This is maybe not needed by osso-global-search: */
1117 const gchar *folder_uri = "TODO:unimplemented";
1118 dbus_message_iter_append_basic (&struct_iter,
1120 &folder_uri); /* The string will be copied. */
1122 dbus_message_iter_close_container (&array_iter,
1127 dbus_message_iter_close_container (&iter, &array_iter);
1133 add_single_folder_to_list (TnyFolder *folder, GList** list)
1138 if (TNY_IS_MERGE_FOLDER (folder)) {
1139 const gchar * folder_name;
1140 /* Ignore these because their IDs ares
1141 * a) not always unique or sensible.
1142 * b) not human-readable, and currently need a human-readable
1143 * ID here, because the osso-email-interface API does not allow
1144 * us to return both an ID and a display name.
1146 * This is actually the merged outbox folder.
1147 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1148 * but that seems unwise. murrayc.
1150 folder_name = tny_folder_get_name (folder);
1151 if (folder_name && !strcmp (folder_name, "Outbox")) {
1152 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1157 /* Add this folder to the list: */
1159 const gchar * folder_name = tny_folder_get_name (folder);
1161 *list = g_list_append(*list, g_strdup (folder_name));
1164 /* osso-global-search only uses one string,
1165 * so ID is the only thing that could possibly identify a folder.
1166 * TODO: osso-global search should probably be changed to
1167 * take an ID and a Name.
1169 const gchar * id = tny_folder_get_id (folder);
1170 if (id && strlen(id)) {
1171 const gchar *prefix = NULL;
1172 TnyFolderType folder_type;
1174 /* dbus global search api expects a prefix identifying the type of
1175 * folder here. Mandatory folders should have MAND: prefix, and
1176 * other user created folders should have USER: prefix
1178 folder_type = modest_tny_folder_guess_folder_type (folder);
1179 switch (folder_type) {
1180 case TNY_FOLDER_TYPE_INBOX:
1183 case TNY_FOLDER_TYPE_OUTBOX:
1184 case TNY_FOLDER_TYPE_DRAFTS:
1185 case TNY_FOLDER_TYPE_SENT:
1186 case TNY_FOLDER_TYPE_ARCHIVE:
1189 case TNY_FOLDER_TYPE_INVALID:
1190 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
1191 return; /* don't add it */
1198 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1203 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1208 /* Add this folder to the list: */
1209 if (TNY_IS_FOLDER (folder_store)) {
1210 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1213 /* Recurse into child folders: */
1215 /* Get the folders list: */
1217 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1218 tny_folder_store_query_add_item (query, NULL,
1219 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1221 TnyList *all_folders = tny_simple_list_new ();
1222 tny_folder_store_get_folders (folder_store,
1227 TnyIterator *iter = tny_list_create_iterator (all_folders);
1228 while (!tny_iterator_is_done (iter)) {
1230 /* Do not recurse, because the osso-global-search UI specification
1231 * does not seem to want the sub-folders, though that spec seems to
1232 * be generally unsuitable for Modest.
1234 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1236 add_single_folder_to_list (TNY_FOLDER (folder), list);
1239 if (TNY_IS_FOLDER_STORE (folder))
1240 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1242 add_single_folder_to_list (TNY_FOLDER (folder), list);
1246 /* tny_iterator_get_current() gave us a reference. */
1247 g_object_unref (folder);
1250 tny_iterator_next (iter);
1252 g_object_unref (G_OBJECT (iter));
1256 /* return >1 for a special folder, 0 for a user-folder */
1258 get_rank (const gchar *folder)
1260 if (strcmp (folder, "INBOX") == 0)
1262 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1264 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1266 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1272 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1274 gint r1 = get_rank (folder1);
1275 gint r2 = get_rank (folder2);
1277 if (r1 > 0 && r2 > 0)
1279 if (r1 > 0 && r2 == 0)
1281 if (r1 == 0 && r2 > 0)
1284 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1288 /* - we're still missing the outbox */
1289 /* - we need to take care of localization (urgh) */
1290 /* - what about 'All mail folders'? */
1292 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1294 DBusMessage *reply = NULL;
1295 ModestAccountMgr *account_mgr = NULL;
1296 gchar *account_name = NULL;
1297 GList *folder_names = NULL;
1298 TnyAccount *account_local = NULL;
1299 TnyAccount *account_mmc = NULL;
1301 /* Get the TnyStoreAccount so we can get the folders: */
1302 account_mgr = modest_runtime_get_account_mgr();
1303 account_name = modest_account_mgr_get_default_account (account_mgr);
1304 if (!account_name) {
1305 g_printerr ("modest: no account found\n");
1309 TnyAccount *account = NULL;
1311 account = modest_tny_account_store_get_server_account (
1312 modest_runtime_get_account_store(), account_name,
1313 TNY_ACCOUNT_TYPE_STORE);
1317 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1320 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1321 g_free (account_name);
1322 account_name = NULL;
1324 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1326 g_object_unref (account);
1330 /* Also add the folders from the local folders account,
1331 * because they are (currently) used with all accounts:
1332 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1335 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1336 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1338 g_object_unref (account_local);
1339 account_local = NULL;
1341 /* Obtain the mmc account */
1343 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1345 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1346 g_object_unref (account_mmc);
1350 /* specs require us to sort the folder names, although
1351 * this is really not the place to do that...
1353 folder_names = g_list_sort (folder_names,
1354 (GCompareFunc)folder_name_compare_func);
1356 /* Put the result in a DBus reply: */
1357 reply = dbus_message_new_method_return (message);
1359 get_folders_result_to_message (reply, folder_names);
1361 if (reply == NULL) {
1362 g_warning ("%s: Could not create reply.", __FUNCTION__);
1366 dbus_uint32_t serial = 0;
1367 dbus_connection_send (con, reply, &serial);
1368 dbus_connection_flush (con);
1369 dbus_message_unref (reply);
1372 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1373 g_list_free (folder_names);
1377 /** This D-Bus handler is used when the main osso-rpc
1378 * D-Bus handler has not handled something.
1379 * We use this for D-Bus methods that need to use more complex types
1380 * than osso-rpc supports.
1383 modest_dbus_req_filter (DBusConnection *con,
1384 DBusMessage *message,
1387 gboolean handled = FALSE;
1389 if (dbus_message_is_method_call (message,
1391 MODEST_DBUS_METHOD_SEARCH)) {
1392 on_dbus_method_search (con, message);
1394 } else if (dbus_message_is_method_call (message,
1396 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1397 on_dbus_method_get_folders (con, message);
1401 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
1403 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
1404 __FUNCTION__, dbus_message_get_interface (message),
1405 dbus_message_get_member(message));
1410 DBUS_HANDLER_RESULT_HANDLED :
1411 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1416 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1419 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1421 if(state->system_inactivity_ind)
1424 else if(state->save_unsaved_data_ind)
1432 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */