summaryrefslogtreecommitdiffstats
path: root/src/ZYZConfig.cc
blob: f00751ce0e11dd19d1d8b9ae12b1d6de56f5837b (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
/* vim:set et ts=4 sts=4:
 *
 * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus
 *
 * Copyright (c) 2014 Peng Wu <alexepico@gmail.com>
 *
 * 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, 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 "ZYZConfig.h"
#include <zhuyin.h>
#include "ZYLibZhuyin.h"
#include <string.h>

#define USE_G_SETTINGS_LIST_KEYS 0

namespace ZY {

const gchar * const CONFIG_FUZZY_ZHUYIN              = "fuzzy-zhuyin";
const gchar * const CONFIG_PAGE_SIZE                 = "candidate-num";

const gchar * const CONFIG_INIT_CHINESE              = "chinese-mode";
const gchar * const CONFIG_INIT_FULL_WIDTH           = "full-half-width";
const gchar * const CONFIG_INIT_TRAD_CHINESE         = "traditional-chinese";
const gchar * const CONFIG_ALWAYS_INPUT_NUMBERS      = "always-input-num";
const gchar * const CONFIG_SPACE_SHOW_CANDIDATES     = "space-show-candidates";
const gchar * const CONFIG_CANDIDATES_AFTER_CURSOR   = "candidates-after-cursor";

const gchar * const CONFIG_KEYBOARD_LAYOUT           = "keyboard-layout";
const gchar * const CONFIG_CANDIDATE_KEYS            = "candidate-keys";

const gchar * const CONFIG_EASY_SYMBOL               = "easy-symbol";
const gchar * const CONFIG_USER_SYMBOL               = "user-symbol";

const gchar * const CONFIG_IMPORT_DICTIONARY         = "import-dictionary";
const gchar * const CONFIG_CLEAR_USER_DATA           = "clear-user-data";

const zhuyin_option_t ZHUYIN_DEFAULT_OPTION =
    USE_TONE           |
    FORCE_TONE         |
    ZHUYIN_CORRECT_ALL |
    0;

std::unique_ptr<ZhuyinConfig> ZhuyinConfig::m_instance;

ZhuyinConfig::ZhuyinConfig ()
    : Config ("com.github.libzhuyin.ibus-libzhuyin")
{
    m_settings = g_settings_new (m_schema_id.c_str ());
    initDefaultValues ();
    g_signal_connect (m_settings,
                      "changed",
                      G_CALLBACK (valueChangedCallback),
                      this);

}

ZhuyinConfig::~ZhuyinConfig (void)
{
    g_object_unref (m_settings);
    m_settings = NULL;
}

void
ZhuyinConfig::init ()
{
    if (m_instance.get () == NULL) {
        m_instance.reset (new ZhuyinConfig ());
        m_instance->readDefaultValues ();
    }
}

void
ZhuyinConfig::initDefaultValues (void)
{
    m_option = ZHUYIN_DEFAULT_OPTION;
    m_option_mask = USE_TONE | FORCE_TONE | ZHUYIN_CORRECT_ALL;

    m_orientation = IBUS_ORIENTATION_VERTICAL;
    m_page_size = 10;

    m_is_zhuyin = TRUE;
    m_zhuyin_scheme = ZHUYIN_DEFAULT;
    m_pinyin_scheme = FULL_PINYIN_DEFAULT;

    m_init_chinese = TRUE;
    m_init_full_width = FALSE;
    m_init_trad_chinese = TRUE;

    m_always_input_numbers = FALSE;
    m_space_show_candidates = FALSE;
    m_candidates_after_cursor = TRUE;

    m_candidate_keys = "1234567890";

    m_easy_symbol = TRUE;
    m_user_symbol = TRUE;
}

/* Here are the zhuyin keyboard layout mapping table. */
static const struct {
    gint layout;
    ZhuyinScheme scheme;
} zhuyin_schemes [] = {
    {0, ZHUYIN_STANDARD},
    {1, ZHUYIN_HSU},
    {2, ZHUYIN_IBM},
    {3, ZHUYIN_GINYIEH},
    {4, ZHUYIN_ETEN},
    {5, ZHUYIN_ETEN26},
    {6, ZHUYIN_STANDARD_DVORAK},
    {7, ZHUYIN_HSU_DVORAK},
    {8, ZHUYIN_DACHEN_CP26},
};

static const struct {
    gint layout;
    FullPinyinScheme scheme;
} pinyin_schemes [] = {
    {9, FULL_PINYIN_HANYU},
    {10, FULL_PINYIN_LUOMA},
    {11, FULL_PINYIN_SECONDARY_ZHUYIN},
};

static const struct {
    const gchar * const name;
    guint option;
} fuzzy_zhuyin_options [] = {
    /* fuzzy pinyin */
    { "fuzzy-zhuyin-c-ch",       ZHUYIN_AMB_C_CH      },
    { "fuzzy-zhuyin-z-zh",       ZHUYIN_AMB_Z_ZH      },
    { "fuzzy-zhuyin-s-sh",       ZHUYIN_AMB_S_SH      },
    { "fuzzy-zhuyin-l-n",        ZHUYIN_AMB_L_N       },
    { "fuzzy-zhuyin-f-h",        ZHUYIN_AMB_F_H       },
    { "fuzzy-zhuyin-l-r",        ZHUYIN_AMB_L_R       },
    { "fuzzy-zhuyin-g-k",        ZHUYIN_AMB_G_K       },
    { "fuzzy-zhuyin-an-ang",     ZHUYIN_AMB_AN_ANG    },
    { "fuzzy-zhuyin-en-eng",     ZHUYIN_AMB_EN_ENG    },
    { "fuzzy-zhuyin-in-ing",     ZHUYIN_AMB_IN_ING    },
};

void
ZhuyinConfig::readDefaultValues (void)
{
#if USE_G_SETTINGS_LIST_KEYS
    /* read all values together */
    initDefaultValues ();
    gchar **keys = g_settings_list_keys (m_settings);
    g_return_if_fail (keys != NULL);

    for (gchar **iter = keys; *iter != NULL; ++iter) {
        gchar *name = *iter;

        /* skip signals here. */
        if (0 == strcmp(CONFIG_IMPORT_DICTIONARY, name))
            continue;

        if (0 == strcmp(CONFIG_CLEAR_USER_DATA, name))
            continue;

        GVariant *value = g_settings_get_value (m_settings, name);
        valueChanged (m_schema_id, name, value);
        g_variant_unref (value);
    }

    g_strfreev (keys);
#else
    /* others */
    m_page_size = read (CONFIG_PAGE_SIZE, 10);
    if (m_page_size > 10) {
        m_page_size = 10;
        g_warn_if_reached ();
    }

    /* init states */
    m_init_chinese = read (CONFIG_INIT_CHINESE, true);
    m_init_full_width = read (CONFIG_INIT_FULL_WIDTH, false);
    m_init_trad_chinese = read (CONFIG_INIT_TRAD_CHINESE, true);

    m_always_input_numbers = read (CONFIG_ALWAYS_INPUT_NUMBERS, false);
    m_space_show_candidates = read (CONFIG_SPACE_SHOW_CANDIDATES, false);
    m_candidates_after_cursor = read (CONFIG_CANDIDATES_AFTER_CURSOR, true);

    gint layout = read (CONFIG_KEYBOARD_LAYOUT, 0);

    m_is_zhuyin = TRUE;
    m_zhuyin_scheme = ZHUYIN_DEFAULT;
    m_pinyin_scheme = FULL_PINYIN_DEFAULT;

    for (guint i = 0; i < G_N_ELEMENTS (pinyin_schemes); ++i) {
        if (pinyin_schemes[i].layout == layout) {
            m_is_zhuyin = FALSE;
            m_pinyin_scheme = pinyin_schemes[i].scheme;
        }
    }

    for (guint i = 0; i < G_N_ELEMENTS (zhuyin_schemes); ++i) {
        if (zhuyin_schemes[i].layout == layout) {
            m_is_zhuyin = TRUE;
            m_zhuyin_scheme = zhuyin_schemes[i].scheme;
        }
    }

    m_candidate_keys = read (CONFIG_CANDIDATE_KEYS, std::string ("1234567890"));

    m_easy_symbol = read (CONFIG_EASY_SYMBOL, true);
    m_user_symbol = read (CONFIG_USER_SYMBOL, true);

    /* fuzzy zhuyin */
    if (read (CONFIG_FUZZY_ZHUYIN, false))
        m_option_mask |= ZHUYIN_AMB_ALL;
    else
        m_option_mask &= ~ZHUYIN_AMB_ALL;

    /* read values */
    for (guint i = 0; i < G_N_ELEMENTS (fuzzy_zhuyin_options); i++) {
        if (read (fuzzy_zhuyin_options[i].name,
                  (fuzzy_zhuyin_options[i].option & ZHUYIN_DEFAULT_OPTION) != 0)) {
            m_option |= fuzzy_zhuyin_options[i].option;
        }
        else {
            m_option &= ~fuzzy_zhuyin_options[i].option;
        }
    }
#endif
}

gboolean
ZhuyinConfig::valueChanged (const std::string &schema_id,
                            const std::string &name,
                            GVariant          *value)
{
    if (m_schema_id != schema_id)
        return FALSE;

    if (Config::valueChanged (schema_id, name, value))
        return TRUE;

    /* init states */
    if (CONFIG_INIT_CHINESE == name)
        m_init_chinese = normalizeGVariant (value, true);
    else if (CONFIG_INIT_FULL_WIDTH == name)
        m_init_full_width = normalizeGVariant (value, false);
    else if (CONFIG_INIT_TRAD_CHINESE == name)
        m_init_trad_chinese = normalizeGVariant (value, true);
    else if (CONFIG_ALWAYS_INPUT_NUMBERS == name)
        m_always_input_numbers = normalizeGVariant (value, false);
    else if (CONFIG_SPACE_SHOW_CANDIDATES == name)
        m_space_show_candidates = normalizeGVariant (value, false);
    else if (CONFIG_CANDIDATES_AFTER_CURSOR == name)
        m_candidates_after_cursor = normalizeGVariant (value, true);
    else if (CONFIG_KEYBOARD_LAYOUT == name) {
        gint layout = normalizeGVariant (value, 0);

        m_is_zhuyin = TRUE;
        m_zhuyin_scheme = ZHUYIN_DEFAULT;
        m_pinyin_scheme = FULL_PINYIN_DEFAULT;

        for (guint i = 0; i < G_N_ELEMENTS (pinyin_schemes); ++i) {
            if (pinyin_schemes[i].layout == layout) {
                m_is_zhuyin = FALSE;
                m_pinyin_scheme = pinyin_schemes[i].scheme;
            }
        }

        for (guint i = 0; i < G_N_ELEMENTS (zhuyin_schemes); ++i) {
            if (zhuyin_schemes[i].layout == layout) {
                m_is_zhuyin = TRUE;
                m_zhuyin_scheme = zhuyin_schemes[i].scheme;
            }
        }
    } else if (CONFIG_CANDIDATE_KEYS == name) {
        m_candidate_keys = normalizeGVariant (value, std::string ("1234567890"));
    } /* lookup table page size */
    else if (CONFIG_PAGE_SIZE == name) {
        m_page_size = normalizeGVariant (value, 10);
        if (m_page_size > 10) {
            m_page_size = 10;
            g_warn_if_reached ();
        }
    }
    else if (CONFIG_EASY_SYMBOL == name) {
        m_easy_symbol = normalizeGVariant (value, true);
    }
    else if (CONFIG_USER_SYMBOL == name) {
        m_user_symbol = normalizeGVariant (value, true);
    }
    else if (CONFIG_IMPORT_DICTIONARY == name) {
        std::string filename = normalizeGVariant (value, std::string(""));
        LibZhuyinBackEnd::instance ().importZhuyinDictionary(filename.c_str ());
    }
    else if (CONFIG_CLEAR_USER_DATA == name) {
        std::string target = normalizeGVariant (value, std::string(""));
        LibZhuyinBackEnd::instance ().clearZhuyinUserData(target.c_str ());
    } /* fuzzy zhuyin */
    else if (CONFIG_FUZZY_ZHUYIN == name) {
        if (normalizeGVariant (value, false))
            m_option_mask |= ZHUYIN_AMB_ALL;
        else
            m_option_mask &= ~ZHUYIN_AMB_ALL;
    }
    else {
        for (guint i = 0; i < G_N_ELEMENTS (fuzzy_zhuyin_options); i++) {
            if (G_LIKELY (fuzzy_zhuyin_options[i].name != name))
                continue;
            if (normalizeGVariant (value,
                    (fuzzy_zhuyin_options[i].option & ZHUYIN_DEFAULT_OPTION) != 0))
                m_option |= fuzzy_zhuyin_options[i].option;
            else
                m_option &= ~fuzzy_zhuyin_options[i].option;
            return TRUE;
        }
        return FALSE;
    }
    return TRUE;

}

void
ZhuyinConfig::valueChangedCallback (GSettings    *settings,
                                    const gchar  *name,
                                    ZhuyinConfig *self)
{
    gchar * property = NULL;
    g_object_get (settings, "schema-id", &property, NULL);
    std::string schema_id (property);
    g_free (property);

    if (self->m_schema_id != schema_id)
        return;

    GVariant * value = g_settings_get_value (settings, name);
    self->valueChanged (self->m_schema_id, name, value);
    g_variant_unref (value);

    if (self->m_schema_id == "com.github.libzhuyin.ibus-libzhuyin")
        LibZhuyinBackEnd::instance ().setZhuyinOptions (self);
}

};