summaryrefslogtreecommitdiff
path: root/gir/anthygcontext.c
blob: 7698d4dcbd6e3f0fd7ab1316e0d67b34405c17ca (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
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* ibus-anthy - The Anthy engine for IBus
 * Copyright (c) 2012-2021 Takao Fujiwara <takao.fujiwara1@gmail.com>
 * Copyright (c) 2012 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (c) 2012-2013 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <glib-object.h>
#include <anthy/anthy.h>

extern void anthy_init_personality (void);
extern int anthy_do_set_personality (const char *id);

#include "anthygcontext.h"

#define ANTHY_GCONTEXT_GET_PRIVATE(o)   \
    ((AnthyGContextPrivate *)anthy_gcontext_get_instance_private (o))

struct _AnthyGContextPrivate {
    anthy_context_t context;
};

static GObject *anthy_gcontext_constructor (GType type,
                                           guint n,
                                           GObjectConstructParam *args);
static void anthy_gcontext_dispose (GObject *gobject);
static void anthy_gcontext_finalize (GObject *gobject);

G_DEFINE_TYPE_WITH_PRIVATE (AnthyGContext,
                            anthy_gcontext,
                            G_TYPE_INITIALLY_UNOWNED)

static void
anthy_gcontext_class_init (AnthyGContextClass *class)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (class);
    gobject_class->constructor = anthy_gcontext_constructor;
    gobject_class->dispose = anthy_gcontext_dispose;
    gobject_class->finalize = anthy_gcontext_finalize;
}

static void
anthy_gcontext_init (AnthyGContext *obj)
{
    obj->priv = ANTHY_GCONTEXT_GET_PRIVATE (obj);

    anthy_init ();
    obj->priv->context = anthy_create_context ();
}

static GObject *
anthy_gcontext_constructor (GType                   type,
                           guint                   n,
                           GObjectConstructParam  *args)
{
    GObject *object;

    object = G_OBJECT_CLASS (anthy_gcontext_parent_class)->constructor (type, n ,args);
    return object;
}

static void
anthy_gcontext_dispose (GObject *gobject)
{
    G_OBJECT_CLASS (anthy_gcontext_parent_class)->dispose (gobject);
}

static void
anthy_gcontext_finalize (GObject *gobject)
{
    G_OBJECT_CLASS (anthy_gcontext_parent_class)->finalize (gobject);
}

AnthyGContext *
anthy_gcontext_new (void)
{
    GObject *gobject = g_object_new (ANTHY_TYPE_GCONTEXT, NULL);
    return ANTHY_GCONTEXT (gobject);
}

#define ANTHY_OBJECT_FUNCTION_ASSERTIONS() \
{                                                                       \
    g_assert (obj != NULL);                                             \
    g_assert (obj->priv != NULL);                                       \
    g_assert (obj->priv->context != NULL);                              \
}

int
anthy_gcontext_set_encoding (AnthyGContext *obj, int encoding)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_context_set_encoding (obj->priv->context, encoding);
}

void
anthy_gcontext_init_personality (AnthyGContext *obj)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    anthy_init_personality ();
}

int
anthy_gcontext_do_set_personality (AnthyGContext *obj, const gchar *dict_name)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_do_set_personality (dict_name);
}

void
anthy_gcontext_resize_segment (AnthyGContext *obj,
                               int            nth,
                               int            resize)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    anthy_resize_segment (obj->priv->context, nth, resize);
}

int
anthy_gcontext_set_string (AnthyGContext *obj, const gchar * string)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_set_string (obj->priv->context, string);
}

int
anthy_gcontext_get_nr_segments (AnthyGContext *obj)
{
    struct anthy_conv_stat conv_stat = { 0, };

    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    anthy_get_stat(obj->priv->context, &conv_stat);
    return conv_stat.nr_segment;
}

gchar *
anthy_gcontext_get_segment (AnthyGContext *obj, int nth_seg, int nth_lookup)
{
    int length;
    static char temp[512];

    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    length = anthy_get_segment (obj->priv->context, nth_seg, nth_lookup,
                                temp, sizeof (temp));
    if (length >= 0) {
        return g_strdup (temp);
    } else {
        return NULL;
    }
}

int
anthy_gcontext_commit_segment(AnthyGContext *obj, int nth_seg, int nth_lookup)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_commit_segment (obj->priv->context, nth_seg, nth_lookup);
}

int
anthy_gcontext_get_nr_candidates (AnthyGContext *obj, int nth_seg)
{
    struct anthy_segment_stat seg_stat = { 0, };

    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    anthy_get_segment_stat (obj->priv->context, nth_seg, &seg_stat);
    return seg_stat.nr_candidate;
}

int
anthy_gcontext_set_prediction_string (AnthyGContext *obj, const gchar * string)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_set_prediction_string (obj->priv->context, string);
}

int
anthy_gcontext_get_nr_predictions (AnthyGContext *obj)
{
    struct anthy_prediction_stat seg_stat = { 0, };

    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    anthy_get_prediction_stat (obj->priv->context, &seg_stat);
    return seg_stat.nr_prediction;
}

gchar *
anthy_gcontext_get_prediction (AnthyGContext *obj, int nth_seg)
{
    int length;
    static char temp[512];

    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    length = anthy_get_prediction (obj->priv->context, nth_seg,
                                   temp, sizeof (temp));
    if (length >= 0) {
        return g_strdup (temp);
    } else {
        return NULL;
    }
}

int
anthy_gcontext_commit_prediction (AnthyGContext *obj, int nth_seg)
{
    ANTHY_OBJECT_FUNCTION_ASSERTIONS ();

    return anthy_commit_prediction (obj->priv->context, nth_seg);
}

void
anthy_gcontext_set_logger (int level)
{
    anthy_set_logger (NULL, level);
}

#undef ANTHY_OBJECT_FUNCTION_ASSERTIONS