Create simple engine
[platform/upstream/ibus.git] / src / ibusfactory.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 "ibusfactory.h"
23 #include "ibusengine.h"
24 #include "ibusmarshalers.h"
25 #include "ibusshare.h"
26 #include "ibusinternal.h"
27
28 #define IBUS_FACTORY_GET_PRIVATE(o)  \
29    (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_FACTORY, IBusFactoryPrivate))
30
31 enum {
32     CREATE_ENGINE,
33     LAST_SIGNAL,
34 };
35
36 enum {
37     PROP_0,
38 };
39
40 /* IBusFactoryPriv */
41 struct _IBusFactoryPrivate {
42     guint id;
43     GList          *engine_list;
44     GHashTable     *engine_table;
45 };
46
47 static guint            factory_signals[LAST_SIGNAL] = { 0 };
48
49 /* functions prototype */
50 static void      ibus_factory_destroy        (IBusFactory        *factory);
51 static void      ibus_factory_set_property   (IBusFactory        *engine,
52                                               guint               prop_id,
53                                               const GValue       *value,
54                                               GParamSpec         *pspec);
55 static void      ibus_factory_get_property   (IBusFactory        *factory,
56                                               guint               prop_id,
57                                               GValue             *value,
58                                               GParamSpec         *pspec);
59 static void      ibus_factory_service_method_call
60                                               (IBusService        *service,
61                                                GDBusConnection    *connection,
62                                                const gchar        *sender,
63                                                const gchar        *object_path,
64                                                const gchar        *interface_name,
65                                                const gchar        *method_name,
66                                                GVariant           *parameters,
67                                                GDBusMethodInvocation
68                                                                   *invocation);
69 static GVariant *ibus_factory_service_get_property
70                                              (IBusService        *service,
71                                               GDBusConnection    *connection,
72                                               const gchar        *sender,
73                                               const gchar        *object_path,
74                                               const gchar        *interface_name,
75                                               const gchar        *property_name,
76                                               GError            **error);
77 static gboolean  ibus_factory_service_set_property
78                                              (IBusService        *service,
79                                               GDBusConnection    *connection,
80                                               const gchar        *sender,
81                                               const gchar        *object_path,
82                                               const gchar        *interface_name,
83                                               const gchar        *property_name,
84                                               GVariant           *value,
85                                               GError            **error);
86 static void      ibus_factory_engine_destroy_cb
87                                              (IBusEngine         *engine,
88                                               IBusFactory        *factory);
89
90 G_DEFINE_TYPE (IBusFactory, ibus_factory, IBUS_TYPE_SERVICE)
91
92 static const gchar introspection_xml[] =
93     "<node>"
94     "  <interface name='org.freedesktop.IBus.Factory'>"
95     "    <method name='CreateEngine'>"
96     "      <arg direction='in'  type='s' name='name' />"
97     "      <arg direction='out' type='o' />"
98     "    </method>"
99     "  </interface>"
100     "</node>";
101
102 static IBusEngine *
103 ibus_factory_real_create_engine (IBusFactory    *factory,
104                                  const gchar    *engine_name)
105 {
106     GType engine_type;
107     gchar *object_path = NULL;
108     IBusEngine *engine = NULL;
109
110     engine_type = (GType) g_hash_table_lookup (factory->priv->engine_table,
111                                                engine_name);
112
113     g_return_val_if_fail (engine_type != G_TYPE_INVALID, NULL);
114
115     object_path = g_strdup_printf ("/org/freedesktop/IBus/Engine/%d",
116                                    ++factory->priv->id);
117     engine = ibus_engine_new_with_type (engine_type,
118                                         engine_name,
119                                         object_path,
120                                         ibus_service_get_connection ((IBusService *)factory));
121     g_free (object_path);
122
123     return engine;
124 }
125
126 static gboolean
127 _ibus_factory_create_engine_accumulator (GSignalInvocationHint *ihint,
128                                          GValue                *return_accu,
129                                          const GValue          *handler_return,
130                                          gpointer               dummy)
131 {
132     gboolean retval = TRUE;
133     GObject *object = g_value_get_object (handler_return);
134
135     if (object != NULL) {
136         g_value_copy (handler_return, return_accu);
137         retval = FALSE;
138     }
139
140     return retval;
141 }
142
143 static void
144 ibus_factory_class_init (IBusFactoryClass *class)
145 {
146     GObjectClass *gobject_class = G_OBJECT_CLASS (class);
147     IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class);
148
149     gobject_class->set_property = (GObjectSetPropertyFunc) ibus_factory_set_property;
150     gobject_class->get_property = (GObjectGetPropertyFunc) ibus_factory_get_property;
151
152     ibus_object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_destroy;
153
154     IBUS_SERVICE_CLASS (class)->service_method_call  = ibus_factory_service_method_call;
155     IBUS_SERVICE_CLASS (class)->service_get_property = ibus_factory_service_get_property;
156     IBUS_SERVICE_CLASS (class)->service_set_property = ibus_factory_service_set_property;
157     class->create_engine = ibus_factory_real_create_engine;
158
159     ibus_service_class_add_interfaces (IBUS_SERVICE_CLASS (class), introspection_xml);
160
161     g_type_class_add_private (class, sizeof (IBusFactoryPrivate));
162
163     /**
164      * IBusFactory::create-engine:
165      * @factory: the factory which received the signal
166      * @engine_name: the engine_name which received the signal
167      * @returns: (transfer full): An IBusEngine
168      *
169      * The ::create-engine signal is a signal to create IBusEngine
170      * with @engine_name, which gets emitted when IBusFactory
171      * received CreateEngine dbus method. The callback functions
172      * will be called until a callback returns a non-null object
173      * of IBusEngine. */
174     factory_signals[CREATE_ENGINE] =
175         g_signal_new (I_("create-engine"),
176             G_TYPE_FROM_CLASS (gobject_class),
177             G_SIGNAL_RUN_LAST,
178             G_STRUCT_OFFSET (IBusFactoryClass, create_engine),
179             _ibus_factory_create_engine_accumulator,
180             NULL,
181             _ibus_marshal_OBJECT__STRING,
182             IBUS_TYPE_ENGINE,
183             1,
184             G_TYPE_STRING);
185 }
186
187 static void
188 ibus_factory_init (IBusFactory *factory)
189 {
190     factory->priv = IBUS_FACTORY_GET_PRIVATE (factory);
191     factory->priv->engine_table =
192         g_hash_table_new_full (g_str_hash,
193                                g_str_equal,
194                                g_free,
195                                NULL);
196 }
197
198 static void
199 ibus_factory_destroy (IBusFactory *factory)
200 {
201     GList *list;
202
203     list = g_list_copy (factory->priv->engine_list);
204     g_list_free_full (list, (GDestroyNotify)ibus_object_destroy);
205     g_list_free(factory->priv->engine_list);
206     factory->priv->engine_list = NULL;
207
208     if (factory->priv->engine_table) {
209         g_hash_table_destroy (factory->priv->engine_table);
210     }
211
212     IBUS_OBJECT_CLASS(ibus_factory_parent_class)->destroy (IBUS_OBJECT (factory));
213 }
214
215 static void
216 ibus_factory_set_property (IBusFactory  *factory,
217                            guint         prop_id,
218                            const GValue *value,
219                            GParamSpec   *pspec)
220 {
221     switch (prop_id) {
222     default:
223         G_OBJECT_WARN_INVALID_PROPERTY_ID (factory, prop_id, pspec);
224     }
225 }
226
227 static void
228 ibus_factory_get_property (IBusFactory *factory,
229                            guint        prop_id,
230                            GValue      *value,
231                            GParamSpec  *pspec)
232 {
233     switch (prop_id) {
234     default:
235         G_OBJECT_WARN_INVALID_PROPERTY_ID (factory, prop_id, pspec);
236     }
237 }
238
239 static void
240 ibus_factory_engine_destroy_cb (IBusEngine  *engine,
241                                 IBusFactory *factory)
242 {
243     factory->priv->engine_list = g_list_remove (factory->priv->engine_list, engine);
244     g_object_unref (engine);
245 }
246
247 static void
248 ibus_factory_service_method_call (IBusService           *service,
249                                   GDBusConnection       *connection,
250                                   const gchar           *sender,
251                                   const gchar           *object_path,
252                                   const gchar           *interface_name,
253                                   const gchar           *method_name,
254                                   GVariant              *parameters,
255                                   GDBusMethodInvocation *invocation)
256 {
257     IBusFactory *factory = IBUS_FACTORY (service);
258
259     if (g_strcmp0 (method_name, "CreateEngine") == 0) {
260         gchar *engine_name = NULL;
261         IBusEngine *engine = NULL;
262
263         g_variant_get (parameters, "(&s)", &engine_name);
264         g_signal_emit (factory, factory_signals[CREATE_ENGINE],
265                        0, engine_name, &engine);
266
267         if (engine != NULL) {
268             gchar *object_path = NULL;
269             GValue value = { 0, };
270
271             g_value_init (&value, G_TYPE_STRING);
272             g_object_get_property (G_OBJECT (engine), "object-path", &value);
273             object_path = g_value_dup_string (&value);
274             g_value_unset (&value);
275
276             g_assert (engine != NULL);
277             g_assert (object_path != NULL);
278             g_object_ref_sink (engine);
279             factory->priv->engine_list = g_list_append (factory->priv->engine_list, engine);
280             g_signal_connect (engine,
281                               "destroy",
282                               G_CALLBACK (ibus_factory_engine_destroy_cb),
283                               factory);
284             g_dbus_method_invocation_return_value (invocation,
285                                                    g_variant_new ("(o)", object_path));
286             g_free (object_path);
287         }
288         else {
289             gchar *error_message = g_strdup_printf ("Can not fond engine %s", engine_name);
290             g_dbus_method_invocation_return_error (invocation,
291                                                    G_DBUS_ERROR,
292                                                    G_DBUS_ERROR_FAILED,
293                                                    error_message);
294             g_free (error_message);
295         }
296         return;
297     }
298
299     IBUS_SERVICE_CLASS (ibus_factory_parent_class)->
300             service_method_call (service,
301                                  connection,
302                                  sender,
303                                  object_path,
304                                  interface_name,
305                                  method_name,
306                                  parameters,
307                                  invocation);
308 }
309
310 static GVariant *
311 ibus_factory_service_get_property (IBusService        *service,
312                                    GDBusConnection    *connection,
313                                    const gchar        *sender,
314                                    const gchar        *object_path,
315                                    const gchar        *interface_name,
316                                    const gchar        *property_name,
317                                    GError            **error)
318 {
319     return IBUS_SERVICE_CLASS (ibus_factory_parent_class)->
320                 service_get_property (service,
321                                       connection,
322                                       sender,
323                                       object_path,
324                                       interface_name,
325                                       property_name,
326                                       error);
327 }
328
329 static gboolean
330 ibus_factory_service_set_property (IBusService        *service,
331                                    GDBusConnection    *connection,
332                                    const gchar        *sender,
333                                    const gchar        *object_path,
334                                    const gchar        *interface_name,
335                                    const gchar        *property_name,
336                                    GVariant           *value,
337                                    GError            **error)
338 {
339     return IBUS_SERVICE_CLASS (ibus_factory_parent_class)->
340                 service_set_property (service,
341                                       connection,
342                                       sender,
343                                       object_path,
344                                       interface_name,
345                                       property_name,
346                                       value,
347                                       error);
348 }
349
350 IBusFactory *
351 ibus_factory_new (GDBusConnection *connection)
352 {
353     g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
354
355     IBusEngine *object = g_object_new (IBUS_TYPE_FACTORY,
356                                        "object-path", IBUS_PATH_FACTORY,
357                                        "connection", connection,
358                                        NULL);
359
360     return IBUS_FACTORY (object);
361 }
362
363 void
364 ibus_factory_add_engine (IBusFactory *factory,
365                          const gchar *engine_name,
366                          GType        engine_type)
367 {
368     g_return_if_fail (IBUS_IS_FACTORY (factory));
369     g_return_if_fail (engine_name != NULL);
370     g_return_if_fail (g_type_is_a (engine_type, IBUS_TYPE_ENGINE));
371
372     g_hash_table_insert (factory->priv->engine_table, g_strdup (engine_name), (gpointer) engine_type);
373 }
374
375 IBusEngine *
376 ibus_factory_create_engine (IBusFactory    *factory,
377                             const gchar    *engine_name)
378 {
379     IBusEngine *engine = NULL;
380
381     g_assert (engine_name != NULL);
382
383     g_signal_emit (factory, factory_signals[CREATE_ENGINE],
384                    0, engine_name, &engine);
385
386     return engine;
387 }