Apply -fvisibility to decrease binary size
[platform/core/uifw/ise-engine-sunpinyin.git] / src / ime-core / imi_options.h
1 // -*- mode: c++ -*-
2 /*
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4  *
5  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6  *
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
16  * following notice:
17  *
18  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19  * (CDDL)
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.
25  *
26  * Contributor(s):
27  *
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.
37  */
38
39 #ifndef _IMI_OPTIONS_H
40 #define _IMI_OPTIONS_H
41
42 #include "utils.h"
43 #include "portability.h"
44 #include "imi_view.h"
45 #include "imi_view_classic.h"
46 #include "imi_funcobjs.h"
47 #include "imi_data.h"
48 #include "imi_option_event.h"
49 #include "userdict.h"
50 #include "ic_history.h"
51 #include "pinyin/shuangpin_seg.h"
52 #include "pinyin/hunpin_seg.h"
53
54 #ifndef SUNPINYIN_USERDATA_DIR_PREFIX
55 #define SUNPINYIN_USERDATA_DIR_PREFIX ".sunpinyin"
56 #endif
57
58 struct EXPORTED CSimplifiedChinesePolicy : public IConfigurable {
59     CSimplifiedChinesePolicy ();
60
61     bool loadResources();
62
63     CIMIContext* createContext();
64     void destroyContext(CIMIContext *context);
65
66     void setPunctMapping(string_pairs punc_map)
67     { m_getFullPunctOp.initPunctMap(punc_map); }
68
69     string_pairs getDefaultPunctMapping() const;
70
71     void enableFullSymbol(bool v = true) { m_bEnableFullSymbol = v; }
72     void enableFullPunct(bool v = true) { m_bEnableFullPunct = v; }
73
74     void setDataDir(const std::string& data_dir)
75     { m_data_dir = data_dir; }
76
77     void setUserDataDir(const std::string& user_data_dir)
78     { m_user_data_dir = user_data_dir; }
79
80     virtual bool onConfigChanged(const COptionEvent& event);
81
82     template<class> friend class SingletonHolder;
83
84 protected: ~CSimplifiedChinesePolicy () {}
85
86     bool createDirectory(char *path);
87     bool saveUserHistory();
88
89     CIMIData m_coreData;
90     CBigramHistory m_historyCache;
91     bool m_bLoaded;
92     bool m_bTried;
93     unsigned m_csLevel;
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;
100 public:
101     CUserDict m_userDict;
102 };
103
104 typedef SingletonHolder<CSimplifiedChinesePolicy> ASimplifiedChinesePolicy;
105
106 struct EXPORTED CQuanpinSchemePolicy : IConfigurable {
107 public:
108
109     IPySegmentor* createPySegmentor(){
110         CQuanpinSegmentor *pseg = new CQuanpinSegmentor();
111         pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
112         pseg->setGetCorrectionPairOp(&m_getCorrectionPairOp);
113         pseg->setGetFuzzySegmentsOp(&m_getFuzzySegmentsOp);
114         return pseg;
115     }
116
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);
121     }
122
123     void clearFuzzyPinyinPairs()
124     { m_getFuzzySyllablesOp.clearFuzzyMap(); }
125
126     void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
127     { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
128
129     void setAutoCorrecting(bool v = true)
130     { m_getCorrectionPairOp.setEnable(v); }
131
132     void setAutoCorrectionPairs(const string_pairs& pairs)
133     { m_getCorrectionPairOp.setCorrectionPairs(pairs); }
134
135     void setFuzzySegmentation(bool v = true)
136     { m_getFuzzySegmentsOp.setEnable(v); }
137
138     void setInnerFuzzySegmentation(bool v = true)
139     { m_getFuzzySegmentsOp.setInnerFuzzyEnable(v); }
140
141     virtual bool onConfigChanged(const COptionEvent& event);
142
143     template<class> friend class SingletonHolder;
144
145 protected: ~CQuanpinSchemePolicy () {}
146
147     CGetFuzzySyllablesOp<CPinyinData>   m_getFuzzySyllablesOp;
148     CGetCorrectionPairOp m_getCorrectionPairOp;
149     CGetFuzzySegmentsOp m_getFuzzySegmentsOp;
150 };
151
152 typedef SingletonHolder<CQuanpinSchemePolicy> AQuanpinSchemePolicy;
153
154 struct EXPORTED CShuangpinSchemePolicy : public IConfigurable {
155 public:
156     CShuangpinSchemePolicy();
157
158     IPySegmentor* createPySegmentor(){
159         CShuangpinSegmentor *pseg = new CShuangpinSegmentor(m_shuangpinType);
160         pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
161         return pseg;
162     }
163
164     void setShuangpinType(EShuangpinType t) { m_shuangpinType = t; }
165
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);
170     }
171
172     void clearFuzzyPinyinPairs()
173     { m_getFuzzySyllablesOp.clearFuzzyMap(); }
174
175     void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
176     { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
177
178     virtual bool onConfigChanged(const COptionEvent& event);
179
180     template<class> friend class SingletonHolder;
181 protected: ~CShuangpinSchemePolicy () {}
182
183     EShuangpinType m_shuangpinType;
184     CGetFuzzySyllablesOp<CPinyinData>   m_getFuzzySyllablesOp;
185 };
186
187 typedef SingletonHolder<CShuangpinSchemePolicy> AShuangpinSchemePolicy;
188
189 struct  CHunpinSchemePolicy : public IConfigurable {
190 public:
191     CHunpinSchemePolicy();
192
193     IPySegmentor* createPySegmentor(){
194         CHunpinSegmentor *pseg = new CHunpinSegmentor(m_shuangpinType);
195         pseg->setGetFuzzySyllablesOp(&m_getFuzzySyllablesOp);
196         return pseg;
197     }
198
199     void setShuangpinType(EShuangpinType t) { m_shuangpinType = t; }
200
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);
205     }
206
207     void clearFuzzyPinyinPairs()
208     { m_getFuzzySyllablesOp.clearFuzzyMap(); }
209
210     void setFuzzyPinyinPairs(const string_pairs& pairs, bool duplex = true)
211     { m_getFuzzySyllablesOp.initFuzzyMap(pairs, duplex); }
212
213     virtual bool onConfigChanged(const COptionEvent& event);
214
215     template<class> friend class SingletonHolder;
216 protected: ~CHunpinSchemePolicy () {}
217
218     EShuangpinType m_shuangpinType;
219     CGetFuzzySyllablesOp<CPinyinData>   m_getFuzzySyllablesOp;
220 };
221
222 typedef SingletonHolder<CHunpinSchemePolicy> AHunpinSchemePolicy;
223
224 struct CClassicStylePolicy : public IConfigurable {
225     CIMIView* createView() { return new CIMIClassicView(); }
226
227     template<class> friend class SingletonHolder;
228 protected: ~CClassicStylePolicy () {}
229 };
230
231 typedef SingletonHolder<CClassicStylePolicy> AClassicStylePolicy;
232
233 struct ISunpinyinProfile {
234     virtual CIMIView* createProfile() = 0;
235     virtual void destroyProfile(CIMIView *) = 0;
236     virtual ~ISunpinyinProfile () {};
237 };
238
239 template <class LanguagePolicy, class PinyinSchemePolicy,
240           class InputStylePolicy>
241 class CSunpinyinProfile : public ISunpinyinProfile
242 {
243 public:
244     CSunpinyinProfile () : m_langPolicy(LanguagePolicy::instance()),
245                            m_pySchemePolicy(PinyinSchemePolicy::instance()),
246                            m_inputStylePolicy(InputStylePolicy::instance())
247     {}
248
249     /* profile by itself is a profile, so we are creating a session here? */
250     virtual CIMIView* createProfile(){
251         if (!m_langPolicy.loadResources())
252             return NULL;
253
254         IPySegmentor* pseg = m_pySchemePolicy.createPySegmentor();
255         if (pseg == NULL)
256             return NULL;
257
258         CIMIContext *pic = m_langPolicy.createContext();
259         CIMIView* pview = m_inputStylePolicy.createView();
260         pview->attachIC(pic);
261         pview->setPySegmentor(pseg);
262
263         return pview;
264     }
265
266     virtual void destroyProfile(CIMIView* pview){
267         if (pview) {
268             LanguagePolicy::instance().destroyContext(pview->getIC());
269             delete pview->getPySegmentor();
270             delete pview;
271         }
272     }
273 private:
274     typename LanguagePolicy::Type & m_langPolicy;
275     typename PinyinSchemePolicy::Type & m_pySchemePolicy;
276     typename InputStylePolicy::Type & m_inputStylePolicy;
277 };
278
279 class CSunpinyinSessionFactory : private CNonCopyable
280 {
281 public:
282     typedef enum {
283         QUANPIN,
284         SHUANGPIN,
285         YUEPIN,
286         ZHUYIN,
287         HUNPIN,
288     } EPyScheme;
289
290     typedef enum {
291         MSPY_STYLE,
292         CLASSIC_STYLE,
293     } EInputStyle;
294
295     typedef enum {
296         SIMPLIFIED_CHINESE,
297         TRADITIONAL_CHINESE,
298     } ELanguage;
299
300 public:
301     static CSunpinyinSessionFactory& getFactory(){
302         static CSunpinyinSessionFactory inst;
303         return inst;
304     }
305
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; }
310
311     CIMIView* createSession(){
312         unsigned key = _policiesToKey(m_lang, m_pyScheme, m_inputStyle);
313         ISunpinyinProfile *profile = _getProfile(key);
314         if (!profile)
315             return NULL;
316
317         CIMIView *pview = profile->createProfile();
318         if (!pview)
319             return NULL;
320
321         pview->setHotkeyProfile(&m_hotkeyProfile);
322         pview->setCandiWindowSize(m_candiWindowSize);
323         return pview;
324     }
325
326     void destroySession(CIMIView* pview){
327         unsigned key = _policiesToKey(m_lang, m_pyScheme, m_inputStyle);
328         ISunpinyinProfile *profile = _getProfile(key);
329         if (!profile)
330             return;
331         profile->destroyProfile(pview);
332     }
333
334 private:
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,
340                                    CLASSIC_STYLE)] =
341             new CSunpinyinProfile <ASimplifiedChinesePolicy,
342                                    AQuanpinSchemePolicy, AClassicStylePolicy> ();
343
344         m_profiles [_policiesToKey(SIMPLIFIED_CHINESE, SHUANGPIN,
345                                    CLASSIC_STYLE)] =
346             new CSunpinyinProfile <ASimplifiedChinesePolicy,
347                                    AShuangpinSchemePolicy, AClassicStylePolicy> ();
348     }
349
350     ~CSunpinyinSessionFactory (){
351         std::map <unsigned, ISunpinyinProfile*>::iterator it = m_profiles.begin();
352         std::map <unsigned, ISunpinyinProfile*>::iterator ite = m_profiles.end();
353
354         for (; it != ite; ++it)
355             delete it->second;
356     }
357
358     ISunpinyinProfile* _getProfile(unsigned key){
359         std::map <unsigned, ISunpinyinProfile*>::iterator it = m_profiles.find(
360             key);
361         if (it != m_profiles.end()) {
362             return it->second;
363         } else {
364             return NULL;
365         }
366     }
367
368     unsigned _policiesToKey(ELanguage lang,
369                             EPyScheme pyScheme,
370                             EInputStyle inputStyle)
371     { return (lang << 16) + (pyScheme << 8) + inputStyle; }
372
373     std::map <unsigned, ISunpinyinProfile*> m_profiles;
374
375     EPyScheme m_pyScheme;
376     EInputStyle m_inputStyle;
377     ELanguage m_lang;
378     unsigned m_candiWindowSize;
379     CHotkeyProfile m_hotkeyProfile;
380 };
381
382 #endif