void _CloudPinyinAddCandidateWord(FcitxCloudPinyin* cloudpinyin, const char* pinyin) { CloudPinyinCache* cacheEntry = CloudPinyinCacheLookup(cloudpinyin, pinyin); FcitxInputState* input = FcitxInstanceGetInputState(cloudpinyin->owner); FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input); int order = (cloudpinyin->config.iCandidateOrder <= 2) ? 1 : (cloudpinyin->config.iCandidateOrder - 1); if (cacheEntry) { FcitxCandidateWord* cand; /* only check the first three page */ int pagesize = FcitxCandidateWordGetPageSize(candList); int size = pagesize * CLOUDPINYIN_CHECK_PAGE_NUMBER; int i; if (cloudpinyin->config.iCandidateOrder <= 1) { order = 0; } for (i = 0;i < size && (cand = FcitxCandidateWordGetByTotalIndex(candList, i));i++) { if (strcmp(cand->strWord, cacheEntry->str) == 0) { if (i > order && i >= pagesize) { FcitxCandidateWordMoveByWord(candList, cand, order); if (order == 0) { CloudSetClientPreedit(cloudpinyin, cacheEntry->str); } } return; } } if (order == 0) { CloudSetClientPreedit(cloudpinyin, cacheEntry->str); } } FcitxCandidateWord candWord; CloudCandWord* cloudCand = fcitx_utils_malloc0(sizeof(CloudCandWord)); if (cacheEntry) { cloudCand->filled = true; cloudCand->timestamp = 0; candWord.strWord = strdup(cacheEntry->str); } else { cloudCand->filled = false; cloudCand->timestamp = CloudGetTimeStamp(); candWord.strWord = strdup(".."); } candWord.callback = CloudPinyinGetCandWord; candWord.owner = cloudpinyin; candWord.priv = cloudCand; candWord.wordType = MSG_TIPS; if (cloudpinyin->config.bDontShowSource) candWord.strExtra = NULL; else { candWord.strExtra = strdup(_(" (via cloud)")); candWord.extraType = MSG_TIPS; } FcitxCandidateWordInsert(candList, &candWord, order); }
FCITX_EXPORT_API FcitxCandidateWord* FcitxCandidateWordGetByIndex(FcitxCandidateWordList* candList, int index) { if (index < candList->wordPerPage && index >= 0) return FcitxCandidateWordGetByTotalIndex( candList, candList->currentPage * candList->wordPerPage + index); return NULL; }
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); } } } }
FCITX_EXPORT_API FcitxCandidateWord* FcitxCandidateWordGetCurrentWindow(FcitxCandidateWordList* candList) { return FcitxCandidateWordGetByTotalIndex( candList, candList->currentPage * candList->wordPerPage); }