summaryrefslogtreecommitdiff
path: root/src/ibuscomponent.c
blob: 2364b24acd257cb6d8bd4fd4495d8df57a77bb4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* bus - The Input Bus
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
#include <glib/gstdio.h>
#include "ibuscomponent.h"
#include "ibusinternal.h"

enum {
    LAST_SIGNAL,
};

enum {
    PROP_0 = 0,
    PROP_NAME,
    PROP_DESCRIPTION,
    PROP_VERSION,
    PROP_LICENSE,
    PROP_AUTHOR,
    PROP_HOMEPAGE,
    PROP_COMMAND_LINE,
    PROP_TEXTDOMAIN,
};

/* IBusComponentPriv */
struct _IBusComponentPrivate {
    gchar *name;
    gchar *description;
    gchar *version;
    gchar *license;
    gchar *author;
    gchar *homepage;
    gchar *exec;
    gchar *textdomain;

    /* engines */
    GList *engines;

    /* observed paths */
    GList *observed_paths;
};

#define IBUS_COMPONENT_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_COMPONENT, IBusComponentPrivate))

// static guint            _signals[LAST_SIGNAL] = { 0 };

/* functions prototype */
static void         ibus_component_set_property (IBusComponent          *component,
                                                 guint                   prop_id,
                                                 const GValue           *value,
                                                 GParamSpec             *pspec);
static void         ibus_component_get_property (IBusComponent          *component,
                                                 guint                   prop_id,
                                                 GValue                 *value,
                                                 GParamSpec             *pspec);
static void         ibus_component_destroy      (IBusComponent          *component);
static gboolean     ibus_component_serialize    (IBusComponent          *component,
                                                 GVariantBuilder        *builder);
static gint         ibus_component_deserialize  (IBusComponent          *component,
                                                 GVariant               *variant);
static gboolean     ibus_component_copy         (IBusComponent          *dest,
                                                 const IBusComponent    *src);
static gboolean     ibus_component_parse_xml_node
                                                (IBusComponent          *component,
                                                 XMLNode                *node,
                                                 gboolean                access_fs);

static void         ibus_component_parse_engines(IBusComponent          *component,
                                                 XMLNode                *node);
static void         ibus_component_parse_observed_paths
                                                (IBusComponent          *component,
                                                 XMLNode                *node,
                                                 gboolean                access_fs);

G_DEFINE_TYPE (IBusComponent, ibus_component, IBUS_TYPE_SERIALIZABLE)

static void
ibus_component_class_init (IBusComponentClass *class)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (class);
    IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
    IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);

    g_type_class_add_private (class, sizeof (IBusComponentPrivate));

    gobject_class->set_property = (GObjectSetPropertyFunc) ibus_component_set_property;
    gobject_class->get_property = (GObjectGetPropertyFunc) ibus_component_get_property;
    object_class->destroy = (IBusObjectDestroyFunc) ibus_component_destroy;

    serializable_class->serialize   = (IBusSerializableSerializeFunc) ibus_component_serialize;
    serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_component_deserialize;
    serializable_class->copy        = (IBusSerializableCopyFunc) ibus_component_copy;

    /* install properties */
    /**
     * IBusComponent:name:
     *
     * The name of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_NAME,
                    g_param_spec_string ("name",
                        "component name",
                        "The name of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:description:
     *
     * The description of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_DESCRIPTION,
                    g_param_spec_string ("description",
                        "component description",
                        "The description of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:version:
     *
     * The version of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_VERSION,
                    g_param_spec_string ("version",
                        "component version",
                        "The version of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:license:
     *
     * The license of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_LICENSE,
                    g_param_spec_string ("license",
                        "component license",
                        "The license of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:author:
     *
     * The author of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_AUTHOR,
                    g_param_spec_string ("author",
                        "component author",
                        "The author of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:homepage:
     *
     * The homepage of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_HOMEPAGE,
                    g_param_spec_string ("homepage",
                        "component homepage",
                        "The homepage of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:command-line:
     *
     * The exec path of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_COMMAND_LINE,
                    g_param_spec_string ("command-line",
                        "component command-line",
                        "The command line of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusComponent:textdomain:
     *
     * The textdomain of component
     */
    g_object_class_install_property (gobject_class,
                    PROP_TEXTDOMAIN,
                    g_param_spec_string ("textdomain",
                        "component textdomain",
                        "The textdomain path of component",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}


static void
ibus_component_init (IBusComponent *component)
{
    component->priv = IBUS_COMPONENT_GET_PRIVATE (component);
}

static void
ibus_component_destroy (IBusComponent *component)
{
    GList *p;

    g_free (component->priv->name);
    g_free (component->priv->description);
    g_free (component->priv->version);
    g_free (component->priv->license);
    g_free (component->priv->author);
    g_free (component->priv->homepage);
    g_free (component->priv->exec);
    g_free (component->priv->textdomain);

    component->priv->name = NULL;
    component->priv->description = NULL;
    component->priv->version = NULL;
    component->priv->license = NULL;
    component->priv->author = NULL;
    component->priv->homepage = NULL;
    component->priv->exec = NULL;
    component->priv->textdomain = NULL;

    g_list_free_full (component->priv->observed_paths, g_object_unref);
    component->priv->observed_paths = NULL;

    for (p = component->priv->engines; p != NULL; p = p->next) {
        g_object_steal_data ((GObject *)p->data, "component");
        ibus_object_destroy ((IBusObject *)p->data);
        g_object_unref (p->data);
    }
    g_list_free (component->priv->engines);
    component->priv->engines = NULL;

    IBUS_OBJECT_CLASS (ibus_component_parent_class)->destroy (IBUS_OBJECT (component));
}

static void
ibus_component_set_property (IBusComponent *component,
                             guint          prop_id,
                             const GValue  *value,
                             GParamSpec    *pspec)
{
    switch (prop_id) {
    case PROP_NAME:
        g_assert (component->priv->name == NULL);
        component->priv->name = g_value_dup_string (value);
        break;
    case PROP_DESCRIPTION:
        g_assert (component->priv->description == NULL);
        component->priv->description = g_value_dup_string (value);
        break;
    case PROP_VERSION:
        g_assert (component->priv->version == NULL);
        component->priv->version = g_value_dup_string (value);
        break;
    case PROP_LICENSE:
        g_assert (component->priv->license == NULL);
        component->priv->license = g_value_dup_string (value);
        break;
    case PROP_AUTHOR:
        g_assert (component->priv->author == NULL);
        component->priv->author = g_value_dup_string (value);
        break;
    case PROP_HOMEPAGE:
        g_assert (component->priv->homepage == NULL);
        component->priv->homepage = g_value_dup_string (value);
        break;
    case PROP_COMMAND_LINE:
        g_assert (component->priv->exec == NULL);
        component->priv->exec = g_value_dup_string (value);
        break;
    case PROP_TEXTDOMAIN:
        g_assert (component->priv->textdomain == NULL);
        component->priv->textdomain = g_value_dup_string (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (component, prop_id, pspec);
    }
}

static void
ibus_component_get_property (IBusComponent *component,
                             guint          prop_id,
                             GValue        *value,
                             GParamSpec    *pspec)
{
    switch (prop_id) {
    case PROP_NAME:
        g_value_set_string (value, ibus_component_get_name (component));
        break;
    case PROP_DESCRIPTION:
        g_value_set_string (value, ibus_component_get_description (component));
        break;
    case PROP_VERSION:
        g_value_set_string (value, ibus_component_get_version (component));
        break;
    case PROP_LICENSE:
        g_value_set_string (value, ibus_component_get_license (component));
        break;
    case PROP_AUTHOR:
        g_value_set_string (value, ibus_component_get_author (component));
        break;
    case PROP_HOMEPAGE:
        g_value_set_string (value, ibus_component_get_homepage (component));
        break;
    case PROP_COMMAND_LINE:
        g_value_set_string (value, ibus_component_get_exec (component));
        break;
    case PROP_TEXTDOMAIN:
        g_value_set_string (value, ibus_component_get_textdomain (component));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (component, prop_id, pspec);
    }
}

static gboolean
ibus_component_serialize (IBusComponent   *component,
                          GVariantBuilder *builder)
{
    gboolean retval;

    retval = IBUS_SERIALIZABLE_CLASS (ibus_component_parent_class)->serialize ((IBusSerializable *)component, builder);
    g_return_val_if_fail (retval, FALSE);

    g_variant_builder_add (builder, "s", component->priv->name);
    g_variant_builder_add (builder, "s", component->priv->description);
    g_variant_builder_add (builder, "s", component->priv->version);
    g_variant_builder_add (builder, "s", component->priv->license);
    g_variant_builder_add (builder, "s", component->priv->author);
    g_variant_builder_add (builder, "s", component->priv->homepage);
    g_variant_builder_add (builder, "s", component->priv->exec);
    g_variant_builder_add (builder, "s", component->priv->textdomain);

    GList *p;
    GVariantBuilder *array;
    /* serialize observed paths */
    array = g_variant_builder_new (G_VARIANT_TYPE ("av"));
    for (p = component->priv->observed_paths; p != NULL; p = p->next) {
        g_variant_builder_add (array, "v", ibus_serializable_serialize ((IBusSerializable *)p->data));
    }
    g_variant_builder_add (builder, "av", array);
    g_variant_builder_unref (array);

    /* serialize engine desc list */
    array = g_variant_builder_new (G_VARIANT_TYPE ("av"));
    for (p = component->priv->engines; p != NULL; p = p->next) {
        g_variant_builder_add (array, "v", ibus_serializable_serialize ((IBusSerializable *)p->data));
    }
    g_variant_builder_add (builder, "av", array);
    g_variant_builder_unref (array);

    return TRUE;
}

static gint
ibus_component_deserialize (IBusComponent   *component,
                            GVariant        *variant)
{
    gboolean retval;

    retval = IBUS_SERIALIZABLE_CLASS (ibus_component_parent_class)->deserialize ((IBusSerializable *)component, variant);
    g_return_val_if_fail (retval, 0);

    ibus_g_variant_get_child_string (variant, retval++, &component->priv->name);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->description);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->version);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->license);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->author);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->homepage);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->exec);
    ibus_g_variant_get_child_string (variant, retval++, &component->priv->textdomain);

    GVariant *var;
    GVariantIter *iter = NULL;
    g_variant_get_child (variant, retval++, "av", &iter);
    while (g_variant_iter_loop (iter, "v", &var)) {
        component->priv->observed_paths = g_list_append (component->priv->observed_paths,
                        IBUS_OBSERVED_PATH (ibus_serializable_deserialize (var)));
    }
    g_variant_iter_free (iter);

    g_variant_get_child (variant, retval++, "av", &iter);
    while (g_variant_iter_loop (iter, "v", &var)) {
        ibus_component_add_engine (component,
                                   IBUS_ENGINE_DESC (ibus_serializable_deserialize (var)));
    }
    g_variant_iter_free (iter);

    return retval;
}

static gboolean
ibus_component_copy (IBusComponent       *dest,
                     const IBusComponent *src)
{
    gboolean retval;

    retval = IBUS_SERIALIZABLE_CLASS (ibus_component_parent_class)->copy ((IBusSerializable *)dest,
                                 (IBusSerializable *)src);
    g_return_val_if_fail (retval, FALSE);

    dest->priv->name          = g_strdup (src->priv->name);
    dest->priv->description   = g_strdup (src->priv->description);
    dest->priv->version       = g_strdup (src->priv->version);
    dest->priv->license       = g_strdup (src->priv->license);
    dest->priv->author        = g_strdup (src->priv->author);
    dest->priv->homepage      = g_strdup (src->priv->homepage);
    dest->priv->exec          = g_strdup (src->priv->exec);
    dest->priv->textdomain    = g_strdup (src->priv->textdomain);

    dest->priv->observed_paths = g_list_copy (src->priv->observed_paths);
    g_list_foreach (dest->priv->observed_paths, (GFunc) g_object_ref, NULL);

    dest->priv->engines = g_list_copy (src->priv->engines);
    g_list_foreach (dest->priv->engines, (GFunc) g_object_ref, NULL);

    return TRUE;
}


#define g_string_append_indent(string, indent)  \
    {                                           \
        gint i;                                 \
        for (i = 0; i < (indent); i++) {        \
            g_string_append (string, "    ");   \
        }                                       \
    }

void
ibus_component_output (IBusComponent *component,
                      GString      *output,
                      gint          indent)
{
    g_assert (IBUS_IS_COMPONENT (component));
    GList *p;

    g_string_append_indent (output, indent);
    g_string_append (output, "<component>\n");

#define OUTPUT_ENTRY(field, element)                                        \
    {                                                                       \
        gchar *escape_text =                                                \
            g_markup_escape_text (component->priv->field ?                  \
                                  component->priv->field : "", -1);         \
        g_string_append_indent (output, indent + 1);                        \
        g_string_append_printf (output, "<"element">%s</"element">\n",      \
                                escape_text);                               \
        g_free (escape_text);                                               \
    }
#define OUTPUT_ENTRY_1(name) OUTPUT_ENTRY(name, #name)
    OUTPUT_ENTRY_1 (name);
    OUTPUT_ENTRY_1 (description);
    OUTPUT_ENTRY_1 (version);
    OUTPUT_ENTRY_1 (license);
    OUTPUT_ENTRY_1 (author);
    OUTPUT_ENTRY_1 (homepage);
    OUTPUT_ENTRY_1 (exec);
    OUTPUT_ENTRY_1 (textdomain);
#undef OUTPUT_ENTRY
#undef OUTPUT_ENTRY_1

    if (component->priv->observed_paths) {
        g_string_append_indent (output, indent + 1);
        g_string_append (output, "<observed-paths>\n");

        for (p = component->priv->observed_paths; p != NULL; p = p->next ) {
            IBusObservedPath *path = (IBusObservedPath *) p->data;

            g_string_append_indent (output, indent + 2);
            g_string_append_printf (output, "<path mtime=\"%ld\" >%s</path>\n",
                                    path->mtime,
                                    path->path);
        }

        g_string_append_indent (output, indent + 1);
        g_string_append (output, "</observed-paths>\n");
    }

    ibus_component_output_engines (component, output, indent + 1);

    g_string_append_indent (output, indent);
    g_string_append (output, "</component>\n");
}

void
ibus_component_output_engines (IBusComponent  *component,
                               GString        *output,
                               gint            indent)
{
    g_assert (IBUS_IS_COMPONENT (component));
    g_assert (output);

    GList *p;

    g_string_append_indent (output, indent);
    g_string_append (output, "<engines>\n");

    for (p = component->priv->engines; p != NULL; p = p->next) {
        ibus_engine_desc_output ((IBusEngineDesc *)p->data, output, indent + 2);
    }

    g_string_append_indent (output, indent);
    g_string_append (output, "</engines>\n");
}

static gboolean
ibus_component_parse_xml_node (IBusComponent   *component,
                              XMLNode          *node,
                              gboolean          access_fs)
{
    g_assert (component);
    g_assert (node);

    if (G_UNLIKELY (g_strcmp0 (node->name, "component") != 0)) {
        return FALSE;
    }

    GList *p;
    for (p = node->sub_nodes; p != NULL; p = p->next) {
        XMLNode *sub_node = (XMLNode *)p->data;

#define PARSE_ENTRY(field_name, element_name)                           \
        if (g_strcmp0 (sub_node->name, element_name) == 0) {            \
            if (component->priv->field_name != NULL) {                  \
                g_free (component->priv->field_name);                   \
            }                                                           \
            component->priv->field_name = g_strdup (sub_node->text);    \
            continue;                                                   \
        }
#define PARSE_ENTRY_1(name) PARSE_ENTRY (name, #name)
        PARSE_ENTRY_1 (name);
        PARSE_ENTRY_1 (description);
        PARSE_ENTRY_1 (version);
        PARSE_ENTRY_1 (license);
        PARSE_ENTRY_1 (author);
        PARSE_ENTRY_1 (homepage);
        PARSE_ENTRY_1 (exec);
        PARSE_ENTRY_1 (textdomain);
#undef PARSE_ENTRY
#undef PARSE_ENTRY_1

        if (g_strcmp0 (sub_node->name, "engines") == 0) {
            ibus_component_parse_engines (component, sub_node);
            continue;
        }

        if (g_strcmp0 (sub_node->name, "observed-paths") == 0) {
            ibus_component_parse_observed_paths (component, sub_node, access_fs);
            continue;
        }

        g_warning ("<component> element contains invalidate element <%s>", sub_node->name);
    }

    return TRUE;
}


static void
ibus_component_parse_engines (IBusComponent *component,
                              XMLNode       *node)
{
    g_assert (IBUS_IS_COMPONENT (component));
    g_assert (node);

    gchar *exec = NULL;
    gchar **p;
    XMLNode *engines_node = NULL;

    if (g_strcmp0 (node->name, "engines") != 0) {
        return;
    }

    for (p = node->attributes; *p != NULL; p += 2) {
        if (g_strcmp0 (*p, "exec") == 0) {
            exec = *(p + 1);
            break;
        }
    }

    if (exec != NULL) {
        gchar *output = NULL;
        if (g_spawn_command_line_sync (exec, &output, NULL, NULL, NULL)) {
            engines_node = ibus_xml_parse_buffer (output);
            g_free (output);

            if (engines_node) {
                if (g_strcmp0 (engines_node->name, "engines") == 0) {
                    node = engines_node;
                }
            }
        }
    }

    GList *pl;
    for (pl = node->sub_nodes; pl != NULL; pl = pl->next) {
        IBusEngineDesc *engine;
        engine = ibus_engine_desc_new_from_xml_node ((XMLNode *)pl->data);

        if (G_UNLIKELY (engine == NULL))
            continue;
        ibus_component_add_engine (component, engine);
    }

    if (engines_node) {
        ibus_xml_free (engines_node);
    }
}

static void
ibus_component_parse_observed_paths (IBusComponent *component,
                                     XMLNode       *node,
                                     gboolean       access_fs)
{
    g_assert (IBUS_IS_COMPONENT (component));
    g_assert (node);

    if (g_strcmp0 (node->name, "observed-paths") != 0) {
        return;
    }

    GList *p;
    for (p = node->sub_nodes; p != NULL; p = p->next) {
        IBusObservedPath *path;

        path = ibus_observed_path_new_from_xml_node ((XMLNode *)p->data, access_fs);
        g_object_ref_sink (path);
        component->priv->observed_paths = g_list_append (component->priv->observed_paths, path);

        if (access_fs && path->is_dir && path->is_exist) {
            component->priv->observed_paths =
                    g_list_concat (component->priv->observed_paths,
                                   ibus_observed_path_traverse (path, TRUE));
        }
    }
}

#define IBUS_COMPONENT_GET_PROPERTY(property, return_type)  \
return_type                                                 \
ibus_component_get_ ## property (IBusComponent *component)  \
{                                                           \
    return component->priv->property;                       \
}

IBUS_COMPONENT_GET_PROPERTY (name, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (description, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (version, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (license, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (author, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (homepage, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (exec, const gchar *)
IBUS_COMPONENT_GET_PROPERTY (textdomain, const gchar *)
#undef IBUS_COMPONENT_GET_PROPERTY

IBusComponent *
ibus_component_new (const gchar *name,
                    const gchar *description,
                    const gchar *version,
                    const gchar *license,
                    const gchar *author,
                    const gchar *homepage,
                    const gchar *command_line,
                    const gchar *textdomain)
{
    return ibus_component_new_varargs ("name", name,
                                       "description", description,
                                       "version", version,
                                       "license", license,
                                       "author", author,
                                       "homepage", homepage,
                                       "command-line", command_line,
                                       "textdomain", textdomain,
                                       NULL);
}


IBusComponent *
ibus_component_new_varargs (const gchar *first_property_name, ...)
{
    va_list var_args;
    IBusComponent *component;
    IBusComponentPrivate *priv;

    g_assert (first_property_name);

    va_start (var_args, first_property_name);
    component = (IBusComponent *) g_object_new_valist (IBUS_TYPE_COMPONENT,
                                                       first_property_name,
                                                       var_args);
    va_end (var_args);

    priv = IBUS_COMPONENT_GET_PRIVATE (component);

    /* name is required. Other properties are set in class_init by default. */
    g_assert (priv->name);

    return component;
}


IBusComponent *
ibus_component_new_from_xml_node (XMLNode  *node)
{
    g_assert (node);

    IBusComponent *component;

    component = (IBusComponent *)g_object_new (IBUS_TYPE_COMPONENT, NULL);
    if (!ibus_component_parse_xml_node (component, node, FALSE)) {
        g_object_unref (component);
        component = NULL;
    }

    return component;
}

IBusComponent *
ibus_component_new_from_file (const gchar *filename)
{
    g_assert (filename);

    XMLNode *node;
    struct stat buf;
    IBusComponent *component;
    gboolean retval;

    if (g_stat (filename, &buf) != 0) {
        g_warning ("Can not get stat of file %s", filename);
        return NULL;
    }

    node = ibus_xml_parse_file (filename);

    if (!node) {
        return NULL;
    }

    component = (IBusComponent *)g_object_new (IBUS_TYPE_COMPONENT, NULL);
    retval = ibus_component_parse_xml_node (component, node, TRUE);
    ibus_xml_free (node);

    if (!retval) {
        g_object_unref (component);
        component = NULL;
    }
    else {
        IBusObservedPath *path;
        path = ibus_observed_path_new (filename, TRUE);
        component->priv->observed_paths =
                g_list_prepend(component->priv->observed_paths, path);
    }

    return component;
}

void
ibus_component_add_observed_path (IBusComponent *component,
                                  const gchar   *path,
                                  gboolean       access_fs)
{
    IBusObservedPath *p;

    p = ibus_observed_path_new (path, access_fs);
    g_object_ref_sink (p);
    component->priv->observed_paths =
            g_list_append (component->priv->observed_paths, p);

    if (access_fs && p->is_dir && p->is_exist) {
        component->priv->observed_paths =
                g_list_concat (component->priv->observed_paths,
                               ibus_observed_path_traverse (p, TRUE));
    }
}

void
ibus_component_add_engine (IBusComponent  *component,
                           IBusEngineDesc *engine)
{
    g_assert (IBUS_IS_COMPONENT (component));
    g_assert (IBUS_IS_ENGINE_DESC (engine));

    g_object_ref_sink (engine);
    component->priv->engines =
            g_list_append (component->priv->engines, engine);
}

GList *
ibus_component_get_engines (IBusComponent *component)
{
    return g_list_copy (component->priv->engines);
}

gboolean
ibus_component_check_modification (IBusComponent *component)
{
    g_assert (IBUS_IS_COMPONENT (component));

    GList *p;

    for (p = component->priv->observed_paths; p != NULL; p = p->next) {
        if (ibus_observed_path_check_modification ((IBusObservedPath *)p->data))
            return TRUE;
    }
    return FALSE;
}

GList *
ibus_component_get_observed_paths (IBusComponent *component)
{
    g_assert (IBUS_IS_COMPONENT (component));
    return g_list_copy (component->priv->observed_paths);
}