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