diff --git a/Telegram/SourceFiles/core/kotato_settings.cpp b/Telegram/SourceFiles/core/kotato_settings.cpp index 4f972da627..496552c459 100644 --- a/Telegram/SourceFiles/core/kotato_settings.cpp +++ b/Telegram/SourceFiles/core/kotato_settings.cpp @@ -18,24 +18,12 @@ For license and copyright information please follow this link: #include #include #include +#include namespace KotatoSettings { namespace { -class Manager { -public: - void fill(); - void clear(); - - const QStringList &errors() const; - -private: - void writeDefaultFile(); - bool readCustomFile(); - - QStringList _errors; - -}; +constexpr auto kWriteJsonTimeout = crl::time(5000); QString DefaultFilePath() { return cWorkingDir() + qsl("tdata/kotato-settings-default.json"); @@ -78,6 +66,15 @@ void WriteDefaultCustomFile() { } } +Manager Data; + +} // namespace + +Manager::Manager() { + _jsonWriteTimer.setSingleShot(true); + connect(&_jsonWriteTimer, SIGNAL(timeout()), this, SLOT(writeTimeout())); +} + void Manager::fill() { if (!DefaultFileIsValid()) { writeDefaultFile(); @@ -87,6 +84,14 @@ void Manager::fill() { } } +void Manager::write(bool force) { + if (!_jsonWriteTimer.isActive()) { + _jsonWriteTimer.start(kWriteJsonTimeout); + } else if (_jsonWriteTimer.remainingTime() <= 0 || (force && _jsonWriteTimer.isActive())) { + writeTimeout(); + } +} + void Manager::clear() { _errors.clear(); } @@ -261,19 +266,83 @@ void Manager::writeDefaultFile() { file.write(document.toJson(QJsonDocument::Indented)); } -Manager Data; +void Manager::writeCurrentSettings() { + auto file = QFile(CustomFilePath()); + if (!file.open(QIODevice::WriteOnly)) { + return; + } + writing(); + const char *customHeader = R"HEADER( +// This file was automatically generated from current settings +// It's better to edit it with app closed, so there will be no rewrites +// You should restart app to see changes -} // namespace +)HEADER"; + file.write(customHeader); + + auto settings = QJsonObject(); + + auto settingsFonts = QJsonObject(); + + if (!cMainFont().isEmpty()) { + settingsFonts.insert(qsl("main"), cMainFont()); + } + + if (!cSemiboldFont().isEmpty()) { + settingsFonts.insert(qsl("semibold"), cSemiboldFont()); + } + + if (!cMonospaceFont().isEmpty()) { + settingsFonts.insert(qsl("monospaced"), cMonospaceFont()); + } + + settingsFonts.insert(qsl("semibold_is_bold"), cSemiboldFontIsBold()); + + settings.insert(qsl("fonts"), settingsFonts); + + settings.insert(qsl("sticker_height"), StickerHeight()); + settings.insert(qsl("big_emoji_outline"), BigEmojiOutline()); + settings.insert(qsl("always_show_scheduled"), cAlwaysShowScheduled()); + settings.insert(qsl("show_chat_id"), cShowChatId()); + settings.insert(qsl("net_speed_boost"), cNetSpeedBoost()); + settings.insert(qsl("show_phone_in_drawer"), cShowPhoneInDrawer()); + + auto settingsScales = QJsonArray(); + auto currentScales = cInterfaceScales(); + + for (int i = 0; i < currentScales.size(); i++) { + settingsScales << currentScales[i]; + } + + settings.insert(qsl("scales"), settingsScales); + + auto document = QJsonDocument(); + document.setObject(settings); + file.write(document.toJson(QJsonDocument::Indented)); +} + +void Manager::writeTimeout() { + writeCurrentSettings(); +} + +void Manager::writing() { + _jsonWriteTimer.stop(); +} void Start() { Data.fill(); } +void Write() { + Data.write(); +} + const QStringList &Errors() { return Data.errors(); } void Finish() { + Data.write(true); Data.clear(); } diff --git a/Telegram/SourceFiles/core/kotato_settings.h b/Telegram/SourceFiles/core/kotato_settings.h index 84cffc5e2d..35c434e814 100644 --- a/Telegram/SourceFiles/core/kotato_settings.h +++ b/Telegram/SourceFiles/core/kotato_settings.h @@ -7,9 +7,37 @@ For license and copyright information please follow this link: */ #pragma once +#include + namespace KotatoSettings { +class Manager : public QObject { + Q_OBJECT + +public: + Manager(); + void fill(); + void clear(); + void write(bool force = false); + + const QStringList &errors() const; + +public slots: + void writeTimeout(); + +private: + void writeDefaultFile(); + void writeCurrentSettings(); + bool readCustomFile(); + void writing(); + + QStringList _errors; + QTimer _jsonWriteTimer; + +}; + void Start(); +void Write(); void Finish(); const QStringList &Errors(); diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index abb4621259..a3b8b1d17b 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -235,6 +235,7 @@ rpl::producer BigEmojiOutlineChanges() { bool gAlwaysShowScheduled = true; bool gShowChatId = true; +int gNetSpeedBoost = 0; int gNetRequestsCount = 2; int gNetDownloadSessionsCount = 2; int gNetUploadSessionsCount = 2; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index ae38fb63d0..9fa9407af7 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -195,6 +195,7 @@ void SetStickerHeight(int height); DeclareSetting(bool, AlwaysShowScheduled); DeclareSetting(bool, ShowChatId); +DeclareSetting(int, NetSpeedBoost); DeclareSetting(int, NetRequestsCount); DeclareSetting(int, NetDownloadSessionsCount); DeclareSetting(int, NetUploadSessionsCount); @@ -203,16 +204,18 @@ DeclareSetting(int, NetUploadRequestInterval); inline void SetNetworkBoost(int boost) { if (boost < 0) { - boost = 0; + cSetNetSpeedBoost(0); } else if (boost > 3) { - boost = 3; + cSetNetSpeedBoost(3); + } else { + cSetNetSpeedBoost(boost); } - cSetNetRequestsCount(2 + (2 * boost)); - cSetNetDownloadSessionsCount(2 + (2 * boost)); - cSetNetUploadSessionsCount(2 + (2 * boost)); - cSetNetMaxFileQueries(16 + (16 * boost)); - cSetNetUploadRequestInterval(500 - (100 * boost)); + cSetNetRequestsCount(2 + (2 * cNetSpeedBoost())); + cSetNetDownloadSessionsCount(2 + (2 * cNetSpeedBoost())); + cSetNetUploadSessionsCount(2 + (2 * cNetSpeedBoost())); + cSetNetMaxFileQueries(16 + (16 * cNetSpeedBoost())); + cSetNetUploadRequestInterval(500 - (100 * cNetSpeedBoost())); } DeclareSetting(bool, ShowPhoneInDrawer); diff --git a/Telegram/SourceFiles/settings/settings_kotato.cpp b/Telegram/SourceFiles/settings/settings_kotato.cpp index 378abf4dcc..37e2f33a18 100644 --- a/Telegram/SourceFiles/settings/settings_kotato.cpp +++ b/Telegram/SourceFiles/settings/settings_kotato.cpp @@ -24,6 +24,7 @@ For license and copyright information please follow this link: #include "window/window_session_controller.h" #include "lang/lang_keys.h" #include "core/update_checker.h" +#include "core/kotato_settings.h" #include "core/application.h" #include "storage/localstorage.h" #include "data/data_session.h" @@ -57,6 +58,7 @@ void SetupKotatoChats(not_null container) { const auto updateStickerHeight = [=](int value) { updateStickerHeightLabel(value); SetStickerHeight(value); + KotatoSettings::Write(); }; stickerHeightSlider->resize(st::settingsAudioVolumeSlider.seekSize); stickerHeightSlider->setPseudoDiscrete( @@ -77,6 +79,7 @@ void SetupKotatoChats(not_null container) { return (enabled != BigEmojiOutline()); }) | rpl::start_with_next([](bool enabled) { SetBigEmojiOutline(enabled); + KotatoSettings::Write(); }, container->lifetime()); AddButton( @@ -91,6 +94,7 @@ void SetupKotatoChats(not_null container) { }) | rpl::start_with_next([](bool enabled) { cSetAlwaysShowScheduled(enabled); Notify::showScheduledButtonChanged(); + KotatoSettings::Write(); }, container->lifetime()); AddSkip(container);