* Refactored s&r actions
[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 static gboolean
52 on_idle_send_mail(gpointer user_data)
53 {
54         SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
55         
56         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
57         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
58         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
59         if (!account_name) {
60                 g_printerr ("modest: no account found\n");
61         }
62         
63         TnyTransportAccount *transport_account = NULL;
64         if (account_mgr) {
65                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_transport_account_for_open_connection
66                                       (modest_runtime_get_account_store(),
67                                        account_name));
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 (MODEST_MAIL_OPERATION_ID_SEND, NULL);
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_transport_account_for_open_connection (
274                         modest_runtime_get_account_store(), account_name);
275         }
276         
277         if (!account) {
278                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
279         } else {
280                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
281                                                                   account_name);
282                 if (!from) {
283                         g_printerr ("modest: no from address for account '%s'\n", account_name);
284                 } else {
285                         const gchar *cc = NULL;
286                         const gchar *bcc = NULL;
287                         const gchar *subject = NULL;
288                         const gchar *body = NULL;
289                         
290                         /* Get the relevant items from the list: */
291                         GSList *list = list_names_and_values;
292                         while (list) {
293                                 const gchar * name = (const gchar*)list->data;
294                                 GSList *list_value = g_slist_next (list);
295                                 const gchar * value = (const gchar*)list_value->data;
296                                 
297                                 if (strcmp (name, "cc") == 0) {
298                                         cc = value;
299                                 } else if (strcmp (name, "bcc") == 0) {
300                                         bcc = value;
301                                 } else if (strcmp (name, "subject") == 0) {
302                                         subject = value;
303                                 } else if (strcmp (name, "body") == 0) {
304                                         body = value;
305                                 }
306                                 
307                                 /* Go to the next pair: */
308                                 if (list_value) {
309                                         list = g_slist_next (list_value);
310                                 } else 
311                                         list = NULL;
312                         }
313                         
314                         /* Create the message: */
315                         TnyMsg *msg  = modest_tny_msg_new (to, from, 
316                                 cc, bcc, subject, body, 
317                                 NULL /* attachments */);
318                                 
319                         if (!msg) {
320                                 g_printerr ("modest: failed to create message\n");
321                         } else
322                         {
323                                 /* Add the message to a folder and show its UI for editing: */
324                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
325                                                                         TNY_FOLDER_TYPE_DRAFTS);
326                                 if (!folder) {
327                                         g_printerr ("modest: failed to find Drafts folder\n");
328                                 } else {
329                         
330                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
331                 
332                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
333                                         gtk_widget_show_all (GTK_WIDGET (win));
334                                 
335                                         g_object_unref (G_OBJECT(folder));
336                                 }
337                         
338                                 g_object_unref (G_OBJECT(msg));
339                         }
340                         
341                         g_object_unref (G_OBJECT(account));
342                 }
343         }
344         
345         g_free (account_name);
346         
347         /* Free the list, as required by the uri_parse_mailto() documentation: */
348         if (list_names_and_values)
349                 g_slist_foreach (list_names_and_values, (GFunc)g_free, NULL);
350         g_slist_free (list_names_and_values);
351         
352         g_free(to);
353                 
354         g_free(uri);
355
356         return FALSE; /* Do not call this callback again. */
357 }
358
359 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
360 {
361         if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
362         return OSSO_ERROR;
363         
364     /* Use g_idle to context-switch into the application's thread: */
365  
366     /* Get the arguments: */
367         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
368         gchar *uri = g_strdup (val.value.s);
369         
370         /* printf("  debug: to=%s\n", idle_data->to); */
371         g_idle_add(on_idle_mail_to, (gpointer)uri);
372         
373         /* Note that we cannot report failures during sending, 
374          * because that would be asynchronous. */
375         return OSSO_OK;
376 }
377
378
379 static gboolean
380 on_idle_open_message(gpointer user_data)
381 {
382         gchar *uri = (gchar*)user_data;
383         
384         g_message ("not implemented yet %s", __FUNCTION__);
385         
386         g_free(uri);
387         return FALSE; /* Do not call this callback again. */
388 }
389
390 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
391 {
392         if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
393         return OSSO_ERROR;
394         
395     /* Use g_idle to context-switch into the application's thread: */
396
397     /* Get the arguments: */
398         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
399         gchar *uri = g_strdup (val.value.s);
400         
401         /* printf("  debug: to=%s\n", idle_data->to); */
402         g_idle_add(on_idle_open_message, (gpointer)uri);
403         
404         /* Note that we cannot report failures during sending, 
405          * because that would be asynchronous. */
406         return OSSO_OK;
407 }
408
409 static gboolean
410 on_idle_send_receive(gpointer user_data)
411 {
412         ModestWindow *win;
413
414         /* Pick the main window if it exists */
415         win = modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ());
416
417         /* Send & receive all if "Update automatically" is set */
418         /* TODO: check the auto-update parametter in the configuration */
419         modest_ui_actions_do_send_receive_all (win);
420         
421         return FALSE; /* Do not call this callback again. */
422 }
423
424 static gint on_send_receive(GArray * arguments, gpointer data, osso_rpc_t * retval)
425 {       
426     /* Use g_idle to context-switch into the application's thread: */
427
428     /* This method has no arguments. */
429         
430         /* printf("  debug: to=%s\n", idle_data->to); */
431         g_idle_add(on_idle_send_receive, NULL);
432         
433         /* Note that we cannot report failures during send/receive, 
434          * because that would be asynchronous. */
435         return OSSO_OK;
436 }
437                       
438 /* Callback for normal D-BUS messages */
439 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
440                       GArray * arguments, gpointer data,
441                       osso_rpc_t * retval)
442 {
443         /*
444         printf("debug: modest_dbus_req_handler()\n");
445         printf("debug: method received: %s\n", method);
446         */
447         if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
448                 return on_send_mail (arguments, data, retval);
449         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
450                 return on_mail_to (arguments, data, retval);
451         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
452                 return on_open_message (arguments, data, retval);
453         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_RECEIVE) == 0) {
454                 return on_send_receive (arguments, data, retval);
455         }
456         else
457                 return OSSO_ERROR;
458 }
459
460 void
461 modest_osso_cb_hw_state_handler(osso_hw_state_t *state, gpointer data)
462 {
463     printf("%s()\n", __PRETTY_FUNCTION__);
464
465     if(state->system_inactivity_ind)
466     {
467     }
468     else if(state->save_unsaved_data_ind)
469     {
470     }
471     else
472     {
473     
474     }
475
476     printf("debug: %s(): return\n", __PRETTY_FUNCTION__);
477 }