implement org.freedesktop.DBus.Properties.GetAll
[framework/uifw/edbus.git] / src / lib / dbus / e_dbus_interfaces.c
1 #include "E_DBus.h"
2 #include "e_dbus_private.h"
3
4 /**
5  * This file contains wrappers around the standard interfaces that
6  * objects on the bus should implement.
7  */
8
9 static inline DBusPendingCall *
10 _dbus_peer_call(E_DBus_Connection *conn, const char *method_name, const char *destination, const char *path, E_DBus_Method_Return_Cb cb_return, const void *data)
11 {
12   DBusMessage *msg;
13   DBusPendingCall *ret;
14
15   msg = dbus_message_new_method_call
16     (destination, path, "org.freedesktop.DBus.Peer", method_name);
17   if (!msg)
18     {
19        fprintf(stderr,
20                "ERROR: failed to create message for method call: %s() at "
21                "\"%s\" at \"%s\"\n",
22                method_name, destination, path);
23        return NULL;
24     }
25
26   ret = e_dbus_message_send(conn, msg, cb_return, -1, (void *)data);
27   dbus_message_unref(msg);
28
29   if (!ret)
30     fprintf(stderr, "ERROR: could not %s() \"%s\" at \"%s\".\n",
31             method_name, destination, path);
32
33   return ret;
34 }
35
36 /**
37  * Ping the dbus peer
38  *
39  * @param conn the dbus connection
40  * @param destination the bus name that the object is on
41  * @param path the object path
42  * @param cb_return a callback for a successful return
43  * @param data data to pass to the callbacks
44  */
45 EAPI DBusPendingCall *
46 e_dbus_peer_ping(E_DBus_Connection *conn, const char *destination, const char *path, E_DBus_Method_Return_Cb cb_return, const void *data)
47 {
48    return _dbus_peer_call(conn, "Ping", destination, path, cb_return, data);
49 }
50
51 /**
52  * Get the UUID of the peer
53  *
54  * @param conn the dbus connection
55  * @param destination the bus name that the object is on
56  * @param path the object path
57  * @param cb_return a callback for a successful return
58  * @param data data to pass to the callbacks
59  */
60 EAPI DBusPendingCall *
61 e_dbus_peer_get_machine_id(E_DBus_Connection *conn, const char *destination, const char *path, E_DBus_Method_Return_Cb cb_return, const void *data)
62 {
63    return _dbus_peer_call(conn, "GetMachineId", destination, path, cb_return, data);
64 }
65
66 static inline DBusMessage *
67 _dbus_message_property_method_call(E_DBus_Connection *conn, const char *method_name, const char *destination, const char *path, const char *interface, const char *property)
68 {
69   DBusMessage *msg;
70
71   if (!conn)
72     {
73        fprintf(stderr, "ERROR: no connection for call of %s\n", method_name);
74        return NULL;
75     }
76
77   msg = dbus_message_new_method_call
78     (destination, path, "org.freedesktop.DBus.Properties", method_name);
79   if (!msg)
80     {
81        fprintf(stderr,
82                "ERROR: failed to create message for method call: %s() at "
83                "\"%s\" at \"%s\"\n",
84                method_name, destination, path);
85        return NULL;
86     }
87
88   if (property)
89   {
90     dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
91                              DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID);
92   }
93   else
94   {
95     dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID);
96   }
97
98   return msg;
99 }
100
101 EAPI DBusPendingCall *
102 e_dbus_properties_get_all(E_DBus_Connection *conn, const char *destination, const char *path, const char *interface, E_DBus_Method_Return_Cb cb_return, const void *data)
103 {
104   DBusMessage *msg;
105   DBusPendingCall *ret;
106
107   msg = _dbus_message_property_method_call
108     (conn, "GetAll", destination, path, interface, NULL);
109   if (!msg)
110     return NULL;
111   ret = e_dbus_message_send(conn, msg, cb_return, -1, (void *)data);
112   dbus_message_unref(msg);
113
114   if (!ret)
115     fprintf(stderr, "ERROR: failed to call GetAll() at \"%s\" at \"%s\"\n",
116             destination, path);
117
118   return ret;
119 }
120
121 /**
122  * Get the value of a property on an object
123  *
124  * @param conn the dbus connection
125  * @param destination the bus name that the object is on
126  * @param path the object path
127  * @param interface the interface name of the property
128  * @param property the name of the property
129  * @param cb_return a callback for a successful return
130  * @param data data to pass to the callbacks
131  */
132 EAPI DBusPendingCall *
133 e_dbus_properties_get(E_DBus_Connection *conn, const char *destination, const char *path, const char *interface, const char *property, E_DBus_Method_Return_Cb cb_return, const void *data)
134 {
135   DBusMessage *msg;
136   DBusPendingCall *ret;
137
138   msg = _dbus_message_property_method_call
139     (conn, "Get", destination, path, interface, property);
140   if (!msg)
141     return NULL;
142   ret = e_dbus_message_send(conn, msg, cb_return, -1, (void *)data);
143   dbus_message_unref(msg);
144
145   if (!ret)
146     fprintf(stderr, "ERROR: failed to call Get() at \"%s\" at \"%s\"\n",
147             destination, path);
148
149   return ret;
150 }
151
152 /**
153  * Set the value of a property on an object
154  *
155  * @param conn the dbus connection
156  * @param destination the bus name that the object is on
157  * @param path the object path
158  * @param interface the interface name of the property
159  * @param property the name of the property
160  * @param value_type the type of the property's value
161  * @param value a pointer to the value
162  * @param cb_return a callback for a successful return
163  * @param data data to pass to the callbacks
164  */
165 EAPI DBusPendingCall *
166 e_dbus_properties_set(E_DBus_Connection *conn, const char *destination, const char *path, const char *interface, const char *property, int value_type, const void *value, E_DBus_Method_Return_Cb cb_return, const void *data)
167 {
168   DBusMessage *msg;
169   DBusMessageIter iter, sub;
170   DBusError err;
171   DBusPendingCall *ret;
172
173   if (!dbus_type_is_basic(value_type))
174   {
175     if (cb_return)
176     {
177       dbus_error_init(&err);
178       dbus_set_error(&err, "org.enlightenment.DBus.InvalidType", "Only basic types may be set using e_dbus_properties_set()");
179       cb_return((void *)data, NULL, &err);
180
181     }
182     return NULL;
183   }
184
185   msg = _dbus_message_property_method_call
186     (conn, "Set", destination, path, interface, property);
187   if (!msg)
188     return NULL;
189
190   dbus_message_iter_init_append(msg, &iter);
191   dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, dbus_message_type_to_string(value_type), &sub);
192   dbus_message_iter_append_basic(&sub, value_type, &value);
193   dbus_message_iter_close_container(&iter, &sub);
194
195   ret = e_dbus_message_send(conn, msg, cb_return, -1, (void *)data);
196   dbus_message_unref(msg);
197
198   if (!ret)
199     fprintf(stderr, "ERROR: failed to call Set() at \"%s\" at \"%s\"\n",
200             destination, path);
201
202   return ret;
203 }