Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / ime-core / imi_options.cpp
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  *
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
15  * following notice:
16  *
17  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18  * (CDDL)
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.
24  *
25  * Contributor(s):
26  *
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.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <algorithm>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <cassert>
47 #include <errno.h>
48
49 #include "imi_option_keys.h"
50 #include "imi_keys.h"
51 #include "imi_options.h"
52 #include "imi_view_classic.h"
53
54 CSimplifiedChinesePolicy::CSimplifiedChinesePolicy()
55     : m_bLoaded(false), m_bTried(false), m_csLevel(3),
56       m_bEnableFullSymbol(false), m_bEnableFullPunct(true)
57 {
58     m_getFullPunctOp.initPunctMap(getDefaultPunctMapping());
59 }
60
61 bool
62 CSimplifiedChinesePolicy::loadResources()
63 {
64     if (m_bLoaded || m_bTried)
65         return m_bLoaded;
66
67     bool suc = true;
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";
71
72     suc &= m_coreData.loadResource(lm_path.c_str(), dict_path.c_str());
73
74     if (!m_user_data_dir.size()) {
75         char path[256];
76         const char *home = getenv("HOME");
77         snprintf(path,
78                  sizeof(path),
79                  "%s/%s",
80                  home,
81                  SUNPINYIN_USERDATA_DIR_PREFIX);
82         m_user_data_dir = path;
83     }
84
85     char * tmp = strdup(m_user_data_dir.c_str());
86     createDirectory(tmp);
87     free(tmp);
88
89     std::string history_path = m_user_data_dir + "/history";
90     suc &= m_historyCache.loadFromFile(history_path.c_str());
91
92     std::string user_dict_path = m_user_data_dir + "/userdict";
93     suc &= m_userDict.load(user_dict_path.c_str());
94
95     m_bTried = true;
96     return m_bLoaded = suc;
97 }
98
99 CIMIContext *
100 CSimplifiedChinesePolicy::createContext()
101 {
102     CIMIContext* pic = new CIMIContext();
103     pic->setCoreData(&m_coreData);
104     pic->setHistoryMemory(&m_historyCache);
105     pic->setUserDict(&m_userDict);
106
107     pic->setCharsetLevel(m_csLevel);
108
109     pic->setFullSymbolForwarding(m_bEnableFullSymbol);
110     pic->setGetFullSymbolOp(&m_getFullSymbolOp);
111
112     pic->setFullPunctForwarding(m_bEnableFullPunct);
113     pic->setGetFullPunctOp(&m_getFullPunctOp);
114     return pic;
115 }
116
117 void
118 CSimplifiedChinesePolicy::destroyContext(CIMIContext *context)
119 {
120     assert(context != NULL);
121     saveUserHistory();
122     delete context;
123 }
124
125 string_pairs
126 CSimplifiedChinesePolicy::getDefaultPunctMapping() const
127 {
128     static const char* punc_map [] = {
129         " ", " ",
130         ",", ",",
131         ";", ";",
132         "!", "!",
133         "?", "?",
134         ".", "。",
135         ":", ":",
136         "^", "……",
137         "\\", "、",
138         "\"", "“",
139         "\"", "”",
140         "'", "‘",
141         "'", "’",
142         "_", "——",
143         "<", "《",
144         ">", "》",
145         "(", "(",
146         ")", ")",
147         "[", "【",
148         "]", "】",
149         "{", "『",
150         "}", "』",
151         "$", "¥",
152         NULL,
153     };
154
155     string_pairs default_punc_map;
156
157     const char *const *p = punc_map;
158     while (*p) {
159         std::string k = *p++;
160         std::string v = *p++;
161         default_punc_map.push_back(std::make_pair(k, v));
162     }
163     return default_punc_map;
164 }
165
166 bool
167 CSimplifiedChinesePolicy::onConfigChanged(const COptionEvent& event)
168 {
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;
175     }
176
177     return false;
178 }
179
180 bool
181 CSimplifiedChinesePolicy::saveUserHistory()
182 {
183     return m_historyCache.saveToFile();
184 }
185
186 bool
187 CSimplifiedChinesePolicy::createDirectory(char *path)
188 {
189     char *p = path;
190     while ((p = strchr(p + 1, '/'))) {
191         *p = 0;
192         if (access(path, F_OK) != 0 && mkdir(path, S_IRWXU) != 0) {
193             fprintf(stderr, "mkdir %s: %s\n", path, strerror(errno));
194             return false;
195         }
196         *p = '/';
197     }
198     return !(access(path, F_OK) != 0 && mkdir(path, S_IRWXU) != 0);
199 }
200
201 CShuangpinSchemePolicy::CShuangpinSchemePolicy()
202     : m_shuangpinType(MS2003)
203 {
204 }
205
206 bool
207 CQuanpinSchemePolicy::onConfigChanged(const COptionEvent& event)
208 {
209     if (event.name == QUANPIN_FUZZY_ENABLED) {
210         setFuzzyForwarding(event.get_bool());
211         return true;
212     } else if (event.name == QUANPIN_FUZZY_PINYINS) {
213         setFuzzyPinyinPairs(event.get_string_pair_list());
214         return true;
215     } else if (event.name == QUANPIN_AUTOCORRECTION_ENABLED) {
216         setAutoCorrecting(event.get_bool());
217         return true;
218     } else if (event.name == QUANPIN_AUTOCORRECTION_PINYINS) {
219         setAutoCorrectionPairs(event.get_string_pair_list());
220         return true;
221     }
222
223     return false;
224 }
225
226 bool
227 CShuangpinSchemePolicy::onConfigChanged(const COptionEvent& event)
228 {
229     if (event.name == SHUANGPIN_TYPE) {
230         setShuangpinType((EShuangpinType)event.get_int());
231         return true;
232     } else if (event.name == QUANPIN_FUZZY_ENABLED) {
233         setFuzzyForwarding(event.get_bool());
234         return true;
235     } else if (event.name == QUANPIN_FUZZY_PINYINS) {
236         setFuzzyPinyinPairs(event.get_string_pair_list());
237         return true;
238     }
239
240     return false;
241 }
242
243 bool
244 CHunpinSchemePolicy::onConfigChanged(const COptionEvent& event)
245 {
246     if (event.name == SHUANGPIN_TYPE) {
247         setShuangpinType((EShuangpinType)event.get_int());
248         //刷新segmentor狀態
249         COptionEventBus& event_bus = AOptionEventBus::instance();
250         event_bus.publishEvent(COptionEvent(PINYIN_SCHEME, -1));        //第二個參數沒有用
251         return true;
252     } else if (event.name == QUANPIN_FUZZY_ENABLED) {
253         setFuzzyForwarding(event.get_bool());
254         return true;
255     } else if (event.name == QUANPIN_FUZZY_PINYINS) {
256         setFuzzyPinyinPairs(event.get_string_pair_list());
257         return true;
258     }
259
260     return false;
261 }
262
263
264 CHunpinSchemePolicy::CHunpinSchemePolicy()
265     : m_shuangpinType(MS2003)
266 {
267 }