1 /* vim:set et sts=4: */
2 /* ibus - The Input Bus
3 * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
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.
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.
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.
21 #include "ibusfactory.h"
22 #include "ibusengine.h"
23 #include "ibusshare.h"
24 #include "ibusinternal.h"
26 #define IBUS_FACTORY_GET_PRIVATE(o) \
27 (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_FACTORY, IBusFactoryPrivate))
34 struct _IBusFactoryPrivate {
36 IBusConnection *connection;
38 GHashTable *engine_table;
40 typedef struct _IBusFactoryPrivate IBusFactoryPrivate;
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);
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);
63 static IBusServiceClass *factory_parent_class = NULL;
64 static IBusSerializableClass *factory_info_parent_class = NULL;
67 ibus_factory_get_type (void)
69 static GType type = 0;
71 static const GTypeInfo type_info = {
72 sizeof (IBusFactoryClass),
74 (GBaseFinalizeFunc) NULL,
75 (GClassInitFunc) ibus_factory_class_init,
76 NULL, /* class finalize */
77 NULL, /* class data */
80 (GInstanceInitFunc) ibus_factory_init,
84 type = g_type_register_static (IBUS_TYPE_SERVICE,
93 ibus_factory_new (IBusConnection *connection)
95 g_assert (IBUS_IS_CONNECTION (connection));
98 IBusFactoryPrivate *priv;
100 factory = (IBusFactory *) g_object_new (IBUS_TYPE_FACTORY,
101 "path", IBUS_PATH_FACTORY,
103 priv = IBUS_FACTORY_GET_PRIVATE (factory);
105 priv->connection = g_object_ref (connection);
106 ibus_service_add_to_connection ((IBusService *)factory, connection);
112 ibus_factory_class_init (IBusFactoryClass *klass)
114 // GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);
117 factory_parent_class = (IBusServiceClass *) g_type_class_peek_parent (klass);
119 g_type_class_add_private (klass, sizeof (IBusFactoryPrivate));
121 ibus_object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_destroy;
123 IBUS_SERVICE_CLASS (klass)->ibus_message = (ServiceIBusMessageFunc) ibus_factory_ibus_message;
128 ibus_factory_init (IBusFactory *factory)
130 IBusFactoryPrivate *priv;
131 priv = IBUS_FACTORY_GET_PRIVATE (factory);
134 priv->connection = NULL;
135 priv->engine_table = g_hash_table_new_full (g_str_hash,
139 priv->engine_list = NULL;
143 ibus_factory_destroy (IBusFactory *factory)
146 IBusFactoryPrivate *priv;
147 priv = IBUS_FACTORY_GET_PRIVATE (factory);
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);
153 priv->engine_list = NULL;
155 if (priv->engine_table) {
156 g_hash_table_destroy (priv->engine_table);
159 if (priv->connection) {
160 ibus_service_remove_from_connection ((IBusService *)factory,
162 g_object_unref (priv->connection);
165 IBUS_OBJECT_CLASS(factory_parent_class)->destroy (IBUS_OBJECT (factory));
169 _engine_destroy_cb (IBusEngine *engine,
170 IBusFactory *factory)
173 IBusFactoryPrivate *priv;
174 priv = IBUS_FACTORY_GET_PRIVATE (factory);
176 list = g_list_remove (priv->engine_list, engine);
179 g_object_unref (engine);
180 g_list_free_1 (list);
185 ibus_factory_ibus_message (IBusFactory *factory,
186 IBusConnection *connection,
187 IBusMessage *message)
189 g_assert (IBUS_IS_FACTORY (factory));
190 g_assert (IBUS_IS_CONNECTION (connection));
191 g_assert (message != NULL);
193 IBusMessage *reply_message;
194 IBusFactoryPrivate *priv;
195 priv = IBUS_FACTORY_GET_PRIVATE (factory);
197 g_assert (priv->connection == connection);
199 if (ibus_message_is_method_call (message,
200 IBUS_INTERFACE_FACTORY,
209 retval = ibus_message_get_args (message,
211 G_TYPE_STRING, &engine_name,
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);
223 engine_type = (GType )g_hash_table_lookup (priv->engine_table, engine_name);
225 if (engine_type == G_TYPE_INVALID) {
226 reply_message = ibus_message_new_error_printf (message,
228 "Can not create engine %s", engine_name);
229 ibus_connection_send (connection, reply_message);
230 ibus_message_unref (reply_message);
235 path = g_strdup_printf ("/org/freedesktop/IBus/Engine/%d", ++priv->id);
237 engine = g_object_new (engine_type,
240 "connection", priv->connection,
243 priv->engine_list = g_list_append (priv->engine_list, engine);
244 g_signal_connect (engine,
246 G_CALLBACK (_engine_destroy_cb),
249 reply_message = ibus_message_new_method_return (message);
250 ibus_message_append_args (reply_message,
251 IBUS_TYPE_OBJECT_PATH, &path,
254 ibus_connection_send (connection, reply_message);
255 ibus_message_unref (reply_message);
259 return factory_parent_class->ibus_message ((IBusService *)factory,
265 ibus_factory_add_engine (IBusFactory *factory,
266 const gchar *engine_name,
269 g_assert (IBUS_IS_FACTORY (factory));
270 g_assert (engine_name);
271 g_assert (g_type_is_a (engine_type, IBUS_TYPE_ENGINE));
273 IBusFactoryPrivate *priv;
274 priv = IBUS_FACTORY_GET_PRIVATE (factory);
276 g_hash_table_insert (priv->engine_table, g_strdup (engine_name), (gpointer) engine_type);
281 ibus_factory_get_info (IBusFactory *factory)
283 IBusFactoryPrivate *priv;
284 priv = IBUS_FACTORY_GET_PRIVATE (factory);
291 ibus_factory_info_get_type (void)
293 static GType type = 0;
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),
304 (GInstanceInitFunc) ibus_factory_info_init,
308 type = g_type_register_static (IBUS_TYPE_SERIALIZABLE,
318 ibus_factory_info_class_init (IBusFactoryInfoClass *klass)
320 IBusObjectClass *object_class = IBUS_OBJECT_CLASS (klass);
321 IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (klass);
323 factory_info_parent_class = (IBusSerializableClass *) g_type_class_peek_parent (klass);
325 object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_info_destroy;
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;
331 g_string_append (serializable_class->signature, "osssss");
335 ibus_factory_info_init (IBusFactoryInfo *info)
341 info->authors = NULL;
342 info->credits = NULL;
346 ibus_factory_info_destroy (IBusFactoryInfo *info)
352 g_free (info->authors);
353 g_free (info->credits);
359 info->authors = NULL;
360 info->credits = NULL;
362 IBUS_OBJECT_CLASS (factory_info_parent_class)->destroy ((IBusObject *)info);
366 ibus_factory_info_serialize (IBusFactoryInfo *info,
367 IBusMessageIter *iter)
371 retval = factory_info_parent_class->serialize ((IBusSerializable *)info, iter);
372 g_return_val_if_fail (retval, FALSE);
374 retval = ibus_message_iter_append (iter, IBUS_TYPE_OBJECT_PATH, &info->path);
375 g_return_val_if_fail (retval, FALSE);
377 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->name);
378 g_return_val_if_fail (retval, FALSE);
380 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->lang);
381 g_return_val_if_fail (retval, FALSE);
383 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->icon);
384 g_return_val_if_fail (retval, FALSE);
386 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->authors);
387 g_return_val_if_fail (retval, FALSE);
389 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->credits);
390 g_return_val_if_fail (retval, FALSE);
396 ibus_factory_info_deserialize (IBusFactoryInfo *info,
397 IBusMessageIter *iter)
401 retval = factory_info_parent_class->deserialize ((IBusSerializable *)info, iter);
402 g_return_val_if_fail (retval, FALSE);
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);
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);
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);
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);
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);
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);
432 ibus_factory_info_copy (IBusFactoryInfo *dest,
433 const IBusFactoryInfo *src)
437 retval = factory_info_parent_class->copy ((IBusSerializable *)dest,
438 (IBusSerializable *)src);
439 g_return_val_if_fail (retval, FALSE);
441 g_return_val_if_fail (IBUS_IS_FACTORY_INFO (dest), FALSE);
442 g_return_val_if_fail (IBUS_IS_FACTORY_INFO (src), FALSE);
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);
455 ibus_factory_info_new (const gchar *path,
459 const gchar *authors,
460 const gchar *credits)
469 IBusFactoryInfo *info;
471 info = (IBusFactoryInfo *)g_object_new (IBUS_TYPE_FACTORY_INFO, 0);
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);