b7c9af36355380a72e90fcb2e9d7ac328b64ebe4
[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.\n", __FUNCTION__);
473                 return NULL;
474         }
475
476         g_debug ("%s: Found account.\n", __FUNCTION__);
477
478         if ( ! TNY_IS_STORE_ACCOUNT (account)) {
479                 goto out;
480         }
481
482         g_debug ("%s: Account is store account.\n", __FUNCTION__);
483         *ac_out = account;
484
485         folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
486                                                 uri,
487                                                 &err);
488
489         if (folder == NULL) {
490                 g_debug ("%s: tny_store_account_find_folder() failed\naccount=%s, uri=%s.\n", __FUNCTION__, 
491                         tny_account_get_id (TNY_ACCOUNT(account)), uri);
492                 goto out;
493         }
494         
495         g_debug ("%s: Found folder. (%s)\n",  __FUNCTION__, uri);
496         
497         msg = tny_folder_find_msg (folder, uri, &err);
498         
499         if (!msg) {
500                 g_debug ("%s: tny_folder_find_msg() failed for folder %s\n  with error=%s.\n",
501                          __FUNCTION__, tny_folder_get_id (folder), err->message);
502         }
503
504 out:
505         if (err)
506                 g_error_free (err);
507
508         if (account && !msg) {
509                 g_object_unref (account);
510                 *ac_out = NULL;
511         }
512
513         if (folder)
514                 g_object_unref (folder);
515
516         return msg;
517 }
518
519 static gboolean
520 on_idle_open_message (gpointer user_data)
521 {
522         ModestWindow *msg_view = NULL;
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         folder = tny_msg_get_folder (msg);
544         if (modest_tny_folder_get_local_folder_type (folder) == TNY_FOLDER_TYPE_DRAFTS) {
545                 g_debug ("TODO: draft messages should be opened in edit mode... ");
546         }
547
548         header = tny_msg_get_header (msg);
549         
550         msg_uid =  modest_tny_folder_get_header_unique_id(header); 
551 /* FIXME:  modest_tny_folder_get_header_unique_id warns against this */
552         win_mgr = modest_runtime_get_window_mgr ();
553                 
554         gdk_threads_enter ();
555
556         if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
557                 g_debug ("modest: %s: A window for this messsage is open already.", __FUNCTION__);
558                 if (!MODEST_IS_MSG_VIEW_WINDOW(msg_view)) 
559                         g_debug ("  DEBUG: But the window is not a msg view");
560                 else {
561                         gtk_window_present (GTK_WINDOW(msg_view));
562                 }
563         } else {
564                 /* g_debug ("creating new window for this msg"); */
565                 modest_window_mgr_register_header (win_mgr, header);
566                 
567                 const gchar *modest_account_name = 
568                         modest_tny_account_get_parent_modest_account_name_for_server_account (account);
569                 msg_view = modest_msg_view_window_new (msg, modest_account_name,
570                                                        msg_uid);
571                 modest_window_mgr_register_window (win_mgr, msg_view);
572                 gtk_widget_show_all (GTK_WIDGET (msg_view));
573         }
574
575         gdk_threads_leave ();
576
577         g_object_unref (header);
578         g_object_unref (account);
579         g_object_unref (folder);
580
581         return FALSE; /* Do not call this callback again. */
582 }
583
584 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
585 {
586         if (arguments->len != MODEST_DBUS_OPEN_MESSAGE_ARGS_COUNT)
587         return OSSO_ERROR;
588         
589     /* Use g_idle to context-switch into the application's thread: */
590
591     /* Get the arguments: */
592         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DBUS_OPEN_MESSAGE_ARG_URI);
593         gchar *uri = g_strdup (val.value.s);
594         
595         /* printf("  debug: to=%s\n", idle_data->to); */
596         g_idle_add(on_idle_open_message, (gpointer)uri);
597         
598         /* Note that we cannot report failures during sending, 
599          * because that would be asynchronous. */
600         return OSSO_OK;
601 }
602
603 static gboolean
604 on_idle_delete_message (gpointer user_data)
605 {
606         TnyList      *headers;
607         TnyFolder    *folder;
608         TnyIterator  *iter; 
609         TnyHeader    *header;
610         TnyHeader    *msg_header;
611         TnyMsg       *msg;
612         TnyAccount   *account;
613         GError       *error;
614         const char   *uri;
615         const char   *uid;
616         gint          res;
617
618         uri = (char *) user_data;
619
620         /* g_debug ("modest: %s Searching for message (delete message)"); */
621         
622         msg = find_message_by_url (uri, &account);
623
624         if (msg == NULL) {
625                 return OSSO_ERROR;
626         }
627
628         g_debug ("modest: %s: Found message", __FUNCTION__);
629         
630         msg_header = tny_msg_get_header (msg);
631         uid = tny_header_get_uid (msg_header);
632         folder = tny_msg_get_folder (msg);
633
634
635         /* tny_msg_get_header () flaw:
636          * From tinythingy doc: You can't use the returned instance with the
637          * TnyFolder operations
638          *
639          * To get a header instance that will work with these folder methods,
640          * you can use tny_folder_get_headers.
641          *
642          * Ok, we will do so then. Sigh.
643          * */
644         headers = tny_simple_list_new ();
645
646         tny_folder_get_headers (folder, headers, TRUE, NULL);
647         iter = tny_list_create_iterator (headers);
648         header = NULL;
649
650         /* g_debug ("Searching header for msg in folder"); */
651         while (!tny_iterator_is_done (iter)) {
652                 const char *cur_id;
653
654                 header = TNY_HEADER (tny_iterator_get_current (iter));
655                 cur_id = tny_header_get_uid (header);
656                 
657                 if (cur_id && uid && g_str_equal (cur_id, uid)) {
658                         /* g_debug ("Found corresponding header from folder"); */
659                         break;
660                 }
661
662                 header = NULL;
663                 g_object_unref (header);
664                 tny_iterator_next (iter);
665         }
666
667         g_object_unref (iter);
668         iter = NULL;
669         g_object_unref (headers);
670         headers = NULL;
671         
672         g_object_unref (msg_header);
673         msg_header = NULL;
674         g_object_unref (msg);
675         msg = NULL;
676
677         if (header == NULL) {
678                 g_object_unref (folder);
679                 return OSSO_ERROR;
680         }       
681                 
682         error = NULL;
683         res = OSSO_OK;
684         
685         gdk_threads_enter ();
686         ModestWindow *win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
687         modest_do_message_delete (header, win);
688
689         if (error != NULL) {
690                 res = OSSO_ERROR;
691                 g_error_free (error);
692         }
693         
694         
695         
696         ModestWindowMgr *win_mgr = modest_runtime_get_window_mgr ();    
697         ModestWindow *msg_view = NULL; 
698         if (modest_window_mgr_find_registered_header (win_mgr, header, &msg_view)) {
699                 if (MODEST_IS_MSG_VIEW_WINDOW (msg_view))
700                         modest_ui_actions_refresh_message_window_after_delete (MODEST_MSG_VIEW_WINDOW (msg_view));
701         }
702         
703         gdk_threads_leave ();
704         
705         g_object_unref (header);
706         g_object_unref (folder);
707         return res;
708 }
709
710 static gint
711 on_delete_message (GArray *arguments, gpointer data, osso_rpc_t *retval)
712 {
713         if (arguments->len != MODEST_DBUS_DELETE_MESSAGE_ARGS_COUNT)
714         return OSSO_ERROR;
715         
716     /* Use g_idle to context-switch into the application's thread: */
717
718     /* Get the arguments: */
719         osso_rpc_t val = g_array_index (arguments,
720                              osso_rpc_t,
721                              MODEST_DBUS_DELETE_MESSAGE_ARG_URI);
722         gchar *uri = g_strdup (val.value.s);
723         
724         /* printf("  debug: to=%s\n", idle_data->to); */
725         g_idle_add(on_idle_delete_message, (gpointer)uri);
726         
727         /* Note that we cannot report failures during sending, 
728          * because that would be asynchronous. */
729         return OSSO_OK;
730 }
731
732 static gboolean
733 on_idle_send_receive(gpointer user_data)
734 {
735         ModestWindow *win;
736
737         gdk_threads_enter ();
738
739         /* Pick the main window if it exists */
740         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
741
742         /* Send & receive all if "Update automatically" is set */
743         /* TODO: check the auto-update parameter in the configuration */
744         modest_ui_actions_do_send_receive_all (win);
745         
746         gdk_threads_leave ();
747         
748         return FALSE; /* Do not call this callback again. */
749 }
750
751 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
752 {       
753     /* Use g_idle to context-switch into the application's thread: */
754
755     /* This method has no arguments. */
756         
757         /* printf("  debug: to=%s\n", idle_data->to); */
758         g_idle_add(on_idle_send_receive, NULL);
759         
760         /* Note that we cannot report failures during send/receive, 
761          * because that would be asynchronous. */
762         return OSSO_OK;
763 }
764
765 static gboolean
766 on_idle_open_default_inbox(gpointer user_data)
767 {
768         gdk_threads_enter ();
769         
770         ModestWindow *win = 
771                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
772
773         /* Get the folder view */
774         GtkWidget *folder_view = modest_main_window_get_child_widget (MODEST_MAIN_WINDOW (win),
775                                                            MODEST_WIDGET_TYPE_FOLDER_VIEW);
776         modest_folder_view_select_first_inbox_or_local (MODEST_FOLDER_VIEW (folder_view));
777         
778         gdk_threads_leave ();
779         
780         return FALSE; /* Do not call this callback again. */
781 }
782
783 static gint on_open_default_inbox(GArray * arguments, gpointer data, osso_rpc_t * retval)
784 {
785     /* Use g_idle to context-switch into the application's thread: */
786
787     /* This method has no arguments. */
788         
789         g_idle_add(on_idle_open_default_inbox, 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
797 static gboolean on_idle_top_application (gpointer user_data)
798 {
799         gdk_threads_enter ();
800
801         ModestWindow *win = 
802                 modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
803         if (win) {
804                 /* Ideally, we would just use gtk_widget_show(), 
805                  * but this widget is not coded correctly to support that: */
806                 gtk_widget_show_all (GTK_WIDGET (win));
807                 gtk_window_present (GTK_WINDOW (win));
808         }
809
810         gdk_threads_leave ();
811         
812         return FALSE; /* Do not call this callback again. */
813 }
814
815 static gint on_top_application(GArray * arguments, gpointer data, osso_rpc_t * retval)
816 {
817     /* Use g_idle to context-switch into the application's thread: */
818
819     /* This method has no arguments. */
820         
821         g_idle_add(on_idle_top_application, NULL);
822         
823         return OSSO_OK;
824 }
825                       
826 /* Callback for normal D-BUS messages */
827 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
828                       GArray * arguments, gpointer data,
829                       osso_rpc_t * retval)
830 {
831         
832         /* g_debug ("debug: %s\n", __FUNCTION__); */
833         g_debug ("debug: %s: method received: %s\n", __FUNCTION__, method);
834         
835         if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
836                 return on_mail_to (arguments, data, retval);
837         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
838                 return on_open_message (arguments, data, retval);
839         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
840                 return on_send_receive (arguments, data, retval);
841         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
842                 return on_compose_mail (arguments, data, retval);
843         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_DELETE_MESSAGE) == 0) {
844                 return on_delete_message (arguments,data, retval);
845         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_OPEN_DEFAULT_INBOX) == 0) {
846                 return on_open_default_inbox (arguments, data, retval);
847         } else if (g_ascii_strcasecmp (method, MODEST_DBUS_METHOD_TOP_APPLICATION) == 0) {
848                 return on_top_application (arguments, data, retval);
849         }
850         else { 
851                 g_debug ("  debug: %s: Unexpected D-Bus method: %s\n", __FUNCTION__, method);
852         
853                 /* We need to return INVALID here so
854                  * libosso will return DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
855                  * so that our modest_dbus_req_filter will then be tried instead.
856                  * */
857                 return OSSO_INVALID;
858         }
859 }
860                                          
861 /* A complex D-Bus type (like a struct),
862  * used to return various information about a search hit.
863  */
864 #define SEARCH_HIT_DBUS_TYPE \
865         DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
866         DBUS_TYPE_STRING_AS_STRING /* msgid */ \
867         DBUS_TYPE_STRING_AS_STRING /* subject */ \
868         DBUS_TYPE_STRING_AS_STRING /* folder */ \
869         DBUS_TYPE_STRING_AS_STRING /* sender */ \
870         DBUS_TYPE_UINT64_AS_STRING /* msize */ \
871         DBUS_TYPE_BOOLEAN_AS_STRING /* has_attachment */ \
872         DBUS_TYPE_BOOLEAN_AS_STRING /* is_unread */ \
873         DBUS_TYPE_INT64_AS_STRING /* timestamp */ \
874         DBUS_STRUCT_END_CHAR_AS_STRING
875
876 static DBusMessage *
877 search_result_to_message (DBusMessage *reply,
878                            GList       *hits)
879 {
880         DBusMessageIter iter;
881         DBusMessageIter array_iter;
882         GList          *hit_iter;
883
884         dbus_message_iter_init_append (reply, &iter); 
885         dbus_message_iter_open_container (&iter,
886                                           DBUS_TYPE_ARRAY,
887                                           SEARCH_HIT_DBUS_TYPE,
888                                           &array_iter); 
889
890         for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
891                 DBusMessageIter  struct_iter;
892                 ModestSearchHit *hit;
893                 char            *msg_url;
894                 const char      *subject;
895                 const char      *folder;
896                 const char      *sender;
897                 guint64          size;
898                 gboolean         has_attachment;
899                 gboolean         is_unread;
900                 gint64           ts;
901
902                 hit = (ModestSearchHit *) hit_iter->data;
903
904                 msg_url = hit->msgid;
905                 subject = hit->subject;
906                 folder  = hit->folder;
907                 sender  = hit->sender;
908                 size           = hit->msize;
909                 has_attachment = hit->has_attachment;
910                 is_unread      = hit->is_unread;
911                 ts             = hit->timestamp;
912
913                 g_debug ("DEBUG: %s: Adding hit: %s", __FUNCTION__, msg_url);   
914                 
915                 dbus_message_iter_open_container (&array_iter,
916                                                   DBUS_TYPE_STRUCT,
917                                                   NULL,
918                                                   &struct_iter);
919
920                 dbus_message_iter_append_basic (&struct_iter,
921                                                 DBUS_TYPE_STRING,
922                                                 &msg_url);
923
924                 dbus_message_iter_append_basic (&struct_iter,
925                                                 DBUS_TYPE_STRING,
926                                                 &subject); 
927
928                 dbus_message_iter_append_basic (&struct_iter,
929                                                 DBUS_TYPE_STRING,
930                                                 &folder);
931
932                 dbus_message_iter_append_basic (&struct_iter,
933                                                 DBUS_TYPE_STRING,
934                                                 &sender);
935
936                 dbus_message_iter_append_basic (&struct_iter,
937                                                 DBUS_TYPE_UINT64,
938                                                 &size);
939
940                 dbus_message_iter_append_basic (&struct_iter,
941                                                 DBUS_TYPE_BOOLEAN,
942                                                 &has_attachment);
943
944                 dbus_message_iter_append_basic (&struct_iter,
945                                                 DBUS_TYPE_BOOLEAN,
946                                                 &is_unread);
947                 
948                 dbus_message_iter_append_basic (&struct_iter,
949                                                 DBUS_TYPE_INT64,
950                                                 &ts);
951
952                 dbus_message_iter_close_container (&array_iter,
953                                                    &struct_iter); 
954
955                 g_free (hit->msgid);
956                 g_free (hit->subject);
957                 g_free (hit->folder);
958                 g_free (hit->sender);
959
960                 g_slice_free (ModestSearchHit, hit);
961         }
962
963         dbus_message_iter_close_container (&iter, &array_iter);
964
965         return reply;
966 }
967
968
969 static void
970 on_dbus_method_search (DBusConnection *con, DBusMessage *message)
971 {
972         ModestDBusSearchFlags dbus_flags;
973         DBusMessage  *reply = NULL;
974         dbus_bool_t  res;
975         dbus_int64_t sd_v;
976         dbus_int64_t ed_v;
977         dbus_int32_t flags_v;
978         dbus_uint32_t size_v;
979         const char *folder;
980         const char *query;
981         time_t start_date;
982         time_t end_date;
983         GList *hits;
984
985         DBusError error;
986         dbus_error_init (&error);
987
988         sd_v = ed_v = 0;
989         flags_v = 0;
990
991         res = dbus_message_get_args (message,
992                                      &error,
993                                      DBUS_TYPE_STRING, &query,
994                                      DBUS_TYPE_STRING, &folder, /* e.g. "INBOX/drafts": TODO: Use both an ID and a display name. */
995                                      DBUS_TYPE_INT64, &sd_v,
996                                      DBUS_TYPE_INT64, &ed_v,
997                                      DBUS_TYPE_INT32, &flags_v,
998                                      DBUS_TYPE_UINT32, &size_v,
999                                      DBUS_TYPE_INVALID);
1000         
1001         dbus_flags = (ModestDBusSearchFlags) flags_v;
1002         start_date = (time_t) sd_v;
1003         end_date = (time_t) ed_v;
1004
1005         ModestSearch search;
1006         memset (&search, 0, sizeof (search));
1007         
1008         /* Remember what folder we are searching in:
1009          *
1010          * Note that we don't copy the strings, 
1011          * because this struct will only be used for the lifetime of this function.
1012          */
1013         search.folder = folder;
1014
1015    /* Remember the text to search for: */
1016 #ifdef MODEST_HAVE_OGS
1017         search.query  = query;
1018 #endif
1019
1020         /* Other criteria: */
1021         search.before = start_date;
1022         search.after  = end_date;
1023         search.flags  = 0;
1024
1025         /* Text to serach for in various parts of the message: */
1026         if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
1027                 search.flags |= MODEST_SEARCH_SUBJECT;
1028                 search.subject = query;
1029         }
1030
1031         if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
1032                 search.flags |=  MODEST_SEARCH_SENDER;
1033                 search.from = query;
1034         }
1035
1036         if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
1037                 search.flags |= MODEST_SEARCH_RECIPIENT; 
1038                 search.recipient = query;
1039         }
1040
1041         if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
1042                 search.flags |=  MODEST_SEARCH_BODY; 
1043                 search.body = query;
1044         }
1045
1046         if (sd_v > 0) {
1047                 search.flags |= MODEST_SEARCH_BEFORE;
1048                 search.before = start_date;
1049         }
1050
1051         if (ed_v > 0) {
1052                 search.flags |= MODEST_SEARCH_AFTER;
1053                 search.after = end_date;
1054         }
1055
1056         if (size_v > 0) {
1057                 search.flags |= MODEST_SEARCH_SIZE;
1058                 search.minsize = size_v;
1059         }
1060
1061 #ifdef MODEST_HAVE_OGS
1062         search.flags |= MODEST_SEARCH_USE_OGS;
1063         g_debug ("%s: Starting search for %s", __FUNCTION__, search.query);
1064 #endif
1065
1066         /* Note that this currently gets folders and messages from the servers, 
1067          * which can take a long time. libmodest_dbus_client_search() can timeout, 
1068          * reporting no results, if this takes a long time: */
1069         hits = modest_search_all_accounts (&search);
1070
1071         reply = dbus_message_new_method_return (message);
1072
1073         search_result_to_message (reply, hits);
1074
1075         if (reply == NULL) {
1076                 g_warning ("%s: Could not create reply.", __FUNCTION__);
1077         }
1078
1079         if (reply) {
1080                 dbus_uint32_t serial = 0;
1081                 dbus_connection_send (con, reply, &serial);
1082         dbus_connection_flush (con);
1083         dbus_message_unref (reply);
1084         }
1085
1086         g_list_free (hits);
1087 }
1088
1089
1090 /* A complex D-Bus type (like a struct),
1091  * used to return various information about a folder.
1092  */
1093 #define GET_FOLDERS_RESULT_DBUS_TYPE \
1094         DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
1095         DBUS_TYPE_STRING_AS_STRING /* Folder Name */ \
1096         DBUS_TYPE_STRING_AS_STRING /* Folder URI */ \
1097         DBUS_STRUCT_END_CHAR_AS_STRING
1098
1099 static DBusMessage *
1100 get_folders_result_to_message (DBusMessage *reply,
1101                            GList *folder_ids)
1102 {
1103         DBusMessageIter iter;   
1104         dbus_message_iter_init_append (reply, &iter); 
1105         
1106         DBusMessageIter array_iter;
1107         dbus_message_iter_open_container (&iter,
1108                                           DBUS_TYPE_ARRAY,
1109                                           GET_FOLDERS_RESULT_DBUS_TYPE,
1110                                           &array_iter); 
1111
1112         GList *list_iter = folder_ids;
1113         for (list_iter = folder_ids; list_iter; list_iter = list_iter->next) {
1114                 
1115                 const gchar *folder_name = (const gchar*)list_iter->data;
1116                 if (folder_name) {
1117                         /* g_debug ("DEBUG: %s: Adding folder: %s", __FUNCTION__, folder_name); */
1118                         
1119                         DBusMessageIter struct_iter;
1120                         dbus_message_iter_open_container (&array_iter,
1121                                                           DBUS_TYPE_STRUCT,
1122                                                           NULL,
1123                                                           &struct_iter);
1124         
1125                         /* name: */
1126                         dbus_message_iter_append_basic (&struct_iter,
1127                                                         DBUS_TYPE_STRING,
1128                                                         &folder_name); /* The string will be copied. */
1129                                                         
1130                         /* URI: This is maybe not needed by osso-global-search: */
1131                         const gchar *folder_uri = "TODO:unimplemented";
1132                         dbus_message_iter_append_basic (&struct_iter,
1133                                                         DBUS_TYPE_STRING,
1134                                                         &folder_uri); /* The string will be copied. */
1135         
1136                         dbus_message_iter_close_container (&array_iter,
1137                                                            &struct_iter); 
1138                 }
1139         }
1140
1141         dbus_message_iter_close_container (&iter, &array_iter);
1142
1143         return reply;
1144 }
1145
1146 static void
1147 add_single_folder_to_list (TnyFolder *folder, GList** list)
1148 {
1149         if (!folder)
1150                 return;
1151                 
1152         /* Add this folder to the list: */
1153         /*
1154         const gchar * folder_name = tny_folder_get_name (folder);
1155         if (folder_name)
1156                 *list = g_list_append(*list, g_strdup (folder_name));
1157         else {
1158         */
1159                 /* osso-global-search only uses one string,
1160                  * so ID is the only thing that could possibly identify a folder.
1161                  * TODO: osso-global search should probably be changed to 
1162                  * take an ID and a Name.
1163                  */
1164                 const gchar * id =  tny_folder_get_id (folder);
1165                 if (id && strlen(id))
1166                         *list = g_list_append(*list, g_strdup (id));
1167                 /*
1168                 else {
1169                         g_warning ("DEBUG: %s: folder has no name or ID.\n", __FUNCTION__);     
1170                 }
1171                 
1172         }
1173         */
1174 }
1175
1176 static void
1177 add_folders_to_list (TnyFolderStore *folder_store, GList** list)
1178 {
1179         if (!folder_store)
1180                 return;
1181         
1182         /* Add this folder to the list: */
1183         if (TNY_IS_FOLDER (folder_store)) {
1184                 add_single_folder_to_list (TNY_FOLDER (folder_store), list);
1185         }       
1186                 
1187         /* Recurse into child folders: */
1188                 
1189         /* Get the folders list: */
1190         /*
1191         TnyFolderStoreQuery *query = tny_folder_store_query_new ();
1192         tny_folder_store_query_add_item (query, NULL, 
1193                 TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
1194         */
1195         TnyList *all_folders = tny_simple_list_new ();
1196         tny_folder_store_get_folders (folder_store,
1197                                       all_folders,
1198                                       NULL /* query */,
1199                                       NULL /* error */);
1200
1201         TnyIterator *iter = tny_list_create_iterator (all_folders);
1202         while (!tny_iterator_is_done (iter)) {
1203                 
1204                 /* Do not recurse, because the osso-global-search UI specification 
1205                  * does not seem to want the sub-folders, though that spec seems to 
1206                  * be generally unsuitable for Modest.
1207                  */
1208                 TnyFolder *folder = TNY_FOLDER (tny_iterator_get_current (iter));
1209                 add_single_folder_to_list (TNY_FOLDER (folder), list);
1210                  
1211                 #if 0
1212                 if (TNY_IS_FOLDER_STORE (folder))
1213                         add_folders_to_list (TNY_FOLDER_STORE (folder), list);
1214                 else {
1215                         add_single_folder_to_list (TNY_FOLDER (folder), list);
1216                 }
1217                 #endif
1218                 
1219                 tny_iterator_next (iter);
1220         }
1221         g_object_unref (G_OBJECT (iter));
1222 }
1223
1224 static void
1225 on_dbus_method_get_folders (DBusConnection *con, DBusMessage *message)
1226 {
1227         DBusMessage  *reply = NULL;
1228         
1229
1230         /* Get the TnyStoreAccount so we can get the folders: */
1231         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
1232         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
1233         if (!account_name) {
1234                 g_printerr ("modest: no account found\n");
1235         }
1236         
1237         TnyAccount *account = NULL;
1238         if (account_mgr) {
1239                 account = modest_tny_account_store_get_server_account (
1240                         modest_runtime_get_account_store(), account_name, 
1241                         TNY_ACCOUNT_TYPE_STORE);
1242         }
1243         
1244         if (!account) {
1245                 g_printerr ("modest: failed to get tny account folder'%s'\n", account_name);
1246         } 
1247                 
1248         printf("DEBUG: %s: Getting folders for account name=%s\n", __FUNCTION__, account_name);
1249         g_free (account_name);
1250         account_name = NULL;
1251         
1252         GList *folder_names = NULL;
1253         add_folders_to_list (TNY_FOLDER_STORE (account), &folder_names);
1254
1255         g_object_unref (account);
1256         account = NULL;
1257         
1258         
1259         /* Also add the folders from the local folders account,
1260          * because they are (currently) used with all accounts:
1261          * TODO: This is not working. It seems to get only the Merged Folder (with an ID of "" (not NULL)).
1262          */
1263         TnyAccount *account_local = 
1264                 modest_tny_account_store_get_local_folders_account (
1265                         TNY_ACCOUNT_STORE (modest_runtime_get_account_store()));
1266         add_folders_to_list (TNY_FOLDER_STORE (account_local), &folder_names);
1267
1268         g_object_unref (account_local);
1269         account_local = NULL;
1270
1271
1272         /* Put the result in a DBus reply: */
1273         reply = dbus_message_new_method_return (message);
1274
1275         get_folders_result_to_message (reply, folder_names);
1276
1277         if (reply == NULL) {
1278                 g_warning ("%s: Could not create reply.", __FUNCTION__);
1279         }
1280
1281         if (reply) {
1282                 dbus_uint32_t serial = 0;
1283                 dbus_connection_send (con, reply, &serial);
1284         dbus_connection_flush (con);
1285         dbus_message_unref (reply);
1286         }
1287
1288         g_list_foreach (folder_names, (GFunc)g_free, NULL);
1289         g_list_free (folder_names);
1290 }
1291
1292
1293 /** This D-Bus handler is used when the main osso-rpc 
1294  * D-Bus handler has not handled something.
1295  * We use this for D-Bus methods that need to use more complex types 
1296  * than osso-rpc supports.
1297  */
1298 DBusHandlerResult
1299 modest_dbus_req_filter (DBusConnection *con,
1300                         DBusMessage    *message,
1301                         void           *user_data)
1302 {
1303         gboolean handled = FALSE;
1304
1305         if (dbus_message_is_method_call (message,
1306                                          MODEST_DBUS_IFACE,
1307                                          MODEST_DBUS_METHOD_SEARCH)) {
1308                 on_dbus_method_search (con, message);
1309                 handled = TRUE;                         
1310         } else if (dbus_message_is_method_call (message,
1311                                          MODEST_DBUS_IFACE,
1312                                          MODEST_DBUS_METHOD_GET_FOLDERS)) {
1313                 on_dbus_method_get_folders (con, message);
1314                 handled = TRUE;                         
1315         }
1316         
1317         return (handled ? 
1318                 DBUS_HANDLER_RESULT_HANDLED :
1319                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
1320 }
1321
1322
1323 void
1324 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
1325 {
1326         /* TODO? */
1327     /* printf("%s()\n", __PRETTY_FUNCTION__); */
1328
1329     if(state->system_inactivity_ind)
1330     {
1331     }
1332     else if(state->save_unsaved_data_ind)
1333     {
1334     }
1335     else
1336     {
1337     
1338     }
1339
1340     /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */
1341 }