1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus-anthy - The Anthy engine for IBus
4  * Copyright (c) 2012-2019 Takao Fujiwara <takao.fujiwara1@gmail.com>
5  * Copyright (c) 2012 Peng Huang <shawn.p.huang@gmail.com>
6  * Copyright (c) 2012-2013 Red Hat, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <glib-object.h>
24 #include <anthy/anthy.h>
25 
26 extern void anthy_init_personality (void);
27 extern int anthy_do_set_personality (const char *id);
28 
29 #include "anthygcontext.h"
30 
31 #define ANTHY_GCONTEXT_GET_PRIVATE(o)   \
32    (G_TYPE_INSTANCE_GET_PRIVATE ((o), ANTHY_TYPE_GCONTEXT, AnthyGContextPrivate))
33 
34 struct _AnthyGContextPrivate {
35     anthy_context_t context;
36 };
37 
38 static GObject *anthy_gcontext_constructor (GType type,
39                                            guint n,
40                                            GObjectConstructParam *args);
41 static void anthy_gcontext_dispose (GObject *gobject);
42 static void anthy_gcontext_finalize (GObject *gobject);
43 
G_DEFINE_TYPE_WITH_PRIVATE(AnthyGContext,anthy_gcontext,G_TYPE_INITIALLY_UNOWNED)44 G_DEFINE_TYPE_WITH_PRIVATE (AnthyGContext,
45                             anthy_gcontext,
46                             G_TYPE_INITIALLY_UNOWNED)
47 
48 static void
49 anthy_gcontext_class_init (AnthyGContextClass *class)
50 {
51     GObjectClass *gobject_class = G_OBJECT_CLASS (class);
52     gobject_class->constructor = anthy_gcontext_constructor;
53     gobject_class->dispose = anthy_gcontext_dispose;
54     gobject_class->finalize = anthy_gcontext_finalize;
55 }
56 
57 static void
anthy_gcontext_init(AnthyGContext * obj)58 anthy_gcontext_init (AnthyGContext *obj)
59 {
60     obj->priv = ANTHY_GCONTEXT_GET_PRIVATE (obj);
61 
62     anthy_init ();
63     obj->priv->context = anthy_create_context ();
64 }
65 
66 static GObject *
anthy_gcontext_constructor(GType type,guint n,GObjectConstructParam * args)67 anthy_gcontext_constructor (GType                   type,
68                            guint                   n,
69                            GObjectConstructParam  *args)
70 {
71     GObject *object;
72 
73     object = G_OBJECT_CLASS (anthy_gcontext_parent_class)->constructor (type, n ,args);
74     return object;
75 }
76 
77 static void
anthy_gcontext_dispose(GObject * gobject)78 anthy_gcontext_dispose (GObject *gobject)
79 {
80     G_OBJECT_CLASS (anthy_gcontext_parent_class)->dispose (gobject);
81 }
82 
83 static void
anthy_gcontext_finalize(GObject * gobject)84 anthy_gcontext_finalize (GObject *gobject)
85 {
86     G_OBJECT_CLASS (anthy_gcontext_parent_class)->finalize (gobject);
87 }
88 
89 AnthyGContext *
anthy_gcontext_new(void)90 anthy_gcontext_new (void)
91 {
92     GObject *gobject = g_object_new (ANTHY_TYPE_GCONTEXT, NULL);
93     return ANTHY_GCONTEXT (gobject);
94 }
95 
96 #define ANTHY_OBJECT_FUNCTION_ASSERTIONS() \
97 {                                                                       \
98     g_assert (obj != NULL);                                             \
99     g_assert (obj->priv != NULL);                                       \
100     g_assert (obj->priv->context != NULL);                              \
101 }
102 
103 int
anthy_gcontext_set_encoding(AnthyGContext * obj,int encoding)104 anthy_gcontext_set_encoding (AnthyGContext *obj, int encoding)
105 {
106     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
107 
108     return anthy_context_set_encoding (obj->priv->context, encoding);
109 }
110 
111 void
anthy_gcontext_init_personality(AnthyGContext * obj)112 anthy_gcontext_init_personality (AnthyGContext *obj)
113 {
114     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
115 
116     anthy_init_personality ();
117 }
118 
119 int
anthy_gcontext_do_set_personality(AnthyGContext * obj,const gchar * dict_name)120 anthy_gcontext_do_set_personality (AnthyGContext *obj, const gchar *dict_name)
121 {
122     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
123 
124     return anthy_do_set_personality (dict_name);
125 }
126 
127 void
anthy_gcontext_resize_segment(AnthyGContext * obj,int nth,int resize)128 anthy_gcontext_resize_segment (AnthyGContext *obj,
129                                int            nth,
130                                int            resize)
131 {
132     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
133 
134     anthy_resize_segment (obj->priv->context, nth, resize);
135 }
136 
137 int
anthy_gcontext_set_string(AnthyGContext * obj,const gchar * string)138 anthy_gcontext_set_string (AnthyGContext *obj, const gchar * string)
139 {
140     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
141 
142     return anthy_set_string (obj->priv->context, string);
143 }
144 
145 int
anthy_gcontext_get_nr_segments(AnthyGContext * obj)146 anthy_gcontext_get_nr_segments (AnthyGContext *obj)
147 {
148     struct anthy_conv_stat conv_stat = { 0, };
149 
150     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
151 
152     anthy_get_stat(obj->priv->context, &conv_stat);
153     return conv_stat.nr_segment;
154 }
155 
156 gchar *
anthy_gcontext_get_segment(AnthyGContext * obj,int nth_seg,int nth_lookup)157 anthy_gcontext_get_segment (AnthyGContext *obj, int nth_seg, int nth_lookup)
158 {
159     int length;
160     static char temp[512];
161 
162     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
163 
164     length = anthy_get_segment (obj->priv->context, nth_seg, nth_lookup,
165                                 temp, sizeof (temp));
166     if (length >= 0) {
167         return g_strdup (temp);
168     } else {
169         return NULL;
170     }
171 }
172 
173 int
anthy_gcontext_commit_segment(AnthyGContext * obj,int nth_seg,int nth_lookup)174 anthy_gcontext_commit_segment(AnthyGContext *obj, int nth_seg, int nth_lookup)
175 {
176     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
177 
178     return anthy_commit_segment (obj->priv->context, nth_seg, nth_lookup);
179 }
180 
181 int
anthy_gcontext_get_nr_candidates(AnthyGContext * obj,int nth_seg)182 anthy_gcontext_get_nr_candidates (AnthyGContext *obj, int nth_seg)
183 {
184     struct anthy_segment_stat seg_stat = { 0, };
185 
186     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
187 
188     anthy_get_segment_stat (obj->priv->context, nth_seg, &seg_stat);
189     return seg_stat.nr_candidate;
190 }
191 
192 int
anthy_gcontext_set_prediction_string(AnthyGContext * obj,const gchar * string)193 anthy_gcontext_set_prediction_string (AnthyGContext *obj, const gchar * string)
194 {
195     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
196 
197     return anthy_set_prediction_string (obj->priv->context, string);
198 }
199 
200 int
anthy_gcontext_get_nr_predictions(AnthyGContext * obj)201 anthy_gcontext_get_nr_predictions (AnthyGContext *obj)
202 {
203     struct anthy_prediction_stat seg_stat = { 0, };
204 
205     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
206 
207     anthy_get_prediction_stat (obj->priv->context, &seg_stat);
208     return seg_stat.nr_prediction;
209 }
210 
211 gchar *
anthy_gcontext_get_prediction(AnthyGContext * obj,int nth_seg)212 anthy_gcontext_get_prediction (AnthyGContext *obj, int nth_seg)
213 {
214     int length;
215     static char temp[512];
216 
217     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
218 
219     length = anthy_get_prediction (obj->priv->context, nth_seg,
220                                    temp, sizeof (temp));
221     if (length >= 0) {
222         return g_strdup (temp);
223     } else {
224         return NULL;
225     }
226 }
227 
228 int
anthy_gcontext_commit_prediction(AnthyGContext * obj,int nth_seg)229 anthy_gcontext_commit_prediction (AnthyGContext *obj, int nth_seg)
230 {
231     ANTHY_OBJECT_FUNCTION_ASSERTIONS ();
232 
233     return anthy_commit_prediction (obj->priv->context, nth_seg);
234 }
235 
236 void
anthy_gcontext_set_logger(int level)237 anthy_gcontext_set_logger (int level)
238 {
239     anthy_set_logger (NULL, level);
240 }
241 
242 #undef ANTHY_OBJECT_FUNCTION_ASSERTIONS
243