Back to index

im-sdk  12.3.91
IMEConfig.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <locale.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 
00006 #include "hzinput.h"
00007 #include "kolelog.h"
00008 #include "encode.h"
00009 
00010 #define MAX_LINE_LEN 256
00011 
00012 /*  Auxiliary Window User Input Method Information  */
00013 IMEListRec    AllIMEList, SelectedIMEList;
00014 
00015 extern char   gLocaleID;
00016 
00017 IMEBaseRec *IMEItem_Malloc()
00018 {
00019        IMEBaseRec *pIME_Base;
00020 
00021        pIME_Base = (IMEBaseRec *)calloc(1, sizeof(IMEBaseRec));
00022        if (pIME_Base == NULL) return(NULL);
00023 
00024        pIME_Base->ename = NULL;
00025        pIME_Base->cname = NULL;
00026 
00027        return(pIME_Base);
00028 }
00029 
00030 void IMEItem_Free(IMEBaseRec *pIME_Base)
00031 {
00032        if (pIME_Base == NULL) return;
00033 
00034        if (pIME_Base->ename) free((char *)pIME_Base->ename);
00035        if (pIME_Base->cname) free((char *)pIME_Base->cname);
00036 
00037        free((char *)pIME_Base);
00038 }
00039 
00040 void IMEList_Init()
00041 {
00042        int i;
00043 
00044        AllIMEList.nIME_Num = 0;
00045        SelectedIMEList.nIME_Num = 0;
00046 
00047        for (i = 0 ; i < MAX_ENGINE_NUM ; i++) {
00048               AllIMEList.arrIME_Base[i] = NULL;
00049               SelectedIMEList.arrIME_Base[i] = NULL;
00050        }
00051 }
00052 
00053 void IMEList_Free()
00054 {
00055        int i;
00056 
00057        for (i = 0 ; i < MAX_ENGINE_NUM ; i++) {
00058               if (AllIMEList.arrIME_Base[i] != NULL) {
00059                      IMEItem_Free(AllIMEList.arrIME_Base[i]);
00060                      AllIMEList.arrIME_Base[i] = NULL;
00061               }
00062        }
00063 
00064        AllIMEList.nIME_Num = 0;
00065        SelectedIMEList.nIME_Num = 0;
00066 }
00067 
00068 void Add_IME_To_AllList(IMEBaseRec *baseinfo)
00069 {
00070        int engine_num, engine_locale_id, system_locale_id, encode_id;
00071        int ilen, olen, ret, nIME_Num;
00072        char *engine_cname, *ime_cname, tmp_name[256];
00073        IMEBaseRec *pIME_Base;
00074 
00075        engine_locale_id = baseinfo->locale_id;
00076        system_locale_id = gLocaleID;
00077 
00078        KOLE_LOG (LOGDEST_STDOUT, "locale_id:%d, engine_id:%d, status:%d, ename:%s, cname:%s\n",
00079               baseinfo->locale_id,
00080               baseinfo->engine_id,
00081               baseinfo->status,
00082               baseinfo->ename,
00083               baseinfo->cname);
00084        
00085        /* if is ENCODES_NUM, means this engine is a common engine for this language. */
00086        if (engine_locale_id != system_locale_id && engine_locale_id != ENCODES_NUM)
00087               return;
00088 
00089        if (baseinfo->status == ENGINE_NOT_INSTALLED)
00090               return;
00091 
00092        nIME_Num = AllIMEList.nIME_Num;
00093        pIME_Base = AllIMEList.arrIME_Base[nIME_Num];
00094        if (pIME_Base != NULL) IMEItem_Free(pIME_Base);
00095        AllIMEList.arrIME_Base[nIME_Num] = NULL;
00096 
00097        pIME_Base = (IMEBaseRec *)IMEItem_Malloc();
00098        if (pIME_Base == NULL) return;
00099 
00100        pIME_Base->engine_id = baseinfo->engine_id;
00101        pIME_Base->locale_id = baseinfo->locale_id;
00102        pIME_Base->encode_id = baseinfo->encode_id;
00103        pIME_Base->status = baseinfo->status;
00104        pIME_Base->ename = (char *)strdup(baseinfo->ename);
00105        
00106        /* convert IME name to native encoding. */
00107        /* Usually: IME name is in UTF-8 or native encoding */
00108        encode_id = pIME_Base->encode_id;
00109        if (encode_id == ENCODE_UTF8) {
00110               engine_cname = (char *)baseinfo->cname;
00111               ilen = strlen((char *)engine_cname);
00112               ime_cname = tmp_name;
00113               olen = 256;
00114               memset(ime_cname, 0, olen);
00115               ret = Convert_UTF8_To_Native(system_locale_id, engine_cname, ilen, &ime_cname, &olen);
00116               KOLE_LOG (LOGDEST_STDOUT, "ret:%d, ime_cname:%s, olen:%d\n", ret, ime_cname, olen);
00117               if (ret == 0)
00118                      pIME_Base->cname = (char *)strdup(tmp_name);
00119               else
00120                      pIME_Base->cname = (char *)strdup(baseinfo->ename);
00121        } else {
00122               pIME_Base->cname = (char *)strdup(baseinfo->cname);
00123        }
00124 
00125        AllIMEList.arrIME_Base[nIME_Num] = pIME_Base;
00126        nIME_Num ++;
00127        AllIMEList.nIME_Num = nIME_Num;
00128 }
00129 
00130 void get_ime_line(FILE *fd, char *line)
00131 {
00132        int line_ptr;
00133        char line_buf[256], *ptr;
00134 
00135        line_ptr = 0;
00136        line[0] = '\0';
00137 
00138        /* get line with no space */
00139        while(fgets(line_buf, 255, fd) != NULL) {
00140               ptr = line_buf;
00141 
00142               /* skip space keys */
00143               while(*ptr && isspace(*ptr)) ptr++;
00144 
00145               /* if is space line, get new line */
00146               if (*ptr == '\n' || *ptr == '\0')
00147                      continue;
00148 
00149               while(*ptr != '\n' && *ptr != '\0' && line_ptr < MAX_LINE_LEN) 
00150                      line[line_ptr++] = *ptr++;
00151 
00152               /* trim right space */
00153               while (isspace(line[line_ptr-1])) line_ptr--;
00154               line[line_ptr] = '\0';
00155 
00156               /* if the line end with '\', then continue read the next line */
00157               if (line[line_ptr-1] == '\\') {
00158                      line_ptr--;
00159                      line[line_ptr] = '\0';
00160                      continue;
00161               }
00162 
00163               break;
00164        }
00165 }
00166 
00167 void Select_IMEs_From_Profile()
00168 {
00169        char            file_name[256], line[MAX_LINE_LEN]; 
00170        int           i, nInstalledIME;
00171     
00172        register char        *ptr, *ime_name;
00173        FILE          *fd;
00174 
00175        char          *locale_name;
00176 
00177        if (AllIMEList.nIME_Num == 0) return;
00178 
00179        locale_name = setlocale(LC_CTYPE, NULL);
00180 
00181        /* read profile from file to memory buffer  */ 
00182        sprintf(file_name, "%s/%s/%s/%s", getenv("HOME"), 
00183               USER_PROFILE_PATH, locale_name, USER_PROFILE_NAME);
00184        KOLE_LOG (LOGDEST_STDOUT, "file name :%s\n",file_name);
00185 
00186        fd = fopen(file_name, "r");
00187        if (! fd) {
00188               SelectedIMEList = AllIMEList;
00189               return;
00190        }
00191 
00192        nInstalledIME = 0;
00193        do {
00194               get_ime_line(fd, line);
00195 
00196               if (line[0] == '\0') break;
00197               if (line[0] == '#') continue;
00198 
00199               /* get IME language engine name */
00200               ime_name = line;
00201 
00202               KOLE_LOG (LOGDEST_STDOUT, "IME Name:%s\n", ime_name);
00203               for (i=0; i<AllIMEList.nIME_Num; i++) {
00204                      if (strcasecmp(ime_name, AllIMEList.arrIME_Base[i]->ename)) {
00205                             continue;
00206                      } else {
00207                             SelectedIMEList.arrIME_Base[nInstalledIME] = AllIMEList.arrIME_Base[i];
00208                             nInstalledIME ++;
00209                             break;
00210                      }
00211               }
00212        } while (1);
00213 
00214        fclose(fd);
00215        
00216        if (nInstalledIME == 0) 
00217               SelectedIMEList = AllIMEList;
00218        else
00219               SelectedIMEList.nIME_Num = nInstalledIME;
00220 
00221        for (i=0; i<SelectedIMEList.nIME_Num; i++) {
00222               KOLE_LOG (LOGDEST_STDOUT, "localeid:%d, ename:%s, cname:%s, imid:%d, status:%d\n", 
00223                      SelectedIMEList.arrIME_Base[i]->locale_id,
00224                      SelectedIMEList.arrIME_Base[i]->ename,
00225                      SelectedIMEList.arrIME_Base[i]->cname,
00226                      SelectedIMEList.arrIME_Base[i]->engine_id,
00227                      SelectedIMEList.arrIME_Base[i]->status);
00228        }
00229 
00230        return;
00231 }
00232 
00233 void Save_IMEList_To_Profile(IMEListRec *imelist)
00234 {
00235        FILE *fd;
00236        int ret, i;
00237        struct stat file_stat;
00238        char path_name[256], tmp_name[256];
00239        char *locale_name;
00240 
00241        /* Create Path $(HOME)/.Xlocale */
00242        sprintf(path_name, "%s/%s", (char *)getenv("HOME"), USER_PROFILE_PATH);
00243        KOLE_LOG (LOGDEST_STDOUT, "path_name: %s\n", path_name);
00244 
00245        ret = stat(path_name, &file_stat);
00246        if (ret == -1) {
00247               KOLE_LOG (LOGDEST_STDOUT, "%s not exist\n", path_name);
00248               ret = mkdir(path_name, 0777);
00249               if (ret == -1) {
00250                      KOLE_LOG (LOGDEST_STDOUT, "can not create path %s\n", path_name);
00251                      return;
00252               }
00253        }
00254 
00255        /* Create Path $(HOME)/.Xlocale/<locale> */
00256        locale_name = setlocale(LC_CTYPE, NULL);
00257 
00258        sprintf(tmp_name, "%s/%s", path_name, locale_name);
00259        strcpy(path_name, tmp_name);
00260 
00261        ret = stat(path_name, &file_stat);
00262        if (ret == -1) {
00263               KOLE_LOG (LOGDEST_STDOUT, "%s not exist\n", path_name);
00264               ret = mkdir(path_name, 0777);
00265               if (ret == -1) {
00266                      KOLE_LOG (LOGDEST_STDOUT, "can not create path %s\n", path_name);
00267                      return;
00268               }
00269        }
00270 
00271        /* Create File $(HOME)/.Xlocale/<locale>/ime.cfg */
00272        sprintf(tmp_name, "%s/%s", path_name, USER_PROFILE_NAME);
00273        strcpy(path_name, tmp_name);
00274        KOLE_LOG (LOGDEST_STDOUT, "path_name: %s\n", path_name);
00275        fd = fopen(path_name, "wb");
00276        if (! fd) {
00277               KOLE_LOG (LOGDEST_STDOUT, "unable open the file: %s\n", path_name);
00278               return;
00279        }
00280 
00281        for (i=0; i<imelist->nIME_Num; i++) {
00282               fprintf(fd, "%s\n", imelist->arrIME_Base[i]->ename);
00283        }
00284 
00285        fclose(fd);
00286 }