summaryrefslogtreecommitdiff
path: root/src/ibusemoji.c
blob: d56c48aad717eb7227461fa8a6231635bb9c16d8 (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
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* bus - The Input Bus
 * Copyright (C) 2017 Takao Fujiwara <takao.fujiwara1@gmail.com>
 * Copyright (C) 2017 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
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>
#include "ibusemoji.h"
#include "ibusinternal.h"

#define IBUS_EMOJI_DATA_MAGIC "IBusEmojiData"
#define IBUS_EMOJI_DATA_VERSION (5)

enum {
    PROP_0 = 0,
    PROP_EMOJI,
    PROP_ANNOTATIONS,
    PROP_DESCRIPTION,
    PROP_CATEGORY,
};

struct _IBusEmojiDataPrivate {
    gchar      *emoji;
    GSList     *annotations;
    gchar      *description;
    gchar      *category;
};

#define IBUS_EMOJI_DATA_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
    IBUS_TYPE_EMOJI_DATA, \
    IBusEmojiDataPrivate))

/* functions prototype */
static void      ibus_emoji_data_set_property  (IBusEmojiData       *emoji,
                                                guint                prop_id,
                                                const GValue        *value,
                                                GParamSpec          *pspec);
static void      ibus_emoji_data_get_property  (IBusEmojiData       *emoji,
                                                guint                prop_id,
                                                GValue              *value,
                                                GParamSpec          *pspec);
static void      ibus_emoji_data_destroy       (IBusEmojiData       *emoji);
static gboolean  ibus_emoji_data_serialize     (IBusEmojiData       *emoji,
                                                GVariantBuilder     *builder);
static gint      ibus_emoji_data_deserialize   (IBusEmojiData       *emoji,
                                                GVariant            *variant);
static gboolean  ibus_emoji_data_copy          (IBusEmojiData       *emoji,
                                                const IBusEmojiData *src);

G_DEFINE_TYPE (IBusEmojiData, ibus_emoji_data, IBUS_TYPE_SERIALIZABLE)

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

    object_class->destroy = (IBusObjectDestroyFunc) ibus_emoji_data_destroy;
    gobject_class->set_property =
            (GObjectSetPropertyFunc) ibus_emoji_data_set_property;
    gobject_class->get_property =
            (GObjectGetPropertyFunc) ibus_emoji_data_get_property;
    serializable_class->serialize   =
            (IBusSerializableSerializeFunc) ibus_emoji_data_serialize;
    serializable_class->deserialize =
            (IBusSerializableDeserializeFunc) ibus_emoji_data_deserialize;
    serializable_class->copy        =
            (IBusSerializableCopyFunc) ibus_emoji_data_copy;

    g_type_class_add_private (class, sizeof (IBusEmojiDataPrivate));

    /* install properties */
    /**
     * IBusEmojiData:emoji:
     *
     * The emoji character
     */
    g_object_class_install_property (gobject_class,
                    PROP_EMOJI,
                    g_param_spec_string ("emoji",
                        "emoji character",
                        "The emoji character UTF-8",
                        NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    /**
     * IBusEmojiData:annotations: (transfer container) (element-type utf8):
     *
     * The emoji annotations
     */
    g_object_class_install_property (gobject_class,
                    PROP_ANNOTATIONS,
                    g_param_spec_pointer ("annotations",
                        "emoji annotations",
                        "The emoji annotation list",
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

    /**
     * IBusEmojiData:description:
     *
     * The emoji description
     */
    g_object_class_install_property (gobject_class,
                    PROP_DESCRIPTION,
                    g_param_spec_string ("description",
                        "emoji description",
                        "The emoji description",
                        "",
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

    /**
     * IBusEmojiData:category:
     *
     * The emoji category
     */
    g_object_class_install_property (gobject_class,
                    PROP_CATEGORY,
                    g_param_spec_string ("category",
                        "emoji category",
                        "The emoji category",
                        "",
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
ibus_emoji_data_init (IBusEmojiData *emoji)
{
    emoji->priv = IBUS_EMOJI_DATA_GET_PRIVATE (emoji);
}

static void
free_dict_words (gpointer list)
{
    g_slist_free_full (list, g_free);
}

static void
ibus_emoji_data_destroy (IBusEmojiData *emoji)
{
    g_clear_pointer (&emoji->priv->emoji, g_free);
    g_clear_pointer (&emoji->priv->annotations, free_dict_words);
    g_clear_pointer (&emoji->priv->description, g_free);
    g_clear_pointer (&emoji->priv->category, g_free);

    IBUS_OBJECT_CLASS (ibus_emoji_data_parent_class)->
            destroy (IBUS_OBJECT (emoji));
}

static void
ibus_emoji_data_set_property (IBusEmojiData *emoji,
                              guint          prop_id,
                              const GValue  *value,
                              GParamSpec    *pspec)
{
    switch (prop_id) {
    case PROP_EMOJI:
        g_assert (emoji->priv->emoji == NULL);
        emoji->priv->emoji = g_value_dup_string (value);
        break;
    case PROP_ANNOTATIONS:
        if (emoji->priv->annotations)
            g_slist_free_full (emoji->priv->annotations, g_free);
        emoji->priv->annotations =
                g_slist_copy_deep (g_value_get_pointer (value),
                                   (GCopyFunc) g_strdup, NULL);
        break;
    case PROP_DESCRIPTION:
        g_free (emoji->priv->description);
        emoji->priv->description = g_value_dup_string (value);
        break;
    case PROP_CATEGORY:
        g_assert (emoji->priv->category == NULL);
        emoji->priv->category = g_value_dup_string (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (emoji, prop_id, pspec);
    }
}

static void
ibus_emoji_data_get_property (IBusEmojiData *emoji,
                              guint          prop_id,
                              GValue        *value,
                              GParamSpec    *pspec)
{
    switch (prop_id) {
    case PROP_EMOJI:
        g_value_set_string (value, ibus_emoji_data_get_emoji (emoji));
        break;
    case PROP_ANNOTATIONS:
        g_value_set_pointer (
                value,
                g_slist_copy_deep (ibus_emoji_data_get_annotations (emoji),
                                   (GCopyFunc) g_strdup, NULL));
        break;
    case PROP_DESCRIPTION:
        g_value_set_string (value, ibus_emoji_data_get_description (emoji));
        break;
    case PROP_CATEGORY:
        g_value_set_string (value, ibus_emoji_data_get_category (emoji));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (emoji, prop_id, pspec);
    }
}

static gboolean
ibus_emoji_data_serialize (IBusEmojiData   *emoji,
                           GVariantBuilder *builder)
{
    GSList *l;
    gboolean retval = IBUS_SERIALIZABLE_CLASS (ibus_emoji_data_parent_class)->
            serialize ((IBusSerializable *)emoji, builder);
    g_return_val_if_fail (retval, FALSE);

#define NOTNULL(s) ((s) != NULL ? (s) : "")
    /* If you will add a new property, you can append it at the end and
     * you should not change the serialized order of name, longname,
     * description, ... because the order is also used in other applications
     * likes ibus-qt. */
    g_variant_builder_add (builder, "s", NOTNULL (emoji->priv->emoji));
    g_variant_builder_add (builder, "u",
                           g_slist_length (emoji->priv->annotations));
    for (l = emoji->priv->annotations; l != NULL; l = l->next) {
        g_variant_builder_add (builder, "s", NOTNULL (l->data));
    }
    g_variant_builder_add (builder, "s", NOTNULL (emoji->priv->description));
    g_variant_builder_add (builder, "s", NOTNULL (emoji->priv->category));
#undef NOTNULL
    return TRUE;
}

static gint
ibus_emoji_data_deserialize (IBusEmojiData *emoji,
                             GVariant      *variant)
{
    guint length, i;
    GSList *annotations = NULL;
    gint retval = IBUS_SERIALIZABLE_CLASS (ibus_emoji_data_parent_class)->
            deserialize ((IBusSerializable *)emoji, variant);
    g_return_val_if_fail (retval, 0);

    /* If you will add a new property, you can append it at the end and
     * you should not change the serialized order of name, longname,
     * description, ... because the order is also used in other applications
     * likes ibus-qt. */
    ibus_g_variant_get_child_string (variant, retval++,
                                     &emoji->priv->emoji);
    g_variant_get_child (variant, retval++, "u", &length);
    for (i = 0; i < length; i++) {
        gchar *s = NULL;
        g_variant_get_child (variant, retval++, "s", &s);
        annotations = g_slist_append (annotations, s);
    }
    emoji->priv->annotations = annotations;
    ibus_g_variant_get_child_string (variant, retval++,
                                     &emoji->priv->description);
    ibus_g_variant_get_child_string (variant, retval++,
                                     &emoji->priv->category);
    return retval;
}

static gboolean
ibus_emoji_data_copy (IBusEmojiData       *dest,
                      const IBusEmojiData *src)
{
    gboolean retval = IBUS_SERIALIZABLE_CLASS (ibus_emoji_data_parent_class)->
            copy ((IBusSerializable *)dest,
                  (IBusSerializable *)src);
    g_return_val_if_fail (retval, FALSE);

    dest->priv->emoji            = g_strdup (src->priv->emoji);
    dest->priv->annotations      = g_slist_copy_deep (src->priv->annotations,
                                                      (GCopyFunc) g_strdup,
                                                      NULL);
    dest->priv->description      = g_strdup (src->priv->description);
    dest->priv->category         = g_strdup (src->priv->category);
    return TRUE;
}

IBusEmojiData *
ibus_emoji_data_new (const gchar *first_property_name, ...)
{
    va_list var_args;
    IBusEmojiData *emoji;

    g_assert (first_property_name != NULL);
    va_start (var_args, first_property_name);
    emoji = (IBusEmojiData *) g_object_new_valist (IBUS_TYPE_EMOJI_DATA,
                                                   first_property_name,
                                                   var_args);
    va_end (var_args);
    /* emoji is required. Other properties are set in class_init by default. */
    g_assert (emoji->priv->emoji != NULL);
    g_assert (emoji->priv->description != NULL);
    g_assert (emoji->priv->category != NULL);
    return emoji;
}

const gchar *
ibus_emoji_data_get_emoji (IBusEmojiData *emoji)
{
    g_return_val_if_fail (IBUS_IS_EMOJI_DATA (emoji), NULL);

    return emoji->priv->emoji;
}

GSList *
ibus_emoji_data_get_annotations (IBusEmojiData *emoji)
{
    g_return_val_if_fail (IBUS_IS_EMOJI_DATA (emoji), NULL);

    return emoji->priv->annotations;
}

void
ibus_emoji_data_set_annotations (IBusEmojiData *emoji,
                                 GSList        *annotations)
{
    g_return_if_fail (IBUS_IS_EMOJI_DATA (emoji));

    if (emoji->priv->annotations)
        g_slist_free_full (emoji->priv->annotations, g_free);
    emoji->priv->annotations = annotations;
}

const gchar *
ibus_emoji_data_get_description (IBusEmojiData *emoji)
{
    g_return_val_if_fail (IBUS_IS_EMOJI_DATA (emoji), NULL);

    return emoji->priv->description;
}

void
ibus_emoji_data_set_description (IBusEmojiData *emoji,
                                 const gchar   *description)
{
    g_return_if_fail (IBUS_IS_EMOJI_DATA (emoji));

    g_free (emoji->priv->description);
    emoji->priv->description = g_strdup (description);
}

const gchar *
ibus_emoji_data_get_category (IBusEmojiData *emoji)
{
    g_return_val_if_fail (IBUS_IS_EMOJI_DATA (emoji), NULL);

    return emoji->priv->category;
}

static void
variant_foreach_add_emoji (IBusEmojiData   *emoji,
                           GVariantBuilder *builder)
{
    g_variant_builder_add (
            builder, "v",
            ibus_serializable_serialize (IBUS_SERIALIZABLE (emoji)));
}

static GVariant *
ibus_emoji_data_list_serialize (GSList *list)
{
    GVariantBuilder builder;

    g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
    g_slist_foreach (list,  (GFunc) variant_foreach_add_emoji, &builder);
    return g_variant_builder_end (&builder);
}

static GSList *
ibus_emoji_data_list_deserialize (GVariant           *variant)
{
    GSList *list = NULL;
    GVariantIter iter;
    GVariant *emoji_variant = NULL;

    g_variant_iter_init (&iter, variant);
    while (g_variant_iter_loop (&iter, "v", &emoji_variant)) {
        IBusEmojiData *data =
                IBUS_EMOJI_DATA (ibus_serializable_deserialize (emoji_variant));
        list = g_slist_append (list, data);
        g_clear_pointer (&emoji_variant, g_variant_unref);
    }

    return list;
}

void
ibus_emoji_dict_save (const gchar *path,
                      GHashTable  *dict)
{
    GList *values, *v;
    GSList *list_for_save = NULL;

    g_return_if_fail (path != NULL);
    g_return_if_fail (dict != NULL);

    values = g_hash_table_get_values (dict);
    for (v = values; v; v = v->next) {
        IBusEmojiData *data = v->data;
        if (!IBUS_IS_EMOJI_DATA (data)) {
            g_warning ("Your dict format of { annotation char, emoji GSList "
                       "} is no longer supported.\n"
                       "{ emoji char, IBusEmojiData GSList } is expected.");
            return;
        }
        list_for_save = g_slist_append (list_for_save, data);
    }

    ibus_emoji_data_save (path, list_for_save);
}

GHashTable *
ibus_emoji_dict_load (const gchar *path)
{
    GSList *list = ibus_emoji_data_load (path);
    GSList *l;
    GHashTable *dict = g_hash_table_new_full (g_str_hash,
                                              g_str_equal,
                                              g_free,
                                              g_object_unref);

    for (l = list; l; l = l->next) {
        IBusEmojiData *data = l->data;
        if (!IBUS_IS_EMOJI_DATA (data)) {
            g_warning ("Your dict format is no longer supported.\n"
                       "Need to create the dictionaries again.");
            return NULL;
        }
        g_hash_table_insert (dict,
                             g_strdup (ibus_emoji_data_get_emoji (data)),
                             g_object_ref_sink (data));
    }

    g_slist_free (list);

    return dict;
}


IBusEmojiData *
ibus_emoji_dict_lookup (GHashTable  *dict,
                        const gchar *emoji)
{
    return (IBusEmojiData *) g_hash_table_lookup (dict, emoji);
}

void
ibus_emoji_data_save (const gchar *path,
                      GSList      *list)
{
    GVariant *variant;
    const gchar *header = IBUS_EMOJI_DATA_MAGIC;
    const guint16 version = IBUS_EMOJI_DATA_VERSION;
    const gchar *contents;
    gsize length;
    gchar *dir;
    GStatBuf buf = { 0, };
    GError *error = NULL;

    g_return_if_fail (path != NULL);
    g_return_if_fail (list != NULL);
    if (list->data == NULL) {
        g_warning ("Failed to save IBus emoji data: Need a list data.");
        return;
    }

    variant = g_variant_new ("(sqv)",
                             header,
                             version,
                             ibus_emoji_data_list_serialize (list));

    contents =  g_variant_get_data (variant);
    length =  g_variant_get_size (variant);

    dir = g_path_get_dirname (path);
    if (g_strcmp0 (dir, ".") != 0 && g_stat (dir, &buf) != 0) {
        g_mkdir_with_parents (dir, 0777);
    }
    g_free (dir);
    if (!g_file_set_contents (path, contents, length, &error)) {
        g_warning ("Failed to save emoji dict %s: %s", path, error->message);
        g_error_free (error);
    }

    g_variant_unref (variant);
}

GSList *
ibus_emoji_data_load (const gchar *path)
{
    gchar *contents = NULL;
    gsize length = 0;
    GError *error = NULL;
    GVariant *variant_table = NULL;
    GVariant *variant = NULL;
    const gchar *header = NULL;
    guint16 version = 0;
    GSList *retval = NULL;

    if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
        g_warning ("Emoji dict does not exist: %s", path);
        goto out_load_cache;
    }

    if (!g_file_get_contents (path, &contents, &length, &error)) {
        g_warning ("Failed to get dict content %s: %s", path, error->message);
        g_error_free (error);
        goto out_load_cache;
    }

    variant_table = g_variant_new_from_data (G_VARIANT_TYPE ("(sq)"),
                                             contents,
                                             length,
                                             FALSE,
                                             NULL,
                                             NULL);

    if (variant_table == NULL) {
        g_warning ("cache table is broken.");
        goto out_load_cache;
    }

    g_variant_get (variant_table, "(&sq)", &header, &version);

    if (g_strcmp0 (header, IBUS_EMOJI_DATA_MAGIC) != 0) {
        g_warning ("cache is not IBusEmojiData.");
        goto out_load_cache;
    }

    if (version > IBUS_EMOJI_DATA_VERSION) {
        g_warning ("cache version is different: %u != %u",
                   version, IBUS_EMOJI_DATA_VERSION);
        goto out_load_cache;
    }

    version = 0;
    header = NULL;
    g_variant_unref (variant_table);

    variant_table = g_variant_new_from_data (G_VARIANT_TYPE ("(sqv)"),
                                             contents,
                                             length,
                                             FALSE,
                                             NULL,
                                             NULL);

    if (variant_table == NULL) {
        g_warning ("cache table is broken.");
        goto out_load_cache;
    }

    g_variant_get (variant_table, "(&sqv)",
                   NULL,
                   NULL,
                   &variant);

    if (variant == NULL) {
        g_warning ("cache dict is broken.");
        goto out_load_cache;
    }

    retval = ibus_emoji_data_list_deserialize (variant);

out_load_cache:
    if (variant)
        g_variant_unref (variant);
    if (variant_table)
        g_variant_unref (variant_table);
    g_free (contents);

    return retval;
}