1 // DBusBinding-gio.c
2 //
3 // Copyright (C) 2007, 2008, 2011, 2012, 2013 Rob Caelers <robc@krandor.nl>
4 // All rights reserved.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "debug.hh"
25 #include <gio/gio.h>
26 
27 #include "DBusGio.hh"
28 #include "dbus/DBusBindingGio.hh"
29 #include "dbus/DBusException.hh"
30 
31 using namespace workrave;
32 using namespace workrave::dbus;
33 
34 
DBusBindingGio(IDBus::Ptr dbus)35 DBusBindingGio::DBusBindingGio(IDBus::Ptr dbus)
36   : dbus(dbus)
37 {
38 }
39 
40 
41 DBusBindingGio::~DBusBindingGio()
42 = default;
43 
44 void
get_int(GVariant * v,int * value)45 DBusMarshallGio::get_int(GVariant *v, int *value)
46 {
47   const GVariantType *argtype = g_variant_get_type(v);
48 
49   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_INT32))
50     {
51       throw DBusRemoteException()
52         << message_info("Type error")
53         << error_code_info(DBUS_ERROR_INVALID_ARGS)
54         << expected_type_info("int");
55     }
56 
57   *value = g_variant_get_int32(v);
58 }
59 
60 void
get_uint8(GVariant * v,uint8_t * value)61 DBusMarshallGio::get_uint8(GVariant *v, uint8_t *value)
62 {
63   const GVariantType *argtype = g_variant_get_type(v);
64 
65   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_BYTE))
66     {
67       throw DBusRemoteException()
68         << message_info("Type error")
69         << error_code_info(DBUS_ERROR_INVALID_ARGS)
70         << expected_type_info("uint8");
71     }
72 
73   *value = g_variant_get_byte(v);
74 }
75 
76 
77 void
get_uint16(GVariant * v,uint16_t * value)78 DBusMarshallGio::get_uint16(GVariant *v, uint16_t *value)
79 {
80   const GVariantType *argtype = g_variant_get_type(v);
81 
82   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_UINT16))
83     {
84       throw DBusRemoteException()
85         << message_info("Type error")
86         << error_code_info(DBUS_ERROR_INVALID_ARGS)
87         << expected_type_info("uint16");
88     }
89 
90   *value = g_variant_get_uint16(v);
91 }
92 
93 
94 void
get_int16(GVariant * v,int16_t * value)95 DBusMarshallGio::get_int16(GVariant *v, int16_t *value)
96 {
97   const GVariantType *argtype = g_variant_get_type(v);
98 
99   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_INT16))
100     {
101       throw DBusRemoteException()
102         << message_info("Type error")
103         << error_code_info(DBUS_ERROR_INVALID_ARGS)
104         << expected_type_info("int16");
105     }
106 
107   *value = g_variant_get_int16(v);
108 }
109 
110 void
get_uint32(GVariant * v,uint32_t * value)111 DBusMarshallGio::get_uint32(GVariant *v, uint32_t *value)
112 {
113   const GVariantType *argtype = g_variant_get_type(v);
114 
115   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_UINT32))
116     {
117       throw DBusRemoteException()
118         << message_info("Type error")
119         << error_code_info(DBUS_ERROR_INVALID_ARGS)
120         << expected_type_info("uint32");
121     }
122 
123   *value = g_variant_get_uint32(v);
124 }
125 
126 
127 void
get_int32(GVariant * v,int32_t * value)128 DBusMarshallGio::get_int32(GVariant *v, int32_t *value)
129 {
130   const GVariantType *argtype = g_variant_get_type(v);
131 
132   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_INT32))
133     {
134       throw DBusRemoteException()
135         << message_info("Type error")
136         << error_code_info(DBUS_ERROR_INVALID_ARGS)
137         << expected_type_info("int32");
138     }
139 
140   *value = g_variant_get_int32(v);
141 }
142 
143 
144 void
get_uint64(GVariant * v,uint64_t * value)145 DBusMarshallGio::get_uint64(GVariant *v, uint64_t *value)
146 {
147   const GVariantType *argtype = g_variant_get_type(v);
148 
149   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_UINT64))
150     {
151       throw DBusRemoteException()
152         << message_info("Type error")
153         << error_code_info(DBUS_ERROR_INVALID_ARGS)
154         << expected_type_info("uint64");
155     }
156 
157   *value = g_variant_get_uint64(v);
158 }
159 
160 
161 void
get_int64(GVariant * v,int64_t * value)162 DBusMarshallGio::get_int64(GVariant *v, int64_t *value)
163 {
164   const GVariantType *argtype = g_variant_get_type(v);
165 
166   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_INT64))
167     {
168       throw DBusRemoteException()
169         << message_info("Type error")
170         << error_code_info(DBUS_ERROR_INVALID_ARGS)
171         << expected_type_info("int64");
172     }
173 
174   *value = g_variant_get_int64(v);
175 }
176 
177 
178 void
get_bool(GVariant * v,bool * value)179 DBusMarshallGio::get_bool(GVariant *v, bool *value)
180 {
181   const GVariantType *argtype = g_variant_get_type(v);
182 
183   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_BOOLEAN))
184     {
185       throw DBusRemoteException()
186         << message_info("Type error")
187         << error_code_info(DBUS_ERROR_INVALID_ARGS)
188         << expected_type_info("bool");
189     }
190 
191   *value = g_variant_get_boolean(v);
192 }
193 
194 
195 void
get_double(GVariant * v,double * value)196 DBusMarshallGio::get_double(GVariant *v, double *value)
197 {
198   const GVariantType *argtype = g_variant_get_type(v);
199 
200   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_DOUBLE))
201     {
202       throw DBusRemoteException()
203         << message_info("Type error")
204         << error_code_info(DBUS_ERROR_INVALID_ARGS)
205         << expected_type_info("double");
206     }
207 
208   *value = g_variant_get_double(v);
209 }
210 
211 
212 void
get_string(GVariant * v,std::string * value)213 DBusMarshallGio::get_string(GVariant *v, std::string *value)
214 {
215   const GVariantType *argtype = g_variant_get_type(v);
216 
217   if (!g_variant_type_equal(argtype, G_VARIANT_TYPE_STRING))
218     {
219       throw DBusRemoteException()
220         << message_info("Type error")
221         << error_code_info(DBUS_ERROR_INVALID_ARGS)
222         << expected_type_info("string");
223     }
224 
225   const char *cstr = g_variant_get_string(v, nullptr);
226   if (cstr != nullptr)
227     {
228       *value = cstr;
229     }
230 }
231 
232 
233 GVariant *
put_int(const int * value)234 DBusMarshallGio::put_int(const int *value)
235 {
236   return g_variant_new_int32(*value);
237 }
238 
239 GVariant *
put_uint8(const uint8_t * value)240 DBusMarshallGio::put_uint8(const uint8_t *value)
241 {
242   return g_variant_new_byte(*value);
243 }
244 
245 GVariant *
put_uint16(const uint16_t * value)246 DBusMarshallGio::put_uint16(const uint16_t *value)
247 {
248   return g_variant_new_uint16(*value);
249 }
250 
251 
252 GVariant *
put_int16(const int16_t * value)253 DBusMarshallGio::put_int16(const int16_t *value)
254 {
255   return g_variant_new_int16(*value);
256 }
257 
258 GVariant *
put_uint32(const uint32_t * value)259 DBusMarshallGio::put_uint32(const uint32_t *value)
260 {
261   return g_variant_new_uint32(*value);
262 }
263 
264 
265 GVariant *
put_int32(const int32_t * value)266 DBusMarshallGio::put_int32(const int32_t *value)
267 {
268   return g_variant_new_int32(*value);
269 }
270 
271 
272 GVariant *
put_uint64(const uint64_t * value)273 DBusMarshallGio::put_uint64(const uint64_t *value)
274 {
275   return g_variant_new_uint64(*value);
276 }
277 
278 
279 GVariant *
put_int64(const int64_t * value)280 DBusMarshallGio::put_int64(const int64_t *value)
281 {
282   return g_variant_new_int64(*value);
283 }
284 
285 
286 GVariant *
put_double(const double * value)287 DBusMarshallGio::put_double(const double *value)
288 {
289   return g_variant_new_double(*value);
290 }
291 
292 
293 GVariant *
put_bool(const bool * value)294 DBusMarshallGio::put_bool(const bool *value)
295 {
296   gboolean v = *value;
297   return g_variant_new_boolean(v);
298 }
299 
300 
301 GVariant *
put_string(const std::string * value)302 DBusMarshallGio::put_string(const std::string *value)
303 {
304   const char *cstr = value->c_str();
305   return g_variant_new_string(cstr);
306 }
307