Remove some unused code.
[platform/upstream/ibus.git] / src / ibusconfigservice.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4  * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5  * Copyright (C) 2008-2010 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 #include "ibusshare.h"
23 #include "ibusconfigservice.h"
24
25 enum {
26     LAST_SIGNAL,
27 };
28
29 enum {
30     PROP_0,
31 };
32
33 /* functions prototype */
34 static void      ibus_config_service_class_init      (IBusConfigServiceClass *class);
35 static void      ibus_config_service_init            (IBusConfigService      *config);
36 static void      ibus_config_service_destroy         (IBusConfigService      *config);
37 static void      ibus_config_service_service_method_call
38                                                      (IBusService            *service,
39                                                       GDBusConnection        *connection,
40                                                       const gchar            *sender,
41                                                       const gchar            *object_path,
42                                                       const gchar            *interface_name,
43                                                       const gchar            *method_name,
44                                                       GVariant               *parameters,
45                                                       GDBusMethodInvocation  *invocation);
46 static GVariant *ibus_config_service_service_get_property
47                                                      (IBusService            *service,
48                                                       GDBusConnection        *connection,
49                                                       const gchar            *sender,
50                                                       const gchar            *object_path,
51                                                       const gchar            *interface_name,
52                                                       const gchar            *property_name,
53                                                       GError                **error);
54 static gboolean  ibus_config_service_service_set_property
55                                                      (IBusService            *service,
56                                                       GDBusConnection        *connection,
57                                                       const gchar            *sender,
58                                                       const gchar            *object_path,
59                                                       const gchar            *interface_name,
60                                                       const gchar            *property_name,
61                                                       GVariant               *value,
62                                                       GError                **error);
63 static gboolean  ibus_config_service_set_value       (IBusConfigService      *config,
64                                                       const gchar            *section,
65                                                       const gchar            *name,
66                                                       GVariant               *value,
67                                                       GError                **error);
68 static GVariant *ibus_config_service_get_value       (IBusConfigService      *config,
69                                                       const gchar            *section,
70                                                       const gchar            *name,
71                                                       GError                **error);
72 static GVariant *ibus_config_service_get_values      (IBusConfigService      *config,
73                                                       const gchar            *section,
74                                                       GError                **error);
75 static gboolean  ibus_config_service_unset_value     (IBusConfigService      *config,
76                                                       const gchar            *section,
77                                                       const gchar            *name,
78                                                       GError                **error);
79
80 G_DEFINE_TYPE (IBusConfigService, ibus_config_service, IBUS_TYPE_SERVICE)
81
82 static const gchar introspection_xml[] =
83     "<node>"
84     "  <interface name='org.freedesktop.IBus.Config'>"
85     "    <method name='SetValue'>"
86     "      <arg direction='in'  type='s' name='section' />"
87     "      <arg direction='in'  type='s' name='name' />"
88     "      <arg direction='in'  type='v' name='value' />"
89     "    </method>"
90     "    <method name='GetValue'>"
91     "      <arg direction='in'  type='s' name='section' />"
92     "      <arg direction='in'  type='s' name='name' />"
93     "      <arg direction='out' type='v' name='value' />"
94     "    </method>"
95     "    <method name='GetValues'>"
96     "      <arg direction='in'  type='s' name='section' />"
97     "      <arg direction='out' type='a{sv}' name='values' />"
98     "    </method>"
99     "    <method name='UnsetValue'>"
100     "      <arg direction='in'  type='s' name='section' />"
101     "      <arg direction='in'  type='s' name='name' />"
102     "    </method>"
103     "    <signal name='ValueChanged'>"
104     "      <arg type='s' name='section' />"
105     "      <arg type='s' name='name' />"
106     "      <arg type='v' name='value' />"
107     "    </signal>"
108     "  </interface>"
109     "</node>";
110
111 static void
112 ibus_config_service_class_init (IBusConfigServiceClass *class)
113 {
114     GObjectClass *gobject_class = G_OBJECT_CLASS (class);
115
116     IBUS_OBJECT_CLASS (gobject_class)->destroy = (IBusObjectDestroyFunc) ibus_config_service_destroy;
117
118     IBUS_SERVICE_CLASS (class)->service_method_call  = ibus_config_service_service_method_call;
119     IBUS_SERVICE_CLASS (class)->service_get_property = ibus_config_service_service_get_property;
120     IBUS_SERVICE_CLASS (class)->service_set_property = ibus_config_service_service_set_property;
121
122     ibus_service_class_add_interfaces (IBUS_SERVICE_CLASS (class), introspection_xml);
123
124     class->set_value   = ibus_config_service_set_value;
125     class->get_value   = ibus_config_service_get_value;
126     class->get_values  = ibus_config_service_get_values;
127     class->unset_value = ibus_config_service_unset_value;
128 }
129
130 static void
131 ibus_config_service_init (IBusConfigService *config)
132 {
133 }
134
135 static void
136 ibus_config_service_destroy (IBusConfigService *config)
137 {
138     IBUS_OBJECT_CLASS(ibus_config_service_parent_class)->destroy ((IBusObject *) config);
139 }
140
141 static void
142 ibus_config_service_service_method_call (IBusService           *service,
143                                          GDBusConnection       *connection,
144                                          const gchar           *sender,
145                                          const gchar           *object_path,
146                                          const gchar           *interface_name,
147                                          const gchar           *method_name,
148                                          GVariant              *parameters,
149                                          GDBusMethodInvocation *invocation)
150 {
151     IBusConfigService *config = IBUS_CONFIG_SERVICE (service);
152
153     if (g_strcmp0 (interface_name, IBUS_INTERFACE_CONFIG) != 0) {
154         IBUS_SERVICE_CLASS (ibus_config_service_parent_class)->
155                 service_method_call (service,
156                                      connection,
157                                      sender,
158                                      object_path,
159                                      interface_name,
160                                      method_name,
161                                      parameters,
162                                      invocation);
163         return;
164     }
165
166     if (g_strcmp0 (method_name, "SetValue") == 0) {
167         gchar *section;
168         gchar *name;
169         GVariant *value;
170         gboolean retval;
171         GError *error = NULL;
172
173         g_variant_get (parameters, "(&s&sv)", &section, &name, &value);
174
175         retval = IBUS_CONFIG_SERVICE_GET_CLASS (config)->set_value (config, section, name, value, &error);
176         if (retval) {
177             g_dbus_method_invocation_return_value (invocation, NULL);
178         }
179         else {
180             g_dbus_method_invocation_return_gerror (invocation, error);
181             g_error_free (error);
182         }
183         return;
184     }
185
186     if (g_strcmp0 (method_name, "GetValue") == 0) {
187         gchar *section;
188         gchar *name;
189         GVariant *value;
190         GError *error = NULL;
191
192         g_variant_get (parameters, "(&s&s)", &section, &name);
193
194         value = IBUS_CONFIG_SERVICE_GET_CLASS (config)->get_value (config, section, name, &error);
195         if (value != NULL) {
196             g_dbus_method_invocation_return_value (invocation, g_variant_new ("(v)", value));
197         }
198         else {
199             g_dbus_method_invocation_return_gerror (invocation, error);
200             g_error_free (error);
201         }
202         return;
203     }
204
205     if (g_strcmp0 (method_name, "GetValues") == 0) {
206         gchar *section;
207         GVariant *value;
208         GError *error = NULL;
209
210         g_variant_get (parameters, "(&s)", &section);
211
212         value = IBUS_CONFIG_SERVICE_GET_CLASS (config)->get_values (config,
213                                                                      section,
214                                                                      &error);
215         if (value) {
216             g_dbus_method_invocation_return_value (invocation,
217                     g_variant_new ("(@a{sv})", value));
218         }
219         else {
220             g_dbus_method_invocation_return_gerror (invocation, error);
221             g_error_free (error);
222         }
223         return;
224     }
225
226     if (g_strcmp0 (method_name, "UnsetValue") == 0) {
227         gchar *section;
228         gchar *name;
229         gboolean retval;
230         GError *error = NULL;
231
232         g_variant_get (parameters, "(&s&s)", &section, &name);
233
234         retval = IBUS_CONFIG_SERVICE_GET_CLASS (config)->unset_value (config, section, name, &error);
235         if (retval) {
236             g_dbus_method_invocation_return_value (invocation, NULL);
237         }
238         else {
239             g_dbus_method_invocation_return_gerror (invocation, error);
240             g_error_free (error);
241         }
242         return;
243     }
244
245     /* should not be reached */
246     g_return_if_reached ();
247 }
248
249 static GVariant *
250 ibus_config_service_service_get_property (IBusService        *service,
251                                           GDBusConnection    *connection,
252                                           const gchar        *sender,
253                                           const gchar        *object_path,
254                                           const gchar        *interface_name,
255                                           const gchar        *property_name,
256                                           GError            **error)
257 {
258     return IBUS_SERVICE_CLASS (ibus_config_service_parent_class)->
259                 service_get_property (service,
260                                       connection,
261                                       sender,
262                                       object_path,
263                                       interface_name,
264                                       property_name,
265                                       error);
266 }
267
268 static gboolean
269 ibus_config_service_service_set_property (IBusService        *service,
270                                           GDBusConnection    *connection,
271                                           const gchar        *sender,
272                                           const gchar        *object_path,
273                                           const gchar        *interface_name,
274                                           const gchar        *property_name,
275                                           GVariant           *value,
276                                           GError            **error)
277 {
278     return IBUS_SERVICE_CLASS (ibus_config_service_parent_class)->
279                 service_set_property (service,
280                                       connection,
281                                       sender,
282                                       object_path,
283                                       interface_name,
284                                       property_name,
285                                       value,
286                                       error);
287 }
288
289 static gboolean
290 ibus_config_service_set_value (IBusConfigService *config,
291                                const gchar       *section,
292                                const gchar       *name,
293                                GVariant          *value,
294                                GError           **error)
295 {
296     if (error) {
297         gchar *str = g_variant_print (value, TRUE);
298         *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
299                               "Cannot set value %s::%s to %s", section, name, str);
300         g_free (str);
301     }
302     return FALSE;
303 }
304
305 static GVariant *
306 ibus_config_service_get_value (IBusConfigService *config,
307                                const gchar       *section,
308                                const gchar       *name,
309                                GError           **error)
310 {
311     if (error) {
312         *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
313                               "Cannot get value %s::%s", section, name);
314     }
315     return NULL;
316 }
317
318 static GVariant *
319 ibus_config_service_get_values (IBusConfigService *config,
320                                 const gchar       *section,
321                                 GError           **error)
322 {
323     if (error) {
324         *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
325                               "Cannot get values %s", section);
326     }
327     return NULL;
328 }
329
330 static gboolean
331 ibus_config_service_unset_value (IBusConfigService *config,
332                                  const gchar       *section,
333                                  const gchar       *name,
334                                  GError           **error)
335 {
336     if (error) {
337         *error = g_error_new (G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
338                               "Cannot unset value %s::%s", section, name);
339     }
340     return FALSE;
341 }
342
343 IBusConfigService *
344 ibus_config_service_new (GDBusConnection *connection)
345 {
346     g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
347
348     GObject *object = g_object_new (IBUS_TYPE_CONFIG_SERVICE,
349                                     "object-path", IBUS_PATH_CONFIG,
350                                     "connection", connection,
351                                     NULL);
352     return IBUS_CONFIG_SERVICE (object);
353 }
354
355 void
356 ibus_config_service_value_changed (IBusConfigService  *config,
357                                    const gchar        *section,
358                                    const gchar        *name,
359                                    GVariant           *value)
360 {
361     g_return_if_fail (IBUS_IS_CONFIG_SERVICE (config));
362     g_return_if_fail (section != NULL);
363     g_return_if_fail (name != NULL);
364     g_return_if_fail (value != NULL);
365
366     ibus_service_emit_signal ((IBusService *) config,
367                               NULL,
368                               IBUS_INTERFACE_CONFIG,
369                               "ValueChanged",
370                               g_variant_new ("(ssv)", section, name, value),
371                               NULL);
372 }