char* FcitxLibPinyinGetSysPath(LIBPINYIN_LANGUAGE_TYPE type) { char* syspath = NULL; #ifdef LIBPINYIN_TOOLS_FOUND if (type == LPLT_Simplified) { #endif /* portable detect here */ if (getenv("FCITXDIR")) { syspath = fcitx_utils_get_fcitx_path_with_filename("datadir", "libpinyin/data"); } else { syspath = strdup(LIBPINYIN_PKGDATADIR "/data"); } #ifdef LIBPINYIN_TOOLS_FOUND } else { /* portable detect here */ if (getenv("FCITXDIR")) { syspath = fcitx_utils_get_fcitx_path_with_filename("pkgdatadir", "libpinyin/zhuyin_data"); } else { syspath = strdup(FCITX_LIBPINYIN_ZHUYIN_DATADIR); } } #endif return syspath; }
FCITX_EXPORT_API FcitxComposeTable* fcitx_compose_table_new(const char* locale) { FcitxComposeTable* table = _fcitx_compose_table_alloc(locale); int nPossibleLocation = 0; const char* possibleLocation[5]; char* needFree[2]; char* env; if ((env = getenv("FCITX_COMPOSE_DIR")) != NULL) { possibleLocation[nPossibleLocation++] = env; } possibleLocation[nPossibleLocation++] = needFree[0] = fcitx_utils_get_fcitx_path_with_filename("datadir", "X11/locale"); possibleLocation[nPossibleLocation++] = needFree[1] = fcitx_utils_get_fcitx_path_with_filename("libdir", "X11/locale"); possibleLocation[nPossibleLocation++] = "/usr/share/X11/locale"; possibleLocation[nPossibleLocation++] = "/usr/lib/X11/locale"; _fcitx_compose_table_find_system_compose_dir(table, nPossibleLocation, possibleLocation); _fcitx_compose_table_find_compose_file(table); _fcitx_compose_table_order_compose_table(table); free(needFree[0]); free(needFree[1]); return table; }
char* FcitxLibpinyinGetSysPath(LIBPINYIN_LANGUAGE_TYPE type) { char* syspath = NULL; if (type == LPLT_Simplified) { #if FCITX_CHECK_VERSION(4,2,1) /* portable detect here */ if (getenv("FCITXDIR")) { syspath = fcitx_utils_get_fcitx_path_with_filename("datadir", "libpinyin/data"); } else #endif { syspath = strdup(LIBPINYIN_PKGDATADIR "/data"); } } else { #if FCITX_CHECK_VERSION(4,2,1) /* portable detect here */ if (getenv("FCITXDIR")) { syspath = fcitx_utils_get_fcitx_path_with_filename("pkgdatadir", "libpinyin/zhuyin_data"); } else #endif { syspath = strdup(FCITX_LIBPINYIN_ZHUYIN_DATADIR); } } return syspath; }
void ScelConverter::convert(const QString& from, const QString& to, bool removeOriginFile) { if (!m_file.open()) { emit message(QMessageBox::Warning, _("Create temporary file failed.")); emit finished(false); return; } else { emit message(QMessageBox::Information, _("Temporary file created.")); } m_file.close(); m_file.setAutoRemove(false); m_fromFile = from; if (removeOriginFile) { connect(this, SIGNAL(finished(bool)), this, SLOT(removeTempFile())); } char* path = fcitx_utils_get_fcitx_path_with_filename("bindir", "scel2org"); QStringList arguments; arguments << "-a" << "-o" << m_file.fileName() << from; m_process.start(path, arguments); m_process.closeReadChannel(QProcess::StandardError); m_process.closeReadChannel(QProcess::StandardOutput); connect(&m_process, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(finished(int,QProcess::ExitStatus))); m_name = to; }
void DictModel::defaults() { char* path = fcitx_utils_get_fcitx_path_with_filename("pkgdatadir", "kkc/dictionary_list"); QFile f(path); if (f.open(QIODevice::ReadOnly)) { load(f); } }
void open_native_file(GtkButton *button, gpointer user_data) { FcitxSubConfigWidget* widget = (FcitxSubConfigWidget*) user_data; char *newpath = NULL; char* qtguiwrapper = fcitx_utils_get_fcitx_path_with_filename ("libdir", "fcitx/libexec/fcitx-qt-gui-wrapper"); if (qtguiwrapper) { gchar* argv[4]; argv[0] = qtguiwrapper; argv[1] = "--test"; argv[2] = widget->subconfig->nativepath; argv[3] = 0; int exit_status = 1; g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &exit_status, NULL); if (exit_status == 0) { gchar* argv2[3]; argv2[0] = qtguiwrapper; argv2[1] = widget->subconfig->nativepath; argv2[2] = 0; g_spawn_async(NULL, argv2, NULL, 0, NULL, NULL, NULL, NULL); free(newpath); } g_free(qtguiwrapper); if (exit_status == 0) { return; } } if (g_hash_table_size(widget->subconfig->filelist) > 0) { GHashTableIter iter; g_hash_table_iter_init(&iter, widget->subconfig->filelist); gpointer key; if (g_hash_table_iter_next(&iter, &key, NULL)) { FILE* fp = FcitxXDGGetFileWithPrefix("", key, "r", &newpath); if (fp) fclose(fp); } } else { FILE* fp = FcitxXDGGetFileUserWithPrefix("", widget->subconfig->nativepath, "w", &newpath); if (fp) { g_hash_table_insert(widget->subconfig->filelist, widget->subconfig->nativepath, NULL); fclose(fp); } } if (newpath) { gchar* filename = newpath; gchar* argv[3]; argv[0] = "xdg-open"; argv[1] = filename; argv[2] = 0; g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); free(newpath); } }
FCITX_EXPORT_API void fcitx_utils_launch_tool(const char* name, const char* arg) { char* command = fcitx_utils_get_fcitx_path_with_filename("bindir", name); char* args[] = { command, (char*)(intptr_t)arg, /* parent process haven't even touched this... */ NULL }; fcitx_utils_start_process(args); free(command); }
cairo_surface_t* LoadVKImage(VKWindow* vkWindow) { FcitxVKState* vkstate = vkWindow->owner; boolean fallback = true; char vkimage[] = "keyboard.png"; cairo_surface_t *image = InvokeVaArgs(vkstate->owner, FCITX_CLASSIC_UI, LOADIMAGE, vkimage, &fallback); if (image) return image; if (!vkWindow->keyboard) { char* path = fcitx_utils_get_fcitx_path_with_filename("pkgdatadir", "skin/default/keyboard.png"); vkWindow->keyboard = cairo_image_surface_create_from_png(path); free(path); } return vkWindow->keyboard; }
void run_plugin(GtkButton* button, gpointer user_data) { FcitxSubConfigWidget* widget = (FcitxSubConfigWidget*) user_data; char *newpath = NULL; char* qtguiwrapper = fcitx_utils_get_fcitx_path_with_filename ("libdir", "fcitx/libexec/fcitx-qt-gui-wrapper"); if (qtguiwrapper) { gchar* argv[4]; argv[0] = qtguiwrapper; argv[1] = "--test"; argv[2] = widget->subconfig->nativepath; argv[3] = 0; int exit_status = 1; g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &exit_status, NULL); if (exit_status == 0) { gchar* argv2[3]; argv2[0] = qtguiwrapper; argv2[1] = widget->subconfig->nativepath; argv2[2] = 0; g_spawn_async(NULL, argv2, NULL, 0, NULL, NULL, NULL, NULL); free(newpath); } g_free(qtguiwrapper); if (exit_status == 0) { return; } } GtkWidget* dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET(widget))), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", _("Didn't install related component.")); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); }
/** @加载皮肤配置文件 */ int LoadSkinConfig(FcitxSkin* sc, char** skinType) { FILE *fp; boolean isreload = False; int ret = 0; if (sc->config.configFile) { utarray_done(&sc->skinMainBar.skinPlacement); FcitxConfigFree(&sc->config); UnloadImage(sc); } memset(sc, 0, sizeof(FcitxSkin)); utarray_init(&sc->skinMainBar.skinPlacement, &place_icd); reload: if (!isreload) { char *buf; fcitx_utils_alloc_cat_str(buf, *skinType, "/fcitx_skin.conf"); fp = FcitxXDGGetFileWithPrefix("skin", buf, "r", NULL); free(buf); } else { char *path = fcitx_utils_get_fcitx_path_with_filename( "pkgdatadir", "/skin/default/fcitx_skin.conf"); fp = fopen(path, "r"); free(path); } if (fp) { FcitxConfigFile *cfile; FcitxConfigFileDesc* skinDesc = GetSkinDesc(); if (sc->config.configFile == NULL) { cfile = FcitxConfigParseConfigFileFp(fp, skinDesc); } else { cfile = sc->config.configFile; cfile = FcitxConfigParseIniFp(fp, cfile); } if (!cfile) { fclose(fp); fp = NULL; } else { FcitxSkinConfigBind(sc, cfile, skinDesc); FcitxConfigBindSync((FcitxGenericConfig*)sc); } } if (!fp) { if (isreload) { FcitxLog(FATAL, _("Can not load default skin, is installion correct?")); perror("fopen"); ret = 1; // 如果安装目录里面也没有配置文件,那就只好告诉用户,无法运行了 } else { perror("fopen"); FcitxLog(WARNING, _("Can not load skin %s, return to default"), *skinType); if (*skinType) free(*skinType); *skinType = strdup("default"); isreload = true; goto reload; } } if (fp) fclose(fp); sc->skinType = skinType; return ret; }
void* DBusCreate(FcitxInstance* instance) { FcitxDBus *dbusmodule = (FcitxDBus*) fcitx_utils_malloc0(sizeof(FcitxDBus)); FcitxAddon* dbusaddon = FcitxAddonsGetAddonByName(FcitxInstanceGetAddons(instance), FCITX_DBUS_NAME); dbusmodule->owner = instance; DBusError err; if (FcitxInstanceIsTryReplace(instance)) { fcitx_utils_launch_tool("fcitx-remote", "-e"); sleep(1); } dbus_threads_init_default(); // first init dbus dbus_error_init(&err); int retry = 0; DBusConnection* conn = NULL; char* servicename = NULL; asprintf(&servicename, "%s-%d", FCITX_DBUS_SERVICE, fcitx_utils_get_display_number()); /* do session dbus initialize */ do { if (!getenv("DISPLAY") && !getenv("DBUS_SESSION_BUS_ADDRESS")) { FcitxLog(WARNING, "Without DISPLAY or DBUS_SESSION_BUS_ADDRESS session bus will not work"); break; } /* try to get session dbus */ while (1) { conn = dbus_bus_get(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)) { FcitxLog(WARNING, "Connection Error (%s)", err.message); dbus_error_free(&err); dbus_error_init(&err); } if (NULL == conn && retry < MAX_RETRY_TIMES) { retry ++; sleep(RETRY_INTERVAL * retry); } else { break; } } if (NULL == conn) { break; } if (!dbus_connection_add_filter(conn, DBusModuleFilter, dbusmodule, NULL)) break; if (!dbus_connection_set_watch_functions(conn, DBusAddWatch, DBusRemoveWatch, NULL, &dbusmodule->watches, NULL)) { FcitxLog(WARNING, "Add Watch Function Error"); dbus_error_free(&err); dbus_error_init(&err); dbus_connection_unref(conn); conn = NULL; break; } /* from here we know dbus connection is successful, now we need to register the service */ dbus_connection_set_exit_on_disconnect(conn, FALSE); dbusmodule->conn = conn; boolean request_retry = false; int replaceCountdown = FcitxInstanceIsTryReplace(instance) ? 3 : 0; FcitxInstanceResetTryReplace(instance); do { request_retry = false; // request a name on the bus int ret = dbus_bus_request_name(conn, servicename, DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); if (dbus_error_is_set(&err)) { FcitxLog(WARNING, "Name Error (%s)", err.message); goto dbus_init_failed; } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { FcitxLog(WARNING, "DBus Service Already Exists"); if (replaceCountdown > 0) { replaceCountdown --; fcitx_utils_launch_tool("fcitx-remote", "-e"); /* sleep for a while and retry */ sleep(1); request_retry = true; continue; } /* if we know fcitx exists, we should exit. */ dbus_error_free(&err); free(servicename); free(dbusmodule); FcitxInstanceEnd(instance); return NULL; } } while (request_retry); dbus_connection_flush(dbusmodule->conn); } while(0); DBusConnection* privconn = NULL; do { int noPrivateDBus = fcitx_utils_get_boolean_env("FCITX_NO_PRIVATE_DBUS", false); if (noPrivateDBus) break; char* file; FILE* dbusfp = FcitxXDGGetFileWithPrefix("dbus", "daemon.conf", "r", &file); if (dbusfp) { fclose(dbusfp); } else { free(file); file = NULL; } dbusmodule->daemon = DBusLaunch(file); fcitx_utils_free(file); if (dbusmodule->daemon.pid == 0) break; privconn = dbus_connection_open(dbusmodule->daemon.address, &err); if (dbus_error_is_set(&err)) { FcitxLog(ERROR, "Private dbus daemon connection error (%s)", err.message); break; } dbus_bus_register(privconn, &err); if (dbus_error_is_set(&err)) { FcitxLog(ERROR, "Private dbus bus register error (%s)", err.message); break; } int ret = dbus_bus_request_name(privconn, servicename, DBUS_NAME_FLAG_DO_NOT_QUEUE, &err); if (dbus_error_is_set(&err)) { FcitxLog(WARNING, "Private Name Error (%s)", err.message); break; } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { FcitxLog(ERROR, "Private DBus Service Already Exists, fcitx being hacked?"); break; } if (!dbus_connection_add_filter(privconn, DBusModuleFilter, dbusmodule, NULL)) break; if (!dbus_connection_set_watch_functions(privconn, DBusAddWatch, DBusRemoveWatch, NULL, &dbusmodule->watches, NULL)) { FcitxLog(WARNING, "Add Watch Function Error"); break; } char* addressFile = NULL; char* localMachineId = dbus_get_local_machine_id(); asprintf(&addressFile, "%s-%d", localMachineId, fcitx_utils_get_display_number()); dbus_free(localMachineId); FILE* fp = FcitxXDGGetFileUserWithPrefix("dbus", addressFile, "w", NULL); free(addressFile); if (!fp) break; fprintf(fp, "%s", dbusmodule->daemon.address); fwrite("\0", sizeof(char), 1, fp); pid_t curPid = getpid(); fwrite(&dbusmodule->daemon.pid, sizeof(pid_t), 1, fp); fwrite(&curPid, sizeof(pid_t), 1, fp); fclose(fp); dbusmodule->privconn = privconn; char* command = fcitx_utils_get_fcitx_path_with_filename("bindir", "/fcitx-dbus-watcher"); char* pidstring = NULL; asprintf(&pidstring, "%d", dbusmodule->daemon.pid); char* args[] = { command, dbusmodule->daemon.address, pidstring, NULL }; fcitx_utils_start_process(args); free(command); free(pidstring); } while(0); if (!dbusmodule->privconn) { if (privconn) { dbus_connection_unref(privconn); DBusKill(&dbusmodule->daemon); } } FcitxModuleAddFunction(dbusaddon, DBusGetConnection); FcitxModuleAddFunction(dbusaddon, DBusGetPrivateConnection); dbus_error_free(&err); dbusmodule->serviceName = servicename; return dbusmodule; dbus_init_failed: dbus_error_free(&err); fcitx_utils_free(servicename); if (conn) dbus_connection_unref(conn); DBusKill(&dbusmodule->daemon); fcitx_utils_free(dbusmodule); return NULL; }
static void py_enhance_load_py(PinyinEnhance *pyenhance) { UT_array *array = &pyenhance->py_list; if (array->icd) return; utarray_init(array, fcitx_ptr_icd); FILE *fp; char *fname; fname = fcitx_utils_get_fcitx_path_with_filename( "pkgdatadir", "py-enhance/"PY_TABLE_FILE); fp = fopen(fname, "r"); free(fname); if (fp) { FcitxMemoryPool *pool = pyenhance->static_pool; char buff[UTF8_MAX_LENGTH + 1]; int buff_size = 33; int8_t *list_buff = malloc(buff_size); size_t res; int8_t word_l; int8_t count; int8_t py_size; int i; int8_t *py_list; int8_t *tmp; /** * Format: * int8_t word_l; * char word[word_l]; * int8_t count; * int8_t py[count][3]; **/ while (true) { res = fread(&word_l, 1, 1, fp); if (!res || word_l < 0 || word_l > UTF8_MAX_LENGTH) break; res = fread(buff, word_l + 1, 1, fp); if (!res) break; count = buff[word_l]; if (count < 0) break; if (count == 0) continue; py_size = count * 3; if (buff_size < py_size) { buff_size = py_size; list_buff = realloc(list_buff, buff_size); } res = fread(list_buff, py_size, 1, fp); if (!res) break; py_list = fcitx_memory_pool_alloc(pool, word_l + py_size + 3); py_list[0] = word_l + 1; py_list++; memcpy(py_list, buff, word_l); tmp = py_list + word_l; *tmp = '\0'; tmp++; *tmp = count; memcpy(tmp + 1, list_buff, py_size); for (i = utarray_len(array) - 1;i >= 0;i--) { if (strcmp(*(char**)_utarray_eltptr(array, i), (char*)py_list) < 0) { break; } } utarray_insert(array, &py_list, i + 1); } free(list_buff); fclose(fp); } }
static void py_enhance_load_py(PinyinEnhance *pyenhance) { UT_array *array = &pyenhance->py_list; if (array->icd) return; utarray_init(array, fcitx_ptr_icd); FILE *fp; char *fname; fname = fcitx_utils_get_fcitx_path_with_filename( "pkgdatadir", "py-enhance/"PY_TABLE_FILE); fp = fopen(fname, "r"); free(fname); if (fp) { FcitxMemoryPool *pool = pyenhance->static_pool; char buff[UTF8_MAX_LENGTH + 1]; int buff_size = 33; int8_t *list_buff = malloc(buff_size); size_t res; int8_t word_l; int8_t count; int8_t min_size; FcitxPYEnhancePYList *py_list; int i; FcitxPYEnhancePY *py; while (true) { res = fread(&word_l, 1, 1, fp); if (!res || word_l < 0 || word_l > UTF8_MAX_LENGTH) break; res = fread(buff, word_l, 1, fp); if (!res) break; res = fread(&count, 1, 1, fp); if (!res || count < 0) break; if (count == 0) continue; min_size = count * 3; if (buff_size < min_size) { buff_size = min_size; list_buff = realloc(list_buff, buff_size); } res = fread(list_buff, min_size, 1, fp); if (!res) break; py_list = fcitx_memory_pool_alloc_align( pool, (sizeof(FcitxPYEnhancePYList) + sizeof(FcitxPYEnhancePY) * count), 1); memcpy(py_list->word, buff, word_l); py_list->word[word_l] = '\0'; py_list->count = count; for (i = 0;i < count;i++) { py = pinyin_enhance_pylist_get(py_list, i); int8_t *tmp = list_buff + i * 3; py->konsonant = tmp[0]; py->vokal = tmp[1]; py->tone = tmp[2]; } for (i = utarray_len(array) - 1;i >= 0;i--) { FcitxPYEnhancePYList *ele; ele = *(FcitxPYEnhancePYList**)_utarray_eltptr(array, i); if (strcmp(ele->word, py_list->word) < 0) { break; } } utarray_insert(array, &py_list, i + 1); } free(list_buff); fclose(fp); } }