2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6 * The contents of this file are subject to the terms of either the GNU Lesser
7 * General Public License Version 2.1 only ("LGPL") or the Common Development and
8 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9 * file except in compliance with the License. You can obtain a copy of the CDDL at
10 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12 * specific language governing permissions and limitations under the License. When
13 * distributing the software, include this License Header Notice in each file and
14 * include the full text of the License in the License file as well as the
17 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19 * For Covered Software in this distribution, this License shall be governed by the
20 * laws of the State of California (excluding conflict-of-law provisions).
21 * Any litigation relating to this License shall be subject to the jurisdiction of
22 * the Federal Courts of the Northern District of California and the state courts
23 * of the State of California, with venue lying in Santa Clara County, California.
27 * If you wish your version of this file to be governed by only the CDDL or only
28 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30 * license." If you don't indicate a single choice of license, a recipient has the
31 * option to distribute your version of this file under either the CDDL or the LGPL
32 * Version 2.1, or to extend the choice of license to its licensees as provided
33 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34 * Version 2 license, then the option applies only if the new code is made subject
35 * to such option by the copyright holder.
43 #include <sys/types.h>
49 #include "imi_option_keys.h"
51 #include "imi_options.h"
52 #include "imi_view_classic.h"
54 CSimplifiedChinesePolicy::CSimplifiedChinesePolicy()
55 : m_bLoaded(false), m_bTried(false), m_csLevel(3),
56 m_bEnableFullSymbol(false), m_bEnableFullPunct(true)
58 m_getFullPunctOp.initPunctMap(getDefaultPunctMapping());
62 CSimplifiedChinesePolicy::loadResources()
64 if (m_bLoaded || m_bTried)
68 std::string data_dir = m_data_dir.size() ? m_data_dir : SUNPINYIN_DATA_DIR;
69 std::string lm_path = data_dir + "/lm_sc.t3g";
70 std::string dict_path = data_dir + "/pydict_sc.bin";
72 suc &= m_coreData.loadResource(lm_path.c_str(), dict_path.c_str());
74 if (!m_user_data_dir.size()) {
76 const char *home = getenv("HOME");
81 SUNPINYIN_USERDATA_DIR_PREFIX);
82 m_user_data_dir = path;
85 char * tmp = strdup(m_user_data_dir.c_str());
89 std::string history_path = m_user_data_dir + "/history";
90 suc &= m_historyCache.loadFromFile(history_path.c_str());
92 std::string user_dict_path = m_user_data_dir + "/userdict";
93 suc &= m_userDict.load(user_dict_path.c_str());
96 return m_bLoaded = suc;
100 CSimplifiedChinesePolicy::createContext()
102 CIMIContext* pic = new CIMIContext();
103 pic->setCoreData(&m_coreData);
104 pic->setHistoryMemory(&m_historyCache);
105 pic->setUserDict(&m_userDict);
107 pic->setCharsetLevel(m_csLevel);
109 pic->setFullSymbolForwarding(m_bEnableFullSymbol);
110 pic->setGetFullSymbolOp(&m_getFullSymbolOp);
112 pic->setFullPunctForwarding(m_bEnableFullPunct);
113 pic->setGetFullPunctOp(&m_getFullPunctOp);
118 CSimplifiedChinesePolicy::destroyContext(CIMIContext *context)
120 assert(context != NULL);
126 CSimplifiedChinesePolicy::getDefaultPunctMapping() const
128 static const char* punc_map [] = {
155 string_pairs default_punc_map;
157 const char *const *p = punc_map;
159 std::string k = *p++;
160 std::string v = *p++;
161 default_punc_map.push_back(std::make_pair(k, v));
163 return default_punc_map;
167 CSimplifiedChinesePolicy::onConfigChanged(const COptionEvent& event)
169 if (event.name == SYSTEM_DATA_DIR) {
170 setDataDir(event.get_string());
171 } else if (event.name == USER_DATA_DIR) {
172 setUserDataDir(event.get_string());
173 } else if (event.name == CONFIG_GENERAL_CHARSET_LEVEL) {
174 m_csLevel = event.get_int() & 3;
181 CSimplifiedChinesePolicy::saveUserHistory()
183 return m_historyCache.saveToFile();
187 CSimplifiedChinesePolicy::createDirectory(char *path)
190 while ((p = strchr(p + 1, '/'))) {
192 if (access(path, F_OK) != 0 && mkdir(path, S_IRWXU) != 0) {
193 fprintf(stderr, "mkdir %s: %s\n", path, strerror(errno));
198 return !(access(path, F_OK) != 0 && mkdir(path, S_IRWXU) != 0);
201 CShuangpinSchemePolicy::CShuangpinSchemePolicy()
202 : m_shuangpinType(MS2003)
207 CQuanpinSchemePolicy::onConfigChanged(const COptionEvent& event)
209 if (event.name == QUANPIN_FUZZY_ENABLED) {
210 setFuzzyForwarding(event.get_bool());
212 } else if (event.name == QUANPIN_FUZZY_PINYINS) {
213 setFuzzyPinyinPairs(event.get_string_pair_list());
215 } else if (event.name == QUANPIN_AUTOCORRECTION_ENABLED) {
216 setAutoCorrecting(event.get_bool());
218 } else if (event.name == QUANPIN_AUTOCORRECTION_PINYINS) {
219 setAutoCorrectionPairs(event.get_string_pair_list());
227 CShuangpinSchemePolicy::onConfigChanged(const COptionEvent& event)
229 if (event.name == SHUANGPIN_TYPE) {
230 setShuangpinType((EShuangpinType)event.get_int());
232 } else if (event.name == QUANPIN_FUZZY_ENABLED) {
233 setFuzzyForwarding(event.get_bool());
235 } else if (event.name == QUANPIN_FUZZY_PINYINS) {
236 setFuzzyPinyinPairs(event.get_string_pair_list());
244 CHunpinSchemePolicy::onConfigChanged(const COptionEvent& event)
246 if (event.name == SHUANGPIN_TYPE) {
247 setShuangpinType((EShuangpinType)event.get_int());
249 COptionEventBus& event_bus = AOptionEventBus::instance();
250 event_bus.publishEvent(COptionEvent(PINYIN_SCHEME, -1)); //第二個參數沒有用
252 } else if (event.name == QUANPIN_FUZZY_ENABLED) {
253 setFuzzyForwarding(event.get_bool());
255 } else if (event.name == QUANPIN_FUZZY_PINYINS) {
256 setFuzzyPinyinPairs(event.get_string_pair_list());
264 CHunpinSchemePolicy::CHunpinSchemePolicy()
265 : m_shuangpinType(MS2003)