added SYSTEM_DATA_DIR configuration event, so we could set it at runtime, making it possible to place SunPinyin.app under ~/Library/Input\ Methods.
authorYong Sun <mail@yongsun.me>
Sun, 28 Feb 2010 13:51:35 +0800
changeset 559 f72c9aca9e96
parent 558 efb83be94929
child 560 463b46805f6f
added SYSTEM_DATA_DIR configuration event, so we could set it at runtime, making it possible to place SunPinyin.app under ~/Library/Input\ Methods.
sunpinyin2/src/ime-core/imi_option_keys.h
sunpinyin2/src/ime-core/imi_options.cpp
sunpinyin2/src/ime-core/imi_options.h
--- a/sunpinyin2/src/ime-core/imi_option_keys.h	Thu Feb 25 23:17:27 2010 +0800
+++ b/sunpinyin2/src/ime-core/imi_option_keys.h	Sun Feb 28 13:51:35 2010 +0800
@@ -4,6 +4,7 @@
 /**
  * these strings are used as names of instances of COptionEvent
  */
+#define SYSTEM_DATA_DIR                 "General/DataDir"
 #define PINYIN_SCHEME                   "Pinyin/Scheme"
 #define PINYIN_PUNCTMAPPING_ENABLED     "General/PunctMapping/Enabled"
 #define PINYIN_PUNCTMAPPING_MAPPINGS    "General/PunctMapping/Mappings"
--- a/sunpinyin2/src/ime-core/imi_options.cpp	Thu Feb 25 23:17:27 2010 +0800
+++ b/sunpinyin2/src/ime-core/imi_options.cpp	Sun Feb 28 13:51:35 2010 +0800
@@ -62,7 +62,11 @@
         return m_bLoaded;
 
     bool suc = true;
-    suc &= m_coreData.loadResource (SUNPINYIN_DATA_DIR"/lm_sc.t3g", SUNPINYIN_DATA_DIR"/pydict_sc.bin");
+    std::string data_dir  = m_data_dir.size()? m_data_dir: SUNPINYIN_DATA_DIR;
+    std::string lm_path   = data_dir + "/lm_sc.t3g";
+    std::string dict_path = data_dir + "/pydict_sc.bin";
+
+    suc &= m_coreData.loadResource (lm_path.c_str(), dict_path.c_str());
 
     char path[256];
     const char *home = getenv ("HOME");
@@ -88,7 +92,7 @@
     pic->setHistoryMemory (&m_historyCache);
     pic->setUserDict (&m_userDict);
 
-	pic->setCharsetLevel (m_csLevel);
+    pic->setCharsetLevel (m_csLevel);
 
     pic->setFullSymbolForwarding (m_bEnableFullSymbol);
     pic->setGetFullSymbolOp (&m_getFullSymbolOp);
@@ -109,7 +113,9 @@
 bool
 CSimplifiedChinesePolicy::onConfigChanged (const COptionEvent& event)
 {
-    if (event.name == PINYIN_PUNCTMAPPING_MAPPINGS) {
+    if (event.name == SYSTEM_DATA_DIR) {
+        setDataDir(event.get_string());
+    } else if (event.name == PINYIN_PUNCTMAPPING_MAPPINGS) {
         CPairParser parser;
         setPunctMapping(parser.get_pairs());
         return true;
@@ -150,22 +156,27 @@
 bool
 CQuanpinSchemePolicy::onConfigChanged(const COptionEvent& event)
 {
-    if (event.name == QUANPIN_FUZZY_ENABLED) {
+    if (event.name == SYSTEM_DATA_DIR) {
+        setDataDir(event.get_string());
+    } else if (event.name == QUANPIN_FUZZY_ENABLED) {
         setFuzzyForwarding(event.get_bool());
+        return true;
     } else if (event.name == QUANPIN_FUZZY_PINYINS) {
         CPairParser parser;
         size_t num = parser.parse(event);
         setFuzzyPinyinPairs(parser.get_pairs(), num);
+        return true;
     } else if (event.name == QUANPIN_AUTOCORRECTION_ENABLED) {
         setAutoCorrecting(event.get_bool());
+        return true;
     } else if (event.name == QUANPIN_AUTOCORRECTION_PINYINS) {
         CPairParser parser;
         size_t num = parser.parse(event);
         setAutoCorrectionPairs(parser.get_pairs(), num);
-    } else {
-        return false;
+        return true;
     }
-    return true;
+
+    return false;
 }
 
 bool
--- a/sunpinyin2/src/ime-core/imi_options.h	Thu Feb 25 23:17:27 2010 +0800
+++ b/sunpinyin2/src/ime-core/imi_options.h	Sun Feb 28 13:51:35 2010 +0800
@@ -105,6 +105,9 @@
     void enableFullSymbol (bool v=true) {m_bEnableFullSymbol = v;}
     void enableFullPunct  (bool v=true) {m_bEnableFullPunct = v;}
 
+    void setDataDir (const std::string& data_dir)
+        {m_data_dir = data_dir;}
+
     virtual bool onConfigChanged (const COptionEvent& event);
     
     template<class> friend class SingletonHolder;
@@ -125,7 +128,7 @@
     CGetFullSymbolOp     m_getFullSymbolOp;
     bool                 m_bEnableFullPunct;
     CGetFullPunctOp      m_getFullPunctOp;
-    const char          *m_userDataDirPrefix;
+    std::string          m_data_dir;
 };
 
 typedef SingletonHolder<CSimplifiedChinesePolicy> ASimplifiedChinesePolicy;
@@ -136,8 +139,11 @@
     
     IPySegmentor* createPySegmentor () 
     {
+        std::string data_dir   = m_data_dir.size()? m_data_dir: SUNPINYIN_DATA_DIR;
+        std::string pytab_path = data_dir + "/quanpin.dat";
+
         CQuanpinSegmentor *pseg = new CQuanpinSegmentor ();
-        if (pseg->load(SUNPINYIN_DATA_DIR"/quanpin.dat")) {
+        if (pseg->load(pytab_path.c_str())) {
             pseg->setGetFuzzySyllablesOp (&m_getFuzzySyllablesOp);
             pseg->setGetCorrectionPairOp (&m_getCorrectionPairOp);
         } else {
@@ -164,6 +170,9 @@
     void setAutoCorrectionPairs (const char* const* pairs, unsigned num) 
         {m_getCorrectionPairOp.setCorrectionPairs (pairs, num);}
 
+    void setDataDir (const std::string& data_dir)
+        {m_data_dir = data_dir;}
+
     virtual bool onConfigChanged(const COptionEvent& event);
     
     template<class> friend class SingletonHolder;
@@ -173,6 +182,7 @@
 
     CGetFuzzySyllablesOp m_getFuzzySyllablesOp;
     CGetCorrectionPairOp m_getCorrectionPairOp;
+    std::string          m_data_dir;
 };
 
 typedef SingletonHolder<CQuanpinSchemePolicy> AQuanpinSchemePolicy;