1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/***************************************************************************
* copyright : (C) 2008 by Benito van der Zander *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "spellerutility.h"
#include "smallUsefulFunctions.h"
int SpellerUtility::spellcheckErrorFormat = -1;
SpellerUtility::SpellerUtility(QString name): mName(name), currentDic(""), pChecker(0), spellCodec(0) {
checkCache.reserve(1020);
}
bool SpellerUtility::loadDictionary(QString dic,QString ignoreFilePrefix) {
if (dic==currentDic) return true;
else unload();
QString base = dic.left(dic.length()-4);
QString dicFile = base+".dic";
QString affFile = base+".aff";
if (!QFileInfo(dicFile).exists() || !QFileInfo(affFile).exists()) {
emit reloadDictionary(); //remove spelling error marks from errors with previous dictionary (because these marks could lead to crashes if not removed)
return false;
}
currentDic=dic;
pChecker = new Hunspell(affFile.toLocal8Bit(),dicFile.toLocal8Bit());
if (!pChecker) {
currentDic="";
ignoreListFileName="";
REQUIRE_RET(false,false);
}
spell_encoding=QString(pChecker->get_dic_encoding());
spellCodec = QTextCodec::codecForName(spell_encoding.toLatin1());
if (spellCodec==0) {
unload();
emit reloadDictionary();
return false;
}
checkCache.clear();
ignoredWords.clear();
ignoredWordList.clear();
ignoredWordsModel.setStringList(QStringList());
ignoreListFileName=base+".ign";
if (!isFileRealWritable(ignoreListFileName))
ignoreListFileName=ignoreFilePrefix+QFileInfo(dic).baseName()+".ign";
if (!isFileRealWritable(ignoreListFileName)) {
ignoreListFileName="";
emit reloadDictionary();
return true;
}
QFile f(ignoreListFileName);
if (!f.open(QFile::ReadOnly)) {
emit reloadDictionary();
return true;
}
ignoredWordList=QTextCodec::codecForName("UTF-8")->toUnicode(f.readAll()).split("\n",QString::SkipEmptyParts);
// add words in user dic
QByteArray encodedString;
QString spell_encoding=QString(pChecker->get_dic_encoding());
QTextCodec *codec = QTextCodec::codecForName(spell_encoding.toLatin1());
foreach(const QString elem,ignoredWordList){
encodedString = codec->fromUnicode(elem);
pChecker->add(encodedString.data());
}
qSort(ignoredWordList.begin(),ignoredWordList.end(),localAwareLessThan);
while (!ignoredWordList.empty() && ignoredWordList.first().startsWith("%")) ignoredWordList.removeFirst();
ignoredWordsModel.setStringList(ignoredWordList);
ignoredWords=ignoredWordList.toSet();
emit reloadDictionary();
return true;
}
void SpellerUtility::unload() {
if (ignoreListFileName!="" && ignoredWords.count()>0) {
QFile f(ignoreListFileName);
if (f.open(QFile::WriteOnly)) {
QTextCodec* utf8=QTextCodec::codecForName("UTF-8");
f.write(utf8->fromUnicode("%Ignored-Words;encoding:utf-8;version:" TEXSTUDIO ":1.8\n"));
foreach(const QString str, ignoredWordList)
if (!str.startsWith("%"))
f.write(utf8->fromUnicode(str+"\n"));
}
}
currentDic="";
ignoreListFileName="";
if (pChecker!=0) {
delete pChecker;
pChecker=0;
}
}
void SpellerUtility::addToIgnoreList(QString toIgnore) {
QString word=latexToPlainWord(toIgnore);
QByteArray encodedString;
QString spell_encoding=QString(pChecker->get_dic_encoding());
QTextCodec *codec = QTextCodec::codecForName(spell_encoding.toLatin1());
encodedString = codec->fromUnicode(word);
pChecker->add(encodedString.data());
ignoredWords.insert(word);
if (!ignoredWordList.contains(word))
ignoredWordList.insert(qLowerBound(ignoredWordList.begin(),ignoredWordList.end(), word, localAwareLessThan), word);
ignoredWordsModel.setStringList(ignoredWordList);
}
void SpellerUtility::removeFromIgnoreList(QString toIgnore) {
QByteArray encodedString;
QString spell_encoding=QString(pChecker->get_dic_encoding());
QTextCodec *codec = QTextCodec::codecForName(spell_encoding.toLatin1());
encodedString = codec->fromUnicode(toIgnore);
pChecker->remove(encodedString.data());
ignoredWords.remove(toIgnore);
ignoredWordList.removeAll(toIgnore);
ignoredWordsModel.setStringList(ignoredWordList);
}
QStringListModel* SpellerUtility::ignoreListModel() {
return &ignoredWordsModel;
}
SpellerUtility::~SpellerUtility() {
emit aboutToDelete();
unload();
}
bool SpellerUtility::check(QString word) {
if (currentDic=="" || pChecker==0) return true; //no speller => everything is correct
if (word.length()<=1) return true;
if (ignoredWords.contains(word)) return true;
if (word.endsWith('.')&&ignoredWords.contains(word.left(word.length()-1))) return true;
if (checkCache.contains(word)) return checkCache.value(word);
QByteArray encodedString = spellCodec->fromUnicode(word);
bool result=pChecker->spell(encodedString.data());
while (checkCacheInsertion.size()>1000) checkCacheInsertion.removeFirst();
checkCache.insert(word,result);
checkCacheInsertion.append(word);
return result;
}
QStringList SpellerUtility::suggest(QString word) {
Q_ASSERT(pChecker);
if (currentDic=="" || pChecker==0) return QStringList(); //no speller => everything is correct
QByteArray encodedString = spellCodec->fromUnicode(word);
char ** wlst;
int ns = pChecker->suggest(&wlst,encodedString.data());
QStringList suggestion;
if (ns > 0) {
for (int i=0; i < ns; i++) {
suggestion << spellCodec->toUnicode(wlst[i]);
free(wlst[i]);
}
free(wlst);
}
return suggestion;
}
SpellerManager::SpellerManager() {
emptySpeller = new SpellerUtility("<none>");
mDefaultSpellerName = emptySpeller->name();
}
SpellerManager::~SpellerManager() {
unloadAll();
if (emptySpeller) {
delete emptySpeller;
emptySpeller = 0;
}
}
void SpellerManager::setIgnoreFilePrefix(const QString &prefix) {
ignoreFilePrefix = prefix;
}
void SpellerManager::setDictPath(const QString &dictPath) {
if (dictPath == m_dictPath) return;
m_dictPath = dictPath;
QList<SpellerUtility *> oldDicts = dicts.values();
dicts.clear();
dictFiles.clear();
QDir dir(dictPath);
foreach (QFileInfo fi, dir.entryInfoList(QStringList() << "*.dic", QDir::Files, QDir::Name)) {
dictFiles.insert(fi.baseName(), fi.canonicalFilePath());
}
// delete after new dict files are identified so a user can reload the new dict in response to a aboutToDelete signal
foreach (SpellerUtility *su, oldDicts) {
delete su;
}
emit dictPathChanged();
}
QStringList SpellerManager::availableDicts() {
if (dictFiles.keys().isEmpty())
return QStringList() << emptySpeller->name();
return QStringList(dictFiles.keys());
}
QStringList SpellerManager::dictNamesForDir(const QString &dir) {
QStringList dictNames;
foreach (QFileInfo fi, QDir(dir).entryInfoList(QStringList() << "*.dic", QDir::Files, QDir::Name)) {
dictNames << fi.baseName();
}
if (dictNames.isEmpty())
return QStringList() << "<none>";
return dictNames;
}
bool SpellerManager::hasSpeller(const QString &name) {
if (name==emptySpeller->name()) return true;
return dictFiles.contains(name);
}
/*!
If the language has not been used yet, a SpellerUtility for the language is loaded. Otherwise
the existing SpellerUtility is returned. Possible names are the dictionary file names without ".dic"
ending and "<default>" for the default speller
*/
SpellerUtility *SpellerManager::getSpeller(QString name) {
if (name == "<default>") name = mDefaultSpellerName;
if (!dictFiles.contains(name)) return emptySpeller;
SpellerUtility *su = dicts.value(name, 0);
if (!su) {
su = new SpellerUtility(name);
if (!su->loadDictionary(dictFiles.value(name), ignoreFilePrefix)) {
delete su;
return emptySpeller;
}
dicts.insert(name, su);
}
return su;
}
QString SpellerManager::defaultSpellerName() {
return mDefaultSpellerName;
}
bool SpellerManager::setDefaultSpeller(const QString &name) {
if (dictFiles.contains(name)) {
mDefaultSpellerName = name;
emit defaultSpellerChanged();
return true;
}
return false;
}
void SpellerManager::unloadAll() {
foreach(SpellerUtility *su, dicts.values()) {
delete su;
}
dicts.clear();
}
QString SpellerManager::prettyName(const QString &name) {
QLocale loc(name);
if (loc == QLocale::c()) {
return name;
} else {
return QString("%1 - %2 (%3)").arg(name).arg(QLocale::languageToString(loc.language())).arg(QLocale::countryToString(loc.country()));
}
}
|