2007-05-23 Marcus Bauer <marcusb@openismus.com>
[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 "widgets/modest-msg-edit-window.h"
37 #include "modest-tny-msg.h"
38 #include <libgnomevfs/gnome-vfs-utils.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 typedef struct 
43 {
44         gchar *to;
45         gchar *cc;
46         gchar *bcc;
47         gchar *subject;
48         gchar *body;
49 } SendMailIdleData;
50
51 typedef struct 
52 {
53         gchar *to;
54         gchar *cc;
55         gchar *bcc;
56         gchar *subject;
57         gchar *body;
58         GSList *attachments;
59 } ComposeMailIdleData;
60
61 static gboolean
62 on_idle_send_mail(gpointer user_data)
63 {
64         SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
65         
66         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
67         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
68         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
69         if (!account_name) {
70                 g_printerr ("modest: no account found\n");
71         }
72         
73         TnyTransportAccount *transport_account = NULL;
74         if (account_mgr) {
75                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
76                                       (modest_runtime_get_account_store(),
77                                        account_name));
78         }
79         
80         if (!transport_account) {
81                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
82         }
83         
84         /* Create the mail operation: */
85         if (transport_account) {        
86                 /* Use the mail operation: */
87                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
88                                                                   account_name);
89                 if (!from) {
90                         g_printerr ("modest: no from address for account '%s'\n", account_name);
91                 } else {
92                         ModestMailOperation *mail_operation = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_SEND, NULL);
93                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
94                         
95                         modest_mail_operation_send_new_mail (mail_operation,
96                                              transport_account,
97                                              from, /* from */
98                                              idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject, 
99                                              idle_data->body, /* plain_body */
100                                              NULL, /* html_body */
101                                              NULL, /* attachments_list, GSList of TnyMimePart. */
102                                              (TnyHeaderFlags)0);
103                                              
104                         g_free (from);
105                         g_object_unref (G_OBJECT (mail_operation));
106                 }
107                                      
108                 g_object_unref (G_OBJECT (transport_account));
109         }
110         
111         g_free (account_name);
112         
113         /* Free the idle data: */
114         g_free (idle_data->to);
115         g_free (idle_data->cc);
116         g_free (idle_data->bcc);
117         g_free (idle_data->subject);
118         g_free (idle_data->body);
119         g_free (idle_data);
120         
121         return FALSE; /* Do not call this callback again. */
122 }
123
124 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
125 {
126         if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
127         return OSSO_ERROR;
128         
129     /* Use g_idle to context-switch into the application's thread: */
130         SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
131         
132     /* Get the arguments: */
133         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
134         idle_data->to = g_strdup (val.value.s);
135         
136         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
137         idle_data->cc = g_strdup (val.value.s);
138         
139         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
140         idle_data->bcc = g_strdup (val.value.s);
141         
142         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
143         idle_data->subject = g_strdup (val.value.s);
144         
145         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
146         idle_data->body = g_strdup (val.value.s);
147         
148         /* printf("  debug: to=%s\n", idle_data->to); */
149         g_idle_add(on_idle_send_mail, (gpointer)idle_data);
150         
151         /* Note that we cannot report failures during sending, 
152          * because that would be asynchronous. */
153         return OSSO_OK;
154 }
155
156 /** uri_unescape:
157  * @uri An escaped URI. URIs should always be escaped.
158  * @len The length of the @uri string, or -1 if the string is null terminated.
159  * 
160  * Decode a URI, or URI fragment, as per RFC 1738.
161  * http://www.ietf.org/rfc/rfc1738.txt
162  * 
163  * Return value: An unescaped string. This should be freed with g_free().
164  */
165 static gchar* uri_unescape(const gchar* uri, size_t len)
166 {
167         if (!uri)
168                 return NULL;
169                 
170         if (len == -1)
171                 len = strlen (uri);
172         
173         /* Allocate an extra string so we can be sure that it is null-terminated,
174          * so we can use gnome_vfs_unescape_string().
175          * This is not efficient. */
176         gchar * escaped_nullterminated = g_strndup (uri, len);
177         gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
178         g_free (escaped_nullterminated);
179         
180         return result;
181 }
182
183 /** uri_parse_mailto:
184  * @mailto A mailto URI, with the mailto: prefix.
185  * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings, 
186  * with each name item being followed by a value item. This list should be freed with g_slist_free) after 
187  * all the string items have been freed. This parameter may be NULL.
188  * Parse a mailto URI as per RFC2368.
189  * http://www.ietf.org/rfc/rfc2368.txt
190  * 
191  * Return value: The to address, unescaped. This should be freed with g_free().
192  */
193 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
194 {
195         const gchar* start_to = NULL;
196         /* Remove the mailto: prefix: 
197          * 7 is the length of "mailto:": */
198         if (strncmp (mailto, "mailto:", 7) == 0) {
199                 start_to = mailto + 7;
200         }
201         
202         if (!start_to)
203                 return NULL;
204         
205         /* Look for ?, or the end of the string, marking the end of the to address: */
206         const size_t len_to = strcspn (start_to, "?");
207         gchar* result_to = uri_unescape (start_to, len_to);
208         printf("debug: result_to=%s\n", result_to);
209         
210         /* Get any other items: */
211         const size_t len_mailto = strlen (start_to);
212         const gchar* p = start_to + len_to + 1; /* parsed so far. */
213         const gchar* end = start_to + len_mailto;
214         /* GSList *items = NULL; */
215         const gchar* start_item_name = p;
216         size_t len_item_name = 0;
217         const gchar* start_item_value = NULL;
218         while (p < end) {
219                 
220                 /* Looking for the end of a name; */
221                 if (start_item_name) {
222                         const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
223                         if (len) {
224                                 /* This marks the end of a name and the start of the value: */
225                                 len_item_name = len;
226                                 
227                                 /* Skip over the name and mark the start of the value: */
228                                 p += (len + 1); /* Skip over the = */
229                                 start_item_value = p;
230                         }
231                 }
232                 
233                 /* Looking for the end of a value: */
234                 if (start_item_value) {
235                         const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
236                         /* ? marks the start of a new item: */
237                         if (len) {
238                                 if (start_item_name && len_item_name) {
239                                         /* Finish the previously-started item: */
240                                         gchar *item_value = uri_unescape (start_item_value, len);
241                                         gchar *item_name = g_strndup (start_item_name, len_item_name);
242                                         /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
243                                         
244                                         /* Append the items to the list */
245                                         if(list_items_and_values) {
246                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
247                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
248                                         }
249                                 }
250                                 
251                                 /* Skip over the value and mark the start of a possible new name/value pair: */
252                                 p += (len + 1); /* Skip over the ? */
253                                 start_item_name = p;
254                                 len_item_name = 0;
255                                 start_item_value = NULL;
256                         }
257                 }
258                 
259         }
260         
261         return result_to;
262 }
263
264
265 static gboolean
266 on_idle_mail_to(gpointer user_data)
267 {
268         /* This is based on the implemenation of main.c:start_uil(): */
269         
270         gchar *uri = (gchar*)user_data;
271         GSList *list_names_and_values = NULL;
272         gchar *to = uri_parse_mailto (uri, &list_names_and_values);
273         
274         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
275         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
276         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
277         if (!account_name) {
278                 g_printerr ("modest: no account found\n");
279         }
280         
281         TnyAccount *account = NULL;
282         if (account_mgr) {
283                 account = modest_tny_account_store_get_transport_account_for_open_connection (
284                         modest_runtime_get_account_store(), account_name);
285         }
286         
287         if (!account) {
288                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
289         } else {
290                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
291                                                                   account_name);
292                 if (!from) {
293                         g_printerr ("modest: no from address for account '%s'\n", account_name);
294                 } else {
295                         const gchar *cc = NULL;
296                         const gchar *bcc = NULL;
297                         const gchar *subject = NULL;
298                         const gchar *body = NULL;
299                         
300                         /* Get the relevant items from the list: */
301                         GSList *list = list_names_and_values;
302                         while (list) {
303                                 const gchar * name = (const gchar*)list->data;
304                                 GSList *list_value = g_slist_next (list);
305                                 const gchar * value = (const gchar*)list_value->data;
306                                 
307                                 if (strcmp (name, "cc") == 0) {
308                                         cc = value;
309                                 } else if (strcmp (name, "bcc") == 0) {
310                                         bcc = value;
311                                 } else if (strcmp (name, "subject") == 0) {
312                                         subject = value;
313                                 } else if (strcmp (name, "body") == 0) {
314                                         body = value;
315                                 }
316                                 
317                                 /* Go to the next pair: */
318                                 if (list_value) {
319                                         list = g_slist_next (list_value);
320                                 } else 
321                                         list = NULL;
322                         }
323                         
324                         /* Create the message: */
325                         TnyMsg *msg  = modest_tny_msg_new (to, from, 
326                                 cc, bcc, subject, body, 
327                                 NULL /* attachments */);
328                                 
329                         if (!msg) {
330                                 g_printerr ("modest: failed to create message\n");
331                         } else
332                         {
333                                 /* Add the message to a folder and show its UI for editing: */
334                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
335                                                                         TNY_FOLDER_TYPE_DRAFTS);
336                                 if (!folder) {
337                                         g_printerr ("modest: failed to find Drafts folder\n");
338                                 } else {
339                         
340                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
341                 
342                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
343                                         gtk_widget_show_all (GTK_WIDGET (win));
344                                 
345                                         g_object_unref (G_OBJECT(folder));
346                                 }
347                         
348                                 g_object_unref (G_OBJECT(msg));
349                         }
350                         
351                         g_object_unref (G_OBJECT(account));
352                 }
353         }
354         
355         g_free (account_name);
356         
357         /* Free the list, as required by the uri_parse_mailto() documentation: */
358         if (list_names_and_values)
359                 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
360         g_slist_free (list_names_and_values);
361         
362         g_free(to);
363                 
364         g_free(uri);
365
366         return FALSE; /* Do not call this callback again. */
367 }
368
369 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
370 {
371         if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
372         return OSSO_ERROR;
373         
374     /* Use g_idle to context-switch into the application's thread: */
375  
376     /* Get the arguments: */
377         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
378         gchar *uri = g_strdup (val.value.s);
379         
380         /* printf("  debug: to=%s\n", idle_data->to); */
381         g_idle_add(on_idle_mail_to, (gpointer)uri);
382         
383         /* Note that we cannot report failures during sending, 
384          * because that would be asynchronous. */
385         return OSSO_OK;
386 }
387
388
389 static gboolean
390 on_idle_compose_mail(gpointer user_data)
391 {
392         ComposeMailIdleData *idle_data = (ComposeMailIdleData*)user_data;
393
394         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
395         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
396         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
397         if (!account_name) {
398                 g_printerr ("modest: no account found\n");
399         }
400         
401         TnyAccount *account = NULL;
402         if (account_mgr) {
403                 account = modest_tny_account_store_get_transport_account_for_open_connection (
404                         modest_runtime_get_account_store(), account_name);
405         }
406         
407         if (!account) {
408                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
409         } else {
410                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
411                                                                   account_name);
412                 if (!from) {
413                         g_printerr ("modest: no from address for account '%s'\n", account_name);
414                 } else {
415                         
416                         /* Create the message: */
417                         TnyMsg *msg  = modest_tny_msg_new (idle_data->to, from, 
418                                 idle_data->cc, idle_data->bcc, idle_data->subject, idle_data->body, 
419                                 idle_data->attachments);
420                                 
421                         if (!msg) {
422                                 g_printerr ("modest: failed to create message\n");
423                         } else
424                         {
425                                 /* Add the message to a folder and show its UI for editing: */
426                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
427                                                                         TNY_FOLDER_TYPE_DRAFTS);
428                                 if (!folder) {
429                                         g_printerr ("modest: failed to find Drafts folder\n");
430                                 } else {
431                         
432                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
433                 
434                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
435                                         gtk_widget_show_all (GTK_WIDGET (win));
436                                 
437                                         g_object_unref (G_OBJECT(folder));
438                                 }
439                         
440                                 g_object_unref (G_OBJECT(msg));
441                         }
442                         
443                         g_object_unref (G_OBJECT(account));
444                 }
445         }
446
447         /* Free the idle data: */
448         g_free (idle_data->to);
449         g_free (idle_data->cc);
450         g_free (idle_data->bcc);
451         g_free (idle_data->subject);
452         g_free (idle_data->body);
453         g_free (idle_data->attachments);
454         g_free (idle_data);
455         
456         g_free (account_name);
457         return FALSE; /* Do not call this callback again. */
458 }
459
460 static gint on_compose_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
461 {
462         gchar **list = NULL;
463         gint i = 0;
464         
465         if (arguments->len != MODEST_DEBUS_COMPOSE_MAIL_ARGS_COUNT)
466         return OSSO_ERROR;
467         
468         /* Use g_idle to context-switch into the application's thread: */
469         ComposeMailIdleData *idle_data = g_new0(ComposeMailIdleData, 1); /* Freed in the idle callback. */
470         
471         /* Get the arguments: */
472         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_TO);
473         idle_data->to = g_strdup (val.value.s);
474         
475         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_CC);
476         idle_data->cc = g_strdup (val.value.s);
477         
478         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BCC);
479         idle_data->bcc = g_strdup (val.value.s);
480         
481         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_SUBJECT);
482         idle_data->subject = g_strdup (val.value.s);
483         
484         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_BODY);
485         idle_data->body = g_strdup (val.value.s);
486         
487         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_COMPOSE_MAIL_ARG_ATTACHMENTS);
488         gchar *attachments_str = g_strdup (val.value.s);
489
490         list = g_strsplit(attachments_str, ",", 0);
491         for (i=0; list[i] != NULL; i++) {
492                 idle_data->attachments = g_slist_append(idle_data->attachments, g_strdup(list[i]));
493         }
494         g_strfreev(list);
495
496         
497         /* printf("  debug: to=%s\n", idle_data->to); */
498         g_idle_add(on_idle_compose_mail, (gpointer)idle_data);
499         
500         /* Note that we cannot report failures during sending, 
501          * because that would be asynchronous. */
502         return OSSO_OK;
503 }
504
505
506 static gboolean
507 on_idle_open_message(gpointer user_data)
508 {
509         gchar *uri = (gchar*)user_data;
510         
511         g_message ("not implemented yet %s", __FUNCTION__);
512         
513         g_free(uri);
514         return FALSE; /* Do not call this callback again. */
515 }
516
517 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
518 {
519         if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
520         return OSSO_ERROR;
521         
522     /* Use g_idle to context-switch into the application's thread: */
523
524     /* Get the arguments: */
525         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
526         gchar *uri = g_strdup (val.value.s);
527         
528         /* printf("  debug: to=%s\n", idle_data->to); */
529         g_idle_add(on_idle_open_message, (gpointer)uri);
530         
531         /* Note that we cannot report failures during sending, 
532          * because that would be asynchronous. */
533         return OSSO_OK;
534 }
535
536
537 static gboolean
538 on_idle_send_receive(gpointer user_data)
539 {
540         ModestWindow *win;
541
542         /* Pick the main window if it exists */
543         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
544
545         /* Send & receive all if "Update automatically" is set */
546         /* TODO: check the auto-update parameter in the configuration */
547         modest_ui_actions_do_send_receive_all (win);
548         
549         return FALSE; /* Do not call this callback again. */
550 }
551
552 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
553 {       
554     /* Use g_idle to context-switch into the application's thread: */
555
556     /* This method has no arguments. */
557         
558         /* printf("  debug: to=%s\n", idle_data->to); */
559         g_idle_add(on_idle_send_receive, NULL);
560         
561         /* Note that we cannot report failures during send/receive, 
562          * because that would be asynchronous. */
563         return OSSO_OK;
564 }
565                       
566 /* Callback for normal D-BUS messages */
567 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
568                       GArray * arguments, gpointer data,
569                       osso_rpc_t * retval)
570 {
571         /*
572         printf("debug: modest_dbus_req_handler()\n");
573         printf("debug: method received: %s\n", method);
574         */
575         if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
576                 return on_send_mail (arguments, data, retval);
577         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
578                 return on_mail_to (arguments, data, retval);
579         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
580                 return on_open_message (arguments, data, retval);
581         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
582                 return on_send_receive (arguments, data, retval);
583         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_COMPOSE_MAIL) == 0) {
584                 return on_compose_mail (arguments, data, retval);
585         }
586         else
587                 return OSSO_ERROR;
588 }
589
590 void
591 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
592 {
593         /* TODO? */
594     /* printf("%s()\n", __PRETTY_FUNCTION__); */
595
596     if(state->system_inactivity_ind)
597     {
598     }
599     else if(state->save_unsaved_data_ind)
600     {
601     }
602     else
603     {
604     
605     }
606
607     /* printf("debug: %s(): return\n", __PRETTY_FUNCTION__); */
608 }