Autosaving of JSON settings
This commit is contained in:
parent
4caedcbf49
commit
838b2e8c1a
5 changed files with 128 additions and 23 deletions
|
|
@ -18,24 +18,12 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
|
||||||
#include <QtCore/QJsonObject>
|
#include <QtCore/QJsonObject>
|
||||||
#include <QtCore/QJsonArray>
|
#include <QtCore/QJsonArray>
|
||||||
#include <QtCore/QJsonValue>
|
#include <QtCore/QJsonValue>
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
|
||||||
namespace KotatoSettings {
|
namespace KotatoSettings {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class Manager {
|
constexpr auto kWriteJsonTimeout = crl::time(5000);
|
||||||
public:
|
|
||||||
void fill();
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
const QStringList &errors() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void writeDefaultFile();
|
|
||||||
bool readCustomFile();
|
|
||||||
|
|
||||||
QStringList _errors;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
QString DefaultFilePath() {
|
QString DefaultFilePath() {
|
||||||
return cWorkingDir() + qsl("tdata/kotato-settings-default.json");
|
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() {
|
void Manager::fill() {
|
||||||
if (!DefaultFileIsValid()) {
|
if (!DefaultFileIsValid()) {
|
||||||
writeDefaultFile();
|
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() {
|
void Manager::clear() {
|
||||||
_errors.clear();
|
_errors.clear();
|
||||||
}
|
}
|
||||||
|
|
@ -261,19 +266,83 @@ void Manager::writeDefaultFile() {
|
||||||
file.write(document.toJson(QJsonDocument::Indented));
|
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() {
|
void Start() {
|
||||||
Data.fill();
|
Data.fill();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Write() {
|
||||||
|
Data.write();
|
||||||
|
}
|
||||||
|
|
||||||
const QStringList &Errors() {
|
const QStringList &Errors() {
|
||||||
return Data.errors();
|
return Data.errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Finish() {
|
void Finish() {
|
||||||
|
Data.write(true);
|
||||||
Data.clear();
|
Data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,37 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
|
||||||
namespace KotatoSettings {
|
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 Start();
|
||||||
|
void Write();
|
||||||
void Finish();
|
void Finish();
|
||||||
|
|
||||||
const QStringList &Errors();
|
const QStringList &Errors();
|
||||||
|
|
|
||||||
|
|
@ -235,6 +235,7 @@ rpl::producer<bool> BigEmojiOutlineChanges() {
|
||||||
bool gAlwaysShowScheduled = true;
|
bool gAlwaysShowScheduled = true;
|
||||||
bool gShowChatId = true;
|
bool gShowChatId = true;
|
||||||
|
|
||||||
|
int gNetSpeedBoost = 0;
|
||||||
int gNetRequestsCount = 2;
|
int gNetRequestsCount = 2;
|
||||||
int gNetDownloadSessionsCount = 2;
|
int gNetDownloadSessionsCount = 2;
|
||||||
int gNetUploadSessionsCount = 2;
|
int gNetUploadSessionsCount = 2;
|
||||||
|
|
|
||||||
|
|
@ -195,6 +195,7 @@ void SetStickerHeight(int height);
|
||||||
DeclareSetting(bool, AlwaysShowScheduled);
|
DeclareSetting(bool, AlwaysShowScheduled);
|
||||||
DeclareSetting(bool, ShowChatId);
|
DeclareSetting(bool, ShowChatId);
|
||||||
|
|
||||||
|
DeclareSetting(int, NetSpeedBoost);
|
||||||
DeclareSetting(int, NetRequestsCount);
|
DeclareSetting(int, NetRequestsCount);
|
||||||
DeclareSetting(int, NetDownloadSessionsCount);
|
DeclareSetting(int, NetDownloadSessionsCount);
|
||||||
DeclareSetting(int, NetUploadSessionsCount);
|
DeclareSetting(int, NetUploadSessionsCount);
|
||||||
|
|
@ -203,16 +204,18 @@ DeclareSetting(int, NetUploadRequestInterval);
|
||||||
|
|
||||||
inline void SetNetworkBoost(int boost) {
|
inline void SetNetworkBoost(int boost) {
|
||||||
if (boost < 0) {
|
if (boost < 0) {
|
||||||
boost = 0;
|
cSetNetSpeedBoost(0);
|
||||||
} else if (boost > 3) {
|
} else if (boost > 3) {
|
||||||
boost = 3;
|
cSetNetSpeedBoost(3);
|
||||||
|
} else {
|
||||||
|
cSetNetSpeedBoost(boost);
|
||||||
}
|
}
|
||||||
|
|
||||||
cSetNetRequestsCount(2 + (2 * boost));
|
cSetNetRequestsCount(2 + (2 * cNetSpeedBoost()));
|
||||||
cSetNetDownloadSessionsCount(2 + (2 * boost));
|
cSetNetDownloadSessionsCount(2 + (2 * cNetSpeedBoost()));
|
||||||
cSetNetUploadSessionsCount(2 + (2 * boost));
|
cSetNetUploadSessionsCount(2 + (2 * cNetSpeedBoost()));
|
||||||
cSetNetMaxFileQueries(16 + (16 * boost));
|
cSetNetMaxFileQueries(16 + (16 * cNetSpeedBoost()));
|
||||||
cSetNetUploadRequestInterval(500 - (100 * boost));
|
cSetNetUploadRequestInterval(500 - (100 * cNetSpeedBoost()));
|
||||||
}
|
}
|
||||||
|
|
||||||
DeclareSetting(bool, ShowPhoneInDrawer);
|
DeclareSetting(bool, ShowPhoneInDrawer);
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL
|
||||||
#include "window/window_session_controller.h"
|
#include "window/window_session_controller.h"
|
||||||
#include "lang/lang_keys.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "core/update_checker.h"
|
#include "core/update_checker.h"
|
||||||
|
#include "core/kotato_settings.h"
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
#include "storage/localstorage.h"
|
#include "storage/localstorage.h"
|
||||||
#include "data/data_session.h"
|
#include "data/data_session.h"
|
||||||
|
|
@ -57,6 +58,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
|
||||||
const auto updateStickerHeight = [=](int value) {
|
const auto updateStickerHeight = [=](int value) {
|
||||||
updateStickerHeightLabel(value);
|
updateStickerHeightLabel(value);
|
||||||
SetStickerHeight(value);
|
SetStickerHeight(value);
|
||||||
|
KotatoSettings::Write();
|
||||||
};
|
};
|
||||||
stickerHeightSlider->resize(st::settingsAudioVolumeSlider.seekSize);
|
stickerHeightSlider->resize(st::settingsAudioVolumeSlider.seekSize);
|
||||||
stickerHeightSlider->setPseudoDiscrete(
|
stickerHeightSlider->setPseudoDiscrete(
|
||||||
|
|
@ -77,6 +79,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
|
||||||
return (enabled != BigEmojiOutline());
|
return (enabled != BigEmojiOutline());
|
||||||
}) | rpl::start_with_next([](bool enabled) {
|
}) | rpl::start_with_next([](bool enabled) {
|
||||||
SetBigEmojiOutline(enabled);
|
SetBigEmojiOutline(enabled);
|
||||||
|
KotatoSettings::Write();
|
||||||
}, container->lifetime());
|
}, container->lifetime());
|
||||||
|
|
||||||
AddButton(
|
AddButton(
|
||||||
|
|
@ -91,6 +94,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
|
||||||
}) | rpl::start_with_next([](bool enabled) {
|
}) | rpl::start_with_next([](bool enabled) {
|
||||||
cSetAlwaysShowScheduled(enabled);
|
cSetAlwaysShowScheduled(enabled);
|
||||||
Notify::showScheduledButtonChanged();
|
Notify::showScheduledButtonChanged();
|
||||||
|
KotatoSettings::Write();
|
||||||
}, container->lifetime());
|
}, container->lifetime());
|
||||||
|
|
||||||
AddSkip(container);
|
AddSkip(container);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue