3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
7 * The contents of this file are subject to the terms of either the GNU Lesser
8 * General Public License Version 2.1 only ("LGPL") or the Common Development and
9 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
10 * file except in compliance with the License. You can obtain a copy of the CDDL at
11 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
12 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
13 * specific language governing permissions and limitations under the License. When
14 * distributing the software, include this License Header Notice in each file and
15 * include the full text of the License in the License file as well as the
18 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
20 * For Covered Software in this distribution, this License shall be governed by the
21 * laws of the State of California (excluding conflict-of-law provisions).
22 * Any litigation relating to this License shall be subject to the jurisdiction of
23 * the Federal Courts of the Northern District of California and the state courts
24 * of the State of California, with venue lying in Santa Clara County, California.
28 * If you wish your version of this file to be governed by only the CDDL or only
29 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
30 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
31 * license." If you don't indicate a single choice of license, a recipient has the
32 * option to distribute your version of this file under either the CDDL or the LGPL
33 * Version 2.1, or to extend the choice of license to its licensees as provided
34 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
35 * Version 2 license, then the option applies only if the new code is made subject
36 * to such option by the copyright holder.
39 #ifndef _IMI_OPTIONS_H
40 #define _IMI_OPTIONS_H
43 #include "portability.h"
45 #include "imi_view_classic.h"
46 #include "imi_funcobjs.h"
48 #include "imi_option_event.h"
50 #include "ic_history.h"
51 #include "pinyin/shuangpin_seg.h"
52 #include "pinyin/hunpin_seg.h"
54 #ifndef SUNPINYIN_USERDATA_DIR_PREFIX
55 #define SUNPINYIN_USERDATA_DIR_PREFIX ".sunpinyin"
58 struct CSimplifiedChinesePolicy : public IConfigurable {
59 CSimplifiedChinesePolicy ();
63 CIMIContext* createContext();
64 void destroyContext(CIMIContext *context);
66 void setPunctMapping(string_pairs punc_map)
67 { m_getFullPunctOp.initPunctMap(punc_map); }
69 string_pairs getDefaultPunctMapping() const;
71 void enableFullSymbol(bool v = true) { m_bEnableFullSymbol = v; }
72 void enableFullPunct(bool v = true) { m_bEnableFullPunct = v; }
74 void setDataDir(const std::string& data_dir)
75 { m_data_dir = data_dir; }
77 void setUserDataDir(const std::string& user_data_dir)
78 { m_user_data_dir = user_data_dir; }
80 virtual bool onConfigChanged(const COptionEvent& event);
82 template<class> friend class SingletonHolder;
84 protected: ~CSimplifiedChinesePolicy () {}
86 bool createDirectory(char *path);
87 bool saveUserHistory();
90 CBigramHistory m_historyCache;
94 bool m_bEnableFullSymbol;
95 CGetFullSymbolOp m_getFullSymbolOp;
96 bool m_bEnableFullPunct;
97 CGetFullPunctOp m_getFullPunctOp;
98 std::string m_data_dir;
99 std::string m_user_data_dir;
101 CUserDict m_userDict;
104 typedef SingletonHolder<CSimplifiedChinesePolicy> ASimplifiedChinesePolicy;
106 struct CQuanpinSchemePolicy : IConfigurable {
109 IPySegmentor* createPySegmentor(){
110 CQuanpinSegmentor *pseg = new CQuanpinSegmentor();
111 pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
112 pseg->setGetCorrectionPairOp(&m_getCorrectionPairOp);
113 pseg->setGetFuzzySegmentsOp(&m_getFuzzySegmentsOp);
117 void setFuzzyForwarding(bool enable_fuzzies = true,
118 bool enable_simpler_initials = true){
119 m_getFuzzySyllablesOp.setEnableFuzzies(enable_fuzzies);
120 m_getFuzzySyllablesOp.setEnableSimplerInitials(enable_simpler_initials);
123 void clearFuzzyPinyinPairs()
124 { m_getFuzzySyllablesOp.clearFuzzyMap(); }
126 void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
127 { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
129 void setAutoCorrecting(bool v = true)
130 { m_getCorrectionPairOp.setEnable(v); }
132 void setAutoCorrectionPairs(const string_pairs& pairs)
133 { m_getCorrectionPairOp.setCorrectionPairs(pairs); }
135 void setFuzzySegmentation(bool v = true)
136 { m_getFuzzySegmentsOp.setEnable(v); }
138 void setInnerFuzzySegmentation(bool v = true)
139 { m_getFuzzySegmentsOp.setInnerFuzzyEnable(v); }
141 virtual bool onConfigChanged(const COptionEvent& event);
143 template<class> friend class SingletonHolder;
145 protected: ~CQuanpinSchemePolicy () {}
147 CGetFuzzySyllablesOp<CPinyinData> m_getFuzzySyllablesOp;
148 CGetCorrectionPairOp m_getCorrectionPairOp;
149 CGetFuzzySegmentsOp m_getFuzzySegmentsOp;
152 typedef SingletonHolder<CQuanpinSchemePolicy> AQuanpinSchemePolicy;
154 struct CShuangpinSchemePolicy : public IConfigurable {
156 CShuangpinSchemePolicy();
158 IPySegmentor* createPySegmentor(){
159 CShuangpinSegmentor *pseg = new CShuangpinSegmentor(m_shuangpinType);
160 pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
164 void setShuangpinType(EShuangpinType t) { m_shuangpinType = t; }
166 void setFuzzyForwarding(bool enable_fuzzies = true,
167 bool enable_simpler_initials = true){
168 m_getFuzzySyllablesOp.setEnableFuzzies(enable_fuzzies);
169 m_getFuzzySyllablesOp.setEnableSimplerInitials(enable_simpler_initials);
172 void clearFuzzyPinyinPairs()
173 { m_getFuzzySyllablesOp.clearFuzzyMap(); }
175 void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
176 { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
178 virtual bool onConfigChanged(const COptionEvent& event);
180 template<class> friend class SingletonHolder;
181 protected: ~CShuangpinSchemePolicy () {}
183 EShuangpinType m_shuangpinType;
184 CGetFuzzySyllablesOp<CPinyinData> m_getFuzzySyllablesOp;
187 typedef SingletonHolder<CShuangpinSchemePolicy> AShuangpinSchemePolicy;
189 struct CHunpinSchemePolicy : public IConfigurable {
191 CHunpinSchemePolicy();
193 IPySegmentor* createPySegmentor(){
194 CHunpinSegmentor *pseg = new CHunpinSegmentor(m_shuangpinType);
195 pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
199 void setShuangpinType(EShuangpinType t) { m_shuangpinType = t; }
201 void setFuzzyForwarding(bool enable_fuzzies = true,
202 bool enable_simpler_initials = true){
203 m_getFuzzySyllablesOp.setEnableFuzzies(enable_fuzzies);
204 m_getFuzzySyllablesOp.setEnableSimplerInitials(enable_simpler_initials);
207 void clearFuzzyPinyinPairs()
208 { m_getFuzzySyllablesOp.clearFuzzyMap(); }
210 void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
211 { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
213 virtual bool onConfigChanged(const COptionEvent& event);
215 template<class> friend class SingletonHolder;
216 protected: ~CHunpinSchemePolicy () {}
218 EShuangpinType m_shuangpinType;
219 CGetFuzzySyllablesOp<CPinyinData> m_getFuzzySyllablesOp;
222 typedef SingletonHolder<CHunpinSchemePolicy> AHunpinSchemePolicy;
224 struct CClassicStylePolicy : public IConfigurable {
225 CIMIView* createView() { return new CIMIClassicView(); }
227 template<class> friend class SingletonHolder;
228 protected: ~CClassicStylePolicy () {}
231 typedef SingletonHolder<CClassicStylePolicy> AClassicStylePolicy;
233 struct ISunpinyinProfile {
234 virtual CIMIView* createProfile() = 0;
235 virtual void destroyProfile(CIMIView *) = 0;
236 virtual ~ISunpinyinProfile () {};
239 template <class LanguagePolicy, class PinyinSchemePolicy,
240 class InputStylePolicy>
241 class CSunpinyinProfile : public ISunpinyinProfile
244 CSunpinyinProfile () : m_langPolicy(LanguagePolicy::instance()),
245 m_pySchemePolicy(PinyinSchemePolicy::instance()),
246 m_inputStylePolicy(InputStylePolicy::instance())
249 /* profile by itself is a profile, so we are creating a session here? */
250 virtual CIMIView* createProfile(){
251 if (!m_langPolicy.loadResources())
254 IPySegmentor* pseg = m_pySchemePolicy.createPySegmentor();
258 CIMIContext *pic = m_langPolicy.createContext();
259 CIMIView* pview = m_inputStylePolicy.createView();
260 pview->attachIC(pic);
261 pview->setPySegmentor(pseg);
266 virtual void destroyProfile(CIMIView* pview){
268 LanguagePolicy::instance().destroyContext(pview->getIC());
269 delete pview->getPySegmentor();
274 typename LanguagePolicy::Type & m_langPolicy;
275 typename PinyinSchemePolicy::Type & m_pySchemePolicy;
276 typename InputStylePolicy::Type & m_inputStylePolicy;
279 class CSunpinyinSessionFactory : private CNonCopyable
301 static CSunpinyinSessionFactory& getFactory(){
302 static CSunpinyinSessionFactory inst;
306 void setLanguage(ELanguage lang) { m_lang = lang; }
307 void setInputStyle(EInputStyle inputStyle) { m_inputStyle = inputStyle; }
308 void setPinyinScheme(EPyScheme pyScheme) { m_pyScheme = pyScheme; }
309 void setCandiWindowSize(unsigned size) { m_candiWindowSize = size; }
311 CIMIView* createSession(){
312 unsigned key = _policiesToKey(m_lang, m_pyScheme, m_inputStyle);
313 ISunpinyinProfile *profile = _getProfile(key);
317 CIMIView *pview = profile->createProfile();
321 pview->setHotkeyProfile(&m_hotkeyProfile);
322 pview->setCandiWindowSize(m_candiWindowSize);
326 void destroySession(CIMIView* pview){
327 unsigned key = _policiesToKey(m_lang, m_pyScheme, m_inputStyle);
328 ISunpinyinProfile *profile = _getProfile(key);
331 profile->destroyProfile(pview);
335 CSunpinyinSessionFactory ()
336 : m_pyScheme(QUANPIN), m_inputStyle(CLASSIC_STYLE),
337 m_lang(SIMPLIFIED_CHINESE),
338 m_candiWindowSize(10){
339 m_profiles [_policiesToKey(SIMPLIFIED_CHINESE, QUANPIN,
341 new CSunpinyinProfile <ASimplifiedChinesePolicy,
342 AQuanpinSchemePolicy, AClassicStylePolicy> ();
344 m_profiles [_policiesToKey(SIMPLIFIED_CHINESE, SHUANGPIN,
346 new CSunpinyinProfile <ASimplifiedChinesePolicy,
347 AShuangpinSchemePolicy, AClassicStylePolicy> ();
350 ~CSunpinyinSessionFactory (){
351 std::map <unsigned, ISunpinyinProfile*>::iterator it = m_profiles.begin();
352 std::map <unsigned, ISunpinyinProfile*>::iterator ite = m_profiles.end();
354 for (; it != ite; ++it)
358 ISunpinyinProfile* _getProfile(unsigned key){
359 std::map <unsigned, ISunpinyinProfile*>::iterator it = m_profiles.find(
361 if (it != m_profiles.end()) {
368 unsigned _policiesToKey(ELanguage lang,
370 EInputStyle inputStyle)
371 { return (lang << 16) + (pyScheme << 8) + inputStyle; }
373 std::map <unsigned, ISunpinyinProfile*> m_profiles;
375 EPyScheme m_pyScheme;
376 EInputStyle m_inputStyle;
378 unsigned m_candiWindowSize;
379 CHotkeyProfile m_hotkeyProfile;