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