diff --git a/src/app/mainview/components/MessageBarTextArea.qml b/src/app/mainview/components/MessageBarTextArea.qml index 001ceea7..3b1637c8 100644 --- a/src/app/mainview/components/MessageBarTextArea.qml +++ b/src/app/mainview/components/MessageBarTextArea.qml @@ -113,9 +113,7 @@ JamiFlickable { } } - readonly property bool spellCheckEnabled: - AppSettingsManager.getValue(Settings.EnableSpellCheck) && - AppSettingsManager.getValue(Settings.SpellLang) !== "" + property bool spellCheckEnabled: AppSettingsManager.getValue(Settings.EnableSpellCheck) && AppSettingsManager.getValue(Settings.SpellLang) !== "" // Spell check is active under the following conditions: // 1. Spell check is enabled in settings @@ -192,7 +190,7 @@ JamiFlickable { onReleased: function (event) { if (event.button === Qt.RightButton) { - if (spellCheckActive) { + if (spellCheckActive && SpellCheckAdapter.hasLoadedDictionary) { var position = textArea.positionAt(event.x, event.y); textArea.moveCursorSelection(position, TextInput.SelectWords); textArea.selectWord(); @@ -254,7 +252,7 @@ JamiFlickable { function updateSpellCorrection() { clearUnderlines(); // We iterate over the whole text to find words to check and underline them if needed - if (spellCheckActive) { + if (spellCheckActive && SpellCheckAdapter.hasLoadedDictionary) { var text = textArea.text; var words = SpellCheckAdapter.findWords(text); if (!words) diff --git a/src/app/spellcheckadapter.cpp b/src/app/spellcheckadapter.cpp index f1f875c4..2e0a276f 100644 --- a/src/app/spellcheckadapter.cpp +++ b/src/app/spellcheckadapter.cpp @@ -134,6 +134,9 @@ SpellCheckAdapter::setDictionary(const QString& locale) auto localPath = dictionaryListModel_->pathForLocale(locale); if (spellChecker_.replaceDictionary(localPath)) { settingsManager_->setValue(Settings::Key::SpellLang, locale); + set_hasLoadedDictionary(true); Q_EMIT dictionaryChanged(); + } else { + qWarning() << "Failed to set dictionary for locale:" << locale; } } diff --git a/src/app/spellcheckadapter.h b/src/app/spellcheckadapter.h index cfc21aaa..e93551ce 100644 --- a/src/app/spellcheckadapter.h +++ b/src/app/spellcheckadapter.h @@ -33,6 +33,7 @@ class SpellCheckAdapter final : public QObject QML_SINGLETON QML_RO_PROPERTY(int, installedDictionaryCount) + QML_RO_PROPERTY(bool, hasLoadedDictionary) public: static SpellCheckAdapter* create(QQmlEngine* engine, QJSEngine*) diff --git a/src/app/spellchecker.cpp b/src/app/spellchecker.cpp index 2245877c..2a7f2d5a 100644 --- a/src/app/spellchecker.cpp +++ b/src/app/spellchecker.cpp @@ -31,7 +31,10 @@ SpellChecker::SpellChecker() : hunspell_(new Hunspell("", "")) -{} +{ + // Initialize with default UTF-8 codec + codec_ = QTextCodec::codecForName("UTF-8"); +} bool SpellChecker::spell(const QString& word) @@ -75,11 +78,26 @@ SpellChecker::replaceDictionary(const QString& dictionaryPath) QString dictFile = dictionaryPath + ".dic"; QString affixFile = dictionaryPath + ".aff"; + + // Check if dictionary files exist + if (!QFile::exists(dictFile) || !QFile::exists(affixFile)) { + qWarning() << "Dictionary files not found:" << dictFile << affixFile; + return false; + } + QByteArray dictFilePath = dictFile.toLocal8Bit(); QByteArray affixFilePath = affixFile.toLocal8Bit(); - hunspell_.reset(new Hunspell(affixFilePath.constData(), dictFilePath.constData())); - encoding_ =hunspell_->get_dic_encoding(); - codec_ = QTextCodec::codecForName(this->encoding_.toLatin1().constData()); + + std::unique_ptr hunspell( + new Hunspell(affixFilePath.constData(), dictFilePath.constData())); + auto encoding = hunspell->get_dic_encoding(); + auto codec = QTextCodec::codecForName(encoding); + if (!codec) { + return false; + } + + hunspell_ = std::move(hunspell); + codec_ = codec; currentDictionaryPath_ = dictionaryPath; return true; } diff --git a/src/app/spellchecker.h b/src/app/spellchecker.h index b57757f9..c6499886 100644 --- a/src/app/spellchecker.h +++ b/src/app/spellchecker.h @@ -57,6 +57,5 @@ private: std::unique_ptr hunspell_; QString currentDictionaryPath_; - QString encoding_; QTextCodec* codec_; };