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

« back to all changes in this revision

Viewing changes to .pc/0001-01-lower_sunpinyin_version.patch.patch/src/eim.cpp

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-02-05 21:23:55 UTC
  • mfrom: (1.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120205212355-dby0rt6cbdrfdk07
Tags: 0.3.3-1
New upstream release: fcitx 4.2.0. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <stdlib.h>
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
#include <errno.h>
 
25
#include <ime-core/imi_view.h>
 
26
#include <ime-core/imi_options.h>
 
27
#include <ime-core/utils.h>
 
28
#include <fcitx/ime.h>
 
29
#include <fcitx-config/hotkey.h>
 
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>
 
37
#include <fcitx/context.h>
 
38
#include <string>
 
39
#include <libintl.h>
 
40
 
 
41
#include "handler.h"
 
42
#include "eim.h"
 
43
 
 
44
#define FCITX_SUNPINYIN_MAX(x, y) ((x) > (y)? (x) : (y))
 
45
 
 
46
#ifdef __cplusplus
 
47
extern "C" {
 
48
#endif
 
49
    FCITX_EXPORT_API
 
50
    FcitxIMClass ime = {
 
51
        FcitxSunpinyinCreate,
 
52
        FcitxSunpinyinDestroy
 
53
    };
 
54
 
 
55
    FCITX_EXPORT_API
 
56
    int ABI_VERSION = FCITX_ABI_VERSION;
 
57
#ifdef __cplusplus
 
58
}
 
59
#endif
 
60
 
 
61
CONFIG_DESC_DEFINE(GetSunpinyinConfigDesc, "fcitx-sunpinyin.desc")
 
62
 
 
63
boolean LoadSunpinyinConfig(FcitxSunpinyinConfig* fs);
 
64
static void SaveSunpinyinConfig(FcitxSunpinyinConfig* fs);
 
65
static void ConfigSunpinyin(FcitxSunpinyin* sunpinyin);
 
66
static void* SunpinyinGetFullPinyin(void* arg, FcitxModuleFunctionArg args);
 
67
static INPUT_RETURN_VALUE FcitxSunpinyinDeleteCandidate (FcitxSunpinyin* sunpinyin, FcitxCandidateWord* candWord);
 
68
 
 
69
 
 
70
static const char* fuzzyPairs[][2] = {
 
71
    {"sh", "s"},
 
72
    {"zh", "z"},
 
73
    {"ch", "c"},
 
74
    {"an", "ang"},
 
75
    {"on", "ong"},
 
76
    {"en", "eng"},
 
77
    {"in", "ing"},
 
78
    {"eng", "ong"},
 
79
    {"ian", "iang"},
 
80
    {"uan", "uang"},
 
81
    {"n", "l"},
 
82
    {"f", "h"},
 
83
    {"l", "r"},
 
84
    {"k", "g"}
 
85
};
 
86
 
 
87
static const char *correctionPairs[][2] = {
 
88
    {"ign", "ing"},
 
89
    {"ogn", "ong"},
 
90
    {"uen", "un"},
 
91
    {"img", "ing"},
 
92
    {"iou", "iu"},
 
93
    {"uei", "ui"}
 
94
};
 
95
 
 
96
/**
 
97
 * @brief Reset the status.
 
98
 *
 
99
 **/
 
100
__EXPORT_API
 
101
void FcitxSunpinyinReset (void* arg)
 
102
{
 
103
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
104
    FcitxUIStatus* puncStatus = FcitxUIGetStatusByName(sunpinyin->owner, "punc");
 
105
    FcitxUIStatus* fullwidthStatus = FcitxUIGetStatusByName(sunpinyin->owner, "fullwidth");
 
106
    sunpinyin->view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, fullwidthStatus->getCurrentStatus(fullwidthStatus->arg));
 
107
    sunpinyin->view->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, puncStatus->getCurrentStatus(puncStatus->arg));
 
108
    sunpinyin->view->clearIC();
 
109
}
 
110
 
 
111
/**
 
112
 * @brief Process Key Input and return the status
 
113
 *
 
114
 * @param keycode keycode from XKeyEvent
 
115
 * @param state state from XKeyEvent
 
116
 * @param count count from XKeyEvent
 
117
 * @return INPUT_RETURN_VALUE
 
118
 **/
 
119
__EXPORT_API
 
120
INPUT_RETURN_VALUE FcitxSunpinyinDoInput(void* arg, FcitxKeySym sym, unsigned int state)
 
121
{
 
122
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
123
    FcitxInputState* input = FcitxInstanceGetInputState(sunpinyin->owner);
 
124
    CIMIView* view = sunpinyin->view;
 
125
    FcitxWindowHandler* windowHandler = sunpinyin->windowHandler;
 
126
    FcitxSunpinyinConfig* fs = &sunpinyin->fs;
 
127
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(sunpinyin->owner);
 
128
    FcitxCandidateWordSetChoose(FcitxInputStateGetCandidateList(input), DIGIT_STR_CHOOSE);
 
129
 
 
130
    int chooseKey = FcitxHotkeyCheckChooseKey(sym, FcitxKeyState_None, DIGIT_STR_CHOOSE);
 
131
    if (state == FcitxKeyState_Ctrl_Alt && chooseKey >= 0)
 
132
    {
 
133
        FcitxCandidateWord* candidateWord = FcitxCandidateWordGetByIndex(FcitxInputStateGetCandidateList(input), chooseKey);
 
134
        return FcitxSunpinyinDeleteCandidate(sunpinyin, candidateWord);
 
135
    }
 
136
 
 
137
    if ( (!FcitxHotkeyIsHotKeySimple(sym, state) || FcitxHotkeyIsHotKey(sym, state, FCITX_SPACE)) && view->getIC()->isEmpty())
 
138
        return IRV_TO_PROCESS;
 
139
 
 
140
    /* there is some special case that ';' is used */
 
141
    if (FcitxHotkeyIsHotKey(sym, state, FCITX_SEMICOLON) &&
 
142
        !(!view->getIC()->isEmpty() && fs->bUseShuangpin && (fs->SPScheme == MS2003 || fs->SPScheme == ZIGUANG)))
 
143
        return IRV_TO_PROCESS;
 
144
 
 
145
    if (FcitxHotkeyIsHotKey(sym, state, FCITX_SEPARATOR) &&
 
146
        view->getIC()->isEmpty())
 
147
        return IRV_TO_PROCESS;
 
148
 
 
149
    if (sym == FcitxKey_KP_Enter)
 
150
        sym = FcitxKey_Return;
 
151
 
 
152
    if (FcitxHotkeyIsHotKeyDigit(sym, state))
 
153
        return IRV_TO_PROCESS;
 
154
 
 
155
    if (FcitxHotkeyIsHotKey(sym, state, FCITX_SPACE))
 
156
        return FcitxCandidateWordChooseByIndex(FcitxInputStateGetCandidateList(input), 0);
 
157
 
 
158
    if (!FcitxHotkeyIsHotKeyUAZ(sym, state)
 
159
        && !FcitxHotkeyIsHotKeyLAZ(sym, state)
 
160
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_SEMICOLON)
 
161
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE)
 
162
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_DELETE)
 
163
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_ENTER)
 
164
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_LEFT)
 
165
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_RIGHT)
 
166
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_HOME)
 
167
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_END)
 
168
        && !FcitxHotkeyIsHotKey(sym, state, FCITX_SEPARATOR)
 
169
        )
 
170
        return IRV_TO_PROCESS;
 
171
 
 
172
    if (FcitxHotkeyIsHotKey(sym, state, config->hkPrevPage) || FcitxHotkeyIsHotKey(sym, state, config->hkNextPage))
 
173
        return IRV_TO_PROCESS;
 
174
 
 
175
    windowHandler->commit_flag = false;
 
176
    windowHandler->candidate_flag = false;
 
177
    unsigned int changeMasks = view->onKeyEvent(CKeyEvent(sym, sym, state));
 
178
 
 
179
    if (windowHandler->commit_flag)
 
180
        return IRV_COMMIT_STRING;
 
181
    if (!(changeMasks & CIMIView::KEYEVENT_USED))
 
182
        return IRV_TO_PROCESS;
 
183
 
 
184
    if (view->getIC()->isEmpty())
 
185
        return IRV_CLEAN;
 
186
 
 
187
    if (windowHandler->candidate_flag)
 
188
    {
 
189
        return IRV_DISPLAY_CANDWORDS;
 
190
    }
 
191
 
 
192
    return IRV_DO_NOTHING;
 
193
}
 
194
 
 
195
boolean FcitxSunpinyinInit(void* arg)
 
196
{
 
197
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin* )arg;
 
198
    FcitxInstanceSetContext(sunpinyin->owner, CONTEXT_IM_KEYBOARD_LAYOUT, "us");
 
199
    return true;
 
200
}
 
201
 
 
202
 
 
203
/**
 
204
 * @brief function DoInput has done everything for us.
 
205
 *
 
206
 * @param searchMode
 
207
 * @return INPUT_RETURN_VALUE
 
208
 **/
 
209
__EXPORT_API
 
210
INPUT_RETURN_VALUE FcitxSunpinyinGetCandWords(void* arg)
 
211
{
 
212
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin* )arg;
 
213
    FcitxInstance* instance = sunpinyin->owner;
 
214
    FcitxInputState* input = FcitxInstanceGetInputState(instance);
 
215
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(sunpinyin->owner);
 
216
    FcitxCandidateWordSetPageSize(FcitxInputStateGetCandidateList(input), config->iMaxCandWord);
 
217
 
 
218
    CPreEditString ppd;
 
219
    sunpinyin->view->getPreeditString(ppd);
 
220
    TIConvSrcPtr src = (TIConvSrcPtr) (ppd.string());
 
221
    
 
222
    int hzlen = 0;    
 
223
    while (hzlen < ppd.charTypeSize())
 
224
    {
 
225
        if ((ppd.charTypeAt(hzlen) & IPreeditString::HANZI_CHAR) != IPreeditString::HANZI_CHAR)
 
226
            break;
 
227
        hzlen ++ ;
 
228
    }
 
229
    
 
230
    FcitxInstanceCleanInputWindowUp(instance);
 
231
 
 
232
    memcpy(sunpinyin->front_src, src, ppd.caret() * sizeof(TWCHAR));
 
233
    memcpy(sunpinyin->end_src, src + ppd.caret() * sizeof(TWCHAR),
 
234
           (ppd.size() - ppd.caret() + 1) * sizeof(TWCHAR));
 
235
    memcpy(sunpinyin->input_src, src, hzlen * sizeof(TWCHAR));
 
236
 
 
237
    sunpinyin->front_src[ppd.caret()] = 0;
 
238
    sunpinyin->end_src[ppd.size() - ppd.caret() + 1] = 0;
 
239
    sunpinyin->input_src[hzlen] = 0;
 
240
 
 
241
    memset(sunpinyin->clientpreedit, 0, FCITX_SUNPINYIN_MAX(hzlen * UTF8_MAX_LENGTH + 1, MAX_USER_INPUT + 1));
 
242
    WCSTOMBS(sunpinyin->clientpreedit, sunpinyin->input_src, MAX_USER_INPUT);
 
243
    FcitxMessagesAddMessageAtLast(FcitxInputStateGetClientPreedit(input), MSG_INPUT, "%s", sunpinyin->clientpreedit);
 
244
    FcitxInputStateSetClientCursorPos(input, 0);
 
245
 
 
246
    memset(sunpinyin->preedit, 0, FCITX_SUNPINYIN_MAX(ppd.size() * UTF8_MAX_LENGTH + 1, MAX_USER_INPUT + 1));
 
247
    WCSTOMBS(sunpinyin->preedit, sunpinyin->front_src, MAX_USER_INPUT);
 
248
    FcitxInputStateSetCursorPos(input, strlen(sunpinyin->preedit));
 
249
    WCSTOMBS(&sunpinyin->preedit[strlen(sunpinyin->preedit)], sunpinyin->end_src, MAX_USER_INPUT);
 
250
    
 
251
    FcitxInputStateSetShowCursor(input, true);
 
252
    
 
253
    FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_INPUT, "%s", sunpinyin->preedit);
 
254
 
 
255
    CCandidateList pcl;
 
256
    sunpinyin->view->getCandidateList(pcl, 0, sunpinyin->candNum);
 
257
    for (int i = 0; i < pcl.size(); i ++ )
 
258
    {
 
259
        const TWCHAR* pcand = pcl.candiString(i);
 
260
        if (pcand == NULL)
 
261
            continue;
 
262
 
 
263
        int *index = (int*) fcitx_utils_malloc0(sizeof(int));
 
264
        *index = i;
 
265
        FcitxCandidateWord candWord;
 
266
        candWord.callback = FcitxSunpinyinGetCandWord;
 
267
        candWord.owner = sunpinyin;
 
268
        candWord.priv = index;
 
269
        candWord.strExtra = NULL;
 
270
 
 
271
        wstring cand_str = pcand;
 
272
        TIConvSrcPtr src = (TIConvSrcPtr)(cand_str.c_str());
 
273
        WCSTOMBS(sunpinyin->ubuf, (const TWCHAR*) src, MAX_CAND_LEN);
 
274
 
 
275
        candWord.strWord = strdup(sunpinyin->ubuf);
 
276
        candWord.wordType = MSG_OTHER;
 
277
 
 
278
        FcitxCandidateWordAppend(FcitxInputStateGetCandidateList(input), &candWord);
 
279
 
 
280
        if (i == 0)
 
281
        {
 
282
            FcitxMessagesAddMessageAtLast(FcitxInputStateGetClientPreedit(input), MSG_INPUT, "%s", candWord.strWord);
 
283
        }
 
284
 
 
285
    }
 
286
    return IRV_DISPLAY_CANDWORDS;
 
287
}
 
288
 
 
289
/**
 
290
 * @brief get the candidate word by index
 
291
 *
 
292
 * @param iIndex index of candidate word
 
293
 * @return the string of canidate word
 
294
 **/
 
295
__EXPORT_API
 
296
INPUT_RETURN_VALUE FcitxSunpinyinGetCandWord (void* arg, FcitxCandidateWord* candWord)
 
297
{
 
298
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin* )arg;
 
299
    sunpinyin->windowHandler->commit_flag = false;
 
300
    sunpinyin->windowHandler->candidate_flag = false;
 
301
    int* index = (int*) candWord->priv;
 
302
    sunpinyin->view->onCandidateSelectRequest(*index);
 
303
 
 
304
    if (sunpinyin->windowHandler->commit_flag)
 
305
        return IRV_COMMIT_STRING;
 
306
 
 
307
    if (sunpinyin->windowHandler->candidate_flag)
 
308
        return IRV_DISPLAY_CANDWORDS;
 
309
 
 
310
    return IRV_DO_NOTHING;
 
311
}
 
312
 
 
313
/**
 
314
 * @brief initialize the extra input method
 
315
 *
 
316
 * @param arg
 
317
 * @return successful or not
 
318
 **/
 
319
__EXPORT_API
 
320
void* FcitxSunpinyinCreate (FcitxInstance* instance)
 
321
{
 
322
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) fcitx_utils_malloc0(sizeof(FcitxSunpinyin));
 
323
    FcitxAddon* addon = FcitxAddonsGetAddonByName(FcitxInstanceGetAddons(instance), "fcitx-sunpinyin");
 
324
    bindtextdomain("fcitx-sunpinyin", LOCALEDIR);
 
325
    sunpinyin->owner = instance;
 
326
    FcitxSunpinyinConfig* fs = &sunpinyin->fs;
 
327
 
 
328
    if (!LoadSunpinyinConfig(&sunpinyin->fs))
 
329
    {
 
330
        free(sunpinyin);
 
331
        return NULL;
 
332
    }
 
333
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
 
334
 
 
335
    if (fs->bUseShuangpin)
 
336
        fac.setPinyinScheme(CSunpinyinSessionFactory::SHUANGPIN);
 
337
    else
 
338
        fac.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
 
339
 
 
340
    ConfigSunpinyin(sunpinyin);
 
341
    sunpinyin->bShuangpin = fs->bUseShuangpin;
 
342
 
 
343
    sunpinyin->view = fac.createSession();
 
344
 
 
345
    if (sunpinyin->view == NULL)
 
346
    {
 
347
        free(sunpinyin);
 
348
        return NULL;
 
349
    }
 
350
 
 
351
    FcitxWindowHandler* windowHandler = new FcitxWindowHandler();
 
352
    sunpinyin->windowHandler = windowHandler;
 
353
    sunpinyin->view->getIC()->setCharsetLevel(3);
 
354
 
 
355
    sunpinyin->view->attachWinHandler(windowHandler);
 
356
    sunpinyin->windowHandler->SetOwner(sunpinyin);
 
357
    ConfigSunpinyin(sunpinyin);
 
358
 
 
359
    FcitxInstanceRegisterIM(instance,
 
360
                    sunpinyin,
 
361
                    "sunpinyin",
 
362
                    _("Sunpinyin"),
 
363
                    "sunpinyin",
 
364
                    FcitxSunpinyinInit,
 
365
                    FcitxSunpinyinReset,
 
366
                    FcitxSunpinyinDoInput,
 
367
                    FcitxSunpinyinGetCandWords,
 
368
                    NULL,
 
369
                    NULL,
 
370
                    ReloadConfigFcitxSunpinyin,
 
371
                    NULL,
 
372
                    fs->iSunpinyinPriority,
 
373
                    "zh_CN"
 
374
                   );
 
375
 
 
376
    AddFunction(addon, (void*) SunpinyinGetFullPinyin);
 
377
 
 
378
    return sunpinyin;
 
379
}
 
380
 
 
381
/**
 
382
 * @brief Destroy the input method while unload it.
 
383
 *
 
384
 * @return int
 
385
 **/
 
386
__EXPORT_API
 
387
void FcitxSunpinyinDestroy (void* arg)
 
388
{
 
389
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
390
    CSunpinyinSessionFactory& fac = CSunpinyinSessionFactory::getFactory();
 
391
    fac.destroySession(sunpinyin->view);
 
392
    if (sunpinyin->shuangpin_data)
 
393
        delete sunpinyin->shuangpin_data;
 
394
 
 
395
    if (sunpinyin->windowHandler)
 
396
        delete sunpinyin->windowHandler;
 
397
 
 
398
    free(arg);
 
399
}
 
400
 
 
401
INPUT_RETURN_VALUE FcitxSunpinyinDeleteCandidate (FcitxSunpinyin* sunpinyin, FcitxCandidateWord* candWord)
 
402
{
 
403
    if (candWord->owner == sunpinyin)
 
404
    {
 
405
        CCandidateList pcl;
 
406
        sunpinyin->view->getCandidateList(pcl, 0, sunpinyin->candNum);
 
407
        int* index = (int*) candWord->priv;
 
408
        CIMIClassicView* classicView = (CIMIClassicView*) sunpinyin->view;
 
409
        unsigned int mask;
 
410
        classicView->deleteCandidate(*index, mask);
 
411
        return IRV_DISPLAY_CANDWORDS;
 
412
    }
 
413
    return IRV_TO_PROCESS;
 
414
}
 
415
 
 
416
/**
 
417
 * @brief Load the config file for fcitx-sunpinyin
 
418
 *
 
419
 * @param Bool is reload or not
 
420
 **/
 
421
boolean LoadSunpinyinConfig(FcitxSunpinyinConfig* fs)
 
422
{
 
423
    FcitxConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
 
424
    if (!configDesc)
 
425
        return false;
 
426
 
 
427
    FILE *fp = FcitxXDGGetFileUserWithPrefix("conf", "fcitx-sunpinyin.config", "rt", NULL);
 
428
 
 
429
    if (!fp)
 
430
    {
 
431
        if (errno == ENOENT)
 
432
            SaveSunpinyinConfig(fs);
 
433
    }
 
434
    FcitxConfigFile *cfile = FcitxConfigParseConfigFileFp(fp, configDesc);
 
435
 
 
436
    FcitxSunpinyinConfigConfigBind(fs, cfile, configDesc);
 
437
    FcitxConfigBindSync(&fs->gconfig);
 
438
 
 
439
    if (fp)
 
440
        fclose(fp);
 
441
    return true;
 
442
}
 
443
 
 
444
void ConfigSunpinyin(FcitxSunpinyin* sunpinyin)
 
445
{
 
446
    FcitxInstance* instance = sunpinyin->owner;
 
447
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(instance);
 
448
    FcitxSunpinyinConfig *fs = &sunpinyin->fs;
 
449
    int i = 0;
 
450
 
 
451
    if (sunpinyin->view)
 
452
    {
 
453
        sunpinyin->view->setCandiWindowSize(2048);
 
454
        // page up/down key
 
455
        CHotkeyProfile* prof = sunpinyin->view->getHotkeyProfile();
 
456
        prof->clear();
 
457
 
 
458
        for (i = 0 ; i < 2; i++)
 
459
        {
 
460
            if (config->hkPrevPage[i].sym)
 
461
                prof->addPageUpKey(CKeyEvent(config->hkPrevPage[i].sym, 0, config->hkPrevPage[i].state));
 
462
            if (config->hkNextPage[i].sym)
 
463
                prof->addPageDownKey(CKeyEvent(config->hkNextPage[i].sym, 0, config->hkNextPage[i].state));
 
464
        }
 
465
        sunpinyin->view->setCancelOnBackspace(1);
 
466
    }
 
467
 
 
468
    string_pairs fuzzy, correction;
 
469
    for (i = 0; i < FUZZY_SIZE; i++)
 
470
        if (fs->bFuzzy[i])
 
471
            fuzzy.push_back(std::make_pair<std::string, std::string>(fuzzyPairs[i][0], fuzzyPairs[i][1]));
 
472
 
 
473
    for (i = 0; i < CORRECT_SIZE; i++)
 
474
        if (fs->bAutoCorrecting[i])
 
475
            correction.push_back(std::make_pair<std::string, std::string>(correctionPairs[i][0], correctionPairs[i][1]));
 
476
 
 
477
    if (fuzzy.size() != 0)
 
478
    {
 
479
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(true);
 
480
        AQuanpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
 
481
        AShuangpinSchemePolicy::instance().setFuzzyForwarding(true);
 
482
        AShuangpinSchemePolicy::instance().setFuzzyPinyinPairs(fuzzy);
 
483
    }
 
484
    else
 
485
    {
 
486
        AQuanpinSchemePolicy::instance().setFuzzyForwarding(false);
 
487
        AQuanpinSchemePolicy::instance().clearFuzzyPinyinPairs();
 
488
        AShuangpinSchemePolicy::instance().setFuzzyForwarding(false);
 
489
        AShuangpinSchemePolicy::instance().clearFuzzyPinyinPairs();
 
490
    }
 
491
 
 
492
    if (correction.size() != 0)
 
493
    {
 
494
        AQuanpinSchemePolicy::instance().setAutoCorrecting(true);
 
495
        AQuanpinSchemePolicy::instance().setAutoCorrectionPairs(correction);
 
496
    }
 
497
    else
 
498
        AQuanpinSchemePolicy::instance().setAutoCorrecting(false);
 
499
 
 
500
    if (sunpinyin->shuangpin_data == NULL)
 
501
        sunpinyin->shuangpin_data = new CShuangpinData(fs->SPScheme);
 
502
    AShuangpinSchemePolicy::instance().setShuangpinType(fs->SPScheme);
 
503
    AQuanpinSchemePolicy::instance().setFuzzySegmentation(fs->bFuzzySegmentation);
 
504
    AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(fs->bFuzzyInnerSegmentation);
 
505
}
 
506
 
 
507
__EXPORT_API void ReloadConfigFcitxSunpinyin(void* arg)
 
508
{
 
509
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
510
    LoadSunpinyinConfig(&sunpinyin->fs);
 
511
    ConfigSunpinyin(sunpinyin);
 
512
}
 
513
 
 
514
/**
 
515
 * @brief Save the config
 
516
 *
 
517
 * @return void
 
518
 **/
 
519
void SaveSunpinyinConfig(FcitxSunpinyinConfig* fs)
 
520
{
 
521
    FcitxConfigFileDesc *configDesc = GetSunpinyinConfigDesc();
 
522
    FILE *fp = FcitxXDGGetFileUserWithPrefix("conf", "fcitx-sunpinyin.config", "wt", NULL);
 
523
    FcitxConfigSaveConfigFileFp(fp, &fs->gconfig, configDesc);
 
524
    if (fp)
 
525
        fclose(fp);
 
526
}
 
527
 
 
528
void* SunpinyinGetFullPinyin(void* arg, FcitxModuleFunctionArg args)
 
529
{
 
530
    FcitxSunpinyin* sunpinyin = (FcitxSunpinyin*) arg;
 
531
    char* pinyin = (char*) args.args[0];
 
532
    boolean *issp = (boolean*) args.args[1];
 
533
    *issp = sunpinyin->bShuangpin;
 
534
    CMappedYin syls;
 
535
    if (sunpinyin->bShuangpin)
 
536
    {
 
537
        sunpinyin->shuangpin_data->getMapString(pinyin, syls);
 
538
        if (syls.size() == 0)
 
539
            return NULL;
 
540
        else
 
541
            return strdup(syls[0].c_str());
 
542
    }
 
543
    else
 
544
        return NULL;
 
545
}
 
546
 
 
547
// kate: indent-mode cstyle; space-indent on; indent-width 0;