Files
picpanel/core_support/fileconfigdecode/fileconfigdecode.cpp
2025-03-26 18:37:05 +08:00

250 lines
5.6 KiB
C++

#include "fileconfigdecode.h"
#include <QDebug>
QScopedPointer<FileConfigDecode> FileConfigDecode::m_instance;
FileConfigDecode::FileConfigDecode()
{
// if (m_instance.isNull()) {
// m_instance.reset(new FileConfigDecode);
// }
initFile();
}
FileConfigDecode *FileConfigDecode::getInstance()
{
if (m_instance.isNull()) {
m_instance.reset(new FileConfigDecode);
}
return m_instance.data();
}
FileConfigDecode::~FileConfigDecode()
{
updateFile();
}
QString FileConfigDecode::getAddress()
{
return this->address;
}
quint16 FileConfigDecode::getPort()
{
return port;
}
bool FileConfigDecode::getAutoSetup()
{
return autoSetup;
}
bool FileConfigDecode::getRename()
{
return rename;
}
bool FileConfigDecode::getTimeRename()
{
return timeRename;
}
bool FileConfigDecode::getAutoCopy()
{
return autoCopy;
}
QString FileConfigDecode::getUserName()
{
return userName;
}
QString FileConfigDecode::getPassword()
{
return password;
}
RenameType FileConfigDecode::getRenameType()
{
return renameType;
}
bool FileConfigDecode::getAutoLogin()
{
return autoLogin;
}
void FileConfigDecode::setAddress(QString value)
{
this->address = value;
updateFile();
}
void FileConfigDecode::setPort(quint16 value)
{
this->port = value;
updateFile();
}
void FileConfigDecode::setAutoSetup(bool value)
{
this->autoSetup = value;
updateFile();
}
void FileConfigDecode::setRename(bool value)
{
this->rename = value;
if (value == true) {
renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
}
else {
renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
}
// qDebug() << renameType;
updateFile();
}
void FileConfigDecode::setTimeRename(bool value, int flag)
{
qDebug() << value;
this->timeRename = value;
if (value) {
renameType = RENAME_TYPE_TIME;
}
if (!value && flag == 1) {
renameType = RENAME_TYPE_NORMAL;
}
if (!value && flag == 0) {
renameType = RENAME_TYPE_NONE;
}
// qDebug() << renameType;
updateFile();
}
void FileConfigDecode::setAutoCopy(bool value)
{
this->autoCopy = value;
updateFile();
}
void FileConfigDecode::setUserName(QString username)
{
this->userName = username;
updateFile();
}
void FileConfigDecode::setPassword(QString password)
{
this->password = password;
updateFile();
}
void FileConfigDecode::setAutoLogin(bool value)
{
autoLogin = value;
updateFile();
}
void FileConfigDecode::fileWrite(QString path)
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QSettings* config = new QSettings(path, QSettings::IniFormat);
config->setIniCodec(QTextCodec::codecForName("utf-8"));
QString section = QString("config");
config->beginGroup(section);
config->setValue("address", address);
config->setValue("autosetup", autosetup);
config->setValue("rename", rename);
config->setValue("timerename", timerename);
config->setValue("autocopy", autocopy);
config->endGroup();
delete config;
#else
QSettings* config = new QSettings(path, QSettings::IniFormat);
QString section = QString("config");
config->beginGroup(section);
config->setValue("address", address);
config->setValue("autosetup", autoSetup);
config->setValue("rename", rename);
config->setValue("timerename", timeRename);
config->setValue("autocopy", autoCopy);
config->setValue("username", userName);
config->setValue("password", password);
config->setValue("autologin", autoLogin);
delete config;
#endif
}
void FileConfigDecode::initFile()
{
QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QString addFilePath = addDataPath + "/config.ini";
qDebug() << addFilePath;
QFile file(addFilePath);
if (file.exists()) {
QSettings* config = new QSettings(addFilePath, QSettings::IniFormat);
QString section = "config/";
this->address = config->value(section + "address").toString();
this->port = config->value(section + "port").toUInt();
this->autoSetup = config->value(section + "autosetup").toBool();
this->rename = config->value(section + "rename").toBool();
this->timeRename = config->value(section + "timerename").toBool();
this->autoCopy = config->value(section + "autocopy").toBool();
this->userName = config->value(section+"username").toString();
this->password = config->value(section+"password").toString();
this->autoLogin = config->value(section+"autologin").toBool();
checkConfig();
this->firstCreate = false;
}
else {
qDebug() << "file not exists";
this->firstCreate = true;
this->address = "127.0.0.1:8080";
this->autoSetup = false;
this->rename = false;
this->timeRename = false;
this->autoCopy = false;
this->userName = "admin";
this->password = "admin";
this->autoLogin = false;
fileWrite(addFilePath);
}
}
void FileConfigDecode::checkConfig()
{
if (address.isEmpty()) {
address = "127.0.0.1:8080";
}
if (rename)
renameType = RENAME_TYPE_NORMAL;
if (timeRename)
renameType = RENAME_TYPE_TIME;
qDebug() << renameType;
}
void FileConfigDecode::updateFile()
{
QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QString addFilePath = addDataPath + "/config.ini";
// qDebug() << "write" << this->autoSetup << this->rename << this->timeRename << this->autoCopy;
fileWrite(addFilePath);
}