63b6f999ee3816ffe18f10617c7405d7da963462
[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-ui-actions.h"
36 #include "modest-search.h"
37 #include "widgets/modest-msg-edit-window.h"
38 #include "modest-tny-msg.h"
39 #include <libmodest-dbus-client/libmodest-dbus-client.h>
40 #include <libgnomevfs/gnome-vfs-utils.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 typedef struct 
45 {
46         gchar *to;
47         gchar *cc;
48         gchar *bcc;
49         gchar *subject;
50         gchar *body;
51 } SendMailIdleData;
52
53 typedef struct 
54 {
55         gchar *to;
56         gchar *cc;
57         gchar *bcc;
58         gchar *subject;
59         gchar *body;
60         GSList *attachments;
61 } ComposeMailIdleData;
62
63 static gboolean
64 on_idle_send_mail(gpointer user_data)
65 {
66         SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
67         
68         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
69         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
70         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
71         if (!account_name) {
72                 g_printerr ("modest: no account found\n");
73         }
74         
75         TnyTransportAccount *transport_account = NULL;
76         if (account_mgr) {
77                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
78                                       (modest_runtime_get_account_store(),
79                                        account_name));
80         }
81         
82         if (!transport_account) {
83                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
84         }
85         
86         /* Create the mail operation: */
87         if (transport_account) {        
88                 /* Use the mail operation: */
89                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
90                                                                   account_name);
91                 if (!from) {
92                         g_printerr ("modest: no from address for account '%s'\n", account_name);
93                 } else {
94                         ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_TYPE_SEND, NULL);
95                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
96                         
97                         modest_mail_operation_send_new_mail (mail_operation,
98                                              transport_account,
99                                              from, /* from */
100                                              idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject, 
101                                              idle_data->body, /* plain_body */
102                                              NULL, /* html_body */
103                                              NULL, /* attachments_list, GSList of TnyMimePart. */
104                                              (TnyHeaderFlags)0);
105                                              
106                         g_free (from);
107                         g_object_unref (G_OBJECT (mail_operation));
108                 }
109                                      
110                 g_object_unref (G_OBJECT (transport_account));
111         }
112         
113         g_free (account_name);
114         
115         /* Free the idle data: */
116         g_free (idle_data->to);
117         g_free (idle_data->cc);
118         g_free (idle_data->bcc);
119         g_free (idle_data->subject);
120         g_free (idle_data->body);
121         g_free (idle_data);
122         
123         return FALSE; /* Do not call this callback again. */
124 }
125
126 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
127 {
128         if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
129         return OSSO_ERROR;
130         
131     /* Use g_idle to context-switch into the application's thread: */
132         SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
133         
134     /* Get the arguments: */
135         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
136         idle_data->to = g_strdup (val.value.s);
137         
138         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
139         idle_data->cc = g_strdup (val.value.s);
140         
141         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
142         idle_data->bcc = g_strdup (val.value.s);
143         
144         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
145         idle_data->subject = g_strdup (val.value.s);
146         
147         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
148         idle_data->body = g_strdup (val.value.s);
149         
150         /* printf("  debug: to=%s\n", idle_data->to); */
151         g_idle_add(on_idle_send_mail, (gpointer)idle_data);
152         
153         /* Note that we cannot report failures during sending, 
154          * because that would be asynchronous. */
155         return OSSO_OK;
156 }
157
158 /** uri_unescape:
159  * @uri An escaped URI. URIs should always be escaped.
160  * @len The length of the @uri string, or -1 if the string is null terminated.
161  * 
162  * Decode a URI, or URI fragment, as per RFC 1738.
163  * http://www.ietf.org/rfc/rfc1738.txt
164  * 
165  * Return value: An unescaped string. This should be freed with g_free().
166  */
167 static gchar* uri_unescape(const gchar* uri, size_t len)
168 {
169         if (!uri)
170                 return NULL;
171                 
172         if (len == -1)
173                 len = strlen (uri);
174         
175         /* Allocate an extra string so we can be sure that it is null-terminated,
176          * so we can use gnome_vfs_unescape_string().
177          * This is not efficient. */
178         gchar * escaped_nullterminated = g_strndup (uri, len);
179         gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
180         g_free (escaped_nullterminated);
181         
182         return result;
183 }
184
185 /** uri_parse_mailto:
186  * @mailto A mailto URI, with the mailto: prefix.
187  * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings, 
188  * with each name item being followed by a value item. This list should be freed with g_slist_free) after 
189  * all the string items have been freed. This parameter may be NULL.
190  * Parse a mailto URI as per RFC2368.
191  * http://www.ietf.org/rfc/rfc2368.txt
192  * 
193  * Return value: The to address, unescaped. This should be freed with g_free().
194  */
195 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
196 {
197         const gchar* start_to = NULL;
198         /* Remove the mailto: prefix: 
199          * 7 is the length of "mailto:": */
200         if (strncmp (mailto, "mailto:", 7) == 0) {
201                 start_to = mailto + 7;
202         }
203         
204         if (!start_to)
205                 return NULL;
206         
207         /* Look for ?, or the end of the string, marking the end of the to address: */
208         const size_t len_to = strcspn (start_to, "?");
209         gchar* result_to = uri_unescape (start_to, len_to);
210         printf("debug: result_to=%s\n", result_to);
211         
212         /* Get any other items: */
213         const size_t len_mailto = strlen (start_to);
214         const gchar* p = start_to + len_to + 1; /* parsed so far. */
215         const gchar* end = start_to + len_mailto;
216         /* GSList *items = NULL; */
217         const gchar* start_item_name = p;
218         size_t len_item_name = 0;
219         const gchar* start_item_value = NULL;
220         while (p < end) {
221                 
222                 /* Looking for the end of a name; */
223                 if (start_item_name) {
224                         const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
225                         if (len) {
226                                 /* This marks the end of a name and the start of the value: */
227                                 len_item_name = len;
228                                 
229                                 /* Skip over the name and mark the start of the value: */
230                                 p += (len + 1); /* Skip over the = */
231                                 start_item_value = p;
232                         }
233                 }
234                 
235                 /* Looking for the end of a value: */
236                 if (start_item_value) {
237                         const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
238                         /* ? marks the start of a new item: */
239                         if (len) {
240                                 if (start_item_name && len_item_name) {
241                                         /* Finish the previously-started item: */
242                                         gchar *item_value = uri_unescape (start_item_value, len);
243                                         gchar *item_name = g_strndup (start_item_name, len_item_name);
244                                         /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
245                                         
246                                         /* Append the items to the list */
247                                         if(list_items_and_values) {
248                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
249                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
250                                         }
251                                 }
252                                 
253                                 /* Skip over the value and mark the start of a possible new name/value pair: */
254                                 p += (len + 1); /* Skip over the ? */
255                                 start_item_name = p;
256                                 len_item_name = 0;
257                                 start_item_value = NULL;
258                         }
259                 }
260                 
261         }
262         
263         return result_to;
264 }
265
266
267 static gboolean
268 on_idle_mail_to(gpointer user_data)
269 {
270         /* This is based on the implemenation of main.c:start_uil(): */
271         
272         gchar *uri = (gchar*)user_data;
273         GSList *list_names_and_values = NULL;
274         gchar *to = uri_parse_mailto (uri, &list_names_and_values);
275         
276         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
277         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
278         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
279         if (!account_name) {
280                 g_printerr ("modest: no account found\n");
281         }
282         
283         TnyAccount *account = NULL;
284         if (account_mgr) {
285                 account = modest_tny_account_store_get_transport_account_for_open_connection (
286                         modest_runtime_get_account_store(), account_name);
287         }
288         
289         if (!account) {
290                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
291         } else {
292                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
293                                                                   account_name);
294                 if (!from) {
295                         g_printerr ("modest: no from address for account '%s'\n", account_name);
296                 } else {
297                         const gchar *cc = NULL;
298                         const gchar *bcc = NULL;
299                         const gchar *subject = NULL;
300                         const gchar *body = NULL;
301                         
302                         /* Get the relevant items from the list: */
303                         GSList *list = list_names_and_values;
304                         while (list) {
305                                 const gchar * name = (const gchar*)list->data;
306                                 GSList *list_value = g_slist_next (list);
307                                 const gchar * value = (const gchar*)list_value->data;
308                                 
309                                 if (strcmp (name, "cc") == 0) {
310                                         cc = value;
311                                 } else if (strcmp (name, "bcc") == 0) {
312                                         bcc = value;
313                                 } else if (strcmp (name, "subject") == 0) {
314                                         subject = value;
315                                 } else if (strcmp (name, "body") == 0) {
316                                         body = value;
317                                 }
318                                 
319                                 /* Go to the next pair: */
320                                 if (list_value) {
321                                         list = g_slist_next (list_value);
322                                 } else 
323                                         list = NULL;
324                         }
325                         
326                         /* Create the message: */
327                         TnyMsg *msg  = modest_tny_msg_new (to, from, 
328                                 cc, bcc, subject, body, 
329                                 NULL /* attachments */);
330                                 
331                         if (!msg) {
332                                 g_printerr ("modest: failed to create message\n");
333                         } else
334                         {
335                                 /* Add the message to a folder and show its UI for editing: */
336                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
337                                                                         TNY_FOLDER_TYPE_DRAFTS);
338                                 if (!folder) {
339                                         g_printerr ("modest: failed to find Drafts folder\n");
340                                 } else {
341                         
342                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
343                 
344                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
345                                         gtk_widget_show_all (GTK_WIDGET (win));
346                                 
347                                         g_object_unref (G_OBJECT(folder));
348                                 }
349                         
350                                 g_object_unref (G_OBJECT(msg));
351                         }
352                         
353                         g_object_unref (G_OBJECT(account));
354                 }
355         }
356         
357         g_free (account_name);
358         
359         /* Free the list, as required by the uri_parse_mailto() documentation: */
360         if (list_names_and_values)
361                 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
362         g_slist_free (list_names_and_values);
363         
364         g_free(to);
365                 
366         g_free(uri);
367
368         return FALSE; /* Do not call this callback again. */
369 }
370
371 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
372 {
373         if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
374         return OSSO_ERROR;
375         
376     /* Use g_idle to context-switch into the application's thread: */
377  
378     /* Get the arguments: */
379         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
380         gchar *uri = g_strdup (val.value.s);
381         
382         /* printf("  debug: to=%s\n", idle_data->to); */
383         g_idle_add(on_idle_mail_to, (gpointer)uri);
384         
385         /* Note that we cannot report failures during sending, 
386          * because that would be asynchronous. */
387         return OSSO_OK;
388 }
389
390
391 static gboolean
392 on_idle_compose_mail(gpointer user_data)
393 {
394         ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
395
396         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
397         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
398         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
399         if (!account_name) {
400                 g_printerr ("modest: no account found\n");
401         }
402         
403         TnyAccount *account = NULL;
404         if (account_mgr) {
405                 account = modest_tny_account_store_get_transport_account_for_open_connection (
406                         modest_runtime_get_account_store(), account_name);
407         }
408         
409         if (!account) {
410                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
411         } else {
412                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
413                                                                   account_name);
414                 if (!from) {
415                         g_printerr ("modest: no from address for account '%s'\n", account_name);
416                 } else {
417                         
418                         /* Create the message: */
419                         TnyMsg *msg  = modest_tny_msg_new (idle_data->to, from, 
420                                 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body, 
421                                 idle_data->attachments);
422                                 
423                         if (!msg) {
424                                 g_printerr ("modest: failed to create message\n");
425                         } else
426                         {
427                                 /* Add the message to a folder and show its UI for editing: */
428                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
429                                                                         TNY_FOLDER_TYPE_DRAFTS);
430                                 if (!folder) {
431                                         g_printerr ("modest: failed to find Drafts folder\n");
432                                 } else {
433                         
434                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
435                 
436                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
437                                         gtk_widget_show_all (GTK_WIDGET (win));
438                                 
439                                         g_object_unref (G_OBJECT(folder));
440                                 }
441                         
442                                 g_object_unref (G_OBJECT(msg));
443                         }
444                         
445                         g_object_unref (G_OBJECT(account));
446                 }
447         }
448
449         /* Free the idle data: */
450         g_free (idle_data->to);
451         g_free (idle_data->cc);
452         g_free (idle_data->bcc);
453         g_free (idle_data->subject);
454         g_free (idle_data->body);
455         g_free (idle_data->attachments);
456         g_free (idle_data);
457         
458         g_free (account_name);
459         return FALSE; /* Do not call this callback again. */
460 }
461
462 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
463 {
464         gchar **list = NULL;
465         gint i = 0;
466         
467         if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
468         return OSSO_ERROR;
469         
470         /* Use g_idle to context-switch into the application's thread: */
471         ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
472         
473         /* Get the arguments: */
474         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
475         idle_data->to = g_strdup (val.value.s);
476         
477         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
478         idle_data->cc = g_strdup (val.value.s);
479         
480         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
481         idle_data->bcc = g_strdup (val.value.s);
482         
483         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
484         idle_data->subject = g_strdup (val.value.s);
485         
486         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
487         idle_data->body = g_strdup (val.value.s);
488         
489         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
490         gchar *attachments_str = g_strdup (val.value.s);
491
492         list = g_strsplit(attachments_str, ",", 0);
493         for (i=0; list[i] != NULL; i++) {
494                 idle_data->attachments = g_slist_append(idle_data->attachments, g_strdup(list[i]));
495         }
496         g_strfreev(list);
497
498         
499         /* printf("  debug: to=%s\n", idle_data->to); */
500         g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
501         
502         /* Note that we cannot report failures during sending, 
503          * because that would be asynchronous. */
504         return OSSO_OK;
505 }
506
507
508 static TnyMsg *
509 find_message_by_url (const char *uri, TnyAccount **ac_out)
510 {
511
512         ModestTnyAccountStore *astore;
513         TnyAccount            *account;
514         TnyFolder             *folder;
515         TnyMsg                *msg;
516
517         account = NULL;
518         msg = NULL;
519         folder = NULL;
520
521         astore = modest_runtime_get_account_store ();
522         
523         if (astore == NULL) {
524                 return NULL;
525         }
526         
527         g_debug ("Got AccountStore, lets go");
528
529         account = tny_account_store_find_account (TNY_ACCOUNT_STORE (astore),
530                                                   uri);
531         
532         if (account == NULL) {
533                 return NULL;
534         }
535
536         g_debug ("Found account");
537
538         if ( ! TNY_IS_STORE_ACCOUNT (account)) {
539                 goto out;
540         }
541
542         g_debug ("Account is store account");
543
544         *ac_out = account;
545
546         folder = tny_store_account_find_folder (TNY_STORE_ACCOUNT (account),
547                                                 uri,
548                                                 NULL);
549
550         if (folder == NULL) {
551                 goto out;
552         }
553         g_debug ("Found folder");
554         
555
556         msg = tny_folder_find_msg (folder, uri, NULL);
557
558 out:
559         if (account && !msg) {
560                 g_object_unref (account);
561                 *ac_out = NULL;
562         }
563
564         if (folder) {
565                 g_object_unref (folder);
566         }
567
568         return msg;
569 }
570
571 static gboolean
572 on_idle_open_message (gpointer user_data)
573 {
574         ModestWindow *msg_view;
575         TnyMsg       *msg;
576         TnyAccount   *account;
577         TnyHeader    *header; 
578         const char   *msg_uid;
579         const char   *account_name;
580         char         *uri;
581        
582         uri = (char *) user_data;
583
584         g_debug ("Trying to find msg by url: %s", uri); 
585         msg = find_message_by_url (uri, &account);
586         g_free (uri);
587
588         if (msg == NULL) {
589                 return FALSE;
590         }
591         g_debug ("Found message");
592
593         header = tny_msg_get_header (msg);
594         account_name = tny_account_get_name (account);
595         msg_uid = tny_header_get_uid (header);
596         
597         msg_view = modest_msg_view_window_new (msg,
598                                                account_name,
599                                                msg_uid);
600         /* TODO: does that leak the msg_view ?! */
601
602         gtk_widget_show_all (GTK_WIDGET (msg_view));
603
604         g_object_unref (header);
605         g_object_unref (account);
606         
607         return FALSE; /* Do not call this callback again. */
608 }
609
610 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
611 {
612         if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
613         return OSSO_ERROR;
614         
615     /* Use g_idle to context-switch into the application's thread: */
616
617     /* Get the arguments: */
618         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
619         gchar *uri = g_strdup (val.value.s);
620         
621         /* printf("  debug: to=%s\n", idle_data->to); */
622         g_idle_add(on_idle_open_message, (gpointer)uri);
623         
624         /* Note that we cannot report failures during sending, 
625          * because that would be asynchronous. */
626         return OSSO_OK;
627 }
628
629
630 static gboolean
631 on_idle_send_receive(gpointer user_data)
632 {
633         ModestWindow *win;
634
635         /* Pick the main window if it exists */
636         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
637
638         /* Send & receive all if "Update automatically" is set */
639         /* TODO: check the auto-update parameter in the configuration */
640         modest_ui_actions_do_send_receive_all (win);
641         
642         return FALSE; /* Do not call this callback again. */
643 }
644
645 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
646 {       
647     /* Use g_idle to context-switch into the application's thread: */
648
649     /* This method has no arguments. */
650         
651         /* printf("  debug: to=%s\n", idle_data->to); */
652         g_idle_add(on_idle_send_receive, NULL);
653         
654         /* Note that we cannot report failures during send/receive, 
655          * because that would be asynchronous. */
656         return OSSO_OK;
657 }
658                       
659 /* Callback for normal D-BUS messages */
660 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
661                       GArray * arguments, gpointer data,
662                       osso_rpc_t * retval)
663 {
664         
665         printf("debug: modest_dbus_req_handler()\n");
666         printf("debug: method received: %s\n", method);
667         
668         if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
669                 return on_send_mail (arguments, data, retval);
670         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
671                 return on_mail_to (arguments, data, retval);
672         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
673                 return on_open_message (arguments, data, retval);
674         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
675                 return on_send_receive (arguments, data, retval);
676         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
677                 return on_compose_mail (arguments, data, retval);
678         }
679         else {
680                 /* We need to return INVALID here so
681                  * osso is returning DBUS_HANDLER_RESULT_NOT_YET_HANDLED 
682                  * so our modest_dbus_req_filter can kick in!
683                  * */
684                 return OSSO_INVALID;
685         }
686 }
687
688 #define SEARCH_HIT_DBUS_TYPE \
689         DBUS_STRUCT_BEGIN_CHAR_AS_STRING \
690         DBUS_TYPE_STRING_AS_STRING \
691         DBUS_TYPE_STRING_AS_STRING \
692         DBUS_TYPE_STRING_AS_STRING \
693         DBUS_TYPE_STRING_AS_STRING \
694         DBUS_TYPE_UINT64_AS_STRING \
695         DBUS_TYPE_BOOLEAN_AS_STRING \
696         DBUS_TYPE_BOOLEAN_AS_STRING \
697         DBUS_TYPE_INT64_AS_STRING \
698         DBUS_STRUCT_END_CHAR_AS_STRING
699
700 static DBusMessage *
701 search_result_to_messsage (DBusMessage *reply,
702                            GList       *hits)
703 {
704         DBusMessageIter iter;
705         DBusMessageIter array_iter;
706         GList          *hit_iter;
707
708         dbus_message_iter_init_append (reply, &iter); 
709         dbus_message_iter_open_container (&iter,
710                                           DBUS_TYPE_ARRAY,
711                                           SEARCH_HIT_DBUS_TYPE,
712                                           &array_iter); 
713
714         for (hit_iter = hits; hit_iter; hit_iter = hit_iter->next) {
715                 DBusMessageIter  struct_iter;
716                 TnyFolder       *tf;
717                 TnyHeader       *header;
718                 TnyHeaderFlags   flags;
719                 char            *msg_url = "";
720                 const char      *subject = "";
721                 const char      *folder = "";
722                 const char      *sender = "";
723                 guint64          size = 0;
724                 gboolean         has_attachemnt = FALSE;
725                 gboolean         is_unread = FALSE;
726                 gint64           ts = 0;
727                 char             *furl;
728                 const char       *uid;
729
730                 g_debug ("Marshalling hit ...(%s)",
731                          TNY_IS_HEADER (hit_iter->data) ? "yes" : "no");
732
733                 header = TNY_HEADER (hit_iter->data);
734                 tf = tny_header_get_folder (header);
735                 furl = tny_folder_get_url_string (tf);
736
737                 uid = tny_header_get_uid (header);
738                 msg_url = g_strdup_printf ("%s/%s", furl, uid);
739                 
740                 subject = tny_header_get_subject (header);
741                 folder = furl;
742                 sender = tny_header_get_from (header);
743                 size = tny_header_get_message_size (header);
744
745                 flags = tny_header_get_flags (header);
746                 has_attachemnt = flags & TNY_HEADER_FLAG_ATTACHMENTS;
747                 is_unread = ! (flags & TNY_HEADER_FLAG_SEEN);
748                 ts = tny_header_get_date_received (header);
749
750                 g_debug ("Adding hit: %s", msg_url);    
751                 
752                 dbus_message_iter_open_container (&array_iter,
753                                                   DBUS_TYPE_STRUCT,
754                                                   NULL,
755                                                   &struct_iter);
756
757                 dbus_message_iter_append_basic (&struct_iter,
758                                                 DBUS_TYPE_STRING,
759                                                 &msg_url);
760
761                 dbus_message_iter_append_basic (&struct_iter,
762                                                 DBUS_TYPE_STRING,
763                                                 &subject); 
764
765                 dbus_message_iter_append_basic (&struct_iter,
766                                                 DBUS_TYPE_STRING,
767                                                 &folder);
768
769                 dbus_message_iter_append_basic (&struct_iter,
770                                                 DBUS_TYPE_STRING,
771                                                 &sender);
772
773                 dbus_message_iter_append_basic (&struct_iter,
774                                                 DBUS_TYPE_UINT64,
775                                                 &size);
776
777                 dbus_message_iter_append_basic (&struct_iter,
778                                                 DBUS_TYPE_BOOLEAN,
779                                                 &has_attachemnt);
780
781                 dbus_message_iter_append_basic (&struct_iter,
782                                                 DBUS_TYPE_BOOLEAN,
783                                                 &is_unread);
784                 
785                 dbus_message_iter_append_basic (&struct_iter,
786                                                 DBUS_TYPE_INT64,
787                                                 &ts);
788
789                 dbus_message_iter_close_container (&array_iter,
790                                                    &struct_iter); 
791
792
793                 g_free (msg_url);
794                 g_free (furl);
795                 
796                 /* Also unref the header, we don't need it anymore */
797                 g_object_unref (header);
798         }
799
800         dbus_message_iter_close_container (&iter, &array_iter);
801
802         return reply;
803 }
804
805 DBusHandlerResult
806 modest_dbus_req_filter (DBusConnection *con,
807                         DBusMessage    *message,
808                         void           *user_data)
809 {
810         gboolean  handled = FALSE;
811         DBusError error;
812
813         if (dbus_message_is_method_call (message,
814                                          MODEST_DBUS_IFACE,
815                                          MODEST_DBUS_METHOD_SEARCH)) {
816                 ModestDBusSearchFlags dbus_flags;
817                 ModestSearch  search;
818                 DBusMessage  *reply = NULL;
819                 dbus_bool_t  res;
820                 dbus_int64_t sd_v;
821                 dbus_int64_t ed_v;
822                 dbus_int32_t flags_v;
823                 dbus_uint32_t serial;
824                 dbus_uint32_t size_v;
825                 char *folder;
826                 char *query;
827                 time_t start_date;
828                 time_t end_date;
829                 GList *hits;
830
831                 handled = TRUE;
832
833                 dbus_error_init (&error);
834
835                 sd_v = ed_v = 0;
836                 flags_v = 0;
837
838                 res = dbus_message_get_args (message,
839                                              &error,
840                                              DBUS_TYPE_STRING, &query,
841                                              DBUS_TYPE_STRING, &folder,
842                                              DBUS_TYPE_INT64, &sd_v,
843                                              DBUS_TYPE_INT64, &ed_v,
844                                              DBUS_TYPE_INT32, &flags_v,
845                                              DBUS_TYPE_UINT32, &size_v,
846                                              DBUS_TYPE_INVALID);
847                 
848                 dbus_flags = (ModestDBusSearchFlags) flags_v;
849                 start_date = (time_t) sd_v;
850                 end_date = (time_t) ed_v;
851
852                 memset (&search, 0, sizeof (search));
853                 search.query  = query;
854                 search.before = start_date;
855                 search.after  = end_date;
856                 search.flags  = 0;
857
858                 if (dbus_flags & MODEST_DBUS_SEARCH_SUBJECT) {
859                         search.flags |= MODEST_SEARCH_SUBJECT;
860                         search.subject = query;
861                 }
862
863                 if (dbus_flags & MODEST_DBUS_SEARCH_SENDER) {
864                         search.flags |=  MODEST_SEARCH_SENDER;
865                         search.from = query;
866                 }
867
868                 if (dbus_flags & MODEST_DBUS_SEARCH_RECIPIENT) {
869                         search.flags |= MODEST_SEARCH_RECIPIENT; 
870                         search.recipient = query;
871                 }
872
873                 if (dbus_flags & MODEST_DBUS_SEARCH_BODY) {
874                         search.flags |=  MODEST_SEARCH_BODY; 
875                         search.subject = query;
876                 }
877
878                 if (sd_v > 0) {
879                         search.flags |= MODEST_SEARCH_BEFORE;
880                         search.before = start_date;
881                 }
882
883                 if (ed_v > 0) {
884                         search.flags |= MODEST_SEARCH_AFTER;
885                         search.after = end_date;
886                 }
887
888                 if (size_v > 0) {
889                         search.flags |= MODEST_SEARCH_SIZE;
890                         search.minsize = size_v;
891                 }
892
893 #ifdef MODEST_HAVE_OGS
894                 search.flags |= MODEST_SEARCH_USE_OGS;
895 #endif
896
897                 g_debug ("Starting search for %s", search.query);
898
899                 hits = modest_search_all_accounts (&search);
900
901                 reply = dbus_message_new_method_return (message);
902
903                 search_result_to_messsage (reply, hits);
904                 
905                 if (reply == NULL) {
906                         g_warning ("Could not create reply");
907                 }
908
909                 if (reply) {
910                         dbus_connection_send (con, reply, &serial);
911                         dbus_connection_flush (con);
912                         dbus_message_unref (reply);
913                 }
914
915         }
916         
917
918         return (handled ? 
919                 DBUS_HANDLER_RESULT_HANDLED :
920                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
921 }
922
923 void
924 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
925 {
926         /* TODO? */
927     /* printf("%s()\n", __PRETTY_FUNCTION__); */
928
929     if(state->system_inactivity_ind)
930     {
931     }
932     else if(state->save_unsaved_data_ind)
933     {
934     }
935     else
936     {
937     
938     }
939
940     /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */
941 }