Merge branch 'master' into c_impl
[platform/upstream/ibus.git] / src / ibusfactory.c
1 /* vim:set et sts=4: */
2 /* ibus - The Input Bus
3  * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "ibusfactory.h"
22 #include "ibusengine.h"
23 #include "ibusshare.h"
24 #include "ibusinternal.h"
25
26 #define IBUS_FACTORY_GET_PRIVATE(o)  \
27    (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_FACTORY, IBusFactoryPrivate))
28
29 enum {
30     LAST_SIGNAL,
31 };
32
33 /* IBusFactoryPriv */
34 struct _IBusFactoryPrivate {
35     guint id;
36     IBusConnection *connection;
37     GList          *engine_list;
38     GHashTable     *engine_table;
39 };
40 typedef struct _IBusFactoryPrivate IBusFactoryPrivate;
41
42 /* functions prototype */
43 static void     ibus_factory_class_init     (IBusFactoryClass   *klass);
44 static void     ibus_factory_init           (IBusFactory        *factory);
45 static void     ibus_factory_destroy        (IBusFactory        *factory);
46 static gboolean ibus_factory_ibus_message   (IBusFactory        *factory,
47                                              IBusConnection     *connection,
48                                              IBusMessage        *message);
49
50 static void     _engine_destroy_cb          (IBusEngine         *engine,
51                                              IBusFactory        *factory);
52 static void     ibus_factory_info_class_init(IBusFactoryInfoClass   *klass);
53 static void     ibus_factory_info_init      (IBusFactoryInfo        *info);
54 static void     ibus_factory_info_destroy   (IBusFactoryInfo        *info);
55 static gboolean ibus_factory_info_serialize (IBusFactoryInfo        *info,
56                                              IBusMessageIter        *iter);
57 static gboolean ibus_factory_info_deserialize
58                                             (IBusFactoryInfo        *info,
59                                              IBusMessageIter        *iter);
60 static gboolean ibus_factory_info_copy      (IBusFactoryInfo        *dest,
61                                              const IBusFactoryInfo  *src);
62
63 static IBusServiceClass *factory_parent_class = NULL;
64 static IBusSerializableClass *factory_info_parent_class = NULL;
65
66 GType
67 ibus_factory_get_type (void)
68 {
69     static GType type = 0;
70
71     static const GTypeInfo type_info = {
72         sizeof (IBusFactoryClass),
73         (GBaseInitFunc)     NULL,
74         (GBaseFinalizeFunc) NULL,
75         (GClassInitFunc)    ibus_factory_class_init,
76         NULL,               /* class finalize */
77         NULL,               /* class data */
78         sizeof (IBusFactory),
79         0,
80         (GInstanceInitFunc) ibus_factory_init,
81     };
82
83     if (type == 0) {
84         type = g_type_register_static (IBUS_TYPE_SERVICE,
85                     "IBusFactory",
86                     &type_info,
87                     (GTypeFlags) 0);
88     }
89     return type;
90 }
91
92 IBusFactory *
93 ibus_factory_new (IBusConnection *connection)
94 {
95     g_assert (IBUS_IS_CONNECTION (connection));
96
97     IBusFactory *factory;
98     IBusFactoryPrivate *priv;
99
100     factory = (IBusFactory *) g_object_new (IBUS_TYPE_FACTORY,
101                                             "path", IBUS_PATH_FACTORY,
102                                             0);
103     priv = IBUS_FACTORY_GET_PRIVATE (factory);
104
105     priv->connection = g_object_ref (connection);
106     ibus_service_add_to_connection ((IBusService *)factory, connection);
107
108     return factory;
109 }
110
111 static void
112 ibus_factory_class_init (IBusFactoryClass *klass)
113 {
114     // GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115     IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);
116
117     factory_parent_class = (IBusServiceClass *) g_type_class_peek_parent (klass);
118
119     g_type_class_add_private (klass, sizeof (IBusFactoryPrivate));
120
121     ibus_object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_destroy;
122
123     IBUS_SERVICE_CLASS (klass)->ibus_message = (ServiceIBusMessageFunc) ibus_factory_ibus_message;
124
125 }
126
127 static void
128 ibus_factory_init (IBusFactory *factory)
129 {
130     IBusFactoryPrivate *priv;
131     priv = IBUS_FACTORY_GET_PRIVATE (factory);
132
133     priv->id = 0;
134     priv->connection = NULL;
135     priv->engine_table = g_hash_table_new_full (g_str_hash,
136                                                 g_str_equal,
137                                                 g_free,
138                                                 NULL);
139     priv->engine_list =  NULL;
140 }
141
142 static void
143 ibus_factory_destroy (IBusFactory *factory)
144 {
145     GList *list;
146     IBusFactoryPrivate *priv;
147     priv = IBUS_FACTORY_GET_PRIVATE (factory);
148
149     list = g_list_copy (priv->engine_list);
150     g_list_foreach (list, (GFunc) ibus_object_destroy, NULL);
151     g_list_free (priv->engine_list);
152     g_list_free (list);
153     priv->engine_list = NULL;
154
155     if (priv->engine_table) {
156         g_hash_table_destroy (priv->engine_table);
157     }
158
159     if (priv->connection) {
160         ibus_service_remove_from_connection ((IBusService *)factory,
161                                              priv->connection);
162         g_object_unref (priv->connection);
163     }
164
165     IBUS_OBJECT_CLASS(factory_parent_class)->destroy (IBUS_OBJECT (factory));
166 }
167
168 static void
169 _engine_destroy_cb (IBusEngine  *engine,
170                     IBusFactory *factory)
171 {
172     GList *list;
173     IBusFactoryPrivate *priv;
174     priv = IBUS_FACTORY_GET_PRIVATE (factory);
175
176     list = g_list_remove (priv->engine_list, engine);
177
178     if (list) {
179         g_object_unref (engine);
180         g_list_free_1 (list);
181     }
182 }
183
184 static gboolean
185 ibus_factory_ibus_message (IBusFactory    *factory,
186                            IBusConnection *connection,
187                            IBusMessage    *message)
188 {
189     g_assert (IBUS_IS_FACTORY (factory));
190     g_assert (IBUS_IS_CONNECTION (connection));
191     g_assert (message != NULL);
192
193     IBusMessage *reply_message;
194     IBusFactoryPrivate *priv;
195     priv = IBUS_FACTORY_GET_PRIVATE (factory);
196
197     g_assert (priv->connection == connection);
198
199     if (ibus_message_is_method_call (message,
200                                      IBUS_INTERFACE_FACTORY,
201                                      "CreateEngine")) {
202         gchar *engine_name;
203         gchar *path;
204         IBusError *error;
205         IBusEngine *engine;
206         gboolean retval;
207         GType engine_type;
208
209         retval = ibus_message_get_args (message,
210                                         &error,
211                                         G_TYPE_STRING, &engine_name,
212                                         G_TYPE_INVALID);
213
214         if (!retval) {
215             reply_message = ibus_message_new_error_printf (message,
216                                         DBUS_ERROR_INVALID_ARGS,
217                                         "The 1st arg should be engine name");
218             ibus_connection_send (connection, reply_message);
219             ibus_message_unref (reply_message);
220             return TRUE;
221         }
222
223         engine_type = (GType )g_hash_table_lookup (priv->engine_table, engine_name);
224
225         if (engine_type == G_TYPE_INVALID) {
226              reply_message = ibus_message_new_error_printf (message,
227                                         DBUS_ERROR_FAILED,
228                                         "Can not create engine %s", engine_name);
229             ibus_connection_send (connection, reply_message);
230             ibus_message_unref (reply_message);
231             return TRUE;
232
233         }
234
235         path = g_strdup_printf ("/org/freedesktop/IBus/Engine/%d", ++priv->id);
236
237         engine = g_object_new (engine_type,
238                                "name", engine_name,
239                                "path", path,
240                                "connection", priv->connection,
241                                0);
242
243         priv->engine_list = g_list_append (priv->engine_list, engine);
244         g_signal_connect (engine,
245                           "destroy",
246                           G_CALLBACK (_engine_destroy_cb),
247                           factory);
248
249         reply_message = ibus_message_new_method_return (message);
250         ibus_message_append_args (reply_message,
251                                   IBUS_TYPE_OBJECT_PATH, &path,
252                                   G_TYPE_INVALID);
253         g_free (path);
254         ibus_connection_send (connection, reply_message);
255         ibus_message_unref (reply_message);
256         return TRUE;
257     }
258
259     return factory_parent_class->ibus_message ((IBusService *)factory,
260                                                connection,
261                                                message);
262 }
263
264 void
265 ibus_factory_add_engine (IBusFactory *factory,
266                          const gchar *engine_name,
267                          GType        engine_type)
268 {
269     g_assert (IBUS_IS_FACTORY (factory));
270     g_assert (engine_name);
271     g_assert (g_type_is_a (engine_type, IBUS_TYPE_ENGINE));
272
273     IBusFactoryPrivate *priv;
274     priv = IBUS_FACTORY_GET_PRIVATE (factory);
275
276     g_hash_table_insert (priv->engine_table, g_strdup (engine_name), (gpointer) engine_type);
277 }
278
279 #if 0
280 IBusFactoryInfo *
281 ibus_factory_get_info (IBusFactory *factory)
282 {
283     IBusFactoryPrivate *priv;
284     priv = IBUS_FACTORY_GET_PRIVATE (factory);
285
286     return priv->info;
287 }
288 #endif
289
290 GType
291 ibus_factory_info_get_type (void)
292 {
293     static GType type = 0;
294
295     static const GTypeInfo type_info = {
296         sizeof (IBusFactoryInfoClass),
297         (GBaseInitFunc)     NULL,
298         (GBaseFinalizeFunc) NULL,
299         (GClassInitFunc)    ibus_factory_info_class_init,
300         NULL,               /* class finialize */
301         NULL,               /* class data */
302         sizeof (IBusFactoryInfo),
303         0,
304         (GInstanceInitFunc) ibus_factory_info_init,
305     };
306
307     if (type == 0) {
308         type = g_type_register_static (IBUS_TYPE_SERIALIZABLE,
309                                        "IBusFactoryInfo",
310                                        &type_info,
311                                        0);
312     }
313
314     return type;
315 }
316
317 static void
318 ibus_factory_info_class_init (IBusFactoryInfoClass *klass)
319 {
320     IBusObjectClass *object_class = IBUS_OBJECT_CLASS (klass);
321     IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (klass);
322
323     factory_info_parent_class = (IBusSerializableClass *) g_type_class_peek_parent (klass);
324
325     object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_info_destroy;
326
327     serializable_class->serialize   = (IBusSerializableSerializeFunc) ibus_factory_info_serialize;
328     serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_factory_info_deserialize;
329     serializable_class->copy        = (IBusSerializableCopyFunc) ibus_factory_info_copy;
330
331     g_string_append (serializable_class->signature, "osssss");
332 }
333
334 static void
335 ibus_factory_info_init (IBusFactoryInfo *info)
336 {
337     info->path = NULL;
338     info->name = NULL;
339     info->lang = NULL;
340     info->icon = NULL;
341     info->authors = NULL;
342     info->credits = NULL;
343 }
344
345 static void
346 ibus_factory_info_destroy (IBusFactoryInfo *info)
347 {
348     g_free (info->path);
349     g_free (info->name);
350     g_free (info->lang);
351     g_free (info->icon);
352     g_free (info->authors);
353     g_free (info->credits);
354
355     info->path = NULL;
356     info->lang = NULL;
357     info->name = NULL;
358     info->icon = NULL;
359     info->authors = NULL;
360     info->credits = NULL;
361
362     IBUS_OBJECT_CLASS (factory_info_parent_class)->destroy ((IBusObject *)info);
363 }
364
365 static gboolean
366 ibus_factory_info_serialize (IBusFactoryInfo *info,
367                              IBusMessageIter *iter)
368 {
369     gboolean retval;
370
371     retval = factory_info_parent_class->serialize ((IBusSerializable *)info, iter);
372     g_return_val_if_fail (retval, FALSE);
373
374     retval = ibus_message_iter_append (iter, IBUS_TYPE_OBJECT_PATH, &info->path);
375     g_return_val_if_fail (retval, FALSE);
376
377     retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->name);
378     g_return_val_if_fail (retval, FALSE);
379
380     retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->lang);
381     g_return_val_if_fail (retval, FALSE);
382
383     retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->icon);
384     g_return_val_if_fail (retval, FALSE);
385
386     retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->authors);
387     g_return_val_if_fail (retval, FALSE);
388
389     retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->credits);
390     g_return_val_if_fail (retval, FALSE);
391
392     return TRUE;
393 }
394
395 static gboolean
396 ibus_factory_info_deserialize (IBusFactoryInfo *info,
397                                IBusMessageIter *iter)
398 {
399     gboolean retval;
400
401     retval = factory_info_parent_class->deserialize ((IBusSerializable *)info, iter);
402     g_return_val_if_fail (retval, FALSE);
403
404     retval = ibus_message_iter_get (iter, IBUS_TYPE_OBJECT_PATH, &info->path);
405     g_return_val_if_fail (retval, FALSE);
406     info->path = g_strdup (info->path);
407
408     retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->name);
409     g_return_val_if_fail (retval, FALSE);
410     info->name = g_strdup (info->name);
411
412     retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->lang);
413     g_return_val_if_fail (retval, FALSE);
414     info->lang = g_strdup (info->lang);
415
416     retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->icon);
417     g_return_val_if_fail (retval, FALSE);
418     info->icon = g_strdup (info->icon);
419
420     retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->authors);
421     g_return_val_if_fail (retval, FALSE);
422     info->authors = g_strdup (info->authors);
423
424     retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->credits);
425     g_return_val_if_fail (retval, FALSE);
426     info->credits = g_strdup (info->credits);
427
428     return TRUE;
429 }
430
431 static gboolean
432 ibus_factory_info_copy (IBusFactoryInfo       *dest,
433                         const IBusFactoryInfo *src)
434 {
435     gboolean retval;
436
437     retval = factory_info_parent_class->copy ((IBusSerializable *)dest,
438                                               (IBusSerializable *)src);
439     g_return_val_if_fail (retval, FALSE);
440
441     g_return_val_if_fail (IBUS_IS_FACTORY_INFO (dest), FALSE);
442     g_return_val_if_fail (IBUS_IS_FACTORY_INFO (src), FALSE);
443
444     dest->path = g_strdup (src->path);
445     dest->name = g_strdup (src->name);
446     dest->lang = g_strdup (src->lang);
447     dest->icon = g_strdup (src->icon);
448     dest->authors = g_strdup (src->authors);
449     dest->credits = g_strdup (src->credits);
450
451     return TRUE;
452 }
453
454 IBusFactoryInfo *
455 ibus_factory_info_new (const gchar *path,
456                        const gchar *name,
457                        const gchar *lang,
458                        const gchar *icon,
459                        const gchar *authors,
460                        const gchar *credits)
461 {
462     g_assert (path);
463     g_assert (name);
464     g_assert (lang);
465     g_assert (icon);
466     g_assert (authors);
467     g_assert (credits);
468
469     IBusFactoryInfo *info;
470
471     info = (IBusFactoryInfo *)g_object_new (IBUS_TYPE_FACTORY_INFO, 0);
472
473     info->path = g_strdup (path);
474     info->name = g_strdup (name);
475     info->lang = g_strdup (lang);
476     info->icon = g_strdup (icon);
477     info->authors = g_strdup (authors);
478     info->credits = g_strdup (credits);
479
480     return info;
481 }
482
483