first commit
This commit is contained in:
301
project/fm_viewer/cfg/rm_settings_cfg_standard.cpp
Normal file
301
project/fm_viewer/cfg/rm_settings_cfg_standard.cpp
Normal file
@@ -0,0 +1,301 @@
|
||||
#include "rm_settings_cfg_standard.h"
|
||||
#if (USE_DEVICE_SETTINGS && !(RM_MODEL_EMT_KR))
|
||||
|
||||
#include <QDebug>
|
||||
#include <stdio.h>
|
||||
#include <QFile>
|
||||
|
||||
#if !(RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
unsigned char CFG::data[SETTINGS_CFG_SIZE] = {0,};
|
||||
unsigned char CFG::stored[SETTINGS_CFG_SIZE] = {0,};
|
||||
#endif // #if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
|
||||
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
|
||||
#if (ENCODE_CFG_BASE64)
|
||||
#include "fm_base64.h"
|
||||
#endif // ENCODE_CFG_BASE64
|
||||
|
||||
QJsonArray CFG::items;
|
||||
|
||||
QJsonObject CFG::_findType(QString key, int* index)
|
||||
{
|
||||
for(int i=0;i<items.size();i++) {
|
||||
QJsonObject obj = items.at(i).toObject();
|
||||
if(obj.value("key").toString() == key) {
|
||||
*index = i;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
*index = -1;
|
||||
return QJsonObject();
|
||||
}
|
||||
#endif // AN6000
|
||||
|
||||
#if (ENCODE_CFG_BASE64)
|
||||
bool CFG::isEncoded = false;
|
||||
#endif // #if (ENCODE_CFG_BASE64)
|
||||
|
||||
void CFG::setDefault()
|
||||
{
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
// CLEAR
|
||||
while(CFG::items.count()) {
|
||||
CFG::items.pop_back();
|
||||
}
|
||||
#if (AN6000_YEC)
|
||||
const char* fname = ":/html/an6000_cfg_yec.json";
|
||||
#else
|
||||
const char* fname = ":/html/an6000_cfg.json";
|
||||
#endif
|
||||
|
||||
QFile file(fname); // ":/html/an6000_cfg.json"
|
||||
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
CFG::items = QJsonDocument::fromJson(data).array();
|
||||
}
|
||||
#endif // #if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
|
||||
for(int i=0;i<items.size();i++) {
|
||||
QJsonObject obj = items.at(i).toObject();
|
||||
if(obj.contains("default")) {
|
||||
QString type = obj.value("type").toString();
|
||||
if(type == "int") {
|
||||
obj.insert("current",QJsonValue(obj.value("default").toInt()));
|
||||
}
|
||||
else if(type == "text") {
|
||||
obj.insert("current",QJsonValue(obj.value("default").toString()));
|
||||
}
|
||||
items.replace(i,obj); // 반영
|
||||
}
|
||||
}
|
||||
//qInfo() << items << __FUNCTION__;
|
||||
#else // !RM_MODEL_TYPE_AN6000
|
||||
// CS-32FH
|
||||
unsigned char def[SETTINGS_CFG_SIZE] = {0x00,// 0
|
||||
0x00,// 1
|
||||
0x00,// 2
|
||||
0x00,// 3
|
||||
0x00,// 4
|
||||
0x00,// 5
|
||||
0x00,// 6
|
||||
0x00,// 7
|
||||
0x00,// 8
|
||||
0x00,// 9
|
||||
0x00,// 10
|
||||
0x00,// 11
|
||||
0x00,// 12
|
||||
0x00,// 13
|
||||
0x00,// 14
|
||||
0x00,// 15
|
||||
0x00,// 16
|
||||
0x00,// 17
|
||||
0x00,// 18
|
||||
0x00,// 19
|
||||
0x00,// 20
|
||||
0x02,// 21 // SD_SENSOR = SENSOR_MIDDLE
|
||||
0x00,// 22 // SD_SIZE = SIZE_800x600
|
||||
0x00,// 23 // SD_FRAME = FRAME_20
|
||||
0x04,// 24 // SD_VOLUME = VOLUME_4
|
||||
0x01,// 25 // SD_VOICE = VOICE_ENABLE
|
||||
0x00 // RESERVED
|
||||
};
|
||||
memcpy(CFG::data,def,SETTINGS_CFG_SIZE);
|
||||
#endif // RM_MODEL_TYPE_AN6000
|
||||
}
|
||||
void CFG::backup()
|
||||
{
|
||||
#if !(RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
memcpy(CFG::stored,CFG::data,SETTINGS_CFG_SIZE);
|
||||
#endif // #if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
}
|
||||
|
||||
void CFG::restore()
|
||||
{
|
||||
#if !(RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
memcpy(CFG::data,CFG::stored,SETTINGS_CFG_SIZE);
|
||||
#endif // #if !(RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
}
|
||||
|
||||
|
||||
bool CFG::load(QString path)
|
||||
{
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
// CLEAR
|
||||
while(CFG::items.count()) {
|
||||
CFG::items.pop_back();
|
||||
}
|
||||
#if (AN6000_YEC)
|
||||
const char* fname = ":/html/an6000_cfg_yec.json";
|
||||
#else
|
||||
const char* fname = ":/html/an6000_cfg.json";
|
||||
#endif
|
||||
QFile file(fname); // ":/html/an6000_cfg.json"
|
||||
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
CFG::items = QJsonDocument::fromJson(data).array();
|
||||
setDefault();
|
||||
}
|
||||
#endif // #if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
|
||||
|
||||
bool bSuccess = false;
|
||||
if(QFile::exists(path)) {
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
#if (ENCODE_CFG_BASE64)
|
||||
QStringList lines = FMBase64::load(path,&isEncoded);
|
||||
qInfo() << lines << __FUNCTION__;
|
||||
for(int i=0;i<lines.size();i++) {
|
||||
QString line = lines.at(i);
|
||||
QStringList items = line.split('=');
|
||||
if(items.size() != 2) {
|
||||
qInfo() << "ERROR:" << line << __FUNCTION__;
|
||||
continue;
|
||||
}
|
||||
QString key = items.at(0);
|
||||
key = key.trimmed();
|
||||
QString value = items.at(1);
|
||||
value = value.trimmed();
|
||||
|
||||
int kidx = -1;
|
||||
QJsonObject obj = _findType(key,&kidx);
|
||||
if(obj.isEmpty() || kidx < 0) {
|
||||
qInfo() << "KEY ERROR:" << line << __FUNCTION__;
|
||||
continue;
|
||||
}
|
||||
QString otype = obj.value("type").toString();
|
||||
if(otype == "text") {
|
||||
obj.insert("current",QJsonValue(value));
|
||||
CFG::items.replace(kidx,obj);
|
||||
} else if (otype == "int") {
|
||||
obj.insert("current",QJsonValue(value.toInt()));
|
||||
CFG::items.replace(kidx,obj);
|
||||
}
|
||||
}
|
||||
#else // #if (ENCODE_CFG_BASE64)
|
||||
QFile file(path);
|
||||
if(file.exists() && file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
QTextStream in(&file);
|
||||
while(!in.atEnd()) {
|
||||
QString line = in.readLine();
|
||||
QStringList items = line.split('=');
|
||||
if(items.size() != 2) {
|
||||
qInfo() << "ERROR:" << line << __FUNCTION__;
|
||||
continue;
|
||||
}
|
||||
QString key = items.at(0);
|
||||
key = key.trimmed();
|
||||
QString value = items.at(1);
|
||||
value = value.trimmed();
|
||||
|
||||
int kidx = -1;
|
||||
QJsonObject obj = _findType(key,&kidx);
|
||||
if(obj.isEmpty() || kidx < 0) {
|
||||
qInfo() << "KEY ERROR:" << line << __FUNCTION__;
|
||||
continue;
|
||||
}
|
||||
QString otype = obj.value("type").toString();
|
||||
if(otype == "text") {
|
||||
obj.insert("current",QJsonValue(value));
|
||||
CFG::items.replace(kidx,obj);
|
||||
} else if (otype == "int") {
|
||||
obj.insert("current",QJsonValue(value.toInt()));
|
||||
CFG::items.replace(kidx,obj);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
#endif // #else // #if (ENCODE_CFG_BASE64)
|
||||
#else // !AN6000
|
||||
FILE* f = fopen(path.toLocal8Bit(),"r+b");
|
||||
if(f != NULL)
|
||||
{
|
||||
unsigned char d[SETTINGS_CFG_SIZE] = {0,};
|
||||
size_t read = fread(d,1,SETTINGS_CFG_SIZE,f);
|
||||
fclose(f);
|
||||
#if (CFG_SEARCH_DISK)
|
||||
bSuccess = read > 0 && strncmp(SETTINGS_TAG,(const char*)d,strlen(SETTINGS_TAG)) == 0;
|
||||
#else
|
||||
bSuccess = read > 0;
|
||||
#endif
|
||||
if(bSuccess == true)
|
||||
{
|
||||
memcpy(CFG::data,d,SETTINGS_CFG_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
setDefault();
|
||||
}
|
||||
}
|
||||
#endif // #else // !AN6000
|
||||
}
|
||||
CFG::backup(); // 현재 데이터 보존
|
||||
return bSuccess;
|
||||
}
|
||||
bool CFG::save(QString path)
|
||||
{
|
||||
bool bSuccess = false;
|
||||
#if (RM_MODEL == RM_MODEL_TYPE_AN6000)
|
||||
|
||||
#if (ENCODE_CFG_BASE64)
|
||||
QStringList lines = QStringList();
|
||||
for(int i=0;i<items.size();i++) {
|
||||
QJsonObject obj = items.at(i).toObject();
|
||||
QString type = obj.value("type").toString();
|
||||
QString line;
|
||||
line += obj.value("key").toString();
|
||||
line += "=";
|
||||
if(type == "int") {
|
||||
line += QString::number(obj.value("current").toInt());
|
||||
} else if (type == "text") {
|
||||
line += obj.value("current").toString();
|
||||
}
|
||||
lines.append(line);
|
||||
}
|
||||
FMBase64::save(path,lines,true); // isEncoded // 항상 인코딩된 상태로 저장
|
||||
#else // ENCODE_CFG_BASE64
|
||||
QFile file(path);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
QTextStream stream(&file);
|
||||
//stream << "something" << endl;
|
||||
for(int i=0;i<items.size();i++) {
|
||||
QJsonObject obj = items.at(i).toObject();
|
||||
QString type = obj.value("type").toString();
|
||||
QString line;
|
||||
line += obj.value("key").toString();
|
||||
line += "=";
|
||||
if(type == "int") {
|
||||
line += QString::number(obj.value("current").toInt());
|
||||
} else if (type == "text") {
|
||||
line += obj.value("current").toString();
|
||||
}
|
||||
stream << line << endl;
|
||||
qInfo() << line << __FUNCTION__;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
#endif // ENCODE_CFG_BASE64
|
||||
|
||||
|
||||
#else // !AN6000
|
||||
FILE* f = fopen(path.toLocal8Bit(),"w+b");
|
||||
if(f != NULL)
|
||||
{
|
||||
size_t write = fwrite(CFG::data,1,SETTINGS_CFG_SIZE,f);
|
||||
bSuccess = write > 0;
|
||||
fclose(f);
|
||||
}
|
||||
#endif // #if (RM_MODEL = RM_MODEL_TYPE_AN6000)
|
||||
CFG::backup();
|
||||
return bSuccess;
|
||||
}
|
||||
#endif // #if (USE_DEVICE_SETTINGS && !(RM_MODEL_EMT_KR))
|
||||
Reference in New Issue
Block a user