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;
191 /* Get the relevant items from the list: */
192 gchar *to = uri_parse_mailto (uri, &list_names_and_values);
193 GSList *list = list_names_and_values;
195 GSList *list_value = g_slist_next (list);
196 const gchar * name = (const gchar*)list->data;
197 const gchar * value = (const gchar*)list_value->data;
199 if (strcmp (name, "cc") == 0) {
201 } else if (strcmp (name, "bcc") == 0) {
203 } else if (strcmp (name, "subject") == 0) {
205 } else if (strcmp (name, "body") == 0) {
209 list = g_slist_next (list_value);
212 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
214 idle_data->to = g_strdup (to);
215 idle_data->cc = g_strdup (cc);
216 idle_data->bcc = g_strdup (bcc);
217 idle_data->subject = g_strdup (subject);
218 idle_data->body = g_strdup (body);
219 idle_data->attachments = NULL;
221 /* Free the to: and the list, as required by uri_parse_mailto() */
223 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
224 g_slist_free (list_names_and_values);
228 on_idle_compose_mail((gpointer)idle_data);
230 return FALSE; /* Do not call this callback again. */
234 on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
236 if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
239 /* Use g_idle to context-switch into the application's thread: */
241 /* Get the arguments: */
242 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
243 gchar *uri = g_strdup (val.value.s);
245 /* printf(" debug: to=%s\n", idle_data->to); */
246 g_idle_add(on_idle_mail_to, (gpointer)uri);
248 /* Note that we cannot report failures during sending,
249 * because that would be asynchronous. */
255 on_idle_compose_mail(gpointer user_data)
257 if (!check_and_offer_account_creation ())
260 ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
264 /* Get the TnyTransportAccount so we can instantiate a mail operation: */
265 ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
266 gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
268 g_printerr ("modest: no account found.\n");
270 /* TODO: If the call to this D-Bus method caused the application to start
271 * then the new-account wizard will now be shown, and we need to wait
272 * until the account exists instead of just failing.
276 TnyAccount *account = NULL;
277 if (account_name && account_mgr) {
278 account = modest_tny_account_store_get_transport_account_for_open_connection (
279 modest_runtime_get_account_store(), account_name);
283 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
285 gchar * from = modest_account_mgr_get_from_string (account_mgr,
288 g_printerr ("modest: no from address for account '%s'\n", account_name);
290 /* Get the signature.
291 * TODO: This, like much of this function is copy/pasted from
292 * modest_ui_actions_on_new_msg(): */
293 gboolean use_signature = FALSE;
294 gchar *signature = modest_account_mgr_get_signature (modest_runtime_get_account_mgr (), account_name, &use_signature);
296 gchar* blank_and_signature = NULL;
298 blank_and_signature = g_strconcat ("\n", signature, NULL);
300 blank_and_signature = g_strdup ("");
304 /* Add it to the body. */
305 gchar *body_with_sig = NULL;
306 if (!(idle_data->body))
307 body_with_sig = g_strdup (blank_and_signature);
309 body_with_sig = g_strconcat (idle_data->body, blank_and_signature, NULL);
312 /* Create the message: */
313 TnyMsg *msg = modest_tny_msg_new (idle_data->to, from,
314 idle_data->cc, idle_data->bcc, idle_data->subject, body_with_sig,
315 NULL); /* NULL because m_t_m_n doesn't use it */
317 g_free (body_with_sig);
318 g_free (blank_and_signature);
321 g_printerr ("modest: failed to create message\n");
324 /* Add the message to a folder and show its UI for editing: */
325 TnyFolder *folder = modest_tny_account_get_special_folder (account,
326 TNY_FOLDER_TYPE_DRAFTS);
328 g_printerr ("modest: failed to find Drafts folder\n");
331 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
333 /* This is a GDK lock because we are an idle callback and
334 * the code below is or does Gtk+ code */
336 gdk_threads_enter (); /* CHECKED */
338 ModestWindow *win = modest_msg_edit_window_new (msg, account_name, FALSE);
340 /* it seems Sketch at least sends a leading ',' -- take that into account,
342 if (idle_data->attachments && idle_data->attachments[0]==',') {
343 gchar *tmp = g_strdup (idle_data->attachments + 1);
344 g_free(idle_data->attachments);
345 idle_data->attachments = tmp;
348 if (idle_data->attachments != NULL) {
349 list = g_strsplit(idle_data->attachments, ",", 0);
350 for (i=0; list[i] != NULL; i++) {
351 modest_msg_edit_window_attach_file_one(
352 (ModestMsgEditWindow *)win, list[i]);
357 modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
358 gtk_widget_show_all (GTK_WIDGET (win));
360 gdk_threads_leave (); /* CHECKED */
362 g_object_unref (G_OBJECT(folder));
363 g_object_unref (win);
366 g_object_unref (G_OBJECT(msg));
369 g_object_unref (G_OBJECT(account));
373 /* Free the idle data: */
374 g_free (idle_data->to);
375 g_free (idle_data->cc);
376 g_free (idle_data->bcc);
377 g_free (idle_data->subject);
378 g_free (idle_data->body);
379 g_free (idle_data->attachments);
382 g_free (account_name);
384 return FALSE; /* Do not call this callback again. */
387 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
389 if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
392 /* Use g_idle to context-switch into the application's thread: */
393 ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
395 /* Get the arguments: */
396 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
397 idle_data->to = g_strdup (val.value.s);
399 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
400 idle_data->cc = g_strdup (val.value.s);
402 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
403 idle_data->bcc = g_strdup (val.value.s);
405 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
406 idle_data->subject = g_strdup (val.value.s);
408 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
409 idle_data->body = g_strdup (val.value.s);
411 val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
412 idle_data->attachments = g_strdup (val.value.s);
414 g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
416 /* Note that we cannot report failures during sending,
417 * because that would be asynchronous. */
422 find_message_by_url (const char *uri, TnyAccount **ac_out)
424 ModestTnyAccountStore *astore;
433 astore = modest_runtime_get_account_store ();
435 if (astore == NULL) {
439 if (uri && g_str_has_prefix (uri, "merge://")) {
440 /* we assume we're talking about outbox folder, as this
441 * is the only merge folder we work with in modest */
442 return modest_tny_account_store_find_msg_in_outboxes (astore, uri, ac_out);
445 printf ("DEBUG: %s: uri=%s\n", __FUNCTION__, uri);
446 /* TODO: When tinymail is built with the extra DBC assertion checks,
447 * this will crash for local folders (such as drafts),
448 * because tny_folder_get_url_string() (in add_hit())
449 * returns mail:/home/murrayc/yaddayadda
450 * instead of mail://localhost/home/murrayc/yaddayadd,
451 * but I'm not sure where that folder URI is built. murrayc.
453 account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
456 if (account == NULL) {
457 g_debug ("%s: tny_account_store_find_account() failed for\n uri=%s\n",
462 g_debug ("%s: Found account.\n", __FUNCTION__);
464 if ( ! TNY_IS_STORE_ACCOUNT (account)) {
468 g_debug ("%s: Account is store account.\n", __FUNCTION__);
471 folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
475 if (folder == NULL) {
476 g_debug ("%s: tny_store_account_find_folder() failed for\n account=%s, uri=%s.\n", __FUNCTION__,
477 tny_account_get_id (TNY_ACCOUNT(account)), uri);
481 g_debug ("%s: Found folder. (%s)\n", __FUNCTION__, uri);
483 msg = tny_folder_find_msg (folder, uri, &err);
486 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n with error=%s.\n",
487 __FUNCTION__, tny_folder_get_id (folder), err->message);
494 if (account && !msg) {
495 g_object_unref (account);
500 g_object_unref (folder);
506 on_idle_open_message (gpointer user_data)
509 TnyAccount *account = NULL;
510 TnyHeader *header = NULL;
511 const char *msg_uid = NULL;
513 ModestWindowMgr *win_mgr = NULL;
514 TnyFolder *folder = NULL;
516 uri = (char *) user_data;
518 /* g_debug ("modest: %s: Trying to find msg by url: %s", __FUNCTION__, uri); */
519 msg = find_message_by_url (uri, &account);
523 g_debug ("modest: %s: message not found.", __FUNCTION__);
526 g_debug ("modest: %s: Found message.", __FUNCTION__);
529 folder = tny_msg_get_folder (msg);
531 /* Drafts will be opened in the editor, instead of the viewer, as per the UI spec: */
532 /* FIXME: same should happen for Outbox; not enabling that, as the handling
533 * of edited messages is not clear in that case */
534 gboolean is_draft = FALSE;
535 if (folder && modest_tny_folder_is_local_folder (folder) &&
536 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
540 header = tny_msg_get_header (msg);
542 /* TODO: The modest_tny_folder_get_header_unique_id() documentation warns against
543 * using it with tny_msg_get_header(), and there is a
544 * " camel_folder_get_full_name: assertion `CAMEL_IS_FOLDER (folder)' failed" runtime warning,
545 * but it seems to work.
547 msg_uid = modest_tny_folder_get_header_unique_id(header);
549 win_mgr = modest_runtime_get_window_mgr ();
551 /* This is a GDK lock because we are an idle callback and
552 * the code below is or does Gtk+ code */
554 gdk_threads_enter (); /* CHECKED */
556 gboolean already_opened = FALSE;
557 ModestWindow *msg_view = NULL;
558 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
560 g_debug ("modest: %s: A window for this message is open already: type=%s",
561 __FUNCTION__, G_OBJECT_TYPE_NAME (msg_view));
565 g_debug ("modest_window_mgr_find_registered_header(): Returned TRUE, but msg_view is NULL");
566 else if (!MODEST_IS_MSG_VIEW_WINDOW (msg_view) && !MODEST_IS_MSG_EDIT_WINDOW (msg_view))
567 g_debug (" DEBUG: But the window is not a msg view or edit window.");
569 gtk_window_present (GTK_WINDOW(msg_view));
570 already_opened = TRUE;
574 if (!already_opened) {
575 /* g_debug ("creating new window for this msg"); */
576 modest_window_mgr_register_header (win_mgr, header, NULL);
578 const gchar *modest_account_name =
579 modest_tny_account_get_parent_modest_account_name_for_server_account (account);
581 /* Drafts will be opened in the editor, and others will be opened in the viewer,
582 * as per the UI spec: */
584 /* TODO: Maybe the msg_uid should be registered for edit windows too,
585 * so we can open the same window again next time: */
586 msg_view = modest_msg_edit_window_new (msg, modest_account_name, TRUE);
588 msg_view = modest_msg_view_window_new_for_search_result (msg, modest_account_name,
592 modest_window_mgr_register_window (win_mgr, msg_view);
593 gtk_widget_show_all (GTK_WIDGET (msg_view));
596 gdk_threads_leave (); /* CHECKED */
598 g_object_unref (header);
599 g_object_unref (account);
600 g_object_unref (folder);
602 return FALSE; /* Do not call this callback again. */
605 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
607 if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
610 /* Use g_idle to context-switch into the application's thread: */
612 /* Get the arguments: */
613 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
614 gchar *uri = g_strdup (val.value.s);
616 /* printf(" debug: to=%s\n", idle_data->to); */
617 g_idle_add(on_idle_open_message, (gpointer)uri);
619 /* Note that we cannot report failures during sending,
620 * because that would be asynchronous. */
625 on_idle_delete_message (gpointer user_data)
627 TnyList *headers = NULL;
628 TnyFolder *folder = NULL;
629 TnyIterator *iter = NULL;
630 TnyHeader *header = NULL;
631 TnyHeader *msg_header = NULL;
633 TnyAccount *account = NULL;
634 const char *uri = NULL;
635 const char *uid = NULL;
638 uri = (char *) user_data;
640 /* g_debug ("modest: %s Searching for message (delete message)"); */
642 msg = find_message_by_url (uri, &account);
648 g_debug ("modest: %s: Found message", __FUNCTION__);
650 msg_header = tny_msg_get_header (msg);
651 uid = tny_header_get_uid (msg_header);
652 folder = tny_msg_get_folder (msg);
655 /* tny_msg_get_header () flaw:
656 * From tinythingy doc: You can't use the returned instance with the
657 * TnyFolder operations
659 * To get a header instance that will work with these folder methods,
660 * you can use tny_folder_get_headers.
662 * Ok, we will do so then. Sigh.
664 headers = tny_simple_list_new ();
666 tny_folder_get_headers (folder, headers, TRUE, NULL);
667 iter = tny_list_create_iterator (headers);
670 /* g_debug ("Searching header for msg in folder"); */
671 while (!tny_iterator_is_done (iter)) {
672 const char *cur_id = NULL;
674 header = TNY_HEADER (tny_iterator_get_current (iter));
676 cur_id = tny_header_get_uid (header);
678 if (cur_id && uid && g_str_equal (cur_id, uid)) {
679 /* g_debug ("Found corresponding header from folder"); */
684 g_object_unref (header);
688 tny_iterator_next (iter);
691 g_object_unref (iter);
693 g_object_unref (headers);
696 g_object_unref (msg_header);
698 g_object_unref (msg);
701 if (header == NULL) {
703 g_object_unref (folder);
710 /* This is a GDK lock because we are an idle callback and
711 * the code below is or does Gtk+ code */
713 gdk_threads_enter (); /* CHECKED */
714 ModestWindow *win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
715 modest_do_message_delete (header, win);
716 ModestWindowMgr *win_mgr = modest_runtime_get_window_mgr ();
717 ModestWindow *msg_view = NULL;
718 if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
719 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
720 modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
723 gdk_threads_leave (); /* CHECKED */
726 g_object_unref (header);
729 /* Trick: do a poke status in order to speed up the signaling
731 A delete via the menu does this, in do_headers_action(),
732 though I don't know why.
734 tny_folder_poke_status (folder);
736 g_object_unref (folder);
740 g_object_unref (account);
742 /* Refilter the header view explicitly, to make sure that
743 * deleted emails are really removed from view.
744 * (They are not really deleted until contact is made with the server,
745 * so they would appear with a strike-through until then):
747 ModestHeaderView *header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (
748 MODEST_MAIN_WINDOW(win), MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
749 if (header_view && MODEST_IS_HEADER_VIEW (header_view))
750 modest_header_view_refilter (header_view);
759 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
761 if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
764 /* Use g_idle to context-switch into the application's thread: */
766 /* Get the arguments: */
767 osso_rpc_t val = g_array_index (arguments,
769 MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
770 gchar *uri = g_strdup (val.value.s);
772 /* printf(" debug: to=%s\n", idle_data->to); */
773 g_idle_add(on_idle_delete_message, (gpointer)uri);
775 /* Note that we cannot report failures during sending,
776 * because that would be asynchronous. */
781 on_idle_send_receive(gpointer user_data)
785 /* This is a GDK lock because we are an idle callback and
786 * the code below is or does Gtk+ code */
788 gdk_threads_enter (); /* CHECKED */
790 /* Pick the main window if it exists */
791 win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
793 /* This seems to be necessary to show new messages in the current window.
794 * I would expect this to be after the send_receive, but maybe
795 * this makes a connection too. murrayc. */
796 modest_do_refresh_current_folder (win);
798 /* Send & receive all if "Update automatically" is set */
799 /* TODO: check the auto-update parameter in the configuration */
800 modest_ui_actions_do_send_receive_all (win);
802 gdk_threads_leave (); /* CHECKED */
804 return FALSE; /* Do not call this callback again. */
807 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
809 printf("DEBUG: modest: %s\n", __FUNCTION__);
810 /* Use g_idle to context-switch into the application's thread: */
812 /* This method has no arguments. */
814 /* printf(" debug: to=%s\n", idle_data->to); */
815 g_idle_add(on_idle_send_receive, NULL);
817 /* Note that we cannot report failures during send/receive,
818 * because that would be asynchronous. */
822 static gboolean on_idle_top_application (gpointer user_data);
825 on_idle_open_default_inbox(gpointer user_data)
827 if (!check_and_offer_account_creation ())
830 /* This is a GDK lock because we are an idle callback and
831 * the code below is or does Gtk+ code */
833 gdk_threads_enter (); /* CHECKED */
836 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
838 /* Get the folder view */
839 GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
840 MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW);
841 modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
843 gdk_threads_leave (); /* CHECKED */
845 /* This D-Bus method is obviously meant to result in the UI being visible,
846 * so show it, by calling this idle handler directly: */
847 on_idle_top_application(user_data);
849 return FALSE; /* Do not call this callback again. */
852 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
854 /* Use g_idle to context-switch into the application's thread: */
856 /* This method has no arguments. */
858 g_idle_add(on_idle_open_default_inbox, NULL);
860 /* Note that we cannot report failures during send/receive,
861 * because that would be asynchronous. */
866 static gboolean on_idle_top_application (gpointer user_data)
869 /* This is a GDK lock because we are an idle callback and
870 * the code below is or does Gtk+ code */
872 gdk_threads_enter (); /* CHECKED */
875 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
877 /* Ideally, we would just use gtk_widget_show(),
878 * but this widget is not coded correctly to support that: */
879 gtk_widget_show_all (GTK_WIDGET (win));
880 gtk_window_present (GTK_WINDOW (win));
883 gdk_threads_leave (); /* CHECKED */
885 return FALSE; /* Do not call this callback again. */
888 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
890 /* Use g_idle to context-switch into the application's thread: */
892 /* This method has no arguments. */
894 g_idle_add(on_idle_top_application, NULL);
899 /* Callback for normal D-BUS messages */
900 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
901 GArray * arguments, gpointer data,
905 /* g_debug ("debug: %s\n", __FUNCTION__); */
906 g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
908 if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
909 return on_mail_to (arguments, data, retval);
910 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
911 return on_open_message (arguments, data, retval);
912 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
913 return on_send_receive (arguments, data, retval);
914 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
915 return on_compose_mail (arguments, data, retval);
916 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
917 return on_delete_message (arguments,data, retval);
918 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
919 return on_open_default_inbox (arguments, data, retval);
920 } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
921 return on_top_application (arguments, data, retval);
924 /* We need to return INVALID here so
925 * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
926 * so that our modest_dbus_req_filter will then be tried instead.
932 /* A complex D-Bus type (like a struct),
933 * used to return various information about a search hit.
935 #define SEARCH_HIT_DBUS_TYPE \
936 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
937 DBUS_TYPE_STRING_AS_STRING /* msgid */ \
938 DBUS_TYPE_STRING_AS_STRING /* subject */ \
939 DBUS_TYPE_STRING_AS_STRING /* folder */ \
940 DBUS_TYPE_STRING_AS_STRING /* sender */ \
941 DBUS_TYPE_UINT64_AS_STRING /* msize */ \
942 DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
943 DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
944 DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
945 DBUS_STRUCT_END_CHAR_AS_STRING
948 search_result_to_message (DBusMessage *reply,
951 DBusMessageIter iter;
952 DBusMessageIter array_iter;
955 dbus_message_iter_init_append (reply, &iter);
956 dbus_message_iter_open_container (&iter,
958 SEARCH_HIT_DBUS_TYPE,
961 for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
962 DBusMessageIter struct_iter;
963 ModestSearchHit *hit;
969 gboolean has_attachment;
973 hit = (ModestSearchHit *) hit_iter->data;
975 msg_url = hit->msgid;
976 subject = hit->subject;
977 folder = hit->folder;
978 sender = hit->sender;
980 has_attachment = hit->has_attachment;
981 is_unread = hit->is_unread;
984 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);
986 dbus_message_iter_open_container (&array_iter,
991 dbus_message_iter_append_basic (&struct_iter,
995 dbus_message_iter_append_basic (&struct_iter,
999 dbus_message_iter_append_basic (&struct_iter,
1003 dbus_message_iter_append_basic (&struct_iter,
1007 dbus_message_iter_append_basic (&struct_iter,
1011 dbus_message_iter_append_basic (&struct_iter,
1015 dbus_message_iter_append_basic (&struct_iter,
1019 dbus_message_iter_append_basic (&struct_iter,
1023 dbus_message_iter_close_container (&array_iter,
1026 g_free (hit->msgid);
1027 g_free (hit->subject);
1028 g_free (hit->folder);
1029 g_free (hit->sender);
1031 g_slice_free (ModestSearchHit, hit);
1034 dbus_message_iter_close_container (&iter, &array_iter);
1041 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1043 ModestDBusSearchFlags dbus_flags;
1044 DBusMessage *reply = NULL;
1048 dbus_int32_t flags_v;
1049 dbus_uint32_t size_v;
1057 dbus_error_init (&error);
1062 res = dbus_message_get_args (message,
1064 DBUS_TYPE_STRING, &query,
1065 DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1066 DBUS_TYPE_INT64, &sd_v,
1067 DBUS_TYPE_INT64, &ed_v,
1068 DBUS_TYPE_INT32, &flags_v,
1069 DBUS_TYPE_UINT32, &size_v,
1072 dbus_flags = (ModestDBusSearchFlags) flags_v;
1073 start_date = (time_t) sd_v;
1074 end_date = (time_t) ed_v;
1076 ModestSearch search;
1077 memset (&search, 0, sizeof (search));
1079 /* Remember what folder we are searching in:
1081 * Note that we don't copy the strings,
1082 * because this struct will only be used for the lifetime of this function.
1084 if (folder && g_str_has_prefix (folder, "MAND:")) {
1085 search.folder = folder + strlen ("MAND:");
1086 } else if (folder && g_str_has_prefix (folder, "USER:")) {
1087 search.folder = folder + strlen ("USER:");
1088 } else if (folder && g_str_has_prefix (folder, "MY:")) {
1089 search.folder = folder + strlen ("MY:");
1091 search.folder = folder;
1094 /* Remember the text to search for: */
1095 #ifdef MODEST_HAVE_OGS
1096 search.query = query;
1099 /* Other criteria: */
1100 search.start_date = start_date;
1101 search.end_date = end_date;
1104 /* Text to serach for in various parts of the message: */
1105 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1106 search.flags |= MODEST_SEARCH_SUBJECT;
1107 search.subject = query;
1110 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1111 search.flags |= MODEST_SEARCH_SENDER;
1112 search.from = query;
1115 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1116 search.flags |= MODEST_SEARCH_RECIPIENT;
1117 search.recipient = query;
1120 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1121 search.flags |= MODEST_SEARCH_BODY;
1122 search.body = query;
1126 search.flags |= MODEST_SEARCH_BEFORE;
1127 search.start_date = start_date;
1131 search.flags |= MODEST_SEARCH_AFTER;
1132 search.end_date = end_date;
1136 search.flags |= MODEST_SEARCH_SIZE;
1137 search.minsize = size_v;
1140 #ifdef MODEST_HAVE_OGS
1141 search.flags |= MODEST_SEARCH_USE_OGS;
1142 g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1145 /* Note that this currently gets folders and messages from the servers,
1146 * which can take a long time. libmodest_dbus_client_search() can timeout,
1147 * reporting no results, if this takes a long time: */
1148 hits = modest_search_all_accounts (&search);
1150 reply = dbus_message_new_method_return (message);
1152 search_result_to_message (reply, hits);
1154 if (reply == NULL) {
1155 g_warning ("%s: Could not create reply.", __FUNCTION__);
1159 dbus_uint32_t serial = 0;
1160 dbus_connection_send (con, reply, &serial);
1161 dbus_connection_flush (con);
1162 dbus_message_unref (reply);
1169 /* A complex D-Bus type (like a struct),
1170 * used to return various information about a folder.
1172 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1173 DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1174 DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1175 DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1176 DBUS_STRUCT_END_CHAR_AS_STRING
1178 static DBusMessage *
1179 get_folders_result_to_message (DBusMessage *reply,
1182 DBusMessageIter iter;
1183 dbus_message_iter_init_append (reply, &iter);
1185 DBusMessageIter array_iter;
1186 dbus_message_iter_open_container (&iter,
1188 GET_FOLDERS_RESULT_DBUS_TYPE,
1191 GList *list_iter = folder_ids;
1192 for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1194 const gchar *folder_name = (const gchar*)list_iter->data;
1196 /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1198 DBusMessageIter struct_iter;
1199 dbus_message_iter_open_container (&array_iter,
1205 dbus_message_iter_append_basic (&struct_iter,
1207 &folder_name); /* The string will be copied. */
1209 /* URI: This is maybe not needed by osso-global-search: */
1210 const gchar *folder_uri = "TODO:unimplemented";
1211 dbus_message_iter_append_basic (&struct_iter,
1213 &folder_uri); /* The string will be copied. */
1215 dbus_message_iter_close_container (&array_iter,
1220 dbus_message_iter_close_container (&iter, &array_iter);
1226 add_single_folder_to_list (TnyFolder *folder, GList** list)
1231 if (TNY_IS_MERGE_FOLDER (folder)) {
1232 const gchar * folder_name;
1233 /* Ignore these because their IDs ares
1234 * a) not always unique or sensible.
1235 * b) not human-readable, and currently need a human-readable
1236 * ID here, because the osso-email-interface API does not allow
1237 * us to return both an ID and a display name.
1239 * This is actually the merged outbox folder.
1240 * We could hack our D-Bus API to understand "outbox" as the merged outboxes,
1241 * but that seems unwise. murrayc.
1243 folder_name = tny_folder_get_name (folder);
1244 if (folder_name && !strcmp (folder_name, "Outbox")) {
1245 *list = g_list_append(*list, g_strdup ("MAND:outbox"));
1250 /* Add this folder to the list: */
1252 const gchar * folder_name = tny_folder_get_name (folder);
1254 *list = g_list_append(*list, g_strdup (folder_name));
1257 /* osso-global-search only uses one string,
1258 * so ID is the only thing that could possibly identify a folder.
1259 * TODO: osso-global search should probably be changed to
1260 * take an ID and a Name.
1262 const gchar * id = tny_folder_get_id (folder);
1263 if (id && strlen(id)) {
1264 const gchar *prefix = NULL;
1265 TnyFolderType folder_type;
1267 /* dbus global search api expects a prefix identifying the type of
1268 * folder here. Mandatory folders should have MAND: prefix, and
1269 * other user created folders should have USER: prefix
1271 folder_type = modest_tny_folder_guess_folder_type (folder);
1272 switch (folder_type) {
1273 case TNY_FOLDER_TYPE_INBOX:
1276 case TNY_FOLDER_TYPE_OUTBOX:
1277 case TNY_FOLDER_TYPE_DRAFTS:
1278 case TNY_FOLDER_TYPE_SENT:
1279 case TNY_FOLDER_TYPE_ARCHIVE:
1287 *list = g_list_append(*list, g_strdup_printf ("%s%s", prefix, id));
1291 g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);
1299 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1304 /* Add this folder to the list: */
1305 if (TNY_IS_FOLDER (folder_store)) {
1306 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1309 /* Recurse into child folders: */
1311 /* Get the folders list: */
1313 TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1314 tny_folder_store_query_add_item (query, NULL,
1315 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1317 TnyList *all_folders = tny_simple_list_new ();
1318 tny_folder_store_get_folders (folder_store,
1323 TnyIterator *iter = tny_list_create_iterator (all_folders);
1324 while (!tny_iterator_is_done (iter)) {
1326 /* Do not recurse, because the osso-global-search UI specification
1327 * does not seem to want the sub-folders, though that spec seems to
1328 * be generally unsuitable for Modest.
1330 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1332 add_single_folder_to_list (TNY_FOLDER (folder), list);
1335 if (TNY_IS_FOLDER_STORE (folder))
1336 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1338 add_single_folder_to_list (TNY_FOLDER (folder), list);
1342 /* tny_iterator_get_current() gave us a reference. */
1343 g_object_unref (folder);
1346 tny_iterator_next (iter);
1348 g_object_unref (G_OBJECT (iter));
1352 /* return >1 for a special folder, 0 for a user-folder */
1354 get_rank (const gchar *folder)
1356 if (strcmp (folder, "INBOX") == 0)
1358 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_SENT)) == 0)
1360 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_DRAFTS)) == 0)
1362 if (strcmp (folder, modest_local_folder_info_get_type_name(TNY_FOLDER_TYPE_OUTBOX)) == 0)
1368 folder_name_compare_func (const gchar* folder1, const gchar* folder2)
1370 gint r1 = get_rank (folder1);
1371 gint r2 = get_rank (folder2);
1373 if (r1 > 0 && r2 > 0)
1375 if (r1 > 0 && r2 == 0)
1377 if (r1 == 0 && r2 > 0)
1380 return modest_text_utils_utf8_strcmp (folder1, folder2, TRUE);
1384 /* - we're still missing the outbox */
1385 /* - we need to take care of localization (urgh) */
1386 /* - what about 'All mail folders'? */
1388 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1390 DBusMessage *reply = NULL;
1391 ModestAccountMgr *account_mgr = NULL;
1392 gchar *account_name = NULL;
1393 GList *folder_names = NULL;
1394 TnyAccount *account_local = NULL;
1395 TnyAccount *account_mmc = NULL;
1397 /* Get the TnyStoreAccount so we can get the folders: */
1398 account_mgr = modest_runtime_get_account_mgr();
1399 account_name = modest_account_mgr_get_default_account (account_mgr);
1400 if (!account_name) {
1401 g_printerr ("modest: no account found\n");
1405 TnyAccount *account = NULL;
1407 account = modest_tny_account_store_get_server_account (
1408 modest_runtime_get_account_store(), account_name,
1409 TNY_ACCOUNT_TYPE_STORE);
1413 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1416 printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1417 g_free (account_name);
1418 account_name = NULL;
1420 add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1422 g_object_unref (account);
1426 /* Also add the folders from the local folders account,
1427 * because they are (currently) used with all accounts:
1428 * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1431 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1432 add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1434 g_object_unref (account_local);
1435 account_local = NULL;
1437 /* Obtain the mmc account */
1439 modest_tny_account_store_get_mmc_folders_account (modest_runtime_get_account_store());
1441 add_folders_to_list (TNY_FOLDER_STORE (account_mmc), &folder_names);
1442 g_object_unref (account_mmc);
1446 /* specs require us to sort the folder names, although
1447 * this is really not the place to do that...
1449 folder_names = g_list_sort (folder_names,
1450 (GCompareFunc)folder_name_compare_func);
1452 /* Put the result in a DBus reply: */
1453 reply = dbus_message_new_method_return (message);
1455 get_folders_result_to_message (reply, folder_names);
1457 if (reply == NULL) {
1458 g_warning ("%s: Could not create reply.", __FUNCTION__);
1462 dbus_uint32_t serial = 0;
1463 dbus_connection_send (con, reply, &serial);
1464 dbus_connection_flush (con);
1465 dbus_message_unref (reply);
1468 g_list_foreach (folder_names, (GFunc)g_free, NULL);
1469 g_list_free (folder_names);
1473 /** This D-Bus handler is used when the main osso-rpc
1474 * D-Bus handler has not handled something.
1475 * We use this for D-Bus methods that need to use more complex types
1476 * than osso-rpc supports.
1479 modest_dbus_req_filter (DBusConnection *con,
1480 DBusMessage *message,
1483 gboolean handled = FALSE;
1485 if (dbus_message_is_method_call (message,
1487 MODEST_DBUS_METHOD_SEARCH)) {
1488 on_dbus_method_search (con, message);
1490 } else if (dbus_message_is_method_call (message,
1492 MODEST_DBUS_METHOD_GET_FOLDERS)) {
1493 on_dbus_method_get_folders (con, message);
1497 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
1499 g_debug (" debug: %s: Unexpected (maybe already handled) D-Bus method:\n Interface=%s, Member=%s\n",
1500 __FUNCTION__, dbus_message_get_interface (message),
1501 dbus_message_get_member(message));
1506 DBUS_HANDLER_RESULT_HANDLED :
1507 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1512 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1515 /* printf("%s()\n", __PRETTY_FUNCTION__); */
1517 if(state->system_inactivity_ind)
1520 else if(state->save_unsaved_data_ind)
1528 /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */