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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/***************************************************************************
* 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"
#include "JlCompress.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()) {
if (!QFileInfo(affFile).exists())
mLastError = tr("Missing .aff file:\n%1").arg(affFile);
else
mLastError = tr("Dictionary does not exist.");
emit dictionaryLoaded(); //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 = "";
mLastError = "Creation of Hunspell object failed.";
REQUIRE_RET(false, false);
}
spell_encoding = QString(pChecker->get_dic_encoding());
spellCodec = QTextCodec::codecForName(spell_encoding.toLatin1());
if (spellCodec == 0) {
mLastError = "Could not determine encoding.";
unload();
emit dictionaryLoaded();
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 = "";
mLastError.clear();
emit dictionaryLoaded();
return true;
}
QFile f(ignoreListFileName);
if (!f.open(QFile::ReadOnly)) {
mLastError.clear();
emit dictionaryLoaded();
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(), localeAwareLessThan);
while (!ignoredWordList.empty() && ignoredWordList.first().startsWith("%")) ignoredWordList.removeFirst();
ignoredWordsModel.setStringList(ignoredWordList);
ignoredWords = ignoredWordList.toSet();
mLastError.clear();
emit dictionaryLoaded();
return true;
}
void SpellerUtility::saveIgnoreList()
{
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"));
}
}
}
void SpellerUtility::unload()
{
saveIgnoreList();
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, localeAwareLessThan), word);
ignoredWordsModel.setStringList(ignoredWordList);
saveIgnoreList();
emit ignoredWordAdded(word);
}
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);
saveIgnoreList();
}
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;
}
}
bool SpellerManager::isOxtDictionary(const QString &fileName)
{
QFileInfo fi(fileName);
if (!(QStringList() << "oxt" << "zip").contains(fi.suffix())) return false;
QStringList files = JlCompress::getFileList(fileName);
QString affFile;
QString dicFile;
foreach (const QString &f, files) {
if (f.endsWith(".aff")) {
affFile = f;
}
if (f.endsWith(".dic")) {
dicFile = f;
}
}
if (affFile.length() <= 4 || dicFile.length() <= 4) return false;
if (affFile.left(affFile.length() - 4) != affFile.left(affFile.length() - 4)) return false; // different names
return true;
}
bool SpellerManager::importDictionary(const QString &fileName, const QString &targetDir)
{
if (!isOxtDictionary(fileName)) {
bool continueAnyway = txsConfirmWarning(tr("The selected file does not seem to contain a Hunspell dictionary. Do you want to import it nevertheless?"));
if (!continueAnyway) {
return false;
}
}
QFileInfo fi(fileName);
QStringList extractedFiles = JlCompress::extractDir(fileName, QDir(targetDir).filePath(fi.fileName()));
if (extractedFiles.isEmpty()) {
txsWarning(tr("Dictionary import failed: No files could be extracted."));
}
return !extractedFiles.isEmpty();
}
void SpellerManager::setIgnoreFilePrefix(const QString &prefix)
{
ignoreFilePrefix = prefix;
}
void SpellerManager::setDictPaths(const QStringList &dictPaths)
{
if (dictPaths == m_dictPaths) return;
m_dictPaths = dictPaths;
QList<SpellerUtility *> oldDicts = dicts.values();
dicts.clear();
dictFiles.clear();
QMap<QString, QString> usedFiles;
foreach (const QString &path, dictPaths) {
scanForDictionaries(path);
}
// 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();
}
void SpellerManager::scanForDictionaries(const QString &path, bool scansubdirs)
{
if (path.isEmpty()) return;
QDirIterator iterator(path);
while (iterator.hasNext()) {
iterator.next();
if (!iterator.fileInfo().isDir()) {
if (iterator.fileName().endsWith(".dic") && !iterator.fileName().startsWith("hyph_")) {
QFileInfo fi(iterator.fileInfo());
QString realDictFile = (fi.isSymLink()) ? QFileInfo(fi.symLinkTarget()).canonicalFilePath() : fi.canonicalFilePath();
if (dictFiles.contains(fi.baseName()))
continue;
dictFiles.insert(fi.baseName(), realDictFile);
}
} else if (scansubdirs) {
scanForDictionaries(iterator.filePath(), false);
}
}
}
QStringList SpellerManager::availableDicts()
{
if (dictFiles.keys().isEmpty())
return QStringList() << emptySpeller->name();
return QStringList(dictFiles.keys());
}
bool SpellerManager::hasSpeller(const QString &name)
{
if (name == emptySpeller->name()) return true;
return dictFiles.contains(name);
}
bool match(QString &guess, const QList<QString> &keys)
{
for (int i = 0; i < keys.length(); i++) {
if (0 == QString::compare(keys[i], guess, Qt::CaseInsensitive)) {
guess = keys[i];
return true;
}
}
return false;
}
bool SpellerManager::hasSimilarSpeller(const QString &name, QString &bestName)
{
if (name.length() < 2) return false;
QList<QString> keys = dictFiles.keys();
// case insensitive match
bestName = name;
if (match(bestName, keys)) return true;
// match also with "_" -> "-" replacement
bestName = name;
if (bestName.contains('_')) {
bestName.replace("_", "-");
if (match(bestName, keys)) return true;
}
// match also with "-" -> "_" replacement
bestName = name;
if (bestName.contains('-')) {
bestName.replace("-", "_");
if (match(bestName, keys)) return true;
}
// match only beginning to beginning of keys
bestName = name;
QString alternative = name;
alternative.replace("_", "-");
for (int i = 0; i < keys.length(); i++)
if (keys[i].startsWith(bestName, Qt::CaseInsensitive) || keys[i].startsWith(alternative)) {
bestName = keys[i];
return true;
}
return false;
}
/*!
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)) {
txsWarning(QString("Loading of dictionary failed:\n%1\n\n%2").arg(dictFiles.value(name)).arg(su->mLastError));
delete su;
return 0;
}
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()));
}
}
|