version and bug fix info update for week 50, 2009 first release
[modest] / src / dbus_api / asdbus.c
1 /* Copyright (c) 2006, 2008 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 "asdbus.h"
31 #include "asdbus-bindings.h"
32 #include <glib-object.h>
33 #include "modest-runtime.h"
34 #include "modest-protocol.h"
35 #include "modest-protocol-registry.h"
36 #include "modest-account-mgr.h"
37
38
39 static
40 void recipient_iter (const GValue *value, gpointer user_data)
41 {
42         AsDbusRecipient *recipient = g_new0 (AsDbusRecipient, 1);
43         g_return_if_fail (dbus_g_type_is_struct (G_VALUE_TYPE (value)));
44         g_return_if_fail (3 >= dbus_g_type_get_struct_size (G_VALUE_TYPE (value)));
45         g_return_if_fail (G_TYPE_STRING == dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 1));
46         g_return_if_fail (G_TYPE_STRING == dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 2));
47         g_return_if_fail (dbus_g_type_struct_get (value,
48                         1, &recipient->display_name,
49                         2, &recipient->email_address,
50                         G_MAXUINT));
51         *((GList **)user_data) = g_list_append (*((GList **)user_data), recipient);
52 }
53
54
55 static
56 void response_iter (const GValue *value, gpointer user_data)
57 {
58         GValue recipients = {0,};
59         g_return_if_fail (dbus_g_type_is_struct (G_VALUE_TYPE (value)));
60         g_return_if_fail (4 >= dbus_g_type_get_struct_size (G_VALUE_TYPE (value)));
61         g_return_if_fail (dbus_g_type_is_collection (dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 3)));
62         g_return_if_fail (dbus_g_type_struct_get_member (value, 3, g_value_init (&recipients, dbus_g_type_get_struct_member_type (G_VALUE_TYPE (value), 3))));
63         dbus_g_type_collection_value_iterate (&recipients, recipient_iter, user_data);
64 }
65
66
67 /* assuming g_type_init is already called */
68 GList * asdbus_resolve_recipients (const gchar *name)
69 {
70         GList *result = NULL;
71         DBusGConnection *bus = NULL;
72         GError *error = NULL;
73         DBusGProxy *asdbus = NULL;
74         GValueArray* reply = NULL;
75         ModestProtocol *protocol = NULL;
76         ModestProtocolType protocol_type;
77
78         protocol = modest_protocol_registry_get_protocol_by_name (
79                         modest_runtime_get_protocol_registry (),
80                         MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS,  // MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS ?
81                         "activesync");
82         protocol_type = modest_protocol_get_type_id (protocol);
83     if (!modest_account_mgr_singleton_protocol_exists(modest_runtime_get_account_mgr(), protocol_type)) {
84                 return NULL;
85         }
86
87         const gchar *names[2];
88         names[0] = name;
89         names[1] = NULL;
90
91         bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
92         if (bus == NULL) {
93                 /** FIXME: proper log */
94                 g_printerr ("Failed to open connection to bus: %s\n", error->message);
95                 g_error_free (error);
96                 goto CLEANUP;
97         }
98
99         asdbus = dbus_g_proxy_new_for_name (bus, "com.nokia.asdbus", "/com/nokia/asdbus", "com.nokia.asdbus");
100
101         if (!com_nokia_asdbus_resolve_recipients (asdbus, names, &reply, &error)) {
102                 g_printerr ("com_nokia_asdbus_many_args failed: %s\n", error->message);
103                 g_error_free (error);
104                 goto CLEANUP;
105         }
106
107         if (!reply) goto CLEANUP;
108         if (2 != reply->n_values) goto CLEANUP;
109         if (G_TYPE_INT != G_VALUE_TYPE (&reply->values[0])) goto CLEANUP;
110         if (!dbus_g_type_is_collection (G_VALUE_TYPE (&reply->values[1]))) goto CLEANUP;
111
112         dbus_g_type_collection_value_iterate (&reply->values[1], response_iter, &result);
113
114 CLEANUP:
115         g_value_array_free (reply);
116         g_object_unref (asdbus);
117
118         return result;
119 }