2018-06-21 16:41:02 +02:00
|
|
|
#include "headers/appsettings.h"
|
|
|
|
|
2018-12-24 21:37:32 +01:00
|
|
|
AppSettings * pGlobalAppSettings = nullptr;
|
2018-06-21 16:41:02 +02:00
|
|
|
|
|
|
|
AppSettings::AppSettings(QObject* parent)
|
|
|
|
:QObject(parent)
|
|
|
|
{
|
2018-12-25 15:16:24 +01:00
|
|
|
qDebug("+----- AppSettings konstruktor -----");
|
|
|
|
|
2018-12-25 19:30:42 +01:00
|
|
|
pGlobalAppSettings = this;
|
|
|
|
|
2018-06-21 16:41:02 +02:00
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2018-12-25 15:16:24 +01:00
|
|
|
qDebug() << "+----- Settings Path:" << path;
|
|
|
|
|
2018-06-21 16:41:02 +02:00
|
|
|
this->settingsManager = new QSettings(path+"/fannyapp/settings.ini", QSettings::IniFormat);
|
|
|
|
|
|
|
|
if(loadSetting("init") == "false"){
|
|
|
|
this->writeSetting("init", 0);
|
|
|
|
}
|
2018-12-25 00:15:25 +01:00
|
|
|
if(loadSetting("grade") == "false"){
|
|
|
|
this->writeSetting("grade", 5);
|
|
|
|
}
|
2018-12-25 15:16:24 +01:00
|
|
|
|
|
|
|
this->filtersFile = new QFile(path + "/fannyapp/filters.json");
|
|
|
|
|
|
|
|
//QList<QStringList> filters = {{"5", "d"}, {"6", "c"}, {"11", ""}};
|
|
|
|
|
|
|
|
//writeFilters(filters);
|
2018-12-26 23:44:24 +01:00
|
|
|
//qDebug() << readFilters();
|
2018-06-21 16:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString AppSettings::loadSetting(const QString &key)
|
|
|
|
{
|
|
|
|
this->settingsManager->beginGroup("AppSettings");
|
|
|
|
QString value = this->settingsManager->value(key , false).toString();
|
|
|
|
this->settingsManager->endGroup();
|
|
|
|
return(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppSettings::writeSetting(const QString &key, const QVariant &variant)
|
|
|
|
{
|
|
|
|
this->settingsManager->beginGroup("AppSettings");
|
|
|
|
this->settingsManager->setValue(key , variant);
|
|
|
|
this->settingsManager->endGroup();
|
|
|
|
}
|
|
|
|
|
2018-12-25 15:16:24 +01:00
|
|
|
QList<QStringList> AppSettings::readFilters() {
|
|
|
|
|
|
|
|
// list to be returned
|
|
|
|
QList<QStringList> filtersList;
|
|
|
|
|
|
|
|
this->filtersFile->open(QFile::ReadOnly | QFile::Text);
|
|
|
|
|
|
|
|
QString jsonString = this->filtersFile->readAll();
|
|
|
|
|
|
|
|
this->filtersFile->close();
|
|
|
|
|
2018-12-26 23:44:24 +01:00
|
|
|
//qDebug() << jsonString;
|
2018-12-25 15:16:24 +01:00
|
|
|
QJsonDocument jsonFilters = QJsonDocument::fromJson(jsonString.toUtf8());
|
|
|
|
// array with all filters in it
|
|
|
|
QJsonArray filtersArray = jsonFilters.array();
|
|
|
|
foreach(const QJsonValue & value, filtersArray){
|
|
|
|
// array of a single filter
|
|
|
|
QJsonArray filterArray = value.toArray();
|
|
|
|
|
2018-12-28 11:37:06 +01:00
|
|
|
QStringList tmpFilterList;
|
|
|
|
|
2018-12-25 15:16:24 +01:00
|
|
|
// extract values from array
|
2018-12-28 11:37:06 +01:00
|
|
|
foreach(const QJsonValue & key, filterArray){
|
|
|
|
tmpFilterList.append(key.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (tmpFilterList.length() < 3) {
|
|
|
|
tmpFilterList.append("");
|
|
|
|
}
|
2018-12-25 15:16:24 +01:00
|
|
|
|
2018-12-28 11:37:06 +01:00
|
|
|
filtersList.append(tmpFilterList);
|
2018-12-25 15:16:24 +01:00
|
|
|
}
|
|
|
|
|
2018-12-28 11:43:57 +01:00
|
|
|
for(int i = 0; i < filtersList.length(); i++){
|
|
|
|
QStringList filterList = filtersList[i];
|
|
|
|
if( filterList[2] == "" ){
|
|
|
|
filtersList.removeAt(i);
|
|
|
|
i = i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 15:16:24 +01:00
|
|
|
return(filtersList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppSettings::writeFilters(QList<QStringList> list) {
|
|
|
|
|
|
|
|
// string to write to file
|
|
|
|
QString jsonString;
|
|
|
|
QJsonArray filtersArray;
|
|
|
|
|
2018-12-28 11:43:57 +01:00
|
|
|
for(int i = 0; i < list.length(); i++){
|
|
|
|
QStringList filterList = list[i];
|
|
|
|
if( filterList[2] == "" ){
|
|
|
|
list.removeAt(i);
|
|
|
|
i = i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 15:16:24 +01:00
|
|
|
foreach(QStringList filter, list){
|
|
|
|
QJsonArray filterArray;
|
2018-12-28 11:37:06 +01:00
|
|
|
|
|
|
|
while (filter.length() < 3) {
|
|
|
|
filter.append("");
|
|
|
|
}
|
|
|
|
|
2018-12-25 15:16:24 +01:00
|
|
|
filterArray.append(filter[0]);
|
|
|
|
filterArray.append(filter[1]);
|
2018-12-28 11:37:06 +01:00
|
|
|
filterArray.append(filter[2]);
|
2018-12-25 15:16:24 +01:00
|
|
|
|
|
|
|
filtersArray.append(filterArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument filtersDoc(filtersArray);
|
2018-12-26 23:44:24 +01:00
|
|
|
//qDebug() << filtersDoc.toJson();
|
2018-12-25 15:16:24 +01:00
|
|
|
|
|
|
|
this->filtersFile->open(QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
this->filtersFile->resize(0);
|
|
|
|
|
|
|
|
this->filtersFile->write(filtersDoc.toJson());
|
|
|
|
|
|
|
|
this->filtersFile->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList AppSettings::readFiltersQml() {
|
|
|
|
|
|
|
|
QStringList filtersList;
|
|
|
|
|
|
|
|
foreach(QStringList filterList, this->readFilters()){
|
|
|
|
filtersList.append(filterList[0]+"|"+filterList[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(filtersList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppSettings::writeFiltersQml(QStringList) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-21 16:41:02 +02:00
|
|
|
AppSettings::~AppSettings()
|
|
|
|
{
|
|
|
|
delete settingsManager;
|
|
|
|
}
|
|
|
|
|