void CloudPinyinFillCandidateWord(FcitxCloudPinyin* cloudpinyin, const char* pinyin)
{
    CloudPinyinCache* cacheEntry = CloudPinyinCacheLookup(cloudpinyin, pinyin);
    FcitxInputState* input = FcitxInstanceGetInputState(cloudpinyin->owner);
    struct _FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
    if (cacheEntry)
    {
        FcitxCandidateWord* candWord;
        for (candWord = FcitxCandidateWordGetFirst(candList);
             candWord != NULL;
             candWord = FcitxCandidateWordGetNext(candList, candWord))
        {
            if (candWord->owner == cloudpinyin)
                break;
        }

        if (candWord == NULL)
            return;

        CloudCandWord* cloudCand = candWord->priv;
        if (cloudCand->filled)
            return;

        FcitxCandidateWord* cand;
        int i = 0;
        int size = FcitxCandidateWordGetPageSize(candList) * CLOUDPINYIN_CHECK_PAGE_NUMBER;
        for (cand = FcitxCandidateWordGetFirst(candList);
             cand != NULL;
             cand = FcitxCandidateWordGetNext(candList, cand))
        {
            if (strcmp(cand->strWord, cacheEntry->str) == 0) {
                FcitxCandidateWordRemove(candList, candWord);
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
                candWord = NULL;
                break;
            }
            i ++;
            if (i >= size)
                break;
        }
        
        if (candWord)
        {
            if (cloudCand->filled == false)
            {
                cloudCand->filled = true;
                free(candWord->strWord);
                candWord->strWord = strdup(cacheEntry->str);
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
            }
        }
    }
}
Beispiel #2
0
void CloudPinyinFillCandidateWord(FcitxCloudPinyin* cloudpinyin,
                                  const char* pinyin)
{
    CloudPinyinCache* cacheEntry = CloudPinyinCacheLookup(cloudpinyin, pinyin);
    FcitxInputState* input = FcitxInstanceGetInputState(cloudpinyin->owner);
    FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
    if (cacheEntry) {
        int cloudidx;
        FcitxCandidateWord *candWord;
        for (cloudidx = 0;
             (candWord = FcitxCandidateWordGetByTotalIndex(candList, cloudidx));
             cloudidx++) {
            if (candWord->owner == cloudpinyin)
                break;
        }

        if (candWord == NULL)
            return;

        CloudCandWord* cloudCand = candWord->priv;
        if (cloudCand->filled)
            return;

        FcitxCandidateWord *cand;
        int i;
        int pagesize = FcitxCandidateWordGetPageSize(candList);
        int size = pagesize * CLOUDPINYIN_CHECK_PAGE_NUMBER;
        for (i = 0;i < size &&
                 (cand = FcitxCandidateWordGetByTotalIndex(candList, i));i++) {
            if (strcmp(cand->strWord, cacheEntry->str) == 0) {
                uint64_t ts = cloudCand->timestamp;
                uint64_t curTs = CloudGetTimeStamp();
                FcitxCandidateWordRemove(candList, candWord);
                /* if cloud word is not on the first page.. impossible */
                if (cloudidx < pagesize) {
                    /* if the duplication before cloud word */
                    if (i < cloudidx) {
                        if (curTs - ts
                            > LOADING_TIME_QUICK_THRESHOLD) {
                            FcitxCandidateWordInsertPlaceHolder(candList, cloudidx);
                            FcitxCandidateWord* placeHolder = FcitxCandidateWordGetByTotalIndex(candList, cloudidx);
                            if (placeHolder && placeHolder->strWord == NULL)
                                placeHolder->strWord = strdup(DUP_PLACE_HOLDER);
                        }
                    } else {
                        if (i >= pagesize) {
                            FcitxCandidateWordMove(candList, i - 1, cloudidx);
                        } else {
                            if (curTs - ts > LOADING_TIME_QUICK_THRESHOLD) {
                                FcitxCandidateWordInsertPlaceHolder(candList, cloudidx);
                                FcitxCandidateWord* placeHolder = FcitxCandidateWordGetByTotalIndex(candList, cloudidx);
                                if (placeHolder && placeHolder->strWord == NULL)
                                    placeHolder->strWord = strdup(DUP_PLACE_HOLDER);
                            }
                        }
                    }
                }
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
                candWord = NULL;
                break;
            }
        }

        if (candWord) {
            if (cloudCand->filled == false) {
                cloudCand->filled = true;
                free(candWord->strWord);
                candWord->strWord = strdup(cacheEntry->str);
                if (cloudpinyin->config.iCandidateOrder <= 1 &&
                    (CloudGetTimeStamp() - cloudCand->timestamp
                     <= LOADING_TIME_QUICK_THRESHOLD)) {
                    FcitxCandidateWordMoveByWord(candList, candWord, 0);
                    CloudSetClientPreedit(cloudpinyin, cacheEntry->str);
                }
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
            }
        }
    }
}