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