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.
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.
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.
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.
22 #include "ibusfactory.h"
23 #include "ibusengine.h"
24 #include "ibusshare.h"
25 #include "ibusinternal.h"
27 #define IBUS_FACTORY_GET_PRIVATE(o) \
28 (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_FACTORY, IBusFactoryPrivate))
35 struct _IBusFactoryPrivate {
37 IBusConnection *connection;
39 GHashTable *engine_table;
41 typedef struct _IBusFactoryPrivate IBusFactoryPrivate;
43 /* functions prototype */
44 static void ibus_factory_class_init (IBusFactoryClass *klass);
45 static void ibus_factory_init (IBusFactory *factory);
46 static void ibus_factory_destroy (IBusFactory *factory);
47 static gboolean ibus_factory_ibus_message (IBusFactory *factory,
48 IBusConnection *connection,
49 IBusMessage *message);
51 static void _engine_destroy_cb (IBusEngine *engine,
52 IBusFactory *factory);
53 static void ibus_factory_info_class_init(IBusFactoryInfoClass *klass);
54 static void ibus_factory_info_init (IBusFactoryInfo *info);
55 static void ibus_factory_info_destroy (IBusFactoryInfo *info);
56 static gboolean ibus_factory_info_serialize (IBusFactoryInfo *info,
57 IBusMessageIter *iter);
58 static gboolean ibus_factory_info_deserialize
59 (IBusFactoryInfo *info,
60 IBusMessageIter *iter);
61 static gboolean ibus_factory_info_copy (IBusFactoryInfo *dest,
62 const IBusFactoryInfo *src);
64 static IBusServiceClass *factory_parent_class = NULL;
65 static IBusSerializableClass *factory_info_parent_class = NULL;
68 ibus_factory_get_type (void)
70 static GType type = 0;
72 static const GTypeInfo type_info = {
73 sizeof (IBusFactoryClass),
75 (GBaseFinalizeFunc) NULL,
76 (GClassInitFunc) ibus_factory_class_init,
77 NULL, /* class finalize */
78 NULL, /* class data */
81 (GInstanceInitFunc) ibus_factory_init,
85 type = g_type_register_static (IBUS_TYPE_SERVICE,
94 ibus_factory_new (IBusConnection *connection)
96 g_assert (IBUS_IS_CONNECTION (connection));
99 IBusFactoryPrivate *priv;
101 factory = (IBusFactory *) g_object_new (IBUS_TYPE_FACTORY,
102 "path", IBUS_PATH_FACTORY,
104 priv = IBUS_FACTORY_GET_PRIVATE (factory);
106 priv->connection = g_object_ref (connection);
107 ibus_service_add_to_connection ((IBusService *)factory, connection);
113 ibus_factory_class_init (IBusFactoryClass *klass)
115 // GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);
118 factory_parent_class = (IBusServiceClass *) g_type_class_peek_parent (klass);
120 g_type_class_add_private (klass, sizeof (IBusFactoryPrivate));
122 ibus_object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_destroy;
124 IBUS_SERVICE_CLASS (klass)->ibus_message = (ServiceIBusMessageFunc) ibus_factory_ibus_message;
129 ibus_factory_init (IBusFactory *factory)
131 IBusFactoryPrivate *priv;
132 priv = IBUS_FACTORY_GET_PRIVATE (factory);
135 priv->connection = NULL;
136 priv->engine_table = g_hash_table_new_full (g_str_hash,
140 priv->engine_list = NULL;
144 ibus_factory_destroy (IBusFactory *factory)
147 IBusFactoryPrivate *priv;
148 priv = IBUS_FACTORY_GET_PRIVATE (factory);
150 list = g_list_copy (priv->engine_list);
151 g_list_foreach (list, (GFunc) ibus_object_destroy, NULL);
152 g_list_free (priv->engine_list);
154 priv->engine_list = NULL;
156 if (priv->engine_table) {
157 g_hash_table_destroy (priv->engine_table);
160 if (priv->connection) {
161 ibus_service_remove_from_connection ((IBusService *)factory,
163 g_object_unref (priv->connection);
166 IBUS_OBJECT_CLASS(factory_parent_class)->destroy (IBUS_OBJECT (factory));
170 _engine_destroy_cb (IBusEngine *engine,
171 IBusFactory *factory)
173 IBusFactoryPrivate *priv;
174 priv = IBUS_FACTORY_GET_PRIVATE (factory);
176 priv->engine_list = g_list_remove (priv->engine_list, engine);
177 g_object_unref (engine);
181 ibus_factory_ibus_message (IBusFactory *factory,
182 IBusConnection *connection,
183 IBusMessage *message)
185 g_assert (IBUS_IS_FACTORY (factory));
186 g_assert (IBUS_IS_CONNECTION (connection));
187 g_assert (message != NULL);
189 IBusMessage *reply_message;
190 IBusFactoryPrivate *priv;
191 priv = IBUS_FACTORY_GET_PRIVATE (factory);
193 g_assert (priv->connection == connection);
195 if (ibus_message_is_method_call (message,
196 IBUS_INTERFACE_FACTORY,
205 retval = ibus_message_get_args (message,
207 G_TYPE_STRING, &engine_name,
211 reply_message = ibus_message_new_error_printf (message,
212 DBUS_ERROR_INVALID_ARGS,
213 "The 1st arg should be engine name");
214 ibus_connection_send (connection, reply_message);
215 ibus_message_unref (reply_message);
219 engine_type = (GType )g_hash_table_lookup (priv->engine_table, engine_name);
221 if (engine_type == G_TYPE_INVALID) {
222 reply_message = ibus_message_new_error_printf (message,
224 "Can not create engine %s", engine_name);
225 ibus_connection_send (connection, reply_message);
226 ibus_message_unref (reply_message);
231 path = g_strdup_printf ("/org/freedesktop/IBus/Engine/%d", ++priv->id);
233 engine = g_object_new (engine_type,
236 "connection", priv->connection,
239 priv->engine_list = g_list_append (priv->engine_list, engine);
240 g_signal_connect (engine,
242 G_CALLBACK (_engine_destroy_cb),
245 reply_message = ibus_message_new_method_return (message);
246 ibus_message_append_args (reply_message,
247 IBUS_TYPE_OBJECT_PATH, &path,
250 ibus_connection_send (connection, reply_message);
251 ibus_message_unref (reply_message);
255 return factory_parent_class->ibus_message ((IBusService *)factory,
261 ibus_factory_add_engine (IBusFactory *factory,
262 const gchar *engine_name,
265 g_assert (IBUS_IS_FACTORY (factory));
266 g_assert (engine_name);
267 g_assert (g_type_is_a (engine_type, IBUS_TYPE_ENGINE));
269 IBusFactoryPrivate *priv;
270 priv = IBUS_FACTORY_GET_PRIVATE (factory);
272 g_hash_table_insert (priv->engine_table, g_strdup (engine_name), (gpointer) engine_type);
277 ibus_factory_get_info (IBusFactory *factory)
279 IBusFactoryPrivate *priv;
280 priv = IBUS_FACTORY_GET_PRIVATE (factory);
287 ibus_factory_info_get_type (void)
289 static GType type = 0;
291 static const GTypeInfo type_info = {
292 sizeof (IBusFactoryInfoClass),
293 (GBaseInitFunc) NULL,
294 (GBaseFinalizeFunc) NULL,
295 (GClassInitFunc) ibus_factory_info_class_init,
296 NULL, /* class finialize */
297 NULL, /* class data */
298 sizeof (IBusFactoryInfo),
300 (GInstanceInitFunc) ibus_factory_info_init,
304 type = g_type_register_static (IBUS_TYPE_SERIALIZABLE,
314 ibus_factory_info_class_init (IBusFactoryInfoClass *klass)
316 IBusObjectClass *object_class = IBUS_OBJECT_CLASS (klass);
317 IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (klass);
319 factory_info_parent_class = (IBusSerializableClass *) g_type_class_peek_parent (klass);
321 object_class->destroy = (IBusObjectDestroyFunc) ibus_factory_info_destroy;
323 serializable_class->serialize = (IBusSerializableSerializeFunc) ibus_factory_info_serialize;
324 serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_factory_info_deserialize;
325 serializable_class->copy = (IBusSerializableCopyFunc) ibus_factory_info_copy;
327 g_string_append (serializable_class->signature, "osssss");
331 ibus_factory_info_init (IBusFactoryInfo *info)
337 info->authors = NULL;
338 info->credits = NULL;
342 ibus_factory_info_destroy (IBusFactoryInfo *info)
348 g_free (info->authors);
349 g_free (info->credits);
355 info->authors = NULL;
356 info->credits = NULL;
358 IBUS_OBJECT_CLASS (factory_info_parent_class)->destroy ((IBusObject *)info);
362 ibus_factory_info_serialize (IBusFactoryInfo *info,
363 IBusMessageIter *iter)
367 retval = factory_info_parent_class->serialize ((IBusSerializable *)info, iter);
368 g_return_val_if_fail (retval, FALSE);
370 retval = ibus_message_iter_append (iter, IBUS_TYPE_OBJECT_PATH, &info->path);
371 g_return_val_if_fail (retval, FALSE);
373 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->name);
374 g_return_val_if_fail (retval, FALSE);
376 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->lang);
377 g_return_val_if_fail (retval, FALSE);
379 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->icon);
380 g_return_val_if_fail (retval, FALSE);
382 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->authors);
383 g_return_val_if_fail (retval, FALSE);
385 retval = ibus_message_iter_append (iter, G_TYPE_STRING, &info->credits);
386 g_return_val_if_fail (retval, FALSE);
392 ibus_factory_info_deserialize (IBusFactoryInfo *info,
393 IBusMessageIter *iter)
397 retval = factory_info_parent_class->deserialize ((IBusSerializable *)info, iter);
398 g_return_val_if_fail (retval, FALSE);
400 retval = ibus_message_iter_get (iter, IBUS_TYPE_OBJECT_PATH, &info->path);
401 g_return_val_if_fail (retval, FALSE);
402 ibus_message_iter_next (iter);
403 info->path = g_strdup (info->path);
405 retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->name);
406 g_return_val_if_fail (retval, FALSE);
407 ibus_message_iter_next (iter);
408 info->name = g_strdup (info->name);
410 retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->lang);
411 g_return_val_if_fail (retval, FALSE);
412 ibus_message_iter_next (iter);
413 info->lang = g_strdup (info->lang);
415 retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->icon);
416 g_return_val_if_fail (retval, FALSE);
417 ibus_message_iter_next (iter);
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 ibus_message_iter_next (iter);
423 info->authors = g_strdup (info->authors);
425 retval = ibus_message_iter_get (iter, G_TYPE_STRING, &info->credits);
426 g_return_val_if_fail (retval, FALSE);
427 ibus_message_iter_next (iter);
428 info->credits = g_strdup (info->credits);
434 ibus_factory_info_copy (IBusFactoryInfo *dest,
435 const IBusFactoryInfo *src)
439 retval = factory_info_parent_class->copy ((IBusSerializable *)dest,
440 (IBusSerializable *)src);
441 g_return_val_if_fail (retval, FALSE);
443 g_return_val_if_fail (IBUS_IS_FACTORY_INFO (dest), FALSE);
444 g_return_val_if_fail (IBUS_IS_FACTORY_INFO (src), FALSE);
446 dest->path = g_strdup (src->path);
447 dest->name = g_strdup (src->name);
448 dest->lang = g_strdup (src->lang);
449 dest->icon = g_strdup (src->icon);
450 dest->authors = g_strdup (src->authors);
451 dest->credits = g_strdup (src->credits);
457 ibus_factory_info_new (const gchar *path,
461 const gchar *authors,
462 const gchar *credits)
471 IBusFactoryInfo *info;
473 info = (IBusFactoryInfo *) g_object_new (IBUS_TYPE_FACTORY_INFO, NULL);
475 info->path = g_strdup (path);
476 info->name = g_strdup (name);
477 info->lang = g_strdup (lang);
478 info->icon = g_strdup (icon);
479 info->authors = g_strdup (authors);
480 info->credits = g_strdup (credits);