1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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"
49 on_idle_send_mail(gpointer user_data)
51 SendMailIdleData *idle_data = (SendMailIdleData*)user_data;
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);
57 g_printerr ("modest: no account found\n");
60 TnyTransportAccount *transport_account = NULL;
62 transport_account = TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
63 (modest_runtime_get_account_store(),
65 TNY_ACCOUNT_TYPE_TRANSPORT));
68 if (!transport_account) {
69 g_printerr ("modest: no transport account found for '%s'\n", account_name);
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,
78 g_printerr ("modest: no from address for account '%s'\n", account_name);
80 ModestMailOperation *mail_operation = modest_mail_operation_new ();
81 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
83 modest_mail_operation_send_new_mail (mail_operation,
86 idle_data->to, idle_data->cc, idle_data->bcc, idle_data->subject,
87 idle_data->body, /* plain_body */
89 NULL, /* attachments_list, GSList of TnyMimePart. */
93 g_object_unref (G_OBJECT (mail_operation));
96 g_object_unref (G_OBJECT (transport_account));
99 g_free (account_name);
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);
109 return FALSE; /* Do not call this callback again. */
112 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
114 if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
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. */
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);
124 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
125 idle_data->cc = g_strdup (val.value.s);
127 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
128 idle_data->bcc = g_strdup (val.value.s);
130 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
131 idle_data->subject = g_strdup (val.value.s);
133 val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
134 idle_data->body = g_strdup (val.value.s);
136 /* printf(" debug: to=%s\n", idle_data->to); */
137 g_idle_add(on_idle_send_mail, (gpointer)idle_data);
139 /* Note that we cannot report failures during sending,
140 * because that would be asynchronous. */
146 on_idle_mail_to(gpointer user_data)
148 /* This is based on the implemenation of main.c:start_uil(): */
150 gchar *uri = (gchar*)user_data;
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);
156 g_printerr ("modest: no account found\n");
159 TnyAccount *account = NULL;
161 account = modest_tny_account_store_get_tny_account_by_account (
162 modest_runtime_get_account_store(), account_name,
163 TNY_ACCOUNT_TYPE_TRANSPORT);
167 g_printerr ("modest: failed to get tny account folder'\n", account_name);
169 gchar * from = modest_account_mgr_get_from_string (account_mgr,
172 g_printerr ("modest: no from address for account '%s'\n", account_name);
174 TnyMsg *msg = modest_tny_msg_new (uri /* mailto */, from,
175 NULL /* cc */, NULL /* bcc */,
176 NULL /* subject */, NULL /* body */, NULL /* attachments */);
178 g_printerr ("modest: failed to create message\n");
181 TnyFolder *folder = modest_tny_account_get_special_folder (account,
182 TNY_FOLDER_TYPE_DRAFTS);
184 g_printerr ("modest: failed to find Drafts folder\n");
187 tny_folder_add_msg (folder, msg, NULL); /* TODO: check err */
189 ModestWindow *win = modest_msg_edit_window_new (msg, account_name);
190 gtk_widget_show_all (GTK_WIDGET (win));
192 g_object_unref (G_OBJECT(folder));
195 g_object_unref (G_OBJECT(msg));
197 g_object_unref (G_OBJECT(account));
202 g_free (account_name);
205 return FALSE; /* Do not call this callback again. */
208 static gint on_mail_to(GArray * arguments, gpointer data, osso_rpc_t * retval)
210 if (arguments->len != MODEST_DEBUS_MAIL_TO_ARGS_COUNT)
213 /* Use g_idle to context-switch into the application's thread: */
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);
219 /* printf(" debug: to=%s\n", idle_data->to); */
220 g_idle_add(on_idle_mail_to, (gpointer)uri);
222 /* Note that we cannot report failures during sending,
223 * because that would be asynchronous. */
229 on_idle_open_message(gpointer user_data)
231 gchar *uri = (gchar*)user_data;
233 g_message ("not implemented yet %s", __FUNCTION__);
236 return FALSE; /* Do not call this callback again. */
239 static gint on_open_message(GArray * arguments, gpointer data, osso_rpc_t * retval)
241 if (arguments->len != MODEST_DEBUS_OPEN_MESSAGE_ARGS_COUNT)
244 /* Use g_idle to context-switch into the application's thread: */
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);
250 /* printf(" debug: to=%s\n", idle_data->to); */
251 g_idle_add(on_idle_open_message, (gpointer)uri);
253 /* Note that we cannot report failures during sending,
254 * because that would be asynchronous. */
258 /* Callback for normal D-BUS messages */
259 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
260 GArray * arguments, gpointer data,
264 printf("debug: modest_dbus_req_handler()\n");
265 printf("debug: method received: %s\n", method);
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);