1
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2
/* vim:set et sts=4: */
4
* Copyright (C) 2010 Peng Huang <shawn.p.huang@gmail.com>
5
* Copyright (C) 2010 Google Inc.
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.
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.
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.
22
#include "component.h"
23
#include <glib/gstdio.h>
29
#include "marshalers.h"
41
struct _BusComponent {
44
/* instance members */
46
/* an object which represents one XML file in the ibus/component/ directory. */
47
IBusComponent *component;
48
/* a proxy object which starts an engine. */
49
BusFactoryProxy *factory;
51
/* TRUE if the component started in the verbose mode. */
53
/* TRUE if the component needs to be restarted when it dies. */
55
/* TRUE if the component will be destroyed with factory. */
56
gboolean destroy_with_factory;
58
/* process id of the process (e.g. ibus-config, ibus-engine-*, ..) of the component. */
60
guint child_source_id;
63
struct _BusComponentClass {
64
IBusObjectClass parent;
68
/* functions prototype */
69
static GObject* bus_component_constructor (GType type,
70
guint n_construct_params,
71
GObjectConstructParam *construct_params);
72
static void bus_component_set_property (BusComponent *component,
76
static void bus_component_get_property (BusComponent *component,
80
static void bus_component_destroy (BusComponent *component);
82
G_DEFINE_TYPE (BusComponent, bus_component, IBUS_TYPE_OBJECT)
85
bus_component_class_init (BusComponentClass *class)
87
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
88
IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class);
90
gobject_class->constructor = bus_component_constructor;
91
gobject_class->set_property = (GObjectSetPropertyFunc) bus_component_set_property;
92
gobject_class->get_property = (GObjectGetPropertyFunc) bus_component_get_property;
93
ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_component_destroy;
95
/* install properties */
96
g_object_class_install_property (gobject_class,
98
g_param_spec_object ("component", /* canonical name of the property */
99
"component", /* nick name */
100
"component", /* description */
101
IBUS_TYPE_COMPONENT, /* object type */
102
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
104
g_object_class_install_property (gobject_class,
106
g_param_spec_object ("factory",
109
BUS_TYPE_FACTORY_PROXY,
114
bus_component_init (BusComponent *component)
119
* bus_component_constructor:
121
* A constructor method which is called after bus_component_init is called.
124
bus_component_constructor (GType type,
125
guint n_construct_params,
126
GObjectConstructParam *construct_params)
129
object = G_OBJECT_CLASS (bus_component_parent_class)->constructor (type,
132
BusComponent *component = (BusComponent *) object;
133
/* we have to override the _constructor method since in _init method, the component->component property is not set yet. */
134
g_assert (IBUS_IS_COMPONENT (component->component));
136
static GQuark quark = 0;
138
quark = g_quark_from_static_string ("BusComponent");
141
/* associate each engine with BusComponent. a component might have one or more components. For example, ibus-engine-pinyin would
142
* have two - 'pinyin' and 'bopomofo' and ibus-engine-m17n has many. On the other hand, the gtkpanel component does not have an
143
* engine, of course. */
144
GList *engines = ibus_component_get_engines (component->component);
146
for (p = engines; p != NULL; p = p->next) {
147
g_object_set_qdata ((GObject *) p->data, quark, component);
149
g_list_free (engines);
155
bus_component_set_property (BusComponent *component,
162
g_assert (component->component == NULL);
163
component->component = g_value_dup_object (value);
166
bus_component_set_factory (component, (BusFactoryProxy *) g_value_get_object (value));
169
G_OBJECT_WARN_INVALID_PROPERTY_ID (component, prop_id, pspec);
174
bus_component_get_property (BusComponent *component,
181
g_value_set_object (value, bus_component_get_component (component));
184
g_value_set_object (value, bus_component_get_factory (component));
187
G_OBJECT_WARN_INVALID_PROPERTY_ID (component, prop_id, pspec);
192
bus_component_destroy (BusComponent *component)
194
if (component->pid != 0) {
195
bus_component_stop (component);
196
g_spawn_close_pid (component->pid);
200
if (component->child_source_id != 0) {
201
g_source_remove (component->child_source_id);
202
component->child_source_id = 0;
205
if (component->component != NULL) {
206
g_object_unref (component->component);
207
component->component = NULL;
210
IBUS_OBJECT_CLASS (bus_component_parent_class)->destroy (IBUS_OBJECT (component));
214
bus_component_new (IBusComponent *component,
215
BusFactoryProxy *factory)
217
g_assert (IBUS_IS_COMPONENT (component));
219
return (BusComponent *) g_object_new (BUS_TYPE_COMPONENT,
220
/* properties below will be set via the bus_component_set_property function. */
221
"component", component,
227
bus_component_factory_destroy_cb (BusFactoryProxy *factory,
228
BusComponent *component)
230
g_return_if_fail (component->factory == factory);
232
g_object_unref (component->factory);
233
component->factory = NULL;
234
/* emit the "notify" signal for the factory property on component. */
235
g_object_notify ((GObject *) component, "factory");
237
if (component->destroy_with_factory)
238
ibus_object_destroy ((IBusObject *) component);
242
bus_component_get_component (BusComponent *component)
244
g_assert (BUS_IS_COMPONENT (component));
245
return component->component;
249
bus_component_set_factory (BusComponent *component,
250
BusFactoryProxy *factory)
252
g_assert (BUS_IS_COMPONENT (component));
254
if (component->factory == factory) {
258
if (component->factory) {
259
g_signal_handlers_disconnect_by_func (component->factory,
260
bus_component_factory_destroy_cb,
262
g_object_unref (component->factory);
263
component->factory = NULL;
267
g_assert (BUS_IS_FACTORY_PROXY (factory));
268
component->factory = (BusFactoryProxy *) g_object_ref (factory);
269
g_signal_connect (factory, "destroy",
270
G_CALLBACK (bus_component_factory_destroy_cb), component);
273
/* emit the "notify" signal for the factory property on component. */
274
g_object_notify ((GObject*) component, "factory");
278
bus_component_get_factory (BusComponent *component)
280
g_assert (BUS_IS_COMPONENT (component));
281
return component->factory;
285
bus_component_get_name (BusComponent *component)
287
g_assert (BUS_IS_COMPONENT (component));
289
return ibus_component_get_name (component->component);
293
bus_component_get_engines (BusComponent *component)
295
g_assert (BUS_IS_COMPONENT (component));
297
return ibus_component_get_engines (component->component);
301
bus_component_set_destroy_with_factory (BusComponent *component,
302
gboolean with_factory)
304
g_assert (BUS_IS_COMPONENT (component));
306
component->destroy_with_factory = with_factory;
310
bus_component_set_restart (BusComponent *component,
313
g_assert (BUS_IS_COMPONENT (component));
314
component->restart = restart;
318
* bus_component_child_cb:
320
* A callback function to be called when the child process is terminated.
323
bus_component_child_cb (GPid pid,
325
BusComponent *component)
327
g_assert (BUS_IS_COMPONENT (component));
328
g_assert (component->pid == pid);
329
g_spawn_close_pid (pid);
331
component->child_source_id = 0;
333
if (component->restart) {
334
bus_component_start (component, component->verbose);
339
bus_component_start (BusComponent *component,
342
g_assert (BUS_IS_COMPONENT (component));
344
if (component->pid != 0)
347
component->verbose = verbose;
353
GError *error = NULL;
354
if (!g_shell_parse_argv (ibus_component_get_exec (component->component),
358
g_warning ("Can not parse component %s exec: %s",
359
ibus_component_get_name (component->component),
361
g_error_free (error);
366
GSpawnFlags flags = G_SPAWN_DO_NOT_REAP_CHILD;
368
flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL;
370
retval = g_spawn_async (NULL, argv, NULL,
373
&(component->pid), &error);
376
g_warning ("Can not execute component %s: %s",
377
ibus_component_get_name (component->component),
379
g_error_free (error);
383
component->child_source_id =
384
g_child_watch_add (component->pid,
385
(GChildWatchFunc) bus_component_child_cb,
392
bus_component_stop (BusComponent *component)
394
g_assert (BUS_IS_COMPONENT (component));
396
if (component->pid == 0)
399
kill (component->pid, SIGTERM);
404
bus_component_is_running (BusComponent *component)
406
g_assert (BUS_IS_COMPONENT (component));
408
return (component->pid != 0);
412
bus_component_from_engine_desc (IBusEngineDesc *engine)
414
g_assert (IBUS_IS_ENGINE_DESC (engine));
416
static GQuark quark = 0;
418
quark = g_quark_from_static_string ("BusComponent");
421
return (BusComponent *) g_object_get_qdata ((GObject *) engine, quark);