Skip to content

Commit

Permalink
plugin: Handle invalid values being set for active and enabled languages
Browse files Browse the repository at this point in the history
When a language is not found in the known paths, reset the value, and
remove it if also set in enabled languages.
  • Loading branch information
dobey committed Feb 5, 2022
1 parent bdcf4a5 commit ff81ea7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/plugin/inputmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ void InputMethod::onEnabledLanguageSettingsChanged()
{
Q_D(InputMethod);
d->enabledLanguages = d->m_settings.enabledLanguages();
// Reset the value if it gets unset
if (d->enabledLanguages.length() == 0) {
d->m_settings.resetEnabledLanguages();
}
// Switch to first language in enabled languages if the currently active
// language is no longer enabled
if (!d->enabledLanguages.contains(d->activeLanguage)) {
Expand Down Expand Up @@ -604,20 +608,33 @@ void InputMethod::selectNextLanguage()
//! \brief InputMethod::setActiveLanguage
//! Sets the currently active/used language
//! \param newLanguage id of the new language. For example "en" or "es"
//! FIXME check if the language is supported - if not use "en" as fallback
void InputMethod::setActiveLanguage(const QString &newLanguage)
{
Q_D(InputMethod);

qDebug() << "in inputMethod.cpp setActiveLanguage() activeLanguage is:" << newLanguage;

QString newPluginPath;
foreach(QString pluginPath, d->languagesPaths) {
QDir testDir(pluginPath + QDir::separator() + newLanguage);
if (testDir.exists()) {
d->currentPluginPath = testDir.absolutePath();
newPluginPath = testDir.absolutePath();
break;
}
}
// The language plpugin was not found, so reset the active language
if (newPluginPath.isEmpty()) {
d->m_settings.resetActiveLanguage();
// If the plugin was not found, and was in enabledLanguages list,
// also remove it from there
auto enabled = enabledLanguages();
if (enabled.contains(newLanguage)) {
enabled.removeAll(newLanguage);
d->m_settings.setEnabledLanguages(enabled);
}
return;
}
d->currentPluginPath = newPluginPath;

if (d->activeLanguage == newLanguage)
return;
Expand Down
24 changes: 20 additions & 4 deletions src/plugin/keyboardsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ KeyboardSettings::KeyboardSettings(QObject *parent) :
auto enabled = enabledLanguages();
if (enabled.contains(emoji)) {
enabled.removeAll(emoji);
if (enabled.isEmpty()) {
enabled << QLatin1String("en");
}
m_settings->set(ENABLED_LANGUAGES_KEY, enabled);
setEnabledLanguages(enabled);
}
if (activeLanguage() == emoji) {
setActiveLanguage(enabled[0]);
Expand All @@ -97,6 +94,11 @@ void KeyboardSettings::setActiveLanguage(const QString& id)
m_settings->set(ACTIVE_LANGUAGE_KEY, QVariant(id));
}

void KeyboardSettings::resetActiveLanguage()
{
m_settings->reset(ACTIVE_LANGUAGE_KEY);
}

/*!
* \brief KeyboardSettings::enabledLanguages returns a list of languages that are
* active
Expand All @@ -107,6 +109,20 @@ QStringList KeyboardSettings::enabledLanguages() const
return m_settings->get(ENABLED_LANGUAGES_KEY).toStringList();
}

void KeyboardSettings::setEnabledLanguages(const QStringList& ids)
{
if (ids.isEmpty()) {
resetEnabledLanguages();
return;
}
m_settings->set(ENABLED_LANGUAGES_KEY, ids);
}

void KeyboardSettings::resetEnabledLanguages()
{
m_settings->reset(ENABLED_LANGUAGES_KEY);
}

/*!
* \brief KeyboardSettings::autoCapitalization returns true id the first letter
* of each sentence should be capitalized
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/keyboardsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class KeyboardSettings : public QObject

QString activeLanguage() const;
void setActiveLanguage(const QString& id);
void resetActiveLanguage();
QStringList enabledLanguages() const;
void setEnabledLanguages(const QStringList& ids);
void resetEnabledLanguages();
bool autoCapitalization() const;
bool autoCompletion() const;
bool predictiveText() const;
Expand Down

0 comments on commit ff81ea7

Please sign in to comment.