Skip to content

Commit

Permalink
Autosaving of JSON settings
Browse files Browse the repository at this point in the history
  • Loading branch information
EricKotato committed Oct 11, 2019
1 parent 4caedcb commit 838b2e8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 23 deletions.
101 changes: 85 additions & 16 deletions Telegram/SourceFiles/core/kotato_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,12 @@ For license and copyright information please follow this link:
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonValue>
#include <QtCore/QTimer>

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");
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand Down
28 changes: 28 additions & 0 deletions Telegram/SourceFiles/core/kotato_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,37 @@ For license and copyright information please follow this link:
*/
#pragma once

#include <QtCore/QTimer>

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();
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ rpl::producer<bool> BigEmojiOutlineChanges() {
bool gAlwaysShowScheduled = true;
bool gShowChatId = true;

int gNetSpeedBoost = 0;
int gNetRequestsCount = 2;
int gNetDownloadSessionsCount = 2;
int gNetUploadSessionsCount = 2;
Expand Down
17 changes: 10 additions & 7 deletions Telegram/SourceFiles/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/settings/settings_kotato.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -57,6 +58,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
const auto updateStickerHeight = [=](int value) {
updateStickerHeightLabel(value);
SetStickerHeight(value);
KotatoSettings::Write();
};
stickerHeightSlider->resize(st::settingsAudioVolumeSlider.seekSize);
stickerHeightSlider->setPseudoDiscrete(
Expand All @@ -77,6 +79,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
return (enabled != BigEmojiOutline());
}) | rpl::start_with_next([](bool enabled) {
SetBigEmojiOutline(enabled);
KotatoSettings::Write();
}, container->lifetime());

AddButton(
Expand All @@ -91,6 +94,7 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
}) | rpl::start_with_next([](bool enabled) {
cSetAlwaysShowScheduled(enabled);
Notify::showScheduledButtonChanged();
KotatoSettings::Write();
}, container->lifetime());

AddSkip(container);
Expand Down

0 comments on commit 838b2e8

Please sign in to comment.