summaryrefslogtreecommitdiffstats
path: root/src/ZYZUserSymbolListAllSection.cc
blob: f9f4114a2881dae6486f761279bb222519bb9e6d (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
/* 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 "ZYZUserSymbolListAllSection.h"
#include <assert.h>
#include "ZYSymbols.h"

namespace ZY {

UserSymbolListAllSection::UserSymbolListAllSection (PhoneticEditor & editor,
                                                    ZhuyinProperties & props) :
    SymbolSection (editor, props)
{
    m_type = BUILTIN_SYMBOL_TYPE;

    /* load user symbol. */
    gchar * path = g_build_filename (g_get_user_config_dir (),
                                     "ibus", "libzhuyin",
                                     "usersymbol.txt", NULL);
    loadUserSymbolFile (".." G_DIR_SEPARATOR_S "data" G_DIR_SEPARATOR_S
                        "usersymbol.txt") ||
        loadUserSymbolFile (path) ||
        loadUserSymbolFile (PKGDATADIR G_DIR_SEPARATOR_S "usersymbol.txt");
    g_free(path);
}

UserSymbolListAllSection::~UserSymbolListAllSection ()
{
}

gboolean
UserSymbolListAllSection::loadUserSymbolFile (const gchar * filename)
{
    gboolean retval = m_user_symbols.loadFromFile (filename);
    return retval;
}

bool
UserSymbolListAllSection::initCandidates (zhuyin_instance_t * instance,
                                          const String & lookup)
{
    if (!lookup)
        return false;

    m_candidates.clear ();

    assert ("`" == lookup);
    m_lookup = lookup;

    /* cache the symbols. */
    const std::vector<String> & indexes = m_user_symbols.getIndexes ();
    for (size_t i = 0; i < indexes.size (); ++i) {
        m_candidates.push_back (indexes[i]);
    }

    return true;
}

bool
UserSymbolListAllSection::fillLookupTableByPage ()
{
    LookupTable & lookup_table = getLookupTable ();

    guint len = m_candidates.size ();

    guint filled_nr = lookup_table.size ();
    guint page_size = lookup_table.pageSize ();

    /* fill lookup table by libzhuyin get candidates. */
    guint need_nr = MIN (page_size, len - filled_nr);
    g_assert (need_nr >=0);
    if (need_nr == 0)
        return FALSE;

    for (guint i = filled_nr; i < filled_nr + need_nr; i++) {
        if (i >= len)  /* no more candidates */
            break;

        Text text (m_candidates[i]);
        lookup_table.appendCandidate (text);
    }

    return TRUE;
}

int
UserSymbolListAllSection::selectCandidate (guint index)
{
    if (index >= m_candidates.size ())
        return 0;

    m_choice = m_user_symbols.find (m_candidates[index]);
    return m_choice.utf8Length ();
}

};