| 1 | /* |
| 2 | * Copyright (c) 2009 Kov Chai <[email protected]> |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of either the GNU Lesser |
| 5 | * General Public License Version 2.1 only ("LGPL") or the Common Development and |
| 6 | * Distribution License ("CDDL")(collectively, the "License"). You may not use this |
| 7 | * file except in compliance with the License. You can obtain a copy of the CDDL at |
| 8 | * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at |
| 9 | * http://www.opensource.org/licenses/lgpl-license.php. See the License for the |
| 10 | * specific language governing permissions and limitations under the License. When |
| 11 | * distributing the software, include this License Header Notice in each file and |
| 12 | * include the full text of the License in the License file as well as the |
| 13 | * following notice: |
| 14 | * |
| 15 | * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE |
| 16 | * (CDDL) |
| 17 | * For Covered Software in this distribution, this License shall be governed by the |
| 18 | * laws of the State of California (excluding conflict-of-law provisions). |
| 19 | * Any litigation relating to this License shall be subject to the jurisdiction of |
| 20 | * the Federal Courts of the Northern District of California and the state courts |
| 21 | * of the State of California, with venue lying in Santa Clara County, California. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * If you wish your version of this file to be governed by only the CDDL or only |
| 26 | * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to |
| 27 | * include this software in this distribution under the [CDDL or LGPL Version 2.1] |
| 28 | * license." If you don't indicate a single choice of license, a recipient has the |
| 29 | * option to distribute your version of this file under either the CDDL or the LGPL |
| 30 | * Version 2.1, or to extend the choice of license to its licensees as provided |
| 31 | * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL |
| 32 | * Version 2 license, then the option applies only if the new code is made subject |
| 33 | * to such option by the copyright holder. |
| 34 | */ |
| 35 | |
| 36 | #include <cassert> |
| 37 | #include <algorithm> |
| 38 | #include <sstream> |
| 39 | |
| 40 | #include <sunpinyin.h> |
| 41 | |
| 42 | #include "sunpinyin_property.h" |
| 43 | #include "sunpinyin_lookup_table.h" |
| 44 | #include "sunpinyin_config.h" |
| 45 | #include "sunpinyin_config_keys.h" |
| 46 | #include "imi_ibus_win.h" |
| 47 | #include "ibus_portable.h" |
| 48 | #include "sunpinyin_engine.h" |
| 49 | |
| 50 | extern ibus::Config config; |
| 51 | |
| 52 | SunPinyinEngine::SunPinyinEngine(IBusEngine *engine) |
| 53 | : m_engine(engine), |
| 54 | m_status_prop(SunPinyinProperty::create_status_prop(engine)), |
| 55 | m_letter_prop(SunPinyinProperty::create_letter_prop(engine)), |
| 56 | m_punct_prop(SunPinyinProperty::create_punct_prop(engine)), |
| 57 | m_wh(NULL), |
| 58 | m_pv(NULL), |
| 59 | m_hotkey_profile(NULL), |
| 60 | m_hard_forward(false) |
| 61 | { |
| 62 | CSunpinyinSessionFactory& factory = CSunpinyinSessionFactory::getFactory(); |
| 63 | |
| 64 | CSunpinyinSessionFactory::EPyScheme pinyin_scheme = |
| 65 | m_config.get_py_scheme(CSunpinyinSessionFactory::QUANPIN); |
| 66 | factory.setPinyinScheme(pinyin_scheme); |
| 67 | if (pinyin_scheme == CSunpinyinSessionFactory::QUANPIN) { |
| 68 | update_fuzzy_pinyins(); |
| 69 | update_correction_pinyins(); |
| 70 | update_fuzzy_segs(); |
| 71 | } else { |
| 72 | update_shuangpin_type(); |
| 73 | } |
| 74 | update_user_data_dir(); |
| 75 | update_punct_mappings(); |
| 76 | |
| 77 | factory.setCandiWindowSize(m_config.get(CONFIG_GENERAL_PAGE_SIZE, 10)); |
| 78 | |
| 79 | m_pv = factory.createSession(); |
| 80 | if (!m_pv) |
| 81 | return; |
| 82 | |
| 83 | m_hotkey_profile = new CHotkeyProfile(); |
| 84 | m_pv->setHotkeyProfile(m_hotkey_profile); |
| 85 | |
| 86 | m_wh = new CIBusWinHandler(this); |
| 87 | m_pv->attachWinHandler(m_wh); |
| 88 | |
| 89 | m_prop_list = ibus_prop_list_new(); |
| 90 | |
| 91 | ibus_prop_list_append(m_prop_list, m_status_prop); |
| 92 | ibus_prop_list_append(m_prop_list, m_letter_prop); |
| 93 | ibus_prop_list_append(m_prop_list, m_punct_prop); |
| 94 | ibus_prop_list_append(m_prop_list, m_setup_prop); |
| 95 | |
| 96 | update_config(); |
| 97 | } |
| 98 | |
| 99 | SunPinyinEngine::~SunPinyinEngine() |
| 100 | { |
| 101 | if (m_pv) { |
| 102 | CSunpinyinSessionFactory& factory = |
| 103 | CSunpinyinSessionFactory::getFactory(); |
| 104 | factory.destroySession(m_pv); |
| 105 | } |
| 106 | |
| 107 | delete m_wh; |
| 108 | delete m_hotkey_profile; |
| 109 | } |
| 110 | |
| 111 | static CKeyEvent |
| 112 | translate_key(guint key_val, guint /*key_code*/, guint modifiers) |
| 113 | { |
| 114 | // XXX: may need to move this logic into CKeyEvent |
| 115 | if (key_val > 0x20 && key_val < 0x7f // isprint(key_val) && !isspace(key_val) |
| 116 | && !(modifiers & IM_CTRL_MASK)) { |
| 117 | // we only care about key_val here |
| 118 | return CKeyEvent(key_val, key_val, modifiers); |
| 119 | } else { |
| 120 | // what matters is key_code, but ibus sents me key_code as key_val |
| 121 | return CKeyEvent(key_val, 0, modifiers); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | gboolean |
| 126 | SunPinyinEngine::process_key_event (guint key_val, |
| 127 | guint key_code, |
| 128 | guint modifiers) |
| 129 | { |
| 130 | CKeyEvent key = translate_key(key_val, key_code, modifiers); |
| 131 | |
| 132 | if (getenv("DISABLE_HARD_FORWARD") == NULL) { |
| 133 | // Ctrl+<space> is pressed. let's just hard code these. |
| 134 | // it looks ridiculous, but on what else do you need to do this hack? |
| 135 | if (key.code == 0x20 && key.modifiers == IM_CTRL_MASK) { |
| 136 | m_hard_forward = !m_hard_forward; |
| 137 | return TRUE; |
| 138 | } else if (m_hard_forward) { |
| 139 | return FALSE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!m_pv->getStatusAttrValue(CIBusWinHandler::STATUS_ID_CN)) { |
| 144 | // we are in English input mode |
| 145 | if (!m_hotkey_profile->isModeSwitchKey(key)) { |
| 146 | m_hotkey_profile->rememberLastKey(key); |
| 147 | return FALSE; |
| 148 | } |
| 149 | } else if (m_hotkey_profile->isModeSwitchKey(key)) { |
| 150 | m_pv->onKeyEvent(CKeyEvent(IM_VK_ENTER, 0, 0)); |
| 151 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_CN, false); |
| 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | return m_pv->onKeyEvent(key); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | SunPinyinEngine::focus_in () |
| 160 | { |
| 161 | ibus_engine_register_properties(m_engine, m_prop_list); |
| 162 | m_pv->updateWindows(CIMIView::PREEDIT_MASK | CIMIView::CANDIDATE_MASK); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | SunPinyinEngine::focus_out () |
| 167 | { |
| 168 | reset(); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | SunPinyinEngine::reset () |
| 173 | { |
| 174 | m_pv->updateWindows(m_pv->clearIC()); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | SunPinyinEngine::enable () |
| 179 | { |
| 180 | bool is_cn = m_config.is_initial_mode_cn(); |
| 181 | m_status_prop.update(is_cn); |
| 182 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_CN, is_cn); |
| 183 | |
| 184 | bool is_letter_full = m_config.is_initial_letter_full(); |
| 185 | m_letter_prop.update(is_letter_full); |
| 186 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, is_letter_full); |
| 187 | |
| 188 | bool is_punct_full = m_config.is_initial_punct_full(); |
| 189 | m_punct_prop.update(is_punct_full); |
| 190 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, is_punct_full); |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | SunPinyinEngine::disable () |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | SunPinyinEngine::page_up () |
| 200 | { |
| 201 | m_pv->onCandidatePageRequest(-1, true /* relative */); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | SunPinyinEngine::page_down () |
| 206 | { |
| 207 | m_pv->onCandidatePageRequest(1, true /* relative */); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | SunPinyinEngine::property_activate (const std::string& property, unsigned /*state*/) |
| 212 | { |
| 213 | if (m_status_prop.toggle(property)) { |
| 214 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_CN, |
| 215 | m_status_prop.state()); |
| 216 | } else if (m_letter_prop.toggle(property)) { |
| 217 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, |
| 218 | m_letter_prop.state()); |
| 219 | } else if (m_punct_prop.toggle(property)) { |
| 220 | m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, |
| 221 | m_punct_prop.state()); |
| 222 | } else { |
| 223 | // try to launch the setup UI |
| 224 | m_setup_prop.launch(property); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | SunPinyinEngine::candidate_clicked (guint index) |
| 230 | { |
| 231 | m_pv->onCandidateSelectRequest(index); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | SunPinyinEngine::cursor_up () |
| 236 | { |
| 237 | if (m_lookup_table.cursor_up()) { |
| 238 | update_lookup_table(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | SunPinyinEngine::cursor_down () |
| 244 | { |
| 245 | if (m_lookup_table.cursor_down()) { |
| 246 | update_lookup_table(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | bool |
| 251 | SunPinyinEngine::onConfigChanged(const COptionEvent& event) |
| 252 | { |
| 253 | if (event.name == CONFIG_GENERAL_MEMORY_POWER) { |
| 254 | update_history_power(); |
| 255 | } else if (event.name == CONFIG_GENERAL_PAGE_SIZE) { |
| 256 | update_cand_window_size(); |
| 257 | } else if (event.name == CONFIG_GENERAL_CHARSET_LEVEL) { |
| 258 | update_charset_level(); |
| 259 | } else if (event.name == CONFIG_GENERAL_MAX_BEST) { |
| 260 | update_max_best(); |
| 261 | } else if (event.name == CONFIG_GENERAL_MAX_TAIL_CANDIDATE) { |
| 262 | update_max_tail_candidate(); |
| 263 | } else if (event.name == CONFIG_KEYBOARD_MODE_SWITCH) { |
| 264 | update_mode_key(); |
| 265 | } else if (event.name == CONFIG_KEYBOARD_PUNCT_SWITCH) { |
| 266 | update_punct_key(); |
| 267 | } else if (event.name == CONFIG_KEYBOARD_PAGE_COMMA) { |
| 268 | update_page_key_comma(); |
| 269 | } else if (event.name == CONFIG_KEYBOARD_PAGE_MINUS) { |
| 270 | update_page_key_minus(); |
| 271 | } else if (event.name == CONFIG_KEYBOARD_PAGE_BRACKET) { |
| 272 | update_page_key_bracket(); |
| 273 | } else if (event.name == CONFIG_QUANPIN_FUZZYSEGS_ENABLED) { |
| 274 | update_fuzzy_segs(); |
| 275 | } else if (event.name == CONFIG_KEYBOARD_CANCEL_BACKSPACE) { |
| 276 | update_cancel_with_backspace(); |
| 277 | } else if (event.name == CONFIG_KEYBOARD_SMARK_PUNCT) { |
| 278 | update_smart_punc(); |
| 279 | } |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | SunPinyinEngine::update_config() |
| 286 | { |
| 287 | update_history_power(); |
| 288 | update_cand_window_size(); |
| 289 | update_max_best(); |
| 290 | update_max_tail_candidate(); |
| 291 | update_charset_level(); |
| 292 | update_page_key_minus(); |
| 293 | update_page_key_comma(); |
| 294 | update_page_key_bracket(); |
| 295 | update_mode_key(); |
| 296 | update_punct_key(); |
| 297 | update_cancel_with_backspace(); |
| 298 | update_smart_punc(); |
| 299 | update_punct_mappings(); |
| 300 | update_candi_delete_key(); |
| 301 | // update_quanpin_config(); |
| 302 | // update_shuangpin_config(); |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | SunPinyinEngine::commit_string (const std::wstring& str) |
| 307 | { |
| 308 | IBusText *text; |
| 309 | text = ibus_text_new_from_ucs4((const gunichar*) str.c_str()); |
| 310 | ibus_engine_commit_text(m_engine, text); |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | SunPinyinEngine::update_candidates(const ICandidateList& cl) |
| 315 | { |
| 316 | if (m_lookup_table.update_candidates(cl) > 0) |
| 317 | update_lookup_table(); |
| 318 | else |
| 319 | ibus_engine_hide_lookup_table (m_engine); |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | SunPinyinEngine::update_lookup_table() |
| 324 | { |
| 325 | ibus_engine_update_lookup_table(m_engine, m_lookup_table, TRUE); |
| 326 | } |
| 327 | |
| 328 | bool |
| 329 | SunPinyinEngine::is_valid() const |
| 330 | { |
| 331 | return m_pv != NULL; |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | find_embed_preedit_pos(const IPreeditString& preedit) |
| 336 | { |
| 337 | int mask = IPreeditString::USER_CHOICE & IPreeditString::HANZI_CHAR; |
| 338 | for (size_t i = 0; i < preedit.charTypeSize(); i++) { |
| 339 | if ((preedit.charTypeAt(i) & mask) == 0) { |
| 340 | return i; |
| 341 | } |
| 342 | } |
| 343 | return preedit.charTypeSize(); |
| 344 | } |
| 345 | |
| 346 | enum {ORANGE = 0xE76F00, GRAY_BLUE = 0x35556B, WHITE = 0xFFFFFF, |
| 347 | BLACK = 0x000000}; |
| 348 | |
| 349 | void |
| 350 | SunPinyinEngine::update_preedit_string(const IPreeditString& preedit) |
| 351 | { |
| 352 | if (preedit.size() > 0) { |
| 353 | wstring wstr(preedit.string()); |
| 354 | int embed_pos = find_embed_preedit_pos(preedit); |
| 355 | wstring embed_wstr = wstr.substr(0, embed_pos); |
| 356 | wstring preedit_wstr = wstr.substr(embed_pos); |
| 357 | const gunichar* embed_cstr = (const gunichar*) embed_wstr.c_str(); |
| 358 | const gunichar* preedit_cstr = (const gunichar*) preedit_wstr.c_str(); |
| 359 | IBusText* preedit_text = ibus_text_new_from_ucs4(preedit_cstr); |
| 360 | int caret = preedit.caret() - embed_pos; |
| 361 | |
| 362 | if (preedit.caret() < preedit.size()) { |
| 363 | ibus_text_append_attribute(preedit_text, IBUS_ATTR_TYPE_FOREGROUND, |
| 364 | WHITE, caret, preedit_wstr.size()); |
| 365 | ibus_text_append_attribute(preedit_text, IBUS_ATTR_TYPE_BACKGROUND, |
| 366 | GRAY_BLUE, caret, preedit_wstr.size()); |
| 367 | } |
| 368 | ibus_engine_update_auxiliary_text(m_engine, preedit_text, TRUE); |
| 369 | |
| 370 | ibus_engine_update_preedit_text(m_engine, |
| 371 | ibus_text_new_from_ucs4(embed_cstr), |
| 372 | preedit.caret(), TRUE); |
| 373 | |
| 374 | } else { |
| 375 | ibus_engine_hide_auxiliary_text(m_engine); |
| 376 | ibus_engine_hide_preedit_text(m_engine); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | SunPinyinEngine::update_status_property(bool cn) |
| 382 | { |
| 383 | m_status_prop.update(cn); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | SunPinyinEngine::update_punct_property(bool full) |
| 388 | { |
| 389 | m_punct_prop.update(full); |
| 390 | } |
| 391 | |
| 392 | void |
| 393 | SunPinyinEngine::update_letter_property(bool full) |
| 394 | { |
| 395 | m_letter_prop.update(full); |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | SunPinyinEngine::update_history_power() |
| 400 | { |
| 401 | unsigned power = m_config.get(CONFIG_GENERAL_MEMORY_POWER, 3); |
| 402 | CIMIContext* ic = m_pv->getIC(); |
| 403 | assert(ic); |
| 404 | ic->setHistoryPower(power); |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | SunPinyinEngine::update_charset_level() |
| 409 | { |
| 410 | std::string charset("GBK"); |
| 411 | charset = m_config.get(CONFIG_GENERAL_CHARSET_LEVEL, charset); |
| 412 | //printf("charset is %s.\n", charset.c_str()); |
| 413 | CIMIContext* ic = m_pv->getIC(); |
| 414 | assert(ic); |
| 415 | if (charset == "GB2312") { |
| 416 | ic->setCharsetLevel(0); |
| 417 | } else if (charset == "GBK") { |
| 418 | ic->setCharsetLevel(1); |
| 419 | } |
| 420 | else { |
| 421 | ic->setCharsetLevel(2); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | SunPinyinEngine::update_cand_window_size() |
| 427 | { |
| 428 | unsigned size = m_config.get(CONFIG_GENERAL_PAGE_SIZE, 10); |
| 429 | m_pv->setCandiWindowSize(size); |
| 430 | } |
| 431 | |
| 432 | void |
| 433 | SunPinyinEngine::update_mode_key() |
| 434 | { |
| 435 | std::string mode_switch("Shift"); |
| 436 | mode_switch = m_config.get(CONFIG_KEYBOARD_MODE_SWITCH, mode_switch); |
| 437 | |
| 438 | CKeyEvent shift_l (IM_VK_SHIFT_L, 0, IM_SHIFT_MASK|IM_RELEASE_MASK); |
| 439 | CKeyEvent shift_r (IM_VK_SHIFT_R, 0, IM_SHIFT_MASK|IM_RELEASE_MASK); |
| 440 | CKeyEvent control_l(IM_VK_CONTROL_L, 0, IM_CTRL_MASK|IM_RELEASE_MASK); |
| 441 | CKeyEvent control_r(IM_VK_CONTROL_R, 0, IM_CTRL_MASK|IM_RELEASE_MASK); |
| 442 | |
| 443 | if (mode_switch == "Shift") { |
| 444 | m_hotkey_profile->removeModeSwitchKey(control_l); |
| 445 | m_hotkey_profile->removeModeSwitchKey(control_r); |
| 446 | m_hotkey_profile->addModeSwitchKey(shift_l); |
| 447 | m_hotkey_profile->addModeSwitchKey(shift_r); |
| 448 | } else if (mode_switch == "Control") { |
| 449 | m_hotkey_profile->removeModeSwitchKey(shift_l); |
| 450 | m_hotkey_profile->removeModeSwitchKey(shift_r); |
| 451 | m_hotkey_profile->addModeSwitchKey(control_l); |
| 452 | m_hotkey_profile->addModeSwitchKey(control_r); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | SunPinyinEngine::update_punct_key() |
| 458 | { |
| 459 | std::string punct_switch("ControlComma"); |
| 460 | punct_switch = m_config.get(CONFIG_KEYBOARD_PUNCT_SWITCH, punct_switch); |
| 461 | if (punct_switch == "ControlComma") { |
| 462 | m_hotkey_profile->setPunctSwitchKey(CKeyEvent(IM_VK_COMMA, 0, IM_CTRL_MASK)); |
| 463 | } else if (punct_switch == "ControlPeriod") { |
| 464 | m_hotkey_profile->setPunctSwitchKey(CKeyEvent(IM_VK_PERIOD, 0, IM_CTRL_MASK)); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | SunPinyinEngine::update_page_key_minus() |
| 470 | { |
| 471 | update_page_key(CONFIG_KEYBOARD_PAGE_MINUS, false, |
| 472 | IM_VK_MINUS, IM_VK_EQUALS); |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | SunPinyinEngine::update_page_key_comma() |
| 477 | { |
| 478 | update_page_key(CONFIG_KEYBOARD_PAGE_COMMA, false, |
| 479 | IM_VK_COMMA, IM_VK_PERIOD); |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | SunPinyinEngine::update_page_key_bracket() |
| 484 | { |
| 485 | update_page_key(CONFIG_KEYBOARD_PAGE_BRACKET, false, |
| 486 | IM_VK_OPEN_BRACKET, IM_VK_CLOSE_BRACKET); |
| 487 | } |
| 488 | |
| 489 | void |
| 490 | SunPinyinEngine::update_page_key(const char* conf_key, bool default_val, |
| 491 | unsigned page_up, unsigned page_down) |
| 492 | { |
| 493 | bool enabled = m_config.get(conf_key, default_val); |
| 494 | |
| 495 | if (enabled) { |
| 496 | m_hotkey_profile->addPageUpKey(CKeyEvent(page_up, 0)); |
| 497 | m_hotkey_profile->addPageDownKey(CKeyEvent(page_down, 0)); |
| 498 | } else { |
| 499 | m_hotkey_profile->removePageUpKey(CKeyEvent(page_up, 0)); |
| 500 | m_hotkey_profile->removePageDownKey(CKeyEvent(page_down, 0)); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | SunPinyinEngine::update_cancel_with_backspace() |
| 506 | { |
| 507 | bool enabled = m_config.get(CONFIG_KEYBOARD_CANCEL_BACKSPACE, true); |
| 508 | m_pv->setCancelOnBackspace(enabled); |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | SunPinyinEngine::update_smart_punc() |
| 513 | { |
| 514 | m_pv->setSmartPunct(m_config.get(CONFIG_KEYBOARD_SMARK_PUNCT, true)); |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | SunPinyinEngine::update_max_best() |
| 519 | { |
| 520 | if (m_pv->getIC() == NULL) { |
| 521 | return; |
| 522 | } |
| 523 | int oldval = (int) m_pv->getIC()->getMaxBest(); |
| 524 | m_pv->getIC()->setMaxBest(m_config.get(CONFIG_GENERAL_MAX_BEST, oldval)); |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | SunPinyinEngine::update_max_tail_candidate() |
| 529 | { |
| 530 | if (m_pv->getIC() == NULL) { |
| 531 | return; |
| 532 | } |
| 533 | int oldval = (int) m_pv->getIC()->getMaxTailCandidateNum(); |
| 534 | m_pv->getIC()->setMaxTailCandidateNum( |
| 535 | m_config.get(CONFIG_GENERAL_MAX_TAIL_CANDIDATE, oldval)); |
| 536 | } |
| 537 | |
| 538 | string_pairs parse_pairs(const std::vector<std::string>& strings) |
| 539 | { |
| 540 | string_pairs pairs; |
| 541 | for (std::vector<std::string>::const_iterator pair = strings.begin(); |
| 542 | pair != strings.end(); ++pair) { |
| 543 | |
| 544 | std::string::size_type found = pair->find(':'); |
| 545 | if (found == pair->npos || pair->length() < 3) |
| 546 | continue; |
| 547 | if (found == 0 && (*pair)[0] == ':') |
| 548 | found = 1; |
| 549 | |
| 550 | pairs.push_back(make_pair(pair->substr(0, found), |
| 551 | pair->substr(found+1))); |
| 552 | } |
| 553 | return pairs; |
| 554 | } |
| 555 | |
| 556 | // the mappings in default_pairs will override the ones in user_pairs |
| 557 | string_pairs merge_pairs(const string_pairs& default_pairs, |
| 558 | const string_pairs& user_pairs) |
| 559 | { |
| 560 | typedef std::map<std::string, int> Indexes; |
| 561 | Indexes indexes; |
| 562 | int index = 0; |
| 563 | for (string_pairs::const_iterator it = default_pairs.begin(); |
| 564 | it != default_pairs.end(); ++it, ++index) { |
| 565 | Indexes::iterator found = indexes.find(it->first); |
| 566 | if (found == indexes.end()) { |
| 567 | indexes[it->first] = index; |
| 568 | } else { |
| 569 | // it is a paired punct. |
| 570 | indexes[it->first] = -found->second; |
| 571 | } |
| 572 | } |
| 573 | string_pairs result(default_pairs); |
| 574 | for (string_pairs::const_iterator it = user_pairs.begin(); |
| 575 | it != user_pairs.end(); ++it) { |
| 576 | Indexes::iterator found = indexes.find(it->first); |
| 577 | if (found == indexes.end()) { |
| 578 | result.push_back(*it); |
| 579 | } else if (found->second >= 0) { |
| 580 | result[found->second] = *it; |
| 581 | } else { |
| 582 | // it is a paired punct, |
| 583 | // but we don't support this kind of mapping yet, |
| 584 | // so quietly ignore it. |
| 585 | } |
| 586 | } |
| 587 | return result; |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | SunPinyinEngine::update_punct_mappings() |
| 592 | { |
| 593 | CSimplifiedChinesePolicy& policy = ASimplifiedChinesePolicy::instance(); |
| 594 | if (m_config.get(PINYIN_PUNCTMAPPING_ENABLED, false)) { |
| 595 | std::vector<std::string> mappings; |
| 596 | mappings = m_config.get(PINYIN_PUNCTMAPPING_MAPPINGS, mappings); |
| 597 | string_pairs pairs(merge_pairs(policy.getDefaultPunctMapping(), |
| 598 | parse_pairs(mappings))); |
| 599 | policy.setPunctMapping(pairs); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | SunPinyinEngine::update_user_data_dir() |
| 605 | { |
| 606 | std::stringstream user_data_dir; |
| 607 | user_data_dir << g_get_home_dir() |
| 608 | << G_DIR_SEPARATOR_S << ".sunpinyin"; |
| 609 | ASimplifiedChinesePolicy::instance().setUserDataDir(user_data_dir.str()); |
| 610 | } |
| 611 | |
| 612 | void |
| 613 | SunPinyinEngine::update_fuzzy_pinyins() |
| 614 | { |
| 615 | bool enabled = m_config.get(QUANPIN_FUZZY_ENABLED, false); |
| 616 | AQuanpinSchemePolicy::instance().setFuzzyForwarding(enabled); |
| 617 | AShuangpinSchemePolicy::instance().setFuzzyForwarding(enabled); |
| 618 | if (!enabled) |
| 619 | return; |
| 620 | std::vector<std::string> fuzzy_pinyins; |
| 621 | fuzzy_pinyins = m_config.get(QUANPIN_FUZZY_PINYINS, fuzzy_pinyins); |
| 622 | AQuanpinSchemePolicy::instance().setFuzzyPinyinPairs(parse_pairs(fuzzy_pinyins)); |
| 623 | AShuangpinSchemePolicy::instance().setFuzzyPinyinPairs(parse_pairs(fuzzy_pinyins)); |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | SunPinyinEngine::update_correction_pinyins() |
| 628 | { |
| 629 | bool enabled = m_config.get(QUANPIN_AUTOCORRECTION_ENABLED, false); |
| 630 | AQuanpinSchemePolicy::instance().setAutoCorrecting(enabled); |
| 631 | if (!enabled) |
| 632 | return; |
| 633 | std::vector<std::string> correction_pinyins; |
| 634 | correction_pinyins = m_config.get(QUANPIN_AUTOCORRECTION_PINYINS, correction_pinyins); |
| 635 | AQuanpinSchemePolicy::instance().setAutoCorrectionPairs(parse_pairs(correction_pinyins)); |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | SunPinyinEngine::update_fuzzy_segs() |
| 640 | { |
| 641 | bool enable_fuzzy_segs = m_config.get(CONFIG_QUANPIN_FUZZYSEGS_ENABLED, false); |
| 642 | AQuanpinSchemePolicy::instance().setFuzzySegmentation(enable_fuzzy_segs); |
| 643 | bool enable_inner_fuzzy = m_config.get(CONFIG_QUANPIN_INNERFUZZY_ENABLED, false); |
| 644 | AQuanpinSchemePolicy::instance().setInnerFuzzySegmentation(CONFIG_QUANPIN_INNERFUZZY_ENABLED); |
| 645 | } |
| 646 | |
| 647 | void |
| 648 | SunPinyinEngine::update_shuangpin_type() |
| 649 | { |
| 650 | EShuangpinType shuangpin_type = MS2003; |
| 651 | shuangpin_type = (EShuangpinType) m_config.get(SHUANGPIN_TYPE, (int) shuangpin_type); |
| 652 | AShuangpinSchemePolicy::instance().setShuangpinType(shuangpin_type); |
| 653 | } |
| 654 | |
| 655 | void |
| 656 | SunPinyinEngine::update_candi_delete_key() |
| 657 | { |
| 658 | /* FIXME: need to get candi_delete_key from user's configuration */ |
| 659 | m_hotkey_profile->setCandiDeleteKey(CKeyEvent(0, 0, IM_ALT_MASK)); |
| 660 | } |
| 661 | |
| 662 | // -*- indent-tabs-mode: nil -*- vim:et:ts=4 |