Add IBusConfigService#unset().
[platform/upstream/ibus.git] / src / ibusconfigservice.c
1 /* vim:set et sts=4: */
2 /* ibus - The Input Bus
3  * Copyright (C) 2008-2009 Peng Huang <shawn.p.huang@gmail.com>
4  * Copyright (C) 2008-2009 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "ibusshare.h"
23 #include "ibusconfigservice.h"
24
25 enum {
26     LAST_SIGNAL,
27 };
28
29 enum {
30     PROP_0,
31     PROP_CONNECTION,
32 };
33
34 // static guint            config_service_signals[LAST_SIGNAL] = { 0 };
35
36 /* functions prototype */
37 static void     ibus_config_service_class_init      (IBusConfigServiceClass *klass);
38 static void     ibus_config_service_init            (IBusConfigService      *config);
39 static void     ibus_config_service_set_property    (IBusConfigService      *config,
40                                                      guint                   prop_id,
41                                                      const GValue           *value,
42                                                      GParamSpec             *pspec);
43 static void     ibus_config_service_get_property    (IBusConfigService      *config,
44                                                      guint                   prop_id,
45                                                      GValue                 *value,
46                                                      GParamSpec             *pspec);
47 static void     ibus_config_service_destroy         (IBusConfigService      *config);
48 static gboolean ibus_config_service_ibus_message    (IBusConfigService      *config,
49                                                      IBusConnection         *connection,
50                                                      IBusMessage            *message);
51 static gboolean ibus_config_service_set_value       (IBusConfigService      *config,
52                                                      const gchar            *section,
53                                                      const gchar            *name,
54                                                      const GValue           *value,
55                                                      IBusError             **error);
56 static gboolean ibus_config_service_get_value       (IBusConfigService      *config,
57                                                      const gchar            *section,
58                                                      const gchar            *name,
59                                                      GValue                 *value,
60                                                      IBusError             **error);
61 static gboolean ibus_config_service_unset       (IBusConfigService      *config,
62                                                  const gchar            *section,
63                                                  const gchar            *name,
64                                                  IBusError             **error);
65
66 static IBusServiceClass  *parent_class = NULL;
67
68 GType
69 ibus_config_service_get_type (void)
70 {
71     static GType type = 0;
72
73     static const GTypeInfo type_info = {
74         sizeof (IBusConfigServiceClass),
75         (GBaseInitFunc)     NULL,
76         (GBaseFinalizeFunc) NULL,
77         (GClassInitFunc)    ibus_config_service_class_init,
78         NULL,               /* class finalize */
79         NULL,               /* class data */
80         sizeof (IBusConfigService),
81         0,
82         (GInstanceInitFunc) ibus_config_service_init,
83     };
84
85     if (type == 0) {
86         type = g_type_register_static (IBUS_TYPE_SERVICE,
87                                        "IBusConfigService",
88                                        &type_info,
89                                        (GTypeFlags) 0);
90     }
91     return type;
92 }
93
94 IBusConfigService *
95 ibus_config_service_new (IBusConnection *connection)
96 {
97     g_assert (IBUS_IS_CONNECTION (connection));
98
99     IBusConfigService *config;
100
101     config = (IBusConfigService *) g_object_new (IBUS_TYPE_CONFIG_SERVICE,
102                                                  "path", IBUS_PATH_CONFIG,
103                                                  "connection", connection,
104                                                  NULL);
105
106     return config;
107 }
108
109 static void
110 ibus_config_service_class_init (IBusConfigServiceClass *klass)
111 {
112     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113
114     parent_class = (IBusServiceClass *) g_type_class_peek_parent (klass);
115
116     gobject_class->set_property = (GObjectSetPropertyFunc) ibus_config_service_set_property;
117     gobject_class->get_property = (GObjectGetPropertyFunc) ibus_config_service_get_property;
118
119     IBUS_OBJECT_CLASS (gobject_class)->destroy = (IBusObjectDestroyFunc) ibus_config_service_destroy;
120
121     IBUS_SERVICE_CLASS (klass)->ibus_message = (ServiceIBusMessageFunc) ibus_config_service_ibus_message;
122
123     klass->set_value = ibus_config_service_set_value;
124     klass->get_value = ibus_config_service_get_value;
125     klass->unset = ibus_config_service_unset;
126
127     /* install properties */
128     /**
129      * IBusConfigService:connection:
130      *
131      * Connection of this IBusConfigService.
132      */
133     g_object_class_install_property (gobject_class,
134                     PROP_CONNECTION,
135                     g_param_spec_object ("connection",
136                         "connection",
137                         "The connection of config object",
138                         IBUS_TYPE_CONNECTION,
139                         G_PARAM_READWRITE |  G_PARAM_CONSTRUCT_ONLY));
140 }
141
142 static void
143 ibus_config_service_init (IBusConfigService *config)
144 {
145 }
146
147 static void
148 ibus_config_service_set_property (IBusConfigService *config,
149                                   guint              prop_id,
150                                   const GValue      *value,
151                                   GParamSpec        *pspec)
152 {
153     switch (prop_id) {
154     case PROP_CONNECTION:
155         ibus_service_add_to_connection ((IBusService *) config,
156                                         g_value_get_object (value));
157         break;
158
159     default:
160         G_OBJECT_WARN_INVALID_PROPERTY_ID (config, prop_id, pspec);
161     }
162 }
163
164 static void
165 ibus_config_service_get_property (IBusConfigService *config,
166                                   guint              prop_id,
167                                   GValue            *value,
168                                   GParamSpec        *pspec)
169 {
170     switch (prop_id) {
171     case PROP_CONNECTION:
172         {
173             GList *connections = ibus_service_get_connections ((IBusService *) config);
174             if (connections) {
175                 g_value_set_object (value, connections->data);
176             }
177             else {
178                 g_value_set_object (value, NULL);
179             }
180             g_list_foreach (connections, (GFunc) g_object_unref, NULL);
181             g_list_free (connections);
182         }
183         break;
184     default:
185         G_OBJECT_WARN_INVALID_PROPERTY_ID (config, prop_id, pspec);
186     }
187 }
188
189 static void
190 ibus_config_service_destroy (IBusConfigService *config)
191 {
192     IBUS_OBJECT_CLASS(parent_class)->destroy ((IBusObject *) config);
193 }
194
195 static gboolean
196 ibus_config_service_ibus_message (IBusConfigService     *config,
197                                   IBusConnection        *connection,
198                                   IBusMessage           *message)
199 {
200     g_assert (IBUS_IS_CONFIG_SERVICE (config));
201     g_assert (IBUS_IS_CONNECTION (connection));
202     g_assert (message != NULL);
203
204     IBusMessage *reply = NULL;
205
206     if (ibus_message_is_method_call (message, IBUS_INTERFACE_CONFIG, "SetValue")) {
207         gchar *section;
208         gchar *name;
209         GValue value = { 0 };
210         IBusError *error = NULL;
211         gboolean retval;
212
213         retval = ibus_message_get_args (message,
214                                         &error,
215                                         G_TYPE_STRING, &section,
216                                         G_TYPE_STRING, &name,
217                                         G_TYPE_VALUE, &value,
218                                         G_TYPE_INVALID);
219         if (!retval) {
220             reply = ibus_message_new_error_printf (message,
221                                                    DBUS_ERROR_INVALID_ARGS,
222                                                    "Can not parse arguments 1 of SetValue");
223             ibus_error_free (error);
224         }
225         else if (!IBUS_CONFIG_SERVICE_GET_CLASS (config)->set_value (config, section, name, &value, &error)) {
226             reply = ibus_message_new_error (message,
227                                             error->name,
228                                             error->message);
229             ibus_error_free (error);
230         }
231         else {
232             reply = ibus_message_new_method_return (message);
233         }
234     }
235     else if (ibus_message_is_method_call (message, IBUS_INTERFACE_CONFIG, "GetValue")) {
236         gchar *section;
237         gchar *name;
238         GValue value = { 0 };
239         IBusError *error = NULL;
240         gboolean retval;
241
242         retval = ibus_message_get_args (message,
243                                         &error,
244                                         G_TYPE_STRING, &section,
245                                         G_TYPE_STRING, &name,
246                                         G_TYPE_INVALID);
247
248         if (!retval) {
249             reply = ibus_message_new_error (message,
250                                             error->name,
251                                             error->message);
252             ibus_error_free (error);
253         }
254         else if (!IBUS_CONFIG_SERVICE_GET_CLASS (config)->get_value (config, section, name, &value, &error)) {
255             reply = ibus_message_new_error (message,
256                                             error->name,
257                                             error->message);
258             ibus_error_free (error);
259         }
260         else {
261             reply = ibus_message_new_method_return (message);
262             ibus_message_append_args (reply,
263                                       G_TYPE_VALUE, &value,
264                                       G_TYPE_INVALID);
265             g_value_unset (&value);
266         }
267     }
268     else if (ibus_message_is_method_call (message, IBUS_INTERFACE_CONFIG, "Unset")) {
269         gchar *section;
270         gchar *name;
271         IBusError *error = NULL;
272         gboolean retval;
273
274         retval = ibus_message_get_args (message,
275                                         &error,
276                                         G_TYPE_STRING, &section,
277                                         G_TYPE_STRING, &name,
278                                         G_TYPE_INVALID);
279         if (!retval) {
280             reply = ibus_message_new_error_printf (message,
281                                                    DBUS_ERROR_INVALID_ARGS,
282                                                    "Can not parse arguments 1 of Unset");
283             ibus_error_free (error);
284         }
285         else if (!IBUS_CONFIG_SERVICE_GET_CLASS (config)->unset (config, section, name, &error)) {
286             reply = ibus_message_new_error (message,
287                                             error->name,
288                                             error->message);
289             ibus_error_free (error);
290         }
291         else {
292             reply = ibus_message_new_method_return (message);
293         }
294     }
295
296     if (reply) {
297         ibus_connection_send (connection, reply);
298         ibus_message_unref (reply);
299         return TRUE;
300     }
301
302     return parent_class->ibus_message ((IBusService *) config, connection, message);
303 }
304
305 static gboolean
306 ibus_config_service_set_value (IBusConfigService *config,
307                                const gchar       *section,
308                                const gchar       *name,
309                                const GValue      *value,
310                                IBusError        **error)
311 {
312     if (error) {
313         *error = ibus_error_new_from_printf (DBUS_ERROR_FAILED,
314                                              "Can not set value [%s, %s]",
315                                              section, name);
316     }
317     return FALSE;
318 }
319
320 static gboolean
321 ibus_config_service_get_value (IBusConfigService *config,
322                                const gchar       *section,
323                                const gchar       *name,
324                                GValue            *value,
325                                IBusError        **error)
326 {
327     if (error) {
328         *error = ibus_error_new_from_printf (DBUS_ERROR_FAILED,
329                                              "Can not get value [%s, %s]",
330                                              section, name);
331     }
332     return FALSE;
333 }
334
335 static gboolean
336 ibus_config_service_unset (IBusConfigService *config,
337                            const gchar       *section,
338                            const gchar       *name,
339                            IBusError        **error)
340 {
341     if (error) {
342         *error = ibus_error_new_from_printf (DBUS_ERROR_FAILED,
343                                              "Can not unset [%s, %s]",
344                                              section, name);
345     }
346     return FALSE;
347 }
348
349 void
350 ibus_config_service_value_changed (IBusConfigService  *config,
351                                    const gchar        *section,
352                                    const gchar        *name,
353                                    const GValue       *value)
354 {
355     g_assert (IBUS_IS_CONFIG_SERVICE (config));
356     g_assert (section);
357     g_assert (name);
358     g_assert (G_IS_VALUE (value));
359
360     ibus_service_send_signal ((IBusService *) config,
361                               IBUS_INTERFACE_CONFIG,
362                               "ValueChanged",
363                               G_TYPE_STRING, &section,
364                               G_TYPE_STRING, &name,
365                               G_TYPE_VALUE, value,
366                               G_TYPE_INVALID);
367 }