Back to index

im-sdk  12.3.91
le_info.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <dlfcn.h>
00003 
00004 #include "le_info.h"
00005 #include "imbean.h"
00006 
00007 #define LE_CONFIG_FILE            "le_config.xml"
00008 
00009 #define CATAGORY_IME_MODULES      "all_engines"
00010 #define CATAGORY_IMM_KEYBINDINGS  "keybindings"
00011 
00012 LeResult le_info_destroy(LeInfoRec * le_info);
00013 LeResult le_info_load_ime_modules(LeInfoRec * le_info,
00014                               IbmlCategory * ibml_category);
00015 LeResult le_info_load_imm_keybindings(LeInfoRec * le_info,
00016                                   IbmlCategory * ibml_category);
00017 LeResult le_info_load_config_file(LeInfoRec * le_info,
00018                               char *le_config_file);
00019 
00020 LeInfoRec *le_info_new()
00021 {
00022     LeResult result;
00023 
00024     LeInfoRec *le_info = NULL;
00025     char *le_config_file = LE_CONFIG_FILE;
00026 
00027     le_info = (LeInfoRec *) calloc(1, sizeof(LeInfoRec));
00028     if (le_info == NULL)
00029        return (NULL);
00030 
00031     le_info->num_ime_modules = 0;
00032     le_info->ime_modules = NULL;
00033 
00034     le_info->num_imm_keybindings = 0;
00035     le_info->imm_keybindings = NULL;
00036 
00037     le_info->time_stamp = time(0);
00038 
00039     result = le_info_load_config_file(le_info, le_config_file);
00040     if (result == LE_FAIL) {
00041         le_info_destroy(le_info);
00042         return (NULL);
00043     }
00044 
00045     return (le_info);
00046 }
00047 
00048 LeResult le_info_destroy(LeInfoRec * le_info)
00049 {
00050     int i;
00051 
00052     if (le_info == NULL)
00053        return (LE_FAIL);
00054 
00055     /* free ime modules */
00056     if (le_info->ime_modules) {
00057        for (i = 0; i < le_info->num_ime_modules; i++) {
00058            ImeModuleContextRec *ime_module_context = le_info->ime_modules[i];
00059            if (ime_module_context == NULL)
00060               continue;
00061            ime_module_context_destroy(ime_module_context);
00062        }
00063        free((char *) le_info->ime_modules);
00064     }
00065 
00066     /* free imm keybindings */
00067     if (le_info->imm_keybindings) {
00068        for (i = 0; i < le_info->num_imm_keybindings; i++) {
00069            ImmKeybindingRec *imm_keybinding = le_info->imm_keybindings[i];
00070            if (imm_keybinding == NULL)
00071               continue;
00072            imm_keybinding_destroy(imm_keybinding);
00073        }
00074        free((char *) le_info->imm_keybindings);
00075     }
00076 
00077     free((char *) le_info);
00078 
00079     return (LE_OK);
00080 }
00081 
00082 LeResult le_info_print(LeInfoRec * le_info)
00083 {
00084 #if DEBUG
00085     int i;
00086 
00087     if (le_info == NULL)
00088        return (LE_FAIL);
00089 
00090     printf("time_stamp:  %d\n", le_info->time_stamp);
00091 
00092     /* print ime modules */
00093     if (le_info->ime_modules) {
00094        for (i = 0; i < le_info->num_ime_modules; i++) {
00095            ImeModuleContextRec *ime_module_context = le_info->ime_modules[i];
00096            if (ime_module_context == NULL)
00097               continue;
00098            ime_module_context_print(ime_module_context);
00099        }
00100     }
00101 
00102     /* print imm keybindings */
00103     if (le_info->imm_keybindings) {
00104        for (i = 0; i < le_info->num_imm_keybindings; i++) {
00105            ImmKeybindingRec *imm_keybinding = le_info->imm_keybindings[i];
00106            if (imm_keybinding == NULL)
00107               continue;
00108            imm_keybinding_print(imm_keybinding);
00109        }
00110     }
00111 
00112     return (LE_OK);
00113 #endif
00114 }
00115 
00116 char *le_info_get_full_file_path(char *file_path)
00117 {
00118     int len;
00119     char *full_file_path = NULL;
00120 
00121     if (!file_path || !*file_path)
00122        return (NULL);
00123 
00124     if (file_path[0] == '/') {
00125        len = strlen(file_path);
00126 
00127        full_file_path = (char *) calloc(len + 1, sizeof(char));
00128        if (full_file_path == NULL)
00129            return (NULL);
00130 
00131        snprintf(full_file_path, len + 1, "%s", file_path);
00132     } else {
00133        len = strlen(LE_BASE_DIR) + 1 + strlen(file_path);
00134 
00135        full_file_path = (char *) calloc(len + 1, sizeof(char));
00136        if (full_file_path == NULL)
00137            return (NULL);
00138 
00139        snprintf(full_file_path, len + 1, "%s/%s", LE_BASE_DIR, file_path);
00140     }
00141 
00142     return (full_file_path);
00143 }
00144 
00145 LeResult le_info_load_config_file(LeInfoRec * le_info,
00146                               char *le_config_file)
00147 {
00148     IbmlData *ibml_data;
00149     IbmlCategory *ibml_category;
00150     char *le_config_file_with_full_path = NULL;
00151     int i;
00152 
00153     LeResult result = LE_OK;
00154 
00155     if (le_info == NULL)
00156        return (LE_FAIL);
00157 
00158     le_config_file_with_full_path =
00159        (char *) le_info_get_full_file_path(le_config_file);
00160     if (!le_config_file_with_full_path || !*le_config_file_with_full_path)
00161        return (LE_FAIL);
00162 
00163     ibml_data = (IbmlData *) imbean_config_new_from_file(le_config_file_with_full_path);
00164     free((char *) le_config_file_with_full_path);
00165 
00166     if (ibml_data == NULL)
00167        return (LE_FAIL);
00168 
00169     for (i = 0; i < ibml_data->num_categories; i++) {
00170        ibml_category = ibml_data->categories[i];
00171 
00172        if (!ibml_category->scope || !*ibml_category->scope)
00173            continue;
00174 
00175        if (!strcasecmp(ibml_category->scope, CATAGORY_IME_MODULES)) {
00176            result = le_info_load_ime_modules(le_info, ibml_category);
00177        } else if (!strcasecmp(ibml_category->scope, CATAGORY_IMM_KEYBINDINGS)) {
00178            result = le_info_load_imm_keybindings(le_info, ibml_category);
00179        }
00180     }
00181 
00182     ibml_data_free(ibml_data);
00183     return (result);
00184 }
00185 
00186 LeResult le_info_load_ime_modules(LeInfoRec * le_info,
00187                               IbmlCategory * ibml_category)
00188 {
00189 #define IME_MODULE_OBJECT_FILE "object_file"
00190 #define IME_MODULE_ARGS        "args"
00191 #define IME_MODULE_ENABLED     "enabled"
00192 
00193     int num_ime_modules;
00194     ImeModuleContextRec **ime_modules;
00195 
00196     IbmlElement *ibml_element;
00197     IbmlProperty *ibml_property;
00198 
00199     int i, j;
00200 
00201     char *id, *scope;
00202     char *name, *value;
00203 
00204     if (le_info == NULL)
00205        return (LE_FAIL);
00206     if (ibml_category == NULL)
00207        return (LE_FAIL);
00208     if (ibml_category->num_elements <= 0)
00209        return (LE_FAIL);
00210 
00211     ime_modules =
00212        (ImeModuleContextRec **) calloc(ibml_category->num_elements,
00213                                    sizeof(ImeModuleContextRec *));
00214     if (ime_modules == NULL) {
00215        return (LE_FAIL);
00216     }
00217 
00218     num_ime_modules = 0;
00219     for (i = 0; i < ibml_category->num_elements; i++) {
00220        ImeModuleContextRec *ime_module_context = NULL;
00221        ImeModuleRec *ime_module = NULL;
00222        char *base_dir = LE_BASE_DIR;
00223        char *object_file_path = NULL;
00224        char *ime_args = NULL;
00225        char *enable_str = NULL;
00226        ImeResult result;
00227 
00228        ibml_element = ibml_category->elements[i];
00229        if (!ibml_element)
00230            continue;
00231 
00232        id = (char *) ibml_element->id;
00233        scope = (char *) ibml_element->scope;
00234        DEBUG_printf("id:%s, scope:%s\n", id ? id : "NULL",
00235                    scope ? scope : "NULL");
00236        if (!id || !*id)
00237            continue;
00238 
00239        for (j = 0; j < ibml_element->num_properties; j++) {
00240            ibml_property = ibml_element->properties[j];
00241            if (!ibml_property)
00242               continue;
00243 
00244            name = (char *) ibml_property->name;
00245            if (!name || !*name)
00246               continue;
00247 
00248            value = (char *) ibml_property->value;
00249            if (!value || !*value)
00250               continue;
00251 
00252            if (!strcasecmp(name, IME_MODULE_OBJECT_FILE)) {
00253               object_file_path = (char *) value;
00254            } else if (!strcasecmp(name, IME_MODULE_ARGS)) {
00255               ime_args = (char *) value;
00256            } else if (!strcasecmp(name, IME_MODULE_ENABLED)) {
00257               enable_str = (char *) value;
00258            }
00259        }
00260 
00261        if (!object_file_path || !*object_file_path)
00262            continue;
00263 
00264        object_file_path =
00265            (char *) le_info_get_full_file_path(object_file_path);
00266        if (!object_file_path || !*object_file_path)
00267            continue;
00268 
00269        ime_module_context = (ImeModuleContextRec *) ime_module_context_new();
00270        if (ime_module_context == NULL) {
00271            free((char *) object_file_path);
00272            continue;
00273        }
00274 
00275        ime_module = (ImeModuleRec *) ime_module_new();
00276        if (ime_module == NULL) {
00277            free((char *) ime_module_context);
00278            free((char *) object_file_path);
00279            continue;
00280        }
00281 
00282        result =
00283            ime_module_load_object_file(ime_module, base_dir,
00284                                    object_file_path, ime_args);
00285        free((char *) object_file_path);
00286        if (result == IME_FAIL) {
00287            free((char *) ime_module_context);
00288            ime_module_destroy(ime_module);
00289            continue;
00290        }
00291 
00292        ime_module_context->enabled = 1;
00293        if (enable_str && *enable_str) {
00294            if (!strcasecmp(enable_str, "0") ||
00295               !strcasecmp(enable_str, "false"))
00296               ime_module_context->enabled = 0;
00297        }
00298 
00299        ime_module_context->ime_module = ime_module;
00300        ime_modules[num_ime_modules] = ime_module_context;
00301        num_ime_modules++;
00302     }
00303 
00304     le_info->num_ime_modules = num_ime_modules;
00305     le_info->ime_modules = ime_modules;
00306 
00307     return (LE_OK);
00308 }
00309 
00310 LeResult le_info_load_imm_keybindings(LeInfoRec * le_info,
00311                                   IbmlCategory * ibml_category)
00312 {
00313 #define IMM_KEYBINDING_KEYCODE   "keycode"
00314 #define IMM_KEYBINDING_MODIFIER  "modifier"
00315 
00316     int num_imm_keybindings;
00317     ImmKeybindingRec **imm_keybindings, imm_keybinding;
00318 
00319     IbmlElement *ibml_element;
00320     IbmlProperty *ibml_property;
00321 
00322     int i, j;
00323 
00324     char *id, *scope;
00325     char *name, *value;
00326 
00327     if (le_info == NULL)
00328        return (LE_FAIL);
00329     if (ibml_category == NULL)
00330        return (LE_FAIL);
00331     if (ibml_category->num_elements <= 0)
00332        return (LE_FAIL);
00333 
00334     imm_keybindings =
00335        (ImmKeybindingRec **) calloc(ibml_category->num_elements,
00336                                  sizeof(ImmKeybindingRec *));
00337     if (imm_keybindings == NULL) {
00338        return (LE_FAIL);
00339     }
00340 
00341     num_imm_keybindings = 0;
00342     for (i = 0; i < ibml_category->num_elements; i++) {
00343        ImmKeybindingRec *imm_keybinding = NULL;
00344        char *keycode = NULL;
00345        char *modifier = NULL;
00346        char *key_type = NULL;
00347        LeResult result;
00348 
00349        ibml_element = ibml_category->elements[i];
00350        if (!ibml_element)
00351            continue;
00352 
00353        id = (char *) ibml_element->id;
00354        scope = (char *) ibml_element->scope;
00355        DEBUG_printf("id:%s, scope:%s\n", id ? id : "NULL",
00356                    scope ? scope : "NULL");
00357        if (!id || !*id)
00358            continue;
00359 
00360        key_type = id;
00361        for (j = 0; j < ibml_element->num_properties; j++) {
00362            ibml_property = ibml_element->properties[j];
00363            if (!ibml_property)
00364               continue;
00365 
00366            name = (char *) ibml_property->name;
00367            if (!name || !*name)
00368               continue;
00369 
00370            value = (char *) ibml_property->value;
00371            if (!value || !*value)
00372               continue;
00373 
00374            if (!strcasecmp(name, IMM_KEYBINDING_KEYCODE)) {
00375               keycode = (char *) value;
00376            } else if (!strcasecmp(name, IMM_KEYBINDING_MODIFIER)) {
00377               modifier = (char *) value;
00378            }
00379        }
00380 
00381        if (!keycode || !*keycode)
00382            continue;
00383        if (!modifier || !*modifier)
00384            continue;
00385 
00386        imm_keybinding = (ImmKeybindingRec *) imm_keybinding_new();
00387        if (imm_keybinding == NULL) {
00388            continue;
00389        }
00390 
00391        result =
00392            imm_keybinding_set_value(imm_keybinding, keycode, modifier,
00393                                  key_type);
00394        if (result == IME_FAIL) {
00395            imm_keybinding_destroy(imm_keybinding);
00396            continue;
00397        }
00398 
00399        imm_keybindings[num_imm_keybindings] = imm_keybinding;
00400        num_imm_keybindings++;
00401     }
00402 
00403     le_info->num_imm_keybindings = num_imm_keybindings;
00404     le_info->imm_keybindings = imm_keybindings;
00405 
00406     return (LE_OK);
00407 }