You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 lines
4.6 KiB

#include <QCoreApplication>
#include "storagesettingspage.h"
#include "../db/db_manager.h"
#include "../transport/gui_bridge.h"
#include "../transport/config_updater.h"
#include "../../lib/mem.h"
extern "C" {
#include "chat/chat_core.h"
}
#include <QVBoxLayout>
#include <QFormLayout>
#include <QCheckBox>
#include <QSpinBox>
#include <QLabel>
#include <QFrame>
static void saveUiStateInt(const char* key, int val) {
QByteArray v = QByteArray::number(val);
size_t klen = strlen(key), vlen = (size_t)v.size(), total = klen + 1 + vlen + 1;
if (total > 256) return;
struct save_ui_state_arg* arg = (struct save_ui_state_arg*)u_malloc(sizeof(struct save_ui_state_arg));
if (!arg) return;
arg->inst = gui_bridge_get_inst();
memcpy(arg->data, key, klen + 1);
memcpy(arg->data + klen + 1, v.constData(), vlen + 1);
gui_bridge_post_uasync_fn(chat_core_save_ui_state_trampoline, arg);
}
void postChatSetting(const QString& configPath, const char* name, int val) {
config_update_option(configPath, "chatserver", name, QString::number(val), CONFIG_UPDATE_REPLACE);
QByteArray v = QByteArray::number(val);
size_t nlen = strlen(name), vlen = (size_t)v.size(), total = nlen + 1 + vlen + 1;
if (total > 256) return;
struct chat_setting_arg* arg = (struct chat_setting_arg*)u_malloc(sizeof(struct chat_setting_arg));
if (!arg) return;
arg->inst = gui_bridge_get_inst();
memcpy(arg->data, name, nlen + 1);
memcpy(arg->data + nlen + 1, v.constData(), vlen + 1);
gui_bridge_post_uasync_fn(chat_core_set_setting_trampoline, arg);
}
StorageSettingsPage::StorageSettingsPage(DbManager* db, QWidget* parent)
: QWidget(parent)
, m_db(db)
{
auto* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 12, 0, 0);
layout->setSpacing(14);
auto* title = new QLabel(QCoreApplication::translate("ChatGui", "Media Storage"), this);
title->setStyleSheet("font-weight: bold; font-size: 13px;");
layout->addWidget(title);
auto* form = new QFormLayout();
form->setSpacing(10);
form->setContentsMargins(0, 8, 0, 0);
m_autoloadCheck = new QCheckBox(QCoreApplication::translate("ChatGui", "Autoload media"), this);
m_autoloadCheck->setStyleSheet("font-size: 13px;");
form->addRow(m_autoloadCheck);
m_autoloadMaxSize = new QSpinBox(this);
m_autoloadMaxSize->setRange(1, 500);
m_autoloadMaxSize->setSuffix(QCoreApplication::translate("ChatGui", " MB"));
m_autoloadMaxSize->setFixedWidth(110);
m_autoloadMaxSize->setStyleSheet(
"QSpinBox { border: 1px solid palette(mid); border-radius: 4px;"
" padding: 5px 8px; font-size: 13px; background: palette(base); }");
form->addRow(QCoreApplication::translate("ChatGui", "Autoload max size:"), m_autoloadMaxSize);
m_maxStorageSize = new QSpinBox(this);
m_maxStorageSize->setRange(1, 100);
m_maxStorageSize->setSuffix(QCoreApplication::translate("ChatGui", " GB"));
m_maxStorageSize->setFixedWidth(110);
m_maxStorageSize->setStyleSheet(
"QSpinBox { border: 1px solid palette(mid); border-radius: 4px;"
" padding: 5px 8px; font-size: 13px; background: palette(base); }");
form->addRow(QCoreApplication::translate("ChatGui", "Max storage size:"), m_maxStorageSize);
layout->addLayout(form);
auto* sep = new QFrame(this);
sep->setFrameShape(QFrame::HLine);
sep->setStyleSheet("QFrame { color: palette(mid); }");
layout->addWidget(sep);
layout->addStretch();
}
void StorageSettingsPage::loadFromDb() {
if (!m_db) return;
bool autoload = m_db->getUiStateInt("storage_autoload", 1);
m_autoloadCheck->setChecked(autoload);
int maxSizeMb = m_db->getUiStateInt("storage_autoload_maxsize_mb", 10);
m_autoloadMaxSize->setValue(maxSizeMb);
int maxGb = m_db->getUiStateInt("storage_maxsize_gb", 1);
m_maxStorageSize->setValue(maxGb);
}
void StorageSettingsPage::applyAndSave() {
int autoload = m_autoloadCheck->isChecked() ? 1 : 0;
int autoloadMaxSize = m_autoloadMaxSize->value();
int maxGb = m_maxStorageSize->value();
saveUiStateInt("storage_autoload", autoload);
saveUiStateInt("storage_autoload_maxsize_mb", autoloadMaxSize);
saveUiStateInt("storage_maxsize_gb", maxGb);
if (!m_configPath.isEmpty()) {
postChatSetting(m_configPath, "storage_autoload", autoload);
config_update_option(m_configPath, "chatserver", "storage_unit_size", QString::number(autoloadMaxSize) + "M", CONFIG_UPDATE_REPLACE);
config_update_option(m_configPath, "chatserver", "storage_total_size", QString::number(maxGb) + "G", CONFIG_UPDATE_REPLACE);
}
}