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 <stdio.h>
35
36 /* Callback for normal D-BUS messages */
37 static gint on_send_mail(GArray * arguments, gpointer data, osso_rpc_t * retval)
38 {
39         if (arguments->len != MODEST_DEBUS_SEND_MAIL_ARGS_COUNT)
40         return OSSO_ERROR;
41         
42     /* Get the arguments: */
43         osso_rpc_t val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_TO);
44         gchar *to = val.value.s;
45         
46         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_CC);
47         gchar *cc = val.value.s;
48         
49         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BCC);
50         gchar *bcc = val.value.s;
51         
52         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_SUBJECT);
53         gchar *subject = val.value.s;
54         
55         val = g_array_index(arguments, osso_rpc_t, MODEST_DEBUS_SEND_MAIL_ARG_BODY);
56         gchar *body = val.value.s;
57         
58         printf("  debug: to=%s\n", to);
59         
60         /* Get the TnyTransportAccount so we can instantiate a mail operation: */
61         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
62         gchar *account_name = modest_account_mgr_get_default_account (account_mgr);
63         if (!account_name) {
64                 g_printerr ("modest: no account found\n");
65                 return OSSO_ERROR;
66         }
67         
68         TnyTransportAccount *transport_account =
69                 TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account
70                                       (modest_runtime_get_account_store(),
71                                        account_name,
72                                        TNY_ACCOUNT_TYPE_TRANSPORT));
73         if (!transport_account) {
74                 g_printerr ("modest: no transport account found for '%s'\n", account_name);
75                 g_free (account_name);
76                 return OSSO_ERROR;
77         }
78         
79         /* Create the mail operation: */                
80         ModestMailOperation *mail_operation = modest_mail_operation_new ();
81         modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_operation);
82         
83         /* Use the mail operation: */
84         gchar * from = modest_account_mgr_get_from_string (account_mgr,
85                                                           account_name);
86         modest_mail_operation_send_new_mail (mail_operation,
87                                      transport_account,
88                                      from, /* from */
89                                      to, cc, bcc, subject, 
90                                      body, /* plain_body */
91                                      NULL, /* html_body */
92                                      NULL, /* attachments_list, GSList of TnyMimePart. */
93                                      (TnyHeaderFlags)0);
94                                      
95         g_object_unref (G_OBJECT (transport_account));
96         g_object_unref (G_OBJECT (mail_operation));
97         g_free (from);
98         g_free (account_name);
99 }
100                       
101 /* Callback for normal D-BUS messages */
102 gint modest_dbus_req_handler(const gchar * interface, const gchar * method,
103                       GArray * arguments, gpointer data,
104                       osso_rpc_t * retval)
105 {
106         printf("debug: modest_dbus_req_handler()\n");
107         printf("debug: method received: %s\n", method);
108         
109          if (g_ascii_strcasecmp(method, MODEST_DBUS_EXAMPLE_MESSAGE) == 0) {
110                 
111      } else if (g_ascii_strcasecmp(method, MODEST_DBUS_METHOD_SEND_MAIL) == 0) {
112         return on_send_mail (arguments, data, retval);
113          }
114     
115         return OSSO_OK;
116 }