Skip to content

Commit

Permalink
Delete unused dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-gromeyer committed Oct 30, 2023
1 parent 3b4bfb4 commit 94d0a6e
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/spellchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Language

#ifndef NO_SPELLCHECK
nuspell::v5::Dictionary *dict = nullptr;
int instanceCount = 0;
#endif
};
QHash<QString, Language> langPathMap;
Expand Down Expand Up @@ -225,12 +226,15 @@ auto SpellChecker::setLanguage(const QString &lang) -> bool
if (dic.dir.empty())
return false;
} else if (dic.dict) {
++dic.instanceCount;
languages.append(lang);
return true;
}

try {
dic.dict = new nuspell::Dictionary();
dic.dict->load_aff_dic(dic.dir);
++dic.instanceCount;
languages.append(lang);
} catch (const nuspell::Dictionary_Loading_Error &e) {
qWarning() << "Failed to load dictionary: " << e.what();
Expand Down Expand Up @@ -461,6 +465,15 @@ void SpellChecker::slotSetLanguage(const bool checked)
{
#ifndef NO_SPELLCHECK
languages.removeAll(lang);
Language &language = langPathMap[lang];
--language.instanceCount;

if (language.instanceCount == 0) {
qDebug() << __func__ << "Deleting dict:" << language.encodedName;
delete language.dict;
language.dict = nullptr;
}

rehighlight();
return;
#endif
Expand Down Expand Up @@ -546,4 +559,18 @@ bool SpellChecker::spellerLoaded() const
#endif
}

SpellChecker::~SpellChecker() = default;
SpellChecker::~SpellChecker()
{
#ifndef NO_SPELLCHECK
for (const QString &lang : languages) {
Language &language = langPathMap[lang];
--language.instanceCount;

if (language.instanceCount == 0) {
qDebug() << __func__ << "Deleting dict:" << language.encodedName;
delete language.dict;
language.dict = nullptr;
}
}
#endif
};

0 comments on commit 94d0a6e

Please sign in to comment.