2007-04-24 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 <stdio.h>
38
39 typedef struct 
40 {
41         gchar *to;
42         gchar *cc;
43         gchar *bcc;
44         gchar *subject;
45         gchar *body;
46 } SendMailIdleData;
47
48 static gboolean
49 on_idle_send_mail(gpointer user_data)
50 {
51         SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
52         
53         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
54         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
55         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
56         if (!account_name) {
57                 g_printerr ("modest: no account found\n");
58         }
59         
60         TnyTransportAccount *transport_account = NULL;
61         if (account_mgr) {
62                 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
63                                       (modest_runtime_get_account_store(),
64                                        account_name,
65                                        TNY_ACCOUNT_TYPE_TRANSPORT));
66         }
67         
68         if (!transport_account) {
69                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
70         }
71         
72         /* Create the mail operation: */
73         if (transport_account) {        
74                 /* Use the mail operation: */
75                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
76                                                                   account_name);
77                 if (!from) {
78                         g_printerr ("modest: no from address for account '%s'\n", account_name);
79                 } else {
80                         ModestMailOperation *mail_operation = modest_mail_operation_new ();
81                         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
82                         
83                         modest_mail_operation_send_new_mail (mail_operation,
84                                              transport_account,
85                                              from, /* from */
86                                              idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject, 
87                                              idle_data->body, /* plain_body */
88                                              NULL, /* html_body */
89                                              NULL, /* attachments_list, GSList of TnyMimePart. */
90                                              (TnyHeaderFlags)0);
91                                              
92                         g_free (from);
93                         g_object_unref (G_OBJECT (mail_operation));
94                 }
95                                      
96                 g_object_unref (G_OBJECT (transport_account));
97         }
98         
99         g_free (account_name);
100         
101         /* Free the idle data: */
102         g_free (idle_data->to);
103         g_free (idle_data->cc);
104         g_free (idle_data->bcc);
105         g_free (idle_data->subject);
106         g_free (idle_data->body);
107         g_free (idle_data);
108         
109         return FALSE; /* Do not call this callback again. */
110 }
111
112 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
113 {
114         if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
115         return OSSO_ERROR;
116         
117     /* Use g_idle to context-switch into the application's thread: */
118         SendMailIdleData *idle_data = g_new0(SendMailIdleData, 1); /* Freed in the idle callback. */
119         
120     /* Get the arguments: */
121         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
122         idle_data->to = g_strdup (val.value.s);
123         
124         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
125         idle_data->cc = g_strdup (val.value.s);
126         
127         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
128         idle_data->bcc = g_strdup (val.value.s);
129         
130         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
131         idle_data->subject = g_strdup (val.value.s);
132         
133         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
134         idle_data->body = g_strdup (val.value.s);
135         
136         /* printf("  debug: to=%s\n", idle_data->to); */
137         g_idle_add(on_idle_send_mail, (gpointer)idle_data);
138         
139         /* Note that we cannot report failures during sending, 
140          * because that would be asynchronous. */
141         return OSSO_OK;
142 }
143
144
145 static gboolean
146 on_idle_mail_to(gpointer user_data)
147 {
148         /* This is based on the implemenation of main.c:start_uil(): */
149         
150         gchar *uri = (gchar*)user_data;
151         
152         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
153         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
154         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
155         if (!account_name) {
156                 g_printerr ("modest: no account found\n");
157         }
158         
159         TnyAccount *account = NULL;
160         if (account_mgr) {
161                 account = modest_tny_account_store_get_tny_account_by_account (
162                         modest_runtime_get_account_store(), account_name,
163                         TNY_ACCOUNT_TYPE_TRANSPORT);
164         }
165         
166         if (!account) {
167                 g_printerr ("modest: failed to get tny account folder'\n", account_name);
168         } else {
169                 gchar * from = modest_account_mgr_get_from_string (account_mgr,
170                                                                   account_name);
171                 if (!from) {
172                         g_printerr ("modest: no from address for account '%s'\n", account_name);
173                 } else {
174                         TnyMsg *msg  = modest_tny_msg_new (uri /* mailto */, from, 
175                                 NULL /* cc */, NULL /* bcc */,
176                                 NULL /* subject */, NULL /* body */, NULL /* attachments */);
177                         if (!msg) {
178                                 g_printerr ("modest: failed to create message\n");
179                         } else
180                         {
181                                 TnyFolder *folder = modest_tny_account_get_special_folder (account,
182                                                                         TNY_FOLDER_TYPE_DRAFTS);
183                                 if (!folder) {
184                                         g_printerr ("modest: failed to find Drafts folder\n");
185                                 } else {
186                         
187                                         tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
188                 
189                                         ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
190                                         gtk_widget_show_all (GTK_WIDGET (win));
191                                 
192                                         g_object_unref (G_OBJECT(folder));
193                                 }
194                         
195                                 g_object_unref (G_OBJECT(msg));
196                         }
197                         g_object_unref (G_OBJECT(account));
198                 
199                 }
200         }
201         
202         g_free (account_name);
203                 
204         g_free(uri);
205         return FALSE; /* Do not call this callback again. */
206 }
207
208 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
209 {
210         if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
211         return OSSO_ERROR;
212         
213     /* Use g_idle to context-switch into the application's thread: */
214  
215     /* Get the arguments: */
216         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_MAIL_TO_ARG_URI);
217         gchar *uri = g_strdup (val.value.s);
218         
219         /* printf("  debug: to=%s\n", idle_data->to); */
220         g_idle_add(on_idle_mail_to, (gpointer)uri);
221         
222         /* Note that we cannot report failures during sending, 
223          * because that would be asynchronous. */
224         return OSSO_OK;
225 }
226
227
228 static gboolean
229 on_idle_open_message(gpointer user_data)
230 {
231         gchar *uri = (gchar*)user_data;
232         
233         g_message ("not implemented yet %s", __FUNCTION__);
234         
235         g_free(uri);
236         return FALSE; /* Do not call this callback again. */
237 }
238
239 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
240 {
241         if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
242         return OSSO_ERROR;
243         
244     /* Use g_idle to context-switch into the application's thread: */
245
246     /* Get the arguments: */
247         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_OPEN_MESSAGE_ARG_URI);
248         gchar *uri = g_strdup (val.value.s);
249         
250         /* printf("  debug: to=%s\n", idle_data->to); */
251         g_idle_add(on_idle_open_message, (gpointer)uri);
252         
253         /* Note that we cannot report failures during sending, 
254          * because that would be asynchronous. */
255         return OSSO_OK;
256 }
257                       
258 /* Callback for normal D-BUS messages */
259 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
260                       GArray * arguments, gpointer data,
261                       osso_rpc_t * retval)
262 {
263         /*
264         printf("debug: modest_dbus_req_handler()\n");
265         printf("debug: method received: %s\n", method);
266         */
267         if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
268                 return on_send_mail (arguments, data, retval);
269         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_MAIL_TO) == 0) {
270                 return on_mail_to (arguments, data, retval);
271         } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_OPEN_MESSAGE) == 0) {
272                 return on_open_message (arguments, data, retval);
273         }
274         else
275                 return OSSO_ERROR;
276 }