| 1 |
/* AbiSuite |
| 2 |
* Copyright (C) 2001 Dom Lachowicz <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "xap_App.h" |
| 25 |
#include "xap_Frame.h" |
| 26 |
#include "xap_Strings.h" |
| 27 |
#include "spell_manager.h" |
| 28 |
#include "ut_debugmsg.h" |
| 29 |
#include "ut_Language.h" |
| 30 |
|
| 31 |
#ifdef WITH_ENCHANT |
| 32 |
#include "enchant_checker.h" |
| 33 |
typedef EnchantChecker SpellCheckerClass; |
| 34 |
#else |
| 35 |
#include "ispell_checker.h" |
| 36 |
typedef ISpellChecker SpellCheckerClass; |
| 37 |
#endif |
| 38 |
|
| 39 |
/*! |
| 40 |
* Abstract constructor |
| 41 |
*/ |
| 42 |
/* protected */ SpellChecker::SpellChecker () |
| 43 |
{ |
| 44 |
// not used, abstract base class |
| 45 |
} |
| 46 |
|
| 47 |
/*! |
| 48 |
* Abstract destructor |
| 49 |
*/ |
| 50 |
/* protected */ SpellChecker::~SpellChecker () |
| 51 |
{ |
| 52 |
// not used, abstract base class |
| 53 |
} |
| 54 |
|
| 55 |
bool SpellChecker::requestDictionary (const char * szLang) |
| 56 |
{ |
| 57 |
bool bSuccess = _requestDictionary(szLang); |
| 58 |
|
| 59 |
m_BarbarismChecker.load(szLang); |
| 60 |
|
| 61 |
return bSuccess; |
| 62 |
} |
| 63 |
|
| 64 |
SpellChecker::SpellCheckResult SpellChecker::checkWord(const UT_UCSChar* word, size_t len) |
| 65 |
{ |
| 66 |
UT_return_val_if_fail( word, SpellChecker::LOOKUP_SUCCEEDED ); |
| 67 |
|
| 68 |
SpellChecker::SpellCheckResult ret = SpellChecker::LOOKUP_FAILED; |
| 69 |
|
| 70 |
m_bIsBarbarism = false; |
| 71 |
m_bIsDictionaryWord = false; |
| 72 |
|
| 73 |
if (m_BarbarismChecker.checkWord (word, len)) |
| 74 |
{ |
| 75 |
UT_DEBUGMSG(("SPELL: spell %p %s barb \"%s\"\n", |
| 76 |
this, getLanguage().c_str(), UT_UTF8String (word, len).utf8_str())); |
| 77 |
m_bIsBarbarism = true; |
| 78 |
return SpellChecker::LOOKUP_FAILED; |
| 79 |
} |
| 80 |
|
| 81 |
// handle hyphenated constructions |
| 82 |
// we split each construction into the constituent words and check each word |
| 83 |
// individually -- if they all pass, we consider the whole phrase good, if any fails, |
| 84 |
// we try the entire hyphenated phrase |
| 85 |
|
| 86 |
const UT_uint32 iMaxWords = 10; |
| 87 |
const UT_UCS4Char * pWords[iMaxWords]; |
| 88 |
size_t iWordLengths[iMaxWords]; |
| 89 |
UT_uint32 iWordCount = 0; |
| 90 |
const UT_UCS4Char * p; |
| 91 |
UT_uint32 i; |
| 92 |
|
| 93 |
// initialise the first word to the start of the string we were given |
| 94 |
pWords[0] = word; |
| 95 |
|
| 96 |
for(i = 0, p = word; i < len; ++i, ++p) |
| 97 |
{ |
| 98 |
if(*p == '-') |
| 99 |
{ |
| 100 |
// calculate langth of this word |
| 101 |
iWordLengths[iWordCount] = p - pWords[iWordCount]; |
| 102 |
|
| 103 |
// store the start of next word |
| 104 |
iWordCount++; |
| 105 |
pWords[iWordCount] = p+1; |
| 106 |
} |
| 107 |
|
| 108 |
if(iWordCount >= iMaxWords - 1) |
| 109 |
break; |
| 110 |
} |
| 111 |
|
| 112 |
// compute the length of the last word |
| 113 |
iWordLengths[iWordCount] = len - (pWords[iWordCount] - word); |
| 114 |
|
| 115 |
// NB iWordCount is really 'word count' - 1 after the loop above, hence the <= |
| 116 |
for(i = 0; i <= iWordCount; ++i) |
| 117 |
{ |
| 118 |
ret = _checkWord(pWords[i], iWordLengths[i]); |
| 119 |
if(ret == SpellChecker::LOOKUP_FAILED) |
| 120 |
break; |
| 121 |
} |
| 122 |
|
| 123 |
if(ret == SpellChecker::LOOKUP_SUCCEEDED) |
| 124 |
return ret; |
| 125 |
|
| 126 |
// try the whole hyphenated phrase ... |
| 127 |
ret = _checkWord(word, len); |
| 128 |
|
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
UT_GenericVector<UT_UCSChar*> *SpellChecker::suggestWord(const UT_UCSChar* word, size_t len) |
| 133 |
{ |
| 134 |
UT_GenericVector<UT_UCSChar*> *pvSugg = _suggestWord(word, len); |
| 135 |
|
| 136 |
m_BarbarismChecker.suggestWord(word, len, pvSugg); |
| 137 |
|
| 138 |
return pvSugg; |
| 139 |
} |
| 140 |
|
| 141 |
bool SpellChecker::addToCustomDict (const UT_UCSChar *word, size_t len) |
| 142 |
{ |
| 143 |
return XAP_App::getApp()->addWordToDict (word, len); |
| 144 |
} |
| 145 |
|
| 146 |
void SpellChecker::correctWord (const UT_UCSChar * /*toCorrect*/, size_t /*toCorrectLen*/, |
| 147 |
const UT_UCSChar * /*correct*/, size_t /*correctLen*/) |
| 148 |
{ |
| 149 |
} |
| 150 |
|
| 151 |
/* static */ void SpellChecker::couldNotLoadDictionary ( const char * szLang ) |
| 152 |
{ |
| 153 |
XAP_App * pApp = XAP_App::getApp (); |
| 154 |
XAP_Frame * pFrame = pApp->getLastFocussedFrame (); |
| 155 |
char szLangName[255]; |
| 156 |
UT_Language lang; |
| 157 |
|
| 158 |
UT_uint32 id = lang.getIndxFromCode(szLang); |
| 159 |
const gchar* pLang = lang.getNthLangName(id); |
| 160 |
sprintf(szLangName, "%s [%s]", pLang, szLang); // language name [language_code] |
| 161 |
|
| 162 |
UT_String buf (UT_String_sprintf(pApp->getStringSet ()->getValue (XAP_STRING_ID_SPELL_CANTLOAD_DICT), |
| 163 |
szLangName)); |
| 164 |
if (pFrame) |
| 165 |
pFrame->showMessageBox (buf.c_str(), |
| 166 |
XAP_Dialog_MessageBox::b_O, |
| 167 |
XAP_Dialog_MessageBox::a_OK); |
| 168 |
else |
| 169 |
{ |
| 170 |
UT_DEBUGMSG(("SpellChecker::could not load dictionary for %s\n", szLang)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/***********************************************************************/ |
| 175 |
/***********************************************************************/ |
| 176 |
|
| 177 |
// some arbitrary number for how many language buckets to have by default |
| 178 |
#define NBUCKETS 3 |
| 179 |
|
| 180 |
/*! |
| 181 |
* Protected constructor |
| 182 |
* |
| 183 |
* This (Singleton) class is responsible for creating, handing out, |
| 184 |
* and destroying instances of the ISpellChecker class |
| 185 |
*/ |
| 186 |
/* private */ SpellManager::SpellManager () |
| 187 |
: m_map (NBUCKETS), m_lastDict(0), m_nLoadedDicts(0) |
| 188 |
{ |
| 189 |
m_missingHashs += "-none-"; |
| 190 |
} |
| 191 |
|
| 192 |
/*! |
| 193 |
* Destructor |
| 194 |
*/ |
| 195 |
SpellManager::~SpellManager () |
| 196 |
{ |
| 197 |
UT_GenericVector<void const *> *pVec = m_map.enumerate(); |
| 198 |
UT_ASSERT(pVec); |
| 199 |
UT_VECTOR_PURGEALL (SpellCheckerClass *, (*pVec)); |
| 200 |
DELETEP(pVec); |
| 201 |
} |
| 202 |
|
| 203 |
/*! |
| 204 |
* If an instance already exists it will be returned for you. |
| 205 |
* If not, one is created and returned for you |
| 206 |
* \return A valid instance of the SpellManager class |
| 207 |
*/ |
| 208 |
/* static */ SpellManager & |
| 209 |
SpellManager::instance () |
| 210 |
{ |
| 211 |
// Singleton implementation |
| 212 |
static SpellManager s_instance; |
| 213 |
return s_instance; |
| 214 |
} |
| 215 |
|
| 216 |
/*! |
| 217 |
* Request an instance of a dictionary capable of checking the language |
| 218 |
* Represented by szLang |
| 219 |
* |
| 220 |
* \param szLang - The language tag ("en-US") we want to use |
| 221 |
* \return A valid SpellChecker for 'szLang' on success, or 0 on failure |
| 222 |
*/ |
| 223 |
SpellChecker * |
| 224 |
SpellManager::requestDictionary (const char * szLang) |
| 225 |
{ |
| 226 |
SpellCheckerClass * checker = 0; |
| 227 |
|
| 228 |
// Don't try to load hashes we know are missing |
| 229 |
if (strstr(m_missingHashs.c_str(), szLang)) |
| 230 |
return 0; |
| 231 |
|
| 232 |
// first look up the entry in the hashtable |
| 233 |
if (m_map.contains (szLang, 0)) |
| 234 |
{ |
| 235 |
return static_cast<SpellCheckerClass *>(const_cast<void *>(m_map.pick (szLang))); |
| 236 |
} |
| 237 |
|
| 238 |
// not found, so insert it |
| 239 |
checker = new SpellCheckerClass (); |
| 240 |
|
| 241 |
checker->setLanguage (szLang); |
| 242 |
|
| 243 |
if (checker->requestDictionary (szLang)) |
| 244 |
{ |
| 245 |
m_map.insert (szLang, static_cast<void *>(checker)); |
| 246 |
m_lastDict = checker; |
| 247 |
m_nLoadedDicts++; |
| 248 |
checker->setDictionaryFound(true); |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
checker->setDictionaryFound(false); |
| 253 |
m_missingHashs += szLang; |
| 254 |
delete checker; |
| 255 |
checker = NULL; |
| 256 |
} |
| 257 |
|
| 258 |
return checker; |
| 259 |
} |
| 260 |
|
| 261 |
/*! |
| 262 |
* Returns the last used dictionary, or 0 |
| 263 |
* \return The last valid SpellChecker or 0 |
| 264 |
*/ |
| 265 |
SpellChecker * |
| 266 |
SpellManager::lastDictionary () const |
| 267 |
{ |
| 268 |
return m_lastDict; |
| 269 |
} |
| 270 |
|
| 271 |
SpellChecker * SpellManager::getInstance() const |
| 272 |
{ |
| 273 |
return new SpellCheckerClass (); |
| 274 |
} |