From bc6e1e7a0d06e2945eb3ba55c0e27e8f00130f34 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 20 Feb 2020 22:27:21 +0300 Subject: [PATCH] Added new setting for automatic dictionaries download. --- Telegram/Resources/langs/lang.strings | 1 + .../chat_helpers/spellchecker_common.cpp | 28 ++++++++++--------- Telegram/SourceFiles/main/main_settings.cpp | 6 ++++ Telegram/SourceFiles/main/main_settings.h | 14 ++++++++++ .../settings/settings_advanced.cpp | 21 ++++++++++++-- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index db08ef19f..9899731a2 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -423,6 +423,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_spellchecker" = "Spell checker"; "lng_settings_system_spellchecker" = "Use system spell checker"; "lng_settings_custom_spellchecker" = "Use spell checker"; +"lng_settings_auto_download_dictionaries" = "Automatic dictionaries download"; "lng_settings_manage_dictionaries" = "Manage dictionaries"; "lng_settings_manage_enabled_dictionary" = "Dictionary is enabled"; "lng_settings_manage_remove_dictionary" = "Remove Dictionary"; diff --git a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp index cf580ae53..7beca4351 100644 --- a/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp +++ b/Telegram/SourceFiles/chat_helpers/spellchecker_common.cpp @@ -343,37 +343,39 @@ void Start(not_null session) { { &ph::lng_spellchecker_remove, tr::lng_spellchecker_remove() }, { &ph::lng_spellchecker_ignore, tr::lng_spellchecker_ignore() }, } }); + const auto settings = &session->settings(); if (!Platform::Spellchecker::IsSystemSpellchecker()) { Spellchecker::SetWorkingDirPath(DictionariesPath()); - session->settings().dictionariesEnabledChanges( + settings->dictionariesEnabledChanges( ) | rpl::start_with_next([](auto dictionaries) { Platform::Spellchecker::UpdateLanguages(dictionaries); }, session->lifetime()); - session->settings().spellcheckerEnabledChanges( + settings->spellcheckerEnabledChanges( ) | rpl::start_with_next([=](auto enabled) { Platform::Spellchecker::UpdateLanguages( enabled - ? session->settings().dictionariesEnabled() + ? settings->dictionariesEnabled() : std::vector()); }, session->lifetime()); - session->data().contactsLoaded().changes( - ) | rpl::start_with_next([=](bool loaded) { - if (!loaded) { - return; - } - - DownloadDictionaryInBackground(session, 0, DefaultLanguages()); - }, session->lifetime()); + if (settings->autoDownloadDictionaries()) { + session->data().contactsLoaded().changes( + ) | rpl::start_with_next([=](bool loaded) { + if (!loaded) { + return; + } + DownloadDictionaryInBackground(session, 0, DefaultLanguages()); + }, session->lifetime()); + } } - if (session->settings().spellcheckerEnabled()) { + if (settings->spellcheckerEnabled()) { Platform::Spellchecker::UpdateLanguages( - session->settings().dictionariesEnabled()); + settings->dictionariesEnabled()); } } diff --git a/Telegram/SourceFiles/main/main_settings.cpp b/Telegram/SourceFiles/main/main_settings.cpp index 95beb2bd9..b9f1f07b1 100644 --- a/Telegram/SourceFiles/main/main_settings.cpp +++ b/Telegram/SourceFiles/main/main_settings.cpp @@ -122,6 +122,7 @@ QByteArray Settings::serialize() const { for (const auto i : _variables.dictionariesEnabled.current()) { stream << quint64(i); } + stream << qint32(_variables.autoDownloadDictionaries.current() ? 1 : 0); } return result; } @@ -175,6 +176,7 @@ void Settings::constructFromSerialized(const QByteArray &serialized) { qint32 videoPlaybackSpeed = SerializePlaybackSpeed(_variables.videoPlaybackSpeed.current()); QByteArray videoPipGeometry = _variables.videoPipGeometry; std::vector dictionariesEnabled; + qint32 autoDownloadDictionaries = _variables.autoDownloadDictionaries.current() ? 1 : 0; stream >> versionTag; if (versionTag == kVersionTag) { @@ -312,6 +314,9 @@ void Settings::constructFromSerialized(const QByteArray &serialized) { } } } + if (!stream.atEnd()) { + stream >> autoDownloadDictionaries; + } if (stream.status() != QDataStream::Ok) { LOG(("App Error: " "Bad data for Main::Settings::constructFromSerialized()")); @@ -402,6 +407,7 @@ void Settings::constructFromSerialized(const QByteArray &serialized) { _variables.videoPlaybackSpeed = DeserializePlaybackSpeed(videoPlaybackSpeed); _variables.videoPipGeometry = videoPipGeometry; _variables.dictionariesEnabled = std::move(dictionariesEnabled); + _variables.autoDownloadDictionaries = (autoDownloadDictionaries == 1); } void Settings::setSupportChatsTimeSlice(int slice) { diff --git a/Telegram/SourceFiles/main/main_settings.h b/Telegram/SourceFiles/main/main_settings.h index ba0d8b9c4..264f51f05 100644 --- a/Telegram/SourceFiles/main/main_settings.h +++ b/Telegram/SourceFiles/main/main_settings.h @@ -253,6 +253,19 @@ public: return _variables.dictionariesEnabled.changes(); } + void setAutoDownloadDictionaries(bool value) { + _variables.autoDownloadDictionaries = value; + } + bool autoDownloadDictionaries() const { + return _variables.autoDownloadDictionaries.current(); + } + rpl::producer autoDownloadDictionariesValue() const { + return _variables.autoDownloadDictionaries.value(); + } + rpl::producer autoDownloadDictionariesChanges() const { + return _variables.autoDownloadDictionaries.changes(); + } + [[nodiscard]] float64 videoPlaybackSpeed() const { return _variables.videoPlaybackSpeed.current(); } @@ -311,6 +324,7 @@ private: rpl::variable videoPlaybackSpeed = 1.; QByteArray videoPipGeometry; rpl::variable> dictionariesEnabled; + rpl::variable autoDownloadDictionaries = true; static constexpr auto kDefaultSupportChatsLimitSlice = 7 * 24 * 60 * 60; diff --git a/Telegram/SourceFiles/settings/settings_advanced.cpp b/Telegram/SourceFiles/settings/settings_advanced.cpp index 3b250a21f..95c9f369e 100644 --- a/Telegram/SourceFiles/settings/settings_advanced.cpp +++ b/Telegram/SourceFiles/settings/settings_advanced.cpp @@ -261,6 +261,7 @@ void SetupSpellchecker( not_null container) { #ifndef TDESKTOP_DISABLE_SPELLCHECK const auto session = &controller->session(); + const auto settings = &session->settings(); const auto isSystem = Platform::Spellchecker::IsSystemSpellchecker(); const auto button = AddButton( container, @@ -269,14 +270,14 @@ void SetupSpellchecker( : tr::lng_settings_custom_spellchecker(), st::settingsButton )->toggleOn( - rpl::single(session->settings().spellcheckerEnabled()) + rpl::single(settings->spellcheckerEnabled()) ); button->toggledValue( ) | rpl::filter([=](bool enabled) { - return (enabled != session->settings().spellcheckerEnabled()); + return (enabled != settings->spellcheckerEnabled()); }) | rpl::start_with_next([=](bool enabled) { - session->settings().setSpellcheckerEnabled(enabled); + settings->setSpellcheckerEnabled(enabled); session->saveSettingsDelayed(); }, container->lifetime()); @@ -289,6 +290,20 @@ void SetupSpellchecker( container, object_ptr(container))); + AddButton( + sliding->entity(), + tr::lng_settings_auto_download_dictionaries(), + st::settingsButton + )->toggleOn( + rpl::single(settings->autoDownloadDictionaries()) + )->toggledValue( + ) | rpl::filter([=](bool enabled) { + return (enabled != settings->autoDownloadDictionaries()); + }) | rpl::start_with_next([=](bool enabled) { + settings->setAutoDownloadDictionaries(enabled); + session->saveSettingsDelayed(); + }, sliding->entity()->lifetime()); + AddButtonWithLabel( sliding->entity(), tr::lng_settings_manage_dictionaries(),