summaryrefslogtreecommitdiffstats
path: root/src/ZYLibZhuyin.cc
blob: 190d3da7ecc6f68b4fd495b8c9a38abccea0851b (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
/* 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 "ZYLibZhuyin.h"

#include <string.h>
#include <assert.h>
#include <zhuyin.h>
#include "ZYZConfig.h"

#define LIBZHUYIN_SAVE_TIMEOUT   (5 * 60)

namespace ZY {

std::unique_ptr<LibZhuyinBackEnd> LibZhuyinBackEnd::m_instance;

static LibZhuyinBackEnd libzhuyin_backend;

LibZhuyinBackEnd::LibZhuyinBackEnd () {
    m_timeout_id = 0;
    m_timer = g_timer_new ();
    m_zhuyin_context = NULL;
}

LibZhuyinBackEnd::~LibZhuyinBackEnd () {
    g_timer_destroy (m_timer);
    if (m_timeout_id != 0) {
        saveUserDB ();
        g_source_remove (m_timeout_id);
    }

    if (m_zhuyin_context)
        zhuyin_fini (m_zhuyin_context);
    m_zhuyin_context = NULL;
}

zhuyin_context_t *
LibZhuyinBackEnd::initZhuyinContext (Config *config)
{
    zhuyin_context_t * context = NULL;

    gchar * userdir = g_build_filename (g_get_user_cache_dir (),
                                        "ibus", "libzhuyin", NULL);
    int retval = g_mkdir_with_parents (userdir, 0700);
    if (retval) {
        g_free (userdir); userdir = NULL;
    }
    context = zhuyin_init (LIBZHUYIN_DATADIR, userdir);
    g_free (userdir);

    /* load user phrase library. */
    zhuyin_load_phrase_library (context, USER_DICTIONARY);

    return context;
}

gboolean
LibZhuyinBackEnd::setZhuyinOptions (Config *config)
{
    if (NULL == m_zhuyin_context)
        return FALSE;

    ZhuyinScheme scheme = config->keyboardLayout ();
    switch (scheme) {
    case CHEWING_STANDARD ... CHEWING_DACHEN_CP26:
        zhuyin_set_chewing_scheme (m_zhuyin_context, scheme);
        break;
    case FULL_PINYIN_HANYU ... FULL_PINYIN_SECONDARY_BOPOMOFO:
        zhuyin_set_full_pinyin_scheme (m_zhuyin_context, scheme);
        break;
    default:
        assert (FALSE);
    }

    zhuyin_option_t options = config->option ();
    zhuyin_set_options (m_zhuyin_context, options);
    return TRUE;
}


zhuyin_instance_t *
LibZhuyinBackEnd::allocZhuyinInstance ()
{
    Config * config = &ZhuyinConfig::instance ();
    if (NULL == m_zhuyin_context) {
        m_zhuyin_context = initZhuyinContext (config);
    }

    setZhuyinOptions (config);
    return zhuyin_alloc_instance (m_zhuyin_context);
}

void
LibZhuyinBackEnd::freeZhuyinInstance (zhuyin_instance_t *instance)
{
    if (instance)
        zhuyin_free_instance (instance);
}

void
LibZhuyinBackEnd::init (void) {
    g_assert (NULL == m_instance.get ());
    LibZhuyinBackEnd * backend = new LibZhuyinBackEnd;
    m_instance.reset (backend);
}

void
LibZhuyinBackEnd::finalize (void) {
    m_instance.reset ();
}

gboolean
LibZhuyinBackEnd::saveUserDB (void)
{
    if (m_zhuyin_context)
        zhuyin_save (m_zhuyin_context);
    return TRUE;
}

void
LibZhuyinBackEnd::modified (void)
{
    /* Restart the timer */
    g_timer_start (m_timer);

    if (m_timeout_id != 0)
        return;

    m_timeout_id = g_timeout_add_seconds (LIBZHUYIN_SAVE_TIMEOUT,
                                          LibZhuyinBackEnd::timeoutCallback,
                                          static_cast<gpointer> (this));
}

gboolean
LibZhuyinBackEnd::timeoutCallback (gpointer data)
{
    LibZhuyinBackEnd *self = static_cast<LibZhuyinBackEnd *>(data);

    /* Get the elapsed time since last modification of database. */
    guint elapsed = (guint)g_timer_elapsed (self->m_timer, NULL);

    if (elapsed >= LIBZHUYIN_SAVE_TIMEOUT &&
        self->saveUserDB ()) {
        self->m_timeout_id = 0;
        return FALSE;
    }

    return TRUE;
}

};