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