2007-08-13 Murray Cumming <murrayc@murrayc.com>
[modest] / src / dbus_api / modest-dbus-callbacks.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29  
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
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>
43 #include <stdio.h>
44 #include <string.h>
45 #include <glib/gstdio.h>
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <libgnomevfs/gnome-vfs-mime-utils.h>
48 #else
49 #include <libgnomevfs/gnome-vfs-mime.h>
50 #endif
51 #include <tny-fs-stream.h>
52
53 #include <tny-list.h>
54 #include <tny-iterator.h>
55 #include <tny-simple-list.h>
56 #include <tny-merge-folder.h>
57
58 typedef struct 
59 {
60         gchar *to;
61         gchar *cc;
62         gchar *bcc;
63         gchar *subject;
64         gchar *body;
65         gchar *attachments;
66 } SendMailIdleData;
67
68 typedef struct 
69 {
70         gchar *to;
71         gchar *cc;
72         gchar *bcc;
73         gchar *subject;
74         gchar *body;
75         gchar *attachments;
76 } ComposeMailIdleData;
77
78 /** uri_unescape:
79  * @uri An escaped URI. URIs should always be escaped.
80  * @len The length of the @uri string, or -1 if the string is null terminated.
81  * 
82  * Decode a URI, or URI fragment, as per RFC 1738.
83  * http://www.ietf.org/rfc/rfc1738.txt
84  * 
85  * Return value: An unescaped string. This should be freed with g_free().
86  */
87 static gchar* uri_unescape(const gchar* uri, size_t len)
88 {
89         if (!uri)
90                 return NULL;
91                 
92         if (len == -1)
93                 len = strlen (uri);
94         
95         /* Allocate an extra string so we can be sure that it is null-terminated,
96          * so we can use gnome_vfs_unescape_string().
97          * This is not efficient. */
98         gchar * escaped_nullterminated = g_strndup (uri, len);
99         gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
100         g_free (escaped_nullterminated);
101         
102         return result;
103 }
104
105 /** uri_parse_mailto:
106  * @mailto A mailto URI, with the mailto: prefix.
107  * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings, 
108  * with each name item being followed by a value item. This list should be freed with g_slist_free) after 
109  * all the string items have been freed. This parameter may be NULL.
110  * Parse a mailto URI as per RFC2368.
111  * http://www.ietf.org/rfc/rfc2368.txt
112  * 
113  * Return value: The to address, unescaped. This should be freed with g_free().
114  */
115 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
116 {
117         const gchar* start_to = NULL;
118         /* Remove the mailto: prefix: 
119          * 7 is the length of "mailto:": */
120         if (strncmp (mailto, "mailto:", 7) == 0) {
121                 start_to = mailto + 7;
122         }
123         
124         if (!start_to)
125                 return NULL;
126         
127         /* Look for ?, or the end of the string, marking the end of the to address: */
128         const size_t len_to = strcspn (start_to, "?");
129         gchar* result_to = uri_unescape (start_to, len_to);
130         printf("debug: result_to=%s\n", result_to);
131         
132         /* Get any other items: */
133         const size_t len_mailto = strlen (start_to);
134         const gchar* p = start_to + len_to + 1; /* parsed so far. */
135         const gchar* end = start_to + len_mailto;
136         /* GSList *items = NULL; */
137         const gchar* start_item_name = p;
138         size_t len_item_name = 0;
139         const gchar* start_item_value = NULL;
140         while (p < end) {
141                 
142                 /* Looking for the end of a name; */
143                 if (start_item_name) {
144                         const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
145                         if (len) {
146                                 /* This marks the end of a name and the start of the value: */
147                                 len_item_name = len;
148                                 
149                                 /* Skip over the name and mark the start of the value: */
150                                 p += (len + 1); /* Skip over the = */
151                                 start_item_value = p;
152                         }
153                 }
154                 
155                 /* Looking for the end of a value: */
156                 if (start_item_value) {
157                         const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
158                         /* ? marks the start of a new item: */
159                         if (len) {
160                                 if (start_item_name && len_item_name) {
161                                         /* Finish the previously-started item: */
162                                         gchar *item_value = uri_unescape (start_item_value, len);
163                                         gchar *item_name = g_strndup (start_item_name, len_item_name);
164                                         /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
165                                         
166                                         /* Append the items to the list */
167                                         if(list_items_and_values) {
168                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
169                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
170                                         }
171                                 }
172                                 
173                                 /* Skip over the value and mark the start of a possible new name/value pair: */
174                                 p += (len + 1); /* Skip over the ? */
175                                 start_item_name = p;
176                                 len_item_name = 0;
177                                 start_item_value = NULL;
178                         }
179                 }
180                 
181         }
182         
183         return result_to;
184 }
185
186 static gboolean
187 on_idle_mail_to(gpointer user_data)
188 {
189         /* This is based on the implemenation of main.c:start_uil(): */
190         
191         gchar *uri = (gchar*)user_data;
192         GSList *list_names_and_values = NULL;
193         gchar *to = uri_parse_mailto (uri, &list_names_and_values);
194         
195         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
196         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
197         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
198         if (!account_name) {
199                 g_printerr ("modest: no account found\n");
200         }
201         
202         TnyAccount *account = NULL;
203         if (account_mgr) {
204                 account = modest_tny_account_store_get_transport_account_for_open_connection (
205                         modest_runtime_get_account_store(), account_name);
206         }
207         
208         if (!account) {
209                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
210         } else {
211                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
212                                                                   account_name);
213                 if (!from) {
214                         g_printerr ("modest: no from address for account '%s'\n", account_name);
215                 } else {
216                         const gchar *cc = NULL;
217                         const gchar *bcc = NULL;
218                         const gchar *subject = NULL;
219                         const gchar *body = NULL;
220                         
221                         /* Get the relevant items from the list: */
222                         GSList *list = list_names_and_values;
223                         while (list) {
224                                 const gchar * name = (const gchar*)list->data;
225                                 GSList *list_value = g_slist_next (list);
226                                 const gchar * value = (const gchar*)list_value->data;
227                                 
228                                 if (strcmp (name, "cc") == 0) {
229                                         cc = value;
230                                 } else if (strcmp (name, "bcc") == 0) {
231                                         bcc = value;
232                                 } else if (strcmp (name, "subject") == 0) {
233                                         subject = value;
234                                 } else if (strcmp (name, "body") == 0) {
235                                         body = value;
236                                 }
237                                 
238                                 /* Go to the next pair: */
239                                 if (list_value) {
240                                         list = g_slist_next (list_value);
241                                 } else 
242                                         list = NULL;
243                         }
244                         
245                         /* Create the message: */
246                         TnyMsg *msg  = modest_tny_msg_new (to, from, 
247                                 cc, bcc, subject, body, 
248                                 NULL /* attachments */);
249                                 
250                         if (!msg) {
251                                 g_printerr ("modest: failed to create message\n");
252                         } else
253                         {
254                                 /* Add the message to a folder and show its UI for editing: */
255                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
256                                                                         TNY_FOLDER_TYPE_DRAFTS);
257                                 if (!folder) {
258                                         g_printerr ("modest: failed to find Drafts folder\n");
259                                 } else {
260                         
261                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
262
263                                         /* This is a GDK lock because we are an idle callback and
264                                          * the code below is or does Gtk+ code */
265
266                                         gdk_threads_enter (); /* CHECKED */
267
268                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name, FALSE);
269                                         modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
270                                         gtk_widget_show_all (GTK_WIDGET (win));
271
272                                         gdk_threads_leave (); /* CHECKED */
273                                 
274                                         g_object_unref (G_OBJECT(folder));
275                                         g_object_unref (win);
276                                 }
277                         
278                                 g_object_unref (G_OBJECT(msg));
279                         }
280                         
281                         g_object_unref (G_OBJECT(account));
282                 }
283         }
284         
285         g_free (account_name);
286         
287         /* Free the list, as required by the uri_parse_mailto() documentation: */
288         if (list_names_and_values)
289                 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
290         g_slist_free (list_names_and_values);
291         
292         g_free(to);
293                 
294         g_free(uri);
295         
296         return FALSE; /* Do not call this callback again. */
297 }
298
299 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
300 {
301         if (arguments->len != MODEST_DBUS_MAIL_TO_ARGS_COUNT)
302         return OSSO_ERROR;
303         
304     /* Use g_idle to context-switch into the application's thread: */
305  
306     /* Get the arguments: */
307         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_MAIL_TO_ARG_URI);
308         gchar *uri = g_strdup (val.value.s);
309         
310         /* printf("  debug: to=%s\n", idle_data->to); */
311         g_idle_add(on_idle_mail_to, (gpointer)uri);
312         
313         /* Note that we cannot report failures during sending, 
314          * because that would be asynchronous. */
315         return OSSO_OK;
316 }
317
318
319 static gboolean
320 on_idle_compose_mail(gpointer user_data)
321 {
322         ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
323         gchar **list = NULL;
324         gint i = 0;
325
326         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
327         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
328         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
329         if (!account_name) {
330                 g_printerr ("modest: no account found.\n");
331                 
332                 /* TODO: If the call to this D-Bus method caused the application to start
333                  * then the new-account wizard will now be shown, and we need to wait 
334                  * until the account exists instead of just failing.
335                  */
336         }
337         
338         TnyAccount *account = NULL;
339         if (account_name && account_mgr) {
340                 account = modest_tny_account_store_get_transport_account_for_open_connection (
341                         modest_runtime_get_account_store(), account_name);
342         }
343         
344         if (!account) {
345                 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
346         } else {
347                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
348                                                                   account_name);
349                 if (!from) {
350                         g_printerr ("modest: no from address for account '%s'\n", account_name);
351                 } else {
352         
353                         /* Create the message: */
354                         TnyMsg *msg  = modest_tny_msg_new (idle_data->to, from, 
355                                 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body, 
356                                 NULL); /* NULL because m_t_m_n doesn't use it */
357                                 
358                         if (!msg) {
359                                 g_printerr ("modest: failed to create message\n");
360                         } else
361                         {
362                                 /* Add the message to a folder and show its UI for editing: */
363                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
364                                                                         TNY_FOLDER_TYPE_DRAFTS);
365                                 if (!folder) {
366                                         g_printerr ("modest: failed to find Drafts folder\n");
367                                 } else {
368                         
369                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
370
371                                         /* This is a GDK lock because we are an idle callback and
372                                          * the code below is or does Gtk+ code */
373
374                                         gdk_threads_enter (); /* CHECKED */
375         
376                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name, FALSE);
377
378                                         /* it seems Sketch at least sends a leading ',' -- take that into account,
379                                          * ie strip that ,*/
380                                         if (idle_data->attachments && idle_data->attachments[0]==',') {
381                                                 gchar *tmp = g_strdup (idle_data->attachments + 1);
382                                                 g_free(idle_data->attachments);
383                                                 idle_data->attachments = tmp;
384                                         }
385
386                                         list = g_strsplit(idle_data->attachments, ",", 0);
387                                         for (i=0; list[i] != NULL; i++) {
388                                                 modest_msg_edit_window_attach_file_one(
389                                                                 (ModestMsgEditWindow *)win, list[i]);
390                                         }
391                                         g_strfreev(list);
392
393                                         modest_window_mgr_register_window (modest_runtime_get_window_mgr (), win);
394                                         gtk_widget_show_all (GTK_WIDGET (win));
395
396                                         gdk_threads_leave (); /* CHECKED */
397                                 
398                                         g_object_unref (G_OBJECT(folder));
399                                         g_object_unref (win);
400                                 }
401                         
402                                 g_object_unref (G_OBJECT(msg));
403                         }
404                         
405                         g_object_unref (G_OBJECT(account));
406                 }
407         }
408
409         /* Free the idle data: */
410         g_free (idle_data->to);
411         g_free (idle_data->cc);
412         g_free (idle_data->bcc);
413         g_free (idle_data->subject);
414         g_free (idle_data->body);
415         g_free (idle_data->attachments);
416         g_free (idle_data);
417         
418         g_free (account_name);
419         
420         return FALSE; /* Do not call this callback again. */
421 }
422
423 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
424 {
425         if (arguments->len != MODEST_DBUS_COMPOSE_MAIL_ARGS_COUNT)
426         return OSSO_ERROR;
427         
428         /* Use g_idle to context-switch into the application's thread: */
429         ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
430         
431         /* Get the arguments: */
432         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_TO);
433         idle_data->to = g_strdup (val.value.s);
434         
435         val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_CC);
436         idle_data->cc = g_strdup (val.value.s);
437         
438         val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BCC);
439         idle_data->bcc = g_strdup (val.value.s);
440         
441         val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_SUBJECT);
442         idle_data->subject = g_strdup (val.value.s);
443         
444         val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_BODY);
445         idle_data->body = g_strdup (val.value.s);
446         
447         val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
448         idle_data->attachments = g_strdup (val.value.s);
449
450         g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
451         
452         /* Note that we cannot report failures during sending, 
453          * because that would be asynchronous. */
454         return OSSO_OK;
455 }
456
457
458 static TnyMsg *
459 find_message_by_url (const char *uri,  TnyAccount **ac_out)
460 {
461         ModestTnyAccountStore *astore;
462         TnyAccount            *account;
463         TnyFolder             *folder;
464         TnyMsg                *msg;
465         GError *err = NULL;
466         account = NULL;
467         msg = NULL;
468         folder = NULL;
469
470         astore = modest_runtime_get_account_store ();
471         
472         if (astore == NULL) {
473                 return NULL;
474         }
475
476         account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
477                                                   uri);
478         
479         if (account == NULL) {
480                 g_debug ("%s: tny_account_store_find_account() failed for\n  uri=%s\n", 
481                         __FUNCTION__, uri);
482                 return NULL;
483         }
484
485         g_debug ("%s: Found account.\n", __FUNCTION__);
486
487         if ( ! TNY_IS_STORE_ACCOUNT (account)) {
488                 goto out;
489         }
490
491         g_debug ("%s: Account is store account.\n", __FUNCTION__);
492         *ac_out = account;
493
494         folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
495                                                 uri,
496                                                 &err);
497
498         if (folder == NULL) {
499                 g_debug ("%s: tny_store_account_find_folder() failed for\n  account=%s, uri=%s.\n", __FUNCTION__, 
500                         tny_account_get_id (TNY_ACCOUNT(account)), uri);
501                 goto out;
502         }
503         
504         g_debug ("%s: Found folder. (%s)\n",  __FUNCTION__, uri);
505         
506         msg = tny_folder_find_msg (folder, uri, &err);
507         
508         if (!msg) {
509                 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n  with error=%s.\n",
510                          __FUNCTION__, tny_folder_get_id (folder), err->message);
511         }
512
513 out:
514         if (err)
515                 g_error_free (err);
516
517         if (account && !msg) {
518                 g_object_unref (account);
519                 *ac_out = NULL;
520         }
521
522         if (folder)
523                 g_object_unref (folder);
524
525         return msg;
526 }
527
528 static gboolean
529 on_idle_open_message (gpointer user_data)
530 {
531         TnyMsg       *msg = NULL;
532         TnyAccount   *account = NULL;
533         TnyHeader    *header = NULL; 
534         const char   *msg_uid = NULL;
535         char         *uri = NULL;
536         ModestWindowMgr *win_mgr = NULL;
537         TnyFolder    *folder = NULL;
538
539         uri = (char *) user_data;
540
541         /* g_debug ("modest: %s: Trying to find msg by url: %s", __FUNCTION__, uri); */
542         msg = find_message_by_url (uri, &account);
543         g_free (uri);
544
545         if (msg == NULL) {
546                 g_debug ("modest:  %s: message not found.", __FUNCTION__);
547                 return FALSE;
548         }
549         g_debug ("modest:  %s: Found message.", __FUNCTION__);
550
551         
552         folder = tny_msg_get_folder (msg);
553         
554         /* Drafts will be opened in the editor, instead of the viewer, as per the UI spec: */
555         /* FIXME: same should happen for Outbox; not enabling that, as the handling
556          * of edited messages is not clear in that case */
557         gboolean is_draft = FALSE;
558         if (folder && modest_tny_folder_is_local_folder (folder) &&
559                 (modest_tny_folder_get_local_or_mmc_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS)) {
560                 is_draft = TRUE;
561         }
562
563         header = tny_msg_get_header (msg);
564         
565         /* TODO:  The modest_tny_folder_get_header_unique_id() documentation warns against 
566          * using it with tny_msg_get_header(), and there is a 
567          * " camel_folder_get_full_name: assertion `CAMEL_IS_FOLDER (folder)' failed" runtime warning,
568          * but it seems to work.
569          */     
570         msg_uid =  modest_tny_folder_get_header_unique_id(header); 
571         
572         win_mgr = modest_runtime_get_window_mgr ();
573
574         /* This is a GDK lock because we are an idle callback and
575          * the code below is or does Gtk+ code */
576
577         gdk_threads_enter (); /* CHECKED */
578
579         gboolean already_opened = FALSE;
580         ModestWindow *msg_view = NULL;
581         if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
582                 if (msg_view) {
583                         g_debug ("modest: %s: A window for this message is open already: type=%s", 
584                         __FUNCTION__, G_OBJECT_TYPE_NAME (msg_view));
585                 }
586                 
587                 if (!msg_view)
588                         g_debug ("modest_window_mgr_find_registered_header(): Returned TRUE, but msg_view is NULL");
589                 else if (!MODEST_IS_MSG_VIEW_WINDOW (msg_view) && !MODEST_IS_MSG_EDIT_WINDOW (msg_view))
590                         g_debug ("  DEBUG: But the window is not a msg view or edit window.");
591                 else {
592                         gtk_window_present (GTK_WINDOW(msg_view));
593                         already_opened = TRUE;
594                 }
595         }
596         
597         if (!already_opened) {
598                 /* g_debug ("creating new window for this msg"); */
599                 modest_window_mgr_register_header (win_mgr, header);
600                 
601                 const gchar *modest_account_name = 
602                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
603                         
604                 /* Drafts will be opened in the editor, and others will be opened in the viewer, 
605                  * as per the UI spec: */
606                 if (is_draft) {
607                         /* TODO: Maybe the msg_uid should be registered for edit windows too,
608                          * so we can open the same window again next time: */
609                         msg_view = modest_msg_edit_window_new (msg, modest_account_name, TRUE);
610                 } else {
611                         msg_view = modest_msg_view_window_new (msg, modest_account_name,
612                                                        msg_uid);
613                 }
614                 
615                 modest_window_mgr_register_window (win_mgr, msg_view);
616                 gtk_widget_show_all (GTK_WIDGET (msg_view));
617         }
618
619         gdk_threads_leave (); /* CHECKED */
620
621         g_object_unref (header);
622         g_object_unref (account);
623         g_object_unref (folder);
624
625         return FALSE; /* Do not call this callback again. */
626 }
627
628 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
629 {
630         if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
631         return OSSO_ERROR;
632         
633     /* Use g_idle to context-switch into the application's thread: */
634
635     /* Get the arguments: */
636         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
637         gchar *uri = g_strdup (val.value.s);
638         
639         /* printf("  debug: to=%s\n", idle_data->to); */
640         g_idle_add(on_idle_open_message, (gpointer)uri);
641         
642         /* Note that we cannot report failures during sending, 
643          * because that would be asynchronous. */
644         return OSSO_OK;
645 }
646
647 static gboolean
648 on_idle_delete_message (gpointer user_data)
649 {
650         TnyList      *headers = NULL;
651         TnyFolder    *folder = NULL;
652         TnyIterator  *iter = NULL; 
653         TnyHeader    *header = NULL;
654         TnyHeader    *msg_header = NULL;
655         TnyMsg       *msg = NULL;
656         TnyAccount   *account = NULL;
657         GError       *error = NULL;
658         const char   *uri = NULL;
659         const char   *uid = NULL;
660         gint          res = 0;
661
662         uri = (char *) user_data;
663
664         /* g_debug ("modest: %s Searching for message (delete message)"); */
665         
666         msg = find_message_by_url (uri, &account);
667
668         if (msg == NULL) {
669                 return OSSO_ERROR;
670         }
671
672         g_debug ("modest: %s: Found message", __FUNCTION__);
673         
674         msg_header = tny_msg_get_header (msg);
675         uid = tny_header_get_uid (msg_header);
676         folder = tny_msg_get_folder (msg);
677
678
679         /* tny_msg_get_header () flaw:
680          * From tinythingy doc: You can't use the returned instance with the
681          * TnyFolder operations
682          *
683          * To get a header instance that will work with these folder methods,
684          * you can use tny_folder_get_headers.
685          *
686          * Ok, we will do so then. Sigh.
687          * */
688         headers = tny_simple_list_new ();
689
690         tny_folder_get_headers (folder, headers, TRUE, NULL);
691         iter = tny_list_create_iterator (headers);
692         header = NULL;
693
694         /* g_debug ("Searching header for msg in folder"); */
695         while (!tny_iterator_is_done (iter)) {
696                 const char *cur_id = NULL;
697
698                 header = TNY_HEADER (tny_iterator_get_current (iter));
699                 if (header)
700                         cur_id = tny_header_get_uid (header);
701                 
702                 if (cur_id && uid && g_str_equal (cur_id, uid)) {
703                         /* g_debug ("Found corresponding header from folder"); */
704                         break;
705                 }
706
707                 if (header) {
708                         g_object_unref (header);
709                         header = NULL;
710                 }
711                 
712                 tny_iterator_next (iter);
713         }
714
715         g_object_unref (iter);
716         iter = NULL;
717         g_object_unref (headers);
718         headers = NULL;
719         
720         g_object_unref (msg_header);
721         msg_header = NULL;
722         g_object_unref (msg);
723         msg = NULL;
724
725         if (header == NULL) {
726                 if (folder)
727                         g_object_unref (folder);
728                         
729                 return OSSO_ERROR;
730         }       
731                 
732         error = NULL;
733         res = OSSO_OK;
734         
735         /* This is a GDK lock because we are an idle callback and
736          * the code below is or does Gtk+ code */
737
738         gdk_threads_enter (); /* CHECKED */
739         ModestWindow *win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
740         modest_do_message_delete (header, win);
741
742         if (error != NULL) {
743                 res = OSSO_ERROR;
744                 g_error_free (error);
745         }
746         
747         
748         
749         ModestWindowMgr *win_mgr = modest_runtime_get_window_mgr ();    
750         ModestWindow *msg_view = NULL; 
751         if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
752                 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
753                         modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
754         }
755         
756         gdk_threads_leave (); /* CHECKED */
757         
758         if (header)
759                 g_object_unref (header);
760         
761         if (folder) {
762                 /* Trick: do a poke status in order to speed up the signaling
763                    of observers.
764                    A delete via the menu does this, in do_headers_action(), 
765                    though I don't know why.
766                  */
767                 tny_folder_poke_status (folder);
768         
769                 g_object_unref (folder);
770         }
771         
772         if (account)
773                 g_object_unref (account);
774                 
775         /* Refilter the header view explicitly, to make sure that 
776          * deleted emails are really removed from view. 
777          * (They are not really deleted until contact is made with the server, 
778          * so they would appear with a strike-through until then):
779          */
780         ModestHeaderView *header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (
781                 MODEST_MAIN_WINDOW(win), MODEST_WIDGET_TYPE_HEADER_VIEW));
782         if (header_view && MODEST_IS_HEADER_VIEW (header_view))
783                 modest_header_view_refilter (header_view);
784         
785         return res;
786 }
787
788
789
790
791 static gint
792 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
793 {
794         if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
795         return OSSO_ERROR;
796         
797     /* Use g_idle to context-switch into the application's thread: */
798
799     /* Get the arguments: */
800         osso_rpc_t val = g_array_index (arguments,
801                              osso_rpc_t,
802                              MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
803         gchar *uri = g_strdup (val.value.s);
804         
805         /* printf("  debug: to=%s\n", idle_data->to); */
806         g_idle_add(on_idle_delete_message, (gpointer)uri);
807         
808         /* Note that we cannot report failures during sending, 
809          * because that would be asynchronous. */
810         return OSSO_OK;
811 }
812
813 static gboolean
814 on_idle_send_receive(gpointer user_data)
815 {
816         ModestWindow *win;
817
818         /* This is a GDK lock because we are an idle callback and
819          * the code below is or does Gtk+ code */
820
821         gdk_threads_enter (); /* CHECKED */
822
823         /* Pick the main window if it exists */
824         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
825
826         /* This seems to be necessary to show new messages in the current window.
827          * I would expect this to be after the send_receive, but maybe 
828          * this makes a connection too. murrayc. */
829         modest_do_refresh_current_folder (win);
830
831         /* Send & receive all if "Update automatically" is set */
832         /* TODO: check the auto-update parameter in the configuration */
833         modest_ui_actions_do_send_receive_all (win);
834         
835         gdk_threads_leave (); /* CHECKED */
836         
837         return FALSE; /* Do not call this callback again. */
838 }
839
840 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
841 {       
842         printf("DEBUG: modest: %s\n", __FUNCTION__);
843     /* Use g_idle to context-switch into the application's thread: */
844
845     /* This method has no arguments. */
846         
847         /* printf("  debug: to=%s\n", idle_data->to); */
848         g_idle_add(on_idle_send_receive, NULL);
849         
850         /* Note that we cannot report failures during send/receive, 
851          * because that would be asynchronous. */
852         return OSSO_OK;
853 }
854
855 static gboolean on_idle_top_application (gpointer user_data);
856
857 static gboolean
858 on_idle_open_default_inbox(gpointer user_data)
859 {
860
861         /* This is a GDK lock because we are an idle callback and
862          * the code below is or does Gtk+ code */
863
864         gdk_threads_enter (); /* CHECKED */
865         
866         ModestWindow *win = 
867                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
868
869         /* Get the folder view */
870         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
871                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
872         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
873         
874         gdk_threads_leave (); /* CHECKED */
875         
876         /* This D-Bus method is obviously meant to result in the UI being visible,
877          * so show it, by calling this idle handler directly: */
878         on_idle_top_application(user_data);
879         
880         return FALSE; /* Do not call this callback again. */
881 }
882
883 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
884 {
885     /* Use g_idle to context-switch into the application's thread: */
886
887     /* This method has no arguments. */
888         
889         g_idle_add(on_idle_open_default_inbox, NULL);
890         
891         /* Note that we cannot report failures during send/receive, 
892          * because that would be asynchronous. */
893         return OSSO_OK;
894 }
895
896
897 static gboolean on_idle_top_application (gpointer user_data)
898 {
899
900         /* This is a GDK lock because we are an idle callback and
901          * the code below is or does Gtk+ code */
902
903         gdk_threads_enter (); /* CHECKED */
904
905         ModestWindow *win = 
906                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
907         if (win) {
908                 /* Ideally, we would just use gtk_widget_show(), 
909                  * but this widget is not coded correctly to support that: */
910                 gtk_widget_show_all (GTK_WIDGET (win));
911                 gtk_window_present (GTK_WINDOW (win));
912         }
913
914         gdk_threads_leave (); /* CHECKED */
915         
916         return FALSE; /* Do not call this callback again. */
917 }
918
919 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
920 {
921     /* Use g_idle to context-switch into the application's thread: */
922
923     /* This method has no arguments. */
924         
925         g_idle_add(on_idle_top_application, NULL);
926         
927         return OSSO_OK;
928 }
929                       
930 /* Callback for normal D-BUS messages */
931 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
932                       GArray * arguments, gpointer data,
933                       osso_rpc_t * retval)
934 {
935         
936         /* g_debug ("debug: %s\n", __FUNCTION__); */
937         g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
938         
939         if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
940                 return on_mail_to (arguments, data, retval);
941         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
942                 return on_open_message (arguments, data, retval);
943         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
944                 return on_send_receive (arguments, data, retval);
945         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
946                 return on_compose_mail (arguments, data, retval);
947         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
948                 return on_delete_message (arguments,data, retval);
949         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
950                 return on_open_default_inbox (arguments, data, retval);
951         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
952                 return on_top_application (arguments, data, retval);
953         }
954         else { 
955                 /* We need to return INVALID here so
956                  * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
957                  * so that our modest_dbus_req_filter will then be tried instead.
958                  * */
959                 return OSSO_INVALID;
960         }
961 }
962                                          
963 /* A complex D-Bus type (like a struct),
964  * used to return various information about a search hit.
965  */
966 #define SEARCH_HIT_DBUS_TYPE \
967         DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
968         DBUS_TYPE_STRING_AS_STRING /* msgid */ \
969         DBUS_TYPE_STRING_AS_STRING /* subject */ \
970         DBUS_TYPE_STRING_AS_STRING /* folder */ \
971         DBUS_TYPE_STRING_AS_STRING /* sender */ \
972         DBUS_TYPE_UINT64_AS_STRING /* msize */ \
973         DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
974         DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
975         DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
976         DBUS_STRUCT_END_CHAR_AS_STRING
977
978 static DBusMessage *
979 search_result_to_message (DBusMessage *reply,
980                            GList       *hits)
981 {
982         DBusMessageIter iter;
983         DBusMessageIter array_iter;
984         GList          *hit_iter;
985
986         dbus_message_iter_init_append (reply, &iter); 
987         dbus_message_iter_open_container (&iter,
988                                           DBUS_TYPE_ARRAY,
989                                           SEARCH_HIT_DBUS_TYPE,
990                                           &array_iter); 
991
992         for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
993                 DBusMessageIter  struct_iter;
994                 ModestSearchHit *hit;
995                 char            *msg_url;
996                 const char      *subject;
997                 const char      *folder;
998                 const char      *sender;
999                 guint64          size;
1000                 gboolean         has_attachment;
1001                 gboolean         is_unread;
1002                 gint64           ts;
1003
1004                 hit = (ModestSearchHit *) hit_iter->data;
1005
1006                 msg_url = hit->msgid;
1007                 subject = hit->subject;
1008                 folder  = hit->folder;
1009                 sender  = hit->sender;
1010                 size           = hit->msize;
1011                 has_attachment = hit->has_attachment;
1012                 is_unread      = hit->is_unread;
1013                 ts             = hit->timestamp;
1014
1015                 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);   
1016                 
1017                 dbus_message_iter_open_container (&array_iter,
1018                                                   DBUS_TYPE_STRUCT,
1019                                                   NULL,
1020                                                   &struct_iter);
1021
1022                 dbus_message_iter_append_basic (&struct_iter,
1023                                                 DBUS_TYPE_STRING,
1024                                                 &msg_url);
1025
1026                 dbus_message_iter_append_basic (&struct_iter,
1027                                                 DBUS_TYPE_STRING,
1028                                                 &subject); 
1029
1030                 dbus_message_iter_append_basic (&struct_iter,
1031                                                 DBUS_TYPE_STRING,
1032                                                 &folder);
1033
1034                 dbus_message_iter_append_basic (&struct_iter,
1035                                                 DBUS_TYPE_STRING,
1036                                                 &sender);
1037
1038                 dbus_message_iter_append_basic (&struct_iter,
1039                                                 DBUS_TYPE_UINT64,
1040                                                 &size);
1041
1042                 dbus_message_iter_append_basic (&struct_iter,
1043                                                 DBUS_TYPE_BOOLEAN,
1044                                                 &has_attachment);
1045
1046                 dbus_message_iter_append_basic (&struct_iter,
1047                                                 DBUS_TYPE_BOOLEAN,
1048                                                 &is_unread);
1049                 
1050                 dbus_message_iter_append_basic (&struct_iter,
1051                                                 DBUS_TYPE_INT64,
1052                                                 &ts);
1053
1054                 dbus_message_iter_close_container (&array_iter,
1055                                                    &struct_iter); 
1056
1057                 g_free (hit->msgid);
1058                 g_free (hit->subject);
1059                 g_free (hit->folder);
1060                 g_free (hit->sender);
1061
1062                 g_slice_free (ModestSearchHit, hit);
1063         }
1064
1065         dbus_message_iter_close_container (&iter, &array_iter);
1066
1067         return reply;
1068 }
1069
1070
1071 static void
1072 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
1073 {
1074         ModestDBusSearchFlags dbus_flags;
1075         DBusMessage  *reply = NULL;
1076         dbus_bool_t  res;
1077         dbus_int64_t sd_v;
1078         dbus_int64_t ed_v;
1079         dbus_int32_t flags_v;
1080         dbus_uint32_t size_v;
1081         const char *folder;
1082         const char *query;
1083         time_t start_date;
1084         time_t end_date;
1085         GList *hits;
1086
1087         DBusError error;
1088         dbus_error_init (&error);
1089
1090         sd_v = ed_v = 0;
1091         flags_v = 0;
1092
1093         res = dbus_message_get_args (message,
1094                                      &error,
1095                                      DBUS_TYPE_STRING, &query,
1096                                      DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
1097                                      DBUS_TYPE_INT64, &sd_v,
1098                                      DBUS_TYPE_INT64, &ed_v,
1099                                      DBUS_TYPE_INT32, &flags_v,
1100                                      DBUS_TYPE_UINT32, &size_v,
1101                                      DBUS_TYPE_INVALID);
1102         
1103         dbus_flags = (ModestDBusSearchFlags) flags_v;
1104         start_date = (time_t) sd_v;
1105         end_date = (time_t) ed_v;
1106
1107         ModestSearch search;
1108         memset (&search, 0, sizeof (search));
1109         
1110         /* Remember what folder we are searching in:
1111          *
1112          * Note that we don't copy the strings, 
1113          * because this struct will only be used for the lifetime of this function.
1114          */
1115         search.folder = folder;
1116
1117    /* Remember the text to search for: */
1118 #ifdef MODEST_HAVE_OGS
1119         search.query  = query;
1120 #endif
1121
1122         /* Other criteria: */
1123         search.before = start_date;
1124         search.after  = end_date;
1125         search.flags  = 0;
1126
1127         /* Text to serach for in various parts of the message: */
1128         if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1129                 search.flags |= MODEST_SEARCH_SUBJECT;
1130                 search.subject = query;
1131         }
1132
1133         if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1134                 search.flags |=  MODEST_SEARCH_SENDER;
1135                 search.from = query;
1136         }
1137
1138         if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1139                 search.flags |= MODEST_SEARCH_RECIPIENT; 
1140                 search.recipient = query;
1141         }
1142
1143         if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1144                 search.flags |=  MODEST_SEARCH_BODY; 
1145                 search.body = query;
1146         }
1147
1148         if (sd_v > 0) {
1149                 search.flags |= MODEST_SEARCH_BEFORE;
1150                 search.before = start_date;
1151         }
1152
1153         if (ed_v > 0) {
1154                 search.flags |= MODEST_SEARCH_AFTER;
1155                 search.after = end_date;
1156         }
1157
1158         if (size_v > 0) {
1159                 search.flags |= MODEST_SEARCH_SIZE;
1160                 search.minsize = size_v;
1161         }
1162
1163 #ifdef MODEST_HAVE_OGS
1164         search.flags |= MODEST_SEARCH_USE_OGS;
1165         g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1166 #endif
1167
1168         /* Note that this currently gets folders and messages from the servers, 
1169          * which can take a long time. libmodest_dbus_client_search() can timeout, 
1170          * reporting no results, if this takes a long time: */
1171         hits = modest_search_all_accounts (&search);
1172
1173         reply = dbus_message_new_method_return (message);
1174
1175         search_result_to_message (reply, hits);
1176
1177         if (reply == NULL) {
1178                 g_warning ("%s: Could not create reply.", __FUNCTION__);
1179         }
1180
1181         if (reply) {
1182                 dbus_uint32_t serial = 0;
1183                 dbus_connection_send (con, reply, &serial);
1184         dbus_connection_flush (con);
1185         dbus_message_unref (reply);
1186         }
1187
1188         g_list_free (hits);
1189 }
1190
1191
1192 /* A complex D-Bus type (like a struct),
1193  * used to return various information about a folder.
1194  */
1195 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1196         DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1197         DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1198         DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1199         DBUS_STRUCT_END_CHAR_AS_STRING
1200
1201 static DBusMessage *
1202 get_folders_result_to_message (DBusMessage *reply,
1203                            GList *folder_ids)
1204 {
1205         DBusMessageIter iter;   
1206         dbus_message_iter_init_append (reply, &iter); 
1207         
1208         DBusMessageIter array_iter;
1209         dbus_message_iter_open_container (&iter,
1210                                           DBUS_TYPE_ARRAY,
1211                                           GET_FOLDERS_RESULT_DBUS_TYPE,
1212                                           &array_iter); 
1213
1214         GList *list_iter = folder_ids;
1215         for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1216                 
1217                 const gchar *folder_name = (const gchar*)list_iter->data;
1218                 if (folder_name) {
1219                         /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1220                         
1221                         DBusMessageIter struct_iter;
1222                         dbus_message_iter_open_container (&array_iter,
1223                                                           DBUS_TYPE_STRUCT,
1224                                                           NULL,
1225                                                           &struct_iter);
1226         
1227                         /* name: */
1228                         dbus_message_iter_append_basic (&struct_iter,
1229                                                         DBUS_TYPE_STRING,
1230                                                         &folder_name); /* The string will be copied. */
1231                                                         
1232                         /* URI: This is maybe not needed by osso-global-search: */
1233                         const gchar *folder_uri = "TODO:unimplemented";
1234                         dbus_message_iter_append_basic (&struct_iter,
1235                                                         DBUS_TYPE_STRING,
1236                                                         &folder_uri); /* The string will be copied. */
1237         
1238                         dbus_message_iter_close_container (&array_iter,
1239                                                            &struct_iter); 
1240                 }
1241         }
1242
1243         dbus_message_iter_close_container (&iter, &array_iter);
1244
1245         return reply;
1246 }
1247
1248 static void
1249 add_single_folder_to_list (TnyFolder *folder, GList** list)
1250 {
1251         if (!folder)
1252                 return;
1253                 
1254         if (TNY_IS_MERGE_FOLDER (folder)) {
1255                 /* Ignore these because their IDs ares
1256                  * a) not always unique or sensible.
1257                  * b) not human-readable, and currently need a human-readable 
1258                  *    ID here, because the osso-email-interface API does not allow 
1259                  *    us to return both an ID and a display name.
1260                  * 
1261                  * This is actually the merged outbox folder.
1262                  * We could hack our D-Bus API to understand "outbox" as the merged outboxes, 
1263                  * but that seems unwise. murrayc.
1264                  */
1265                  return;        
1266         }
1267                 
1268         /* Add this folder to the list: */
1269         /*
1270         const gchar * folder_name = tny_folder_get_name (folder);
1271         if (folder_name)
1272                 *list = g_list_append(*list, g_strdup (folder_name));
1273         else {
1274         */
1275                 /* osso-global-search only uses one string,
1276                  * so ID is the only thing that could possibly identify a folder.
1277                  * TODO: osso-global search should probably be changed to 
1278                  * take an ID and a Name.
1279                  */
1280                 const gchar * id =  tny_folder_get_id (folder);
1281                 if (id && strlen(id))
1282                         *list = g_list_append(*list, g_strdup (id));
1283                 /*
1284                 else {
1285                         g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);     
1286                 }
1287                 
1288         }
1289         */
1290 }
1291
1292 static void
1293 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1294 {
1295         if (!folder_store)
1296                 return;
1297         
1298         /* Add this folder to the list: */
1299         if (TNY_IS_FOLDER (folder_store)) {
1300                 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1301         }       
1302                 
1303         /* Recurse into child folders: */
1304                 
1305         /* Get the folders list: */
1306         /*
1307         TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1308         tny_folder_store_query_add_item (query, NULL, 
1309                 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1310         */
1311         TnyList *all_folders = tny_simple_list_new ();
1312         tny_folder_store_get_folders (folder_store,
1313                                       all_folders,
1314                                       NULL /* query */,
1315                                       NULL /* error */);
1316
1317         TnyIterator *iter = tny_list_create_iterator (all_folders);
1318         while (!tny_iterator_is_done (iter)) {
1319                 
1320                 /* Do not recurse, because the osso-global-search UI specification 
1321                  * does not seem to want the sub-folders, though that spec seems to 
1322                  * be generally unsuitable for Modest.
1323                  */
1324                 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1325                 if (folder) {
1326                         add_single_folder_to_list (TNY_FOLDER (folder), list);
1327                          
1328                         #if 0
1329                         if (TNY_IS_FOLDER_STORE (folder))
1330                                 add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1331                         else {
1332                                 add_single_folder_to_list (TNY_FOLDER (folder), list);
1333                         }
1334                         #endif
1335                         
1336                         /* tny_iterator_get_current() gave us a reference. */
1337                         g_object_unref (folder);
1338                 }
1339                 
1340                 tny_iterator_next (iter);
1341         }
1342         g_object_unref (G_OBJECT (iter));
1343 }
1344
1345 static void
1346 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1347 {
1348         DBusMessage  *reply = NULL;
1349         
1350
1351         /* Get the TnyStoreAccount so we can get the folders: */
1352         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1353         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
1354         if (!account_name) {
1355                 g_printerr ("modest: no account found\n");
1356         }
1357         
1358         TnyAccount *account = NULL;
1359         if (account_mgr) {
1360                 account = modest_tny_account_store_get_server_account (
1361                         modest_runtime_get_account_store(), account_name, 
1362                         TNY_ACCOUNT_TYPE_STORE);
1363         }
1364         
1365         if (!account) {
1366                 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1367         } 
1368                 
1369         printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1370         g_free (account_name);
1371         account_name = NULL;
1372         
1373         GList *folder_names = NULL;
1374         add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1375
1376         g_object_unref (account);
1377         account = NULL;
1378         
1379         /* Also add the folders from the local folders account,
1380          * because they are (currently) used with all accounts:
1381          * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1382          */
1383         TnyAccount *account_local = 
1384                 modest_tny_account_store_get_local_folders_account (modest_runtime_get_account_store());
1385         add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1386
1387         g_object_unref (account_local);
1388         account_local = NULL;
1389
1390
1391         /* Put the result in a DBus reply: */
1392         reply = dbus_message_new_method_return (message);
1393
1394         get_folders_result_to_message (reply, folder_names);
1395
1396         if (reply == NULL) {
1397                 g_warning ("%s: Could not create reply.", __FUNCTION__);
1398         }
1399
1400         if (reply) {
1401                 dbus_uint32_t serial = 0;
1402                 dbus_connection_send (con, reply, &serial);
1403         dbus_connection_flush (con);
1404         dbus_message_unref (reply);
1405         }
1406
1407         g_list_foreach (folder_names, (GFunc)g_free, NULL);
1408         g_list_free (folder_names);
1409 }
1410
1411
1412 /** This D-Bus handler is used when the main osso-rpc 
1413  * D-Bus handler has not handled something.
1414  * We use this for D-Bus methods that need to use more complex types 
1415  * than osso-rpc supports.
1416  */
1417 DBusHandlerResult
1418 modest_dbus_req_filter (DBusConnection *con,
1419                         DBusMessage    *message,
1420                         void           *user_data)
1421 {
1422         gboolean handled = FALSE;
1423
1424         if (dbus_message_is_method_call (message,
1425                                          MODEST_DBUS_IFACE,
1426                                          MODEST_DBUS_METHOD_SEARCH)) {
1427                 printf ("  DEBUG1: %s\n", __FUNCTION__);
1428                 on_dbus_method_search (con, message);
1429                 handled = TRUE;                         
1430         } else if (dbus_message_is_method_call (message,
1431                                          MODEST_DBUS_IFACE,
1432                                          MODEST_DBUS_METHOD_GET_FOLDERS)) {
1433                 printf ("  DEBUG2: %s\n", __FUNCTION__);
1434                 on_dbus_method_get_folders (con, message);
1435                 handled = TRUE;                         
1436         }
1437         else {
1438                 /* Note that this mentions methods that were already handled in modest_dbus_req_handler(). */
1439                 /* 
1440                 g_debug ("  debug: %s: Unexpected (maybe already handled) D-Bus method:\n   Interface=%s, Member=%s\n", 
1441                         __FUNCTION__, dbus_message_get_interface (message),
1442                         dbus_message_get_member(message));
1443                 */
1444         }
1445         
1446         return (handled ? 
1447                 DBUS_HANDLER_RESULT_HANDLED :
1448                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1449 }
1450
1451
1452 void
1453 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1454 {
1455         /* TODO? */
1456     /* printf("%s()\n", __PRETTY_FUNCTION__); */
1457
1458     if(state->system_inactivity_ind)
1459     {
1460     }
1461     else if(state->save_unsaved_data_ind)
1462     {
1463     }
1464     else
1465     {
1466     
1467     }
1468
1469     /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */
1470 }