2007-04-25 Murray Cumming <murrayc@murrayc.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 "widgets/modest-msg-edit-window.h"
36 #include "modest-tny-msg.h"
37 #include <libgnomevfs/gnome-vfs-utils.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 typedef struct 
42 {
43         gchar *to;
44         gchar *cc;
45         gchar *bcc;
46         gchar *subject;
47         gchar *body;
48 } SendMailIdleData;
49
50 static gboolean
51 on_idle_send_mail(gpointer user_data)
52 {
53         SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
54         
55         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
56         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
57         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
58         if (!account_name) {
59                 g_printerr ("modest: no account found\n");
60         }
61         
62         TnyTransportAccount *transport_account = NULL;
63         if (account_mgr) {
64                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
65                                       (modest_runtime_get_account_store(),
66                                        account_name,
67                                        TNY_ACCOUNT_TYPE_TRANSPORT));
68         }
69         
70         if (!transport_account) {
71                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
72         }
73         
74         /* Create the mail operation: */
75         if (transport_account) {        
76                 /* Use the mail operation: */
77                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
78                                                                   account_name);
79                 if (!from) {
80                         g_printerr ("modest: no from address for account '%s'\n", account_name);
81                 } else {
82                         ModestMailOperation *mail_operation = modest_mail_operation_new ();
83                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
84                         
85                         modest_mail_operation_send_new_mail (mail_operation,
86                                              transport_account,
87                                              from, /* from */
88                                              idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject, 
89                                              idle_data->body, /* plain_body */
90                                              NULL, /* html_body */
91                                              NULL, /* attachments_list, GSList of TnyMimePart. */
92                                              (TnyHeaderFlags)0);
93                                              
94                         g_free (from);
95                         g_object_unref (G_OBJECT (mail_operation));
96                 }
97                                      
98                 g_object_unref (G_OBJECT (transport_account));
99         }
100         
101         g_free (account_name);
102         
103         /* Free the idle data: */
104         g_free (idle_data->to);
105         g_free (idle_data->cc);
106         g_free (idle_data->bcc);
107         g_free (idle_data->subject);
108         g_free (idle_data->body);
109         g_free (idle_data);
110         
111         return FALSE; /* Do not call this callback again. */
112 }
113
114 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
115 {
116         if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
117         return OSSO_ERROR;
118         
119     /* Use g_idle to context-switch into the application's thread: */
120         SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
121         
122     /* Get the arguments: */
123         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
124         idle_data->to = g_strdup (val.value.s);
125         
126         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
127         idle_data->cc = g_strdup (val.value.s);
128         
129         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
130         idle_data->bcc = g_strdup (val.value.s);
131         
132         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
133         idle_data->subject = g_strdup (val.value.s);
134         
135         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
136         idle_data->body = g_strdup (val.value.s);
137         
138         /* printf("  debug: to=%s\n", idle_data->to); */
139         g_idle_add(on_idle_send_mail, (gpointer)idle_data);
140         
141         /* Note that we cannot report failures during sending, 
142          * because that would be asynchronous. */
143         return OSSO_OK;
144 }
145
146 /** uri_unescape:
147  * @uri An escaped URI. URIs should always be escaped.
148  * @len The length of the @uri string, or -1 if the string is null terminated.
149  * 
150  * Decode a URI, or URI fragment, as per RFC 1738.
151  * http://www.ietf.org/rfc/rfc1738.txt
152  * 
153  * Return value: An unescaped string. This should be freed with g_free().
154  */
155 static gchar* uri_unescape(const gchar* uri, size_t len)
156 {
157         if (!uri)
158                 return NULL;
159                 
160         if (len == -1)
161                 len = strlen (uri);
162         
163         /* Allocate an extra string so we can be sure that it is null-terminated,
164          * so we can use gnome_vfs_unescape_string().
165          * This is not efficient. */
166         gchar * escaped_nullterminated = g_strndup (uri, len);
167         gchar *result = gnome_vfs_unescape_string (escaped_nullterminated, NULL);
168         g_free (escaped_nullterminated);
169         
170         return result;
171 }
172
173 /** uri_parse_mailto:
174  * @mailto A mailto URI, with the mailto: prefix.
175  * @list_items_and_values: A pointer to a list that should be filled with item namesand value strings, 
176  * with each name item being followed by a value item. This list should be freed with g_slist_free) after 
177  * all the string items have been freed. This parameter may be NULL.
178  * Parse a mailto URI as per RFC2368.
179  * http://www.ietf.org/rfc/rfc2368.txt
180  * 
181  * Return value: The to address, unescaped. This should be freed with g_free().
182  */
183 static gchar* uri_parse_mailto (const gchar* mailto, GSList** list_items_and_values)
184 {
185         const gchar* start_to = NULL;
186         /* Remove the mailto: prefix: 
187          * 7 is the length of "mailto:": */
188         if (strncmp (mailto, "mailto:", 7) == 0) {
189                 start_to = mailto + 7;
190         }
191         
192         if (!start_to)
193                 return NULL;
194         
195         /* Look for ?, or the end of the string, marking the end of the to address: */
196         const size_t len_to = strcspn (start_to, "?");
197         gchar* result_to = uri_unescape (start_to, len_to);
198         printf("debug: result_to=%s\n", result_to);
199         
200         /* Get any other items: */
201         const size_t len_mailto = strlen (start_to);
202         const gchar* p = start_to + len_to + 1; /* parsed so far. */
203         const gchar* end = start_to + len_mailto;
204         /* GSList *items = NULL; */
205         const gchar* start_item_name = p;
206         size_t len_item_name = 0;
207         const gchar* start_item_value = NULL;
208         while (p < end) {
209                 
210                 /* Looking for the end of a name; */
211                 if (start_item_name) {
212                         const size_t len = strcspn (p, "="); /* Returns whole string if none found. */
213                         if (len) {
214                                 /* This marks the end of a name and the start of the value: */
215                                 len_item_name = len;
216                                 
217                                 /* Skip over the name and mark the start of the value: */
218                                 p += (len + 1); /* Skip over the = */
219                                 start_item_value = p;
220                         }
221                 }
222                 
223                 /* Looking for the end of a value: */
224                 if (start_item_value) {
225                         const size_t len = strcspn (p, "?"); /* Returns whole string if none found. */
226                         /* ? marks the start of a new item: */
227                         if (len) {
228                                 if (start_item_name && len_item_name) {
229                                         /* Finish the previously-started item: */
230                                         gchar *item_value = uri_unescape (start_item_value, len);
231                                         gchar *item_name = g_strndup (start_item_name, len_item_name);
232                                         /* printf ("debug: item name=%s, value=%s\n", item_name, item_value); */
233                                         
234                                         /* Append the items to the list */
235                                         if(list_items_and_values) {
236                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_name);
237                                                 *list_items_and_values = g_slist_append (*list_items_and_values, item_value);
238                                         }
239                                 }
240                                 
241                                 /* Skip over the value and mark the start of a possible new name/value pair: */
242                                 p += (len + 1); /* Skip over the ? */
243                                 start_item_name = p;
244                                 len_item_name = 0;
245                                 start_item_value = NULL;
246                         }
247                 }
248                 
249         }
250         
251         return result_to;
252 }
253
254
255 static gboolean
256 on_idle_mail_to(gpointer user_data)
257 {
258         /* This is based on the implemenation of main.c:start_uil(): */
259         
260         gchar *uri = (gchar*)user_data;
261         GSList *list_names_and_values = NULL;
262         gchar *to = uri_parse_mailto (uri, &list_names_and_values);
263         
264         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
265         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
266         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
267         if (!account_name) {
268                 g_printerr ("modest: no account found\n");
269         }
270         
271         TnyAccount *account = NULL;
272         if (account_mgr) {
273                 account = modest_tny_account_store_get_tny_account_by_account (
274                         modest_runtime_get_account_store(), account_name,
275                         TNY_ACCOUNT_TYPE_TRANSPORT);
276         }
277         
278         if (!account) {
279                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
280         } else {
281                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
282                                                                   account_name);
283                 if (!from) {
284                         g_printerr ("modest: no from address for account '%s'\n", account_name);
285                 } else {
286                         const gchar *cc = NULL;
287                         const gchar *bcc = NULL;
288                         const gchar *subject = NULL;
289                         const gchar *body = NULL;
290                         
291                         /* Get the relevant items from the list: */
292                         GSList *list = list_names_and_values;
293                         while (list) {
294                                 const gchar * name = (const gchar*)list->data;
295                                 GSList *list_value = g_slist_next (list);
296                                 const gchar * value = (const gchar*)list_value->data;
297                                 
298                                 if (strcmp (name, "cc") == 0) {
299                                         cc = value;
300                                 } else if (strcmp (name, "bcc") == 0) {
301                                         bcc = value;
302                                 } else if (strcmp (name, "subject") == 0) {
303                                         subject = value;
304                                 } else if (strcmp (name, "body") == 0) {
305                                         body = value;
306                                 }
307                                 
308                                 /* Go to the next pair: */
309                                 if (list_value) {
310                                         list = g_slist_next (list_value);
311                                 } else 
312                                         list = NULL;
313                         }
314                         
315                         /* Create the message: */
316                         TnyMsg *msg  = modest_tny_msg_new (to, from, 
317                                 cc, bcc, subject, body, 
318                                 NULL /* attachments */);
319                                 
320                         if (!msg) {
321                                 g_printerr ("modest: failed to create message\n");
322                         } else
323                         {
324                                 /* Add the message to a folder and show its UI for editing: */
325                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
326                                                                         TNY_FOLDER_TYPE_DRAFTS);
327                                 if (!folder) {
328                                         g_printerr ("modest: failed to find Drafts folder\n");
329                                 } else {
330                         
331                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
332                 
333                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
334                                         gtk_widget_show_all (GTK_WIDGET (win));
335                                 
336                                         g_object_unref (G_OBJECT(folder));
337                                 }
338                         
339                                 g_object_unref (G_OBJECT(msg));
340                         }
341                         
342                         g_object_unref (G_OBJECT(account));
343                 }
344         }
345         
346         g_free (account_name);
347         
348         /* Free the list, as required by the uri_parse_mailto() documentation: */
349         if (list_names_and_values)
350                 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
351         g_slist_free (list_names_and_values);
352         
353         g_free(to);
354                 
355         g_free(uri);
356
357         return FALSE; /* Do not call this callback again. */
358 }
359
360 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
361 {
362         if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
363         return OSSO_ERROR;
364         
365     /* Use g_idle to context-switch into the application's thread: */
366  
367     /* Get the arguments: */
368         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
369         gchar *uri = g_strdup (val.value.s);
370         
371         /* printf("  debug: to=%s\n", idle_data->to); */
372         g_idle_add(on_idle_mail_to, (gpointer)uri);
373         
374         /* Note that we cannot report failures during sending, 
375          * because that would be asynchronous. */
376         return OSSO_OK;
377 }
378
379
380 static gboolean
381 on_idle_open_message(gpointer user_data)
382 {
383         gchar *uri = (gchar*)user_data;
384         
385         g_message ("not implemented yet %s", __FUNCTION__);
386         
387         g_free(uri);
388         return FALSE; /* Do not call this callback again. */
389 }
390
391 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
392 {
393         if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
394         return OSSO_ERROR;
395         
396     /* Use g_idle to context-switch into the application's thread: */
397
398     /* Get the arguments: */
399         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
400         gchar *uri = g_strdup (val.value.s);
401         
402         /* printf("  debug: to=%s\n", idle_data->to); */
403         g_idle_add(on_idle_open_message, (gpointer)uri);
404         
405         /* Note that we cannot report failures during sending, 
406          * because that would be asynchronous. */
407         return OSSO_OK;
408 }
409                       
410 /* Callback for normal D-BUS messages */
411 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
412                       GArray * arguments, gpointer data,
413                       osso_rpc_t * retval)
414 {
415         /*
416         printf("debug: modest_dbus_req_handler()\n");
417         printf("debug: method received: %s\n", method);
418         */
419         if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
420                 return on_send_mail (arguments, data, retval);
421         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
422                 return on_mail_to (arguments, data, retval);
423         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
424                 return on_open_message (arguments, data, retval);
425         }
426         else
427                 return OSSO_ERROR;
428 }