~ubuntu-branches/ubuntu/precise/fcitx-sunpinyin/precise

« back to all changes in this revision

Viewing changes to src/eim.cpp

  • Committer: Package Import Robot
  • Author(s): Aron Xu, UPSTREAM
  • Date: 2011-10-21 16:42:11 UTC
  • mfrom: (1.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20111021164211-w3aoorjcs609kxop
Tags: 0.3.2-2
debian/patches/01-fix_shuangpin_fuzzy.patch:
[UPSTREAM] Fix a problem in the fuzzy functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  Copyright (C) 2010~2010 by CSSlayer
2
 
    wengxt@gmail.com 
3
 
 
4
 
   This program is free software: you can redistribute it and/or modify
5
 
   it under the terms of the GNU General Public License as published by
6
 
   the Free Software Foundation; either version 3 of the License, or
7
 
   (at your option) any later version.
8
 
 
9
 
   This program is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
   GNU General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU General Public License
15
 
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2010~2010 by CSSlayer                                   *
 
3
 *   wengxt@gmail.com                                                      *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
16
20
 
17
21
#include <stdlib.h>
18
 
#include <fcitx/im.h>
19
22
#include <stdio.h>
20
23
#include <string.h>
21
24
#include <errno.h>
22
 
#include <fcitx-config/hotkey.h>
23
25
#include <ime-core/imi_view.h>
24
26
#include <ime-core/imi_options.h>
25
27
#include <ime-core/utils.h>
 
28
#include <fcitx/ime.h>
 
29
#include <fcitx-config/hotkey.h>
26
30
#include <fcitx-config/xdg.h>
 
31
#include <fcitx-utils/log.h>
 
32
#include <fcitx-config/fcitx-config.h>
 
33
#include <fcitx-utils/utils.h>
 
34
#include <fcitx/instance.h>
 
35
#include <fcitx/keys.h>
 
36
#include <fcitx/module.h>
27
37
#include <string>
28
38
#include <libintl.h>
29
39
 
30
40
#include "handler.h"
31
41
#include "eim.h"
32
42
 
33
 
#define _(x) (x)
 
43
#define FCITX_SUNPINYIN_MAX(x, y) ((x) > (y)? (x) : (y))
34
44
 
35
45
#ifdef __cplusplus
36
46
extern "C" {
37
47
#endif
38
 
    EXTRA_IM EIM = {
39
 
        _("Sunpinyin"), /* Name */
40
 
        "fcitx-sunpinyin", /* IconName */
41
 
        Reset, /* Reset */
42
 
        DoInput, /* DoInput */
43
 
        GetCandWords, /* GetCandWords */
44
 
        GetCandWord,
45
 
        Init,
46
 
        Destroy,
47
 
        NULL,
48
 
        NULL,
49
 
        NULL,
50
 
        NULL,
51
 
        NULL,
52
 
        0,
53
 
        0,
54
 
        0,
55
 
        0,
56
 
        0,
57
 
        0,
58
 
        0,
59
 
        NULL,
60
 
        NULL
 
48
    FCITX_EXPORT_API
 
49
    FcitxIMClass ime = {
 
50
        FcitxSunpinyinCreate,
 
51
        FcitxSunpinyinDestroy
61
52
    };
 
53
 
 
54
    FCITX_EXPORT_API
 
55
    int ABI_VERSION = FCITX_ABI_VERSION;
62
56
#ifdef __cplusplus
63
57
}
64
58
#endif
65
59
 
66
 
static ConfigFileDesc* GetSunpinyinConfigDesc();
67
 
static void LoadConfig(Bool reload = False);
68
 
static void SaveConfig();
69
 
 
70
 
static FcitxWindowHandler* instance = NULL;
71
 
static CIMIView* view = NULL;
72
 
static ConfigFileDesc* sunpinyinConfigDesc;
73
 
static FcitxSunpinyinConfig fs;
 
60
CONFIG_DESC_DEFINE(GetSunpinyinConfigDesc, "fcitx-sunpinyin.desc")
 
61
 
 
62
boolean LoadSunpinyinConfig(FcitxSunpinyinConfig* fs);
 
63
static void SaveSunpinyinConfig(FcitxSunpinyinConfig* fs);
 
64
static void ConfigSunpinyin(FcitxSunpinyin* sunpinyin);
 
65
static void* SunpinyinGetFullPinyin(void* arg, FcitxModuleFunctionArg args);
 
66
static INPUT_RETURN_VALUE FcitxSunpinyinDeleteCandidate (FcitxSunpinyin* sunpinyin, CandidateWord* candWord);
 
67
 
74
68
 
75
69
static const char* fuzzyPairs[][2] = {
76
70
    {"sh", "s"},
103
97
 *
104
98
 **/
105
99
__EXPORT_API
106
 
void Reset (void)
 
100
void FcitxSunpinyinReset (void* arg)
107
101
{
108
 
    GenericConfig *profile = (GenericConfig*) EIM.profile;
109
 
    ConfigValueType corner = ConfigGetBindValue(profile, "Profile", "Corner");
110
 
    ConfigValueType punc = ConfigGetBindValue(profile, "Profile", "ChnPunc");
111
 
    view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, *corner.boolean);
112
 
    view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, *punc.boolean);
113
 
    view->clearIC();
 
102
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
103
    FcitxUIStatus* puncStatus = GetUIStatus(sunpinyin->owner, "punc");
 
104
    FcitxUIStatus* fullwidthStatus = GetUIStatus(sunpinyin->owner, "fullwidth");
 
105
    sunpinyin->view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, fullwidthStatus->getCurrentStatus(fullwidthStatus->arg));
 
106
    sunpinyin->view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, puncStatus->getCurrentStatus(puncStatus->arg));
 
107
    sunpinyin->view->clearIC();
114
108
}
115
109
 
116
110
/**
122
116
 * @return INPUT_RETURN_VALUE
123
117
 **/
124
118
__EXPORT_API
125
 
INPUT_RETURN_VALUE DoInput (unsigned int keycode, unsigned int state, int count)
 
119
INPUT_RETURN_VALUE FcitxSunpinyinDoInput(void* arg, FcitxKeySym sym, unsigned int state)
126
120
{
127
 
    if ((keycode <= 0x20 || keycode > 0x7E) && view->getIC()->isEmpty())
128
 
        return IRV_TO_PROCESS;
129
 
    
130
 
    if (keycode == 0x003b && view->getIC()->isEmpty())
131
 
        return IRV_TO_PROCESS;         
132
 
 
133
 
    if (keycode == 0xFF8D)
134
 
        keycode = 0xFF0D;
135
 
 
136
 
    instance->commit_flag = false;
137
 
    instance->candidate_flag = false;
138
 
    unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
139
 
 
140
 
    if (instance->commit_flag)
141
 
        return IRV_GET_CANDWORDS;
 
121
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
122
    FcitxInputState* input = FcitxInstanceGetInputState(sunpinyin->owner);
 
123
    CIMIView* view = sunpinyin->view;
 
124
    FcitxWindowHandler* windowHandler = sunpinyin->windowHandler;
 
125
    FcitxSunpinyinConfig* fs = &sunpinyin->fs;
 
126
    FcitxConfig* config = FcitxInstanceGetConfig(sunpinyin->owner);
 
127
    CandidateWordSetChoose(FcitxInputStateGetCandidateList(input), DIGIT_STR_CHOOSE);
 
128
 
 
129
    int chooseKey = CheckChooseKey(sym, KEY_NONE, DIGIT_STR_CHOOSE);
 
130
    if (state == KEY_CTRL_ALT_COMP && chooseKey >= 0)
 
131
    {
 
132
        CandidateWord* candidateWord = CandidateWordGetByIndex(FcitxInputStateGetCandidateList(input), chooseKey);
 
133
        return FcitxSunpinyinDeleteCandidate(sunpinyin, candidateWord);
 
134
    }
 
135
 
 
136
    if ( (!IsHotKeySimple(sym, state) || IsHotKey(sym, state, FCITX_SPACE)) && view->getIC()->isEmpty())
 
137
        return IRV_TO_PROCESS;
 
138
 
 
139
    /* there is some special case that ';' is used */
 
140
    if (IsHotKey(sym, state, FCITX_SEMICOLON) &&
 
141
        !(!view->getIC()->isEmpty() && fs->bUseShuangpin && (fs->SPScheme == MS2003 || fs->SPScheme == ZIGUANG)))
 
142
        return IRV_TO_PROCESS;
 
143
 
 
144
    if (IsHotKey(sym, state, FCITX_SEPARATOR) &&
 
145
        view->getIC()->isEmpty())
 
146
        return IRV_TO_PROCESS;
 
147
 
 
148
    if (sym == Key_KP_Enter)
 
149
        sym = Key_Return;
 
150
 
 
151
    if (IsHotKeyDigit(sym, state))
 
152
        return IRV_TO_PROCESS;
 
153
 
 
154
    if (IsHotKey(sym, state, FCITX_SPACE))
 
155
        return CandidateWordChooseByIndex(FcitxInputStateGetCandidateList(input), 0);
 
156
 
 
157
    if (!IsHotKeyUAZ(sym, state)
 
158
        && !IsHotKeyLAZ(sym, state)
 
159
        && !IsHotKey(sym, state, FCITX_SEMICOLON)
 
160
        && !IsHotKey(sym, state, FCITX_BACKSPACE)
 
161
        && !IsHotKey(sym, state, FCITX_DELETE)
 
162
        && !IsHotKey(sym, state, FCITX_ENTER)
 
163
        && !IsHotKey(sym, state, FCITX_LEFT)
 
164
        && !IsHotKey(sym, state, FCITX_RIGHT)
 
165
        && !IsHotKey(sym, state, FCITX_HOME)
 
166
        && !IsHotKey(sym, state, FCITX_END)
 
167
        && !IsHotKey(sym, state, FCITX_SEPARATOR)
 
168
        )
 
169
        return IRV_TO_PROCESS;
 
170
 
 
171
    if (IsHotKey(sym, state, config->hkPrevPage) || IsHotKey(sym, state, config->hkNextPage))
 
172
        return IRV_TO_PROCESS;
 
173
 
 
174
    windowHandler->commit_flag = false;
 
175
    windowHandler->candidate_flag = false;
 
176
    unsigned int changeMasks = view->onKeyEvent(CKeyEvent(sym, sym, state));
 
177
 
 
178
    if (windowHandler->commit_flag)
 
179
        return IRV_COMMIT_STRING;
142
180
    if (!(changeMasks & CIMIView::KEYEVENT_USED))
143
181
        return IRV_TO_PROCESS;
144
 
    
 
182
 
145
183
    if (view->getIC()->isEmpty())
146
184
        return IRV_CLEAN;
147
185
 
148
 
    if (instance->candidate_flag)
 
186
    if (windowHandler->candidate_flag)
149
187
    {
150
188
        return IRV_DISPLAY_CANDWORDS;
151
189
    }
152
190
 
153
 
    return IRV_TO_PROCESS;
154
 
}
 
191
    return IRV_DO_NOTHING;
 
192
}
 
193
 
 
194
boolean FcitxSunpinyinInit(void* arg)
 
195
{
 
196
    return true;
 
197
}
 
198
 
155
199
 
156
200
/**
157
201
 * @brief function DoInput has done everything for us.
160
204
 * @return INPUT_RETURN_VALUE
161
205
 **/
162
206
__EXPORT_API
163
 
INPUT_RETURN_VALUE GetCandWords(SEARCH_MODE searchMode)
 
207
INPUT_RETURN_VALUE FcitxSunpinyinGetCandWords(void* arg)
164
208
{
165
 
    return IRV_DO_NOTHING;
 
209
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin* )arg;
 
210
    FcitxInstance* instance = sunpinyin->owner;
 
211
    FcitxInputState* input = FcitxInstanceGetInputState(instance);
 
212
 
 
213
    CPreEditString ppd;
 
214
    sunpinyin->view->getPreeditString(ppd);
 
215
    TIConvSrcPtr src = (TIConvSrcPtr) (ppd.string());
 
216
    
 
217
    int hzlen = 0;    
 
218
    while (hzlen < ppd.charTypeSize())
 
219
    {
 
220
        if (! (ppd.charTypeAt(hzlen) & IPreeditString::USER_CHOICE))
 
221
            break;
 
222
        hzlen ++ ;
 
223
    }
 
224
    
 
225
    CleanInputWindowUp(instance);
 
226
 
 
227
    memcpy(sunpinyin->front_src, src, ppd.caret() * sizeof(TWCHAR));
 
228
    memcpy(sunpinyin->end_src, src + ppd.caret() * sizeof(TWCHAR),
 
229
           (ppd.size() - ppd.caret() + 1) * sizeof(TWCHAR));
 
230
    memcpy(sunpinyin->input_src, src, hzlen * sizeof(TWCHAR));
 
231
    
 
232
    FcitxLog(INFO, "%d", ppd.candi_start());
 
233
 
 
234
    sunpinyin->front_src[ppd.caret()] = 0;
 
235
    sunpinyin->end_src[ppd.size() - ppd.caret() + 1] = 0;
 
236
    sunpinyin->input_src[hzlen] = 0;
 
237
 
 
238
    memset(sunpinyin->clientpreedit, 0, FCITX_SUNPINYIN_MAX(hzlen * UTF8_MAX_LENGTH + 1, MAX_USER_INPUT + 1));
 
239
    WCSTOMBS(sunpinyin->clientpreedit, sunpinyin->input_src, MAX_USER_INPUT);
 
240
    AddMessageAtLast(FcitxInputStateGetClientPreedit(input), MSG_INPUT, "%s", sunpinyin->clientpreedit);
 
241
    FcitxInputStateSetClientCursorPos(input, 0);
 
242
 
 
243
    memset(sunpinyin->preedit, 0, FCITX_SUNPINYIN_MAX(ppd.size() * UTF8_MAX_LENGTH + 1, MAX_USER_INPUT + 1));
 
244
    WCSTOMBS(sunpinyin->preedit, sunpinyin->front_src, MAX_USER_INPUT);
 
245
    FcitxInputStateSetCursorPos(input, strlen(sunpinyin->preedit));
 
246
    WCSTOMBS(&sunpinyin->preedit[strlen(sunpinyin->preedit)], sunpinyin->end_src, MAX_USER_INPUT);
 
247
    
 
248
    FcitxInputStateSetShowCursor(input, true);
 
249
    
 
250
    AddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_INPUT, "%s", sunpinyin->preedit);
 
251
 
 
252
    CCandidateList pcl;
 
253
    sunpinyin->view->getCandidateList(pcl, 0, sunpinyin->candNum);
 
254
    for (int i = 0; i < pcl.size(); i ++ )
 
255
    {
 
256
        const TWCHAR* pcand = pcl.candiString(i);
 
257
        if (pcand == NULL)
 
258
            continue;
 
259
 
 
260
        int *index = (int*) fcitx_malloc0(sizeof(int));
 
261
        *index = i;
 
262
        CandidateWord candWord;
 
263
        candWord.callback = FcitxSunpinyinGetCandWord;
 
264
        candWord.owner = sunpinyin;
 
265
        candWord.priv = index;
 
266
        candWord.strExtra = NULL;
 
267
 
 
268
        wstring cand_str = pcand;
 
269
        TIConvSrcPtr src = (TIConvSrcPtr)(cand_str.c_str());
 
270
        WCSTOMBS(sunpinyin->ubuf, (const TWCHAR*) src, MAX_CAND_LEN);
 
271
 
 
272
        candWord.strWord = strdup(sunpinyin->ubuf);
 
273
 
 
274
        CandidateWordAppend(FcitxInputStateGetCandidateList(input), &candWord);
 
275
 
 
276
        if (i == 0)
 
277
        {
 
278
            AddMessageAtLast(FcitxInputStateGetClientPreedit(input), MSG_INPUT, "%s", candWord.strWord);
 
279
        }
 
280
 
 
281
    }
 
282
    return IRV_DISPLAY_CANDWORDS;
166
283
}
167
284
 
168
285
/**
172
289
 * @return the string of canidate word
173
290
 **/
174
291
__EXPORT_API
175
 
char *GetCandWord (int iIndex)
 
292
INPUT_RETURN_VALUE FcitxSunpinyinGetCandWord (void* arg, CandidateWord* candWord)
176
293
{
177
 
    EIM.CandWordCount = 0;
178
 
    instance->commit_flag = false;
179
 
    instance->candidate_flag = false;
180
 
    if (iIndex <= 8)
181
 
    {
182
 
        unsigned int keycode = '1' + iIndex;
183
 
        unsigned int state = 0;
184
 
        unsigned int changeMasks = view->onKeyEvent(CKeyEvent(keycode, keycode, state));
185
 
 
186
 
        if (instance->commit_flag)
187
 
            return EIM.StringGet;
188
 
    }
189
 
    
190
 
    return NULL;
 
294
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin* )arg;
 
295
    sunpinyin->windowHandler->commit_flag = false;
 
296
    sunpinyin->windowHandler->candidate_flag = false;
 
297
    int* index = (int*) candWord->priv;
 
298
    sunpinyin->view->onCandidateSelectRequest(*index);
 
299
 
 
300
    if (sunpinyin->windowHandler->commit_flag)
 
301
        return IRV_COMMIT_STRING;
 
302
 
 
303
    if (sunpinyin->windowHandler->candidate_flag)
 
304
        return IRV_DISPLAY_CANDWORDS;
 
305
 
 
306
    return IRV_DO_NOTHING;
191
307
}
192
308
 
193
309
/**
197
313
 * @return successful or not
198
314
 **/
199
315
__EXPORT_API
200
 
int Init (char *arg)
 
316
void* FcitxSunpinyinCreate (FcitxInstance* instance)
201
317
{
 
318
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) fcitx_malloc0(sizeof(FcitxSunpinyin));
 
319
    FcitxAddon* addon = GetAddonByName(FcitxInstanceGetAddons(instance), "fcitx-sunpinyin");
202
320
    bindtextdomain("fcitx-sunpinyin", LOCALEDIR);
203
 
    GenericConfig *fc = (GenericConfig*)EIM.fc;
204
 
    ConfigValueType candword;
205
 
    ConfigValueType prevpage;
206
 
    ConfigValueType nextpage;
207
 
 
208
 
    LoadConfig();
209
 
 
210
 
    candword = ConfigGetBindValue(fc, "Appearance", "CandidateWordNumber");
211
 
    prevpage = ConfigGetBindValue(fc, "Hotkey", "PrevPageKey");
212
 
    nextpage = ConfigGetBindValue(fc, "Hotkey", "NextPageKey");
213
 
 
 
321
    sunpinyin->owner = instance;
 
322
    FcitxSunpinyinConfig* fs = &sunpinyin->fs;
 
323
 
 
324
    if (!LoadSunpinyinConfig(&sunpinyin->fs))
 
325
    {
 
326
        free(sunpinyin);
 
327
        return NULL;
 
328
    }
214
329
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
215
330
 
216
 
    if (fs.bUseShuangpin)
 
331
    if (fs->bUseShuangpin)
217
332
        fac.setPinyinScheme(CSunpinyinSessionFactory::SHUANGPIN);
218
333
    else
219
334
        fac.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
220
335
 
221
 
    AShuangpinSchemePolicy::instance().setShuangpinType(fs.SPScheme);
222
 
    AQuanpinSchemePolicy::instance().setFuzzySegmentation(fs.bFuzzySegmentation);
223
 
    AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(fs.bFuzzyInnerSegmentation);
224
 
    view = fac.createSession();
225
 
 
226
 
    instance = new FcitxWindowHandler();
227
 
    view->getIC()->setCharsetLevel(1);// GBK
228
 
 
229
 
    view->setCandiWindowSize(*candword.integer);
230
 
    view->attachWinHandler(instance);
231
 
    // page up/down key
232
 
    CHotkeyProfile* prof = view->getHotkeyProfile();
233
 
    prof->clear();
234
 
 
 
336
    ConfigSunpinyin(sunpinyin);
 
337
    sunpinyin->bShuangpin = fs->bUseShuangpin;
 
338
 
 
339
    sunpinyin->view = fac.createSession();
 
340
 
 
341
    if (sunpinyin->view == NULL)
 
342
    {
 
343
        free(sunpinyin);
 
344
        return NULL;
 
345
    }
 
346
 
 
347
    FcitxWindowHandler* windowHandler = new FcitxWindowHandler();
 
348
    sunpinyin->windowHandler = windowHandler;
 
349
    sunpinyin->view->getIC()->setCharsetLevel(3);
 
350
 
 
351
    sunpinyin->view->attachWinHandler(windowHandler);
 
352
    sunpinyin->windowHandler->SetOwner(sunpinyin);
 
353
    ConfigSunpinyin(sunpinyin);
 
354
 
 
355
    FcitxRegisterIMv2(instance,
 
356
                    sunpinyin,
 
357
                    "sunpinyin",
 
358
                    _("Sunpinyin"),
 
359
                    "sunpinyin",
 
360
                    FcitxSunpinyinInit,
 
361
                    FcitxSunpinyinReset,
 
362
                    FcitxSunpinyinDoInput,
 
363
                    FcitxSunpinyinGetCandWords,
 
364
                    NULL,
 
365
                    NULL,
 
366
                    ReloadConfigFcitxSunpinyin,
 
367
                    NULL,
 
368
                    fs->iSunpinyinPriority,
 
369
                    "zh_CN"
 
370
                   );
 
371
 
 
372
    AddFunction(addon, (void*) SunpinyinGetFullPinyin);
 
373
 
 
374
    return sunpinyin;
 
375
}
 
376
 
 
377
/**
 
378
 * @brief Destroy the input method while unload it.
 
379
 *
 
380
 * @return int
 
381
 **/
 
382
__EXPORT_API
 
383
void FcitxSunpinyinDestroy (void* arg)
 
384
{
 
385
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
386
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
 
387
    fac.destroySession(sunpinyin->view);
 
388
    if (sunpinyin->shuangpin_data)
 
389
        delete sunpinyin->shuangpin_data;
 
390
 
 
391
    if (sunpinyin->windowHandler)
 
392
        delete sunpinyin->windowHandler;
 
393
 
 
394
    free(arg);
 
395
}
 
396
 
 
397
INPUT_RETURN_VALUE FcitxSunpinyinDeleteCandidate (FcitxSunpinyin* sunpinyin, CandidateWord* candWord)
 
398
{
 
399
/*
 
400
        if (candWord->owner == sunpinyin)
 
401
    {
 
402
        CCandidateList pcl;
 
403
        sunpinyin->view->getCandidateList(pcl, 0, sunpinyin->candNum);
 
404
        int* index = (int*) candWord->priv;
 
405
        CIMIClassicView* classicView = (CIMIClassicView*) sunpinyin->view;
 
406
        unsigned int mask;
 
407
        classicView->deleteCandidate(*index, mask);
 
408
        return IRV_DISPLAY_CANDWORDS;
 
409
    }
 
410
*/
 
411
    return IRV_TO_PROCESS;
 
412
}
 
413
 
 
414
/**
 
415
 * @brief Load the config file for fcitx-sunpinyin
 
416
 *
 
417
 * @param Bool is reload or not
 
418
 **/
 
419
boolean LoadSunpinyinConfig(FcitxSunpinyinConfig* fs)
 
420
{
 
421
    ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
 
422
    if (!configDesc)
 
423
        return false;
 
424
 
 
425
    FILE *fp = GetXDGFileUserWithPrefix("conf", "fcitx-sunpinyin.config", "rt", NULL);
 
426
 
 
427
    if (!fp)
 
428
    {
 
429
        if (errno == ENOENT)
 
430
            SaveSunpinyinConfig(fs);
 
431
    }
 
432
    ConfigFile *cfile = ParseConfigFileFp(fp, configDesc);
 
433
 
 
434
    FcitxSunpinyinConfigConfigBind(fs, cfile, configDesc);
 
435
    ConfigBindSync(&fs->gconfig);
 
436
 
 
437
    if (fp)
 
438
        fclose(fp);
 
439
    return true;
 
440
}
 
441
 
 
442
void ConfigSunpinyin(FcitxSunpinyin* sunpinyin)
 
443
{
 
444
    FcitxInstance* instance = sunpinyin->owner;
 
445
    FcitxConfig* config = FcitxInstanceGetConfig(instance);
 
446
    FcitxSunpinyinConfig *fs = &sunpinyin->fs;
235
447
    int i = 0;
236
 
    for (i = 0 ; i < 2; i++)
 
448
 
 
449
    if (sunpinyin->view)
237
450
    {
238
 
        if (prevpage.hotkey[i].sym)
239
 
            prof->addPageUpKey(CKeyEvent(prevpage.hotkey[i].sym, 0, prevpage.hotkey[i].state));
240
 
        if (nextpage.hotkey[i].sym)
241
 
            prof->addPageDownKey(CKeyEvent(nextpage.hotkey[i].sym, 0, nextpage.hotkey[i].state));
 
451
        sunpinyin->view->setCandiWindowSize(2048);
 
452
        // page up/down key
 
453
        CHotkeyProfile* prof = sunpinyin->view->getHotkeyProfile();
 
454
        prof->clear();
 
455
 
 
456
        for (i = 0 ; i < 2; i++)
 
457
        {
 
458
            if (config->hkPrevPage[i].sym)
 
459
                prof->addPageUpKey(CKeyEvent(config->hkPrevPage[i].sym, 0, config->hkPrevPage[i].state));
 
460
            if (config->hkNextPage[i].sym)
 
461
                prof->addPageDownKey(CKeyEvent(config->hkNextPage[i].sym, 0, config->hkNextPage[i].state));
 
462
        }
 
463
        sunpinyin->view->setCancelOnBackspace(1);
242
464
    }
243
 
    
 
465
 
244
466
    string_pairs fuzzy, correction;
245
467
    for (i = 0; i < FUZZY_SIZE; i++)
246
 
        if (fs.bFuzzy[i])
 
468
        if (fs->bFuzzy[i])
247
469
            fuzzy.push_back(std::make_pair<std::string, std::string>(fuzzyPairs[i][0], fuzzyPairs[i][1]));
248
 
    
 
470
 
249
471
    for (i = 0; i < CORRECT_SIZE; i++)
250
 
        if (fs.bAutoCorrecting[i])
 
472
        if (fs->bAutoCorrecting[i])
251
473
            correction.push_back(std::make_pair<std::string, std::string>(correctionPairs[i][0], correctionPairs[i][1]));
252
 
    
 
474
 
253
475
    if (fuzzy.size() != 0)
254
476
    {
255
477
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(true);
256
478
        AQuanpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
 
479
        AShuangpinSchemePolicy::instance().setFuzzyForwarding(true);
 
480
        AShuangpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
257
481
    }
258
482
    else
259
483
    {
260
484
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(false);
261
485
        AQuanpinSchemePolicy::instance().clearFuzzyPinyinPairs();
 
486
        AShuangpinSchemePolicy::instance().setFuzzyForwarding(false);
 
487
        AShuangpinSchemePolicy::instance().clearFuzzyPinyinPairs();
262
488
    }
263
 
    
 
489
 
264
490
    if (correction.size() != 0)
265
491
    {
266
492
        AQuanpinSchemePolicy::instance().setAutoCorrecting(true);
268
494
    }
269
495
    else
270
496
        AQuanpinSchemePolicy::instance().setAutoCorrecting(false);
271
 
        
272
 
    view->setCancelOnBackspace(1);
273
 
    instance->set_eim(&EIM);
274
 
 
275
 
    return 0;
276
 
}
277
 
 
278
 
 
279
 
/**
280
 
 * @brief Destroy the input method while unload it.
281
 
 *
282
 
 * @return int
283
 
 **/
284
 
__EXPORT_API
285
 
int Destroy (void)
286
 
{
287
 
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
288
 
    fac.destroySession(view);
289
 
 
290
 
    if (instance)
291
 
        delete instance;
292
 
 
293
 
    return 0;
294
 
}
295
 
 
296
 
/**
297
 
 * @brief Get the config description of fcitx-sunpinyin.
298
 
 *
299
 
 * @return ConfigFileDesc*
300
 
 **/
301
 
ConfigFileDesc* GetSunpinyinConfigDesc()
302
 
{
303
 
    if (!sunpinyinConfigDesc)
304
 
    {
305
 
        FILE *tmpfp;
306
 
        tmpfp = GetXDGFileData("addon/fcitx-sunpinyin.desc", "r", NULL);
307
 
        sunpinyinConfigDesc = ParseConfigFileDescFp(tmpfp);
308
 
        fclose(tmpfp);
309
 
    }
310
 
 
311
 
    return sunpinyinConfigDesc;
312
 
}
313
 
 
314
 
/**
315
 
 * @brief Load the config file for fcitx-sunpinyin
316
 
 *
317
 
 * @param Bool is reload or not
318
 
 **/
319
 
void LoadConfig(Bool reload)
320
 
{
321
 
    ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
322
 
 
323
 
    FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "rt", NULL);
324
 
 
325
 
    if (!fp)
326
 
    {
327
 
        if (!reload && errno == ENOENT)
328
 
        {
329
 
            char *lastdomain = strdup(textdomain(NULL));
330
 
            textdomain("fcitx-sunpinyin");
331
 
            SaveConfig();
332
 
            textdomain(lastdomain);
333
 
            free(lastdomain);
334
 
            LoadConfig(True);
335
 
        }
336
 
        return;
337
 
    }
338
 
    ConfigFile *cfile = ParseConfigFileFp(fp, configDesc);
339
 
 
340
 
    if (cfile)
341
 
    {
342
 
        FcitxSunpinyinConfigConfigBind(&fs, cfile, configDesc);
343
 
        ConfigBindSync((GenericConfig*)&fs);
344
 
    }
345
 
    else
346
 
    {
347
 
        fs.bUseShuangpin = False;
348
 
        fs.SPScheme = MS2003;
349
 
        fs.bFuzzySegmentation = False;
350
 
        fs.bFuzzyInnerSegmentation = False;
351
 
        int i = 0;
352
 
        for (i = 0; i < FUZZY_SIZE; i ++)
353
 
            fs.bFuzzy[i] = False;
354
 
        
355
 
        for (i = 0; i < CORRECT_SIZE; i ++)
356
 
            fs.bAutoCorrecting[i] = False;
357
 
    }
358
 
 
 
497
 
 
498
    if (sunpinyin->shuangpin_data == NULL)
 
499
        sunpinyin->shuangpin_data = new CShuangpinData(fs->SPScheme);
 
500
    AShuangpinSchemePolicy::instance().setShuangpinType(fs->SPScheme);
 
501
    AQuanpinSchemePolicy::instance().setFuzzySegmentation(fs->bFuzzySegmentation);
 
502
    AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(fs->bFuzzyInnerSegmentation);
 
503
}
 
504
 
 
505
__EXPORT_API void ReloadConfigFcitxSunpinyin(void* arg)
 
506
{
 
507
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
508
    LoadSunpinyinConfig(&sunpinyin->fs);
 
509
    ConfigSunpinyin(sunpinyin);
359
510
}
360
511
 
361
512
/**
363
514
 *
364
515
 * @return void
365
516
 **/
366
 
void SaveConfig()
 
517
void SaveSunpinyinConfig(FcitxSunpinyinConfig* fs)
367
518
{
368
519
    ConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
369
 
    FILE *fp = GetXDGFileUser( "addon/fcitx-sunpinyin.config", "wt", NULL);
370
 
    SaveConfigFileFp(fp, fs.gconfig.configFile, configDesc);
371
 
    fclose(fp);
372
 
}
 
520
    FILE *fp = GetXDGFileUserWithPrefix("conf", "fcitx-sunpinyin.config", "wt", NULL);
 
521
    SaveConfigFileFp(fp, &fs->gconfig, configDesc);
 
522
    if (fp)
 
523
        fclose(fp);
 
524
}
 
525
 
 
526
void* SunpinyinGetFullPinyin(void* arg, FcitxModuleFunctionArg args)
 
527
{
 
528
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
529
    char* pinyin = (char*) args.args[0];
 
530
    boolean *issp = (boolean*) args.args[1];
 
531
    *issp = sunpinyin->bShuangpin;
 
532
    CMappedYin syls;
 
533
    if (sunpinyin->bShuangpin)
 
534
    {
 
535
        sunpinyin->shuangpin_data->getMapString(pinyin, syls);
 
536
        if (syls.size() == 0)
 
537
            return NULL;
 
538
        else
 
539
            return strdup(syls[0].c_str());
 
540
    }
 
541
    else
 
542
        return NULL;
 
543
}
 
544
 
 
545
// kate: indent-mode cstyle; space-indent on; indent-width 0;