Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <QDebug>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QDir>
#include "configstorage.h"
//-------------------------------------------------------------------------------------------------
ConfigStorage::ConfigStorage()
: mConfigFile(nullptr)
{
}
//-------------------------------------------------------------------------------------------------
bool ConfigStorage::init()
{
Q_ASSERT(!mConfigFile); // call init only once
QString cfgPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ "/" + qApp->applicationName();
if (!QDir(cfgPath).mkpath(cfgPath)) {
qWarning() << "Failed to create path" << cfgPath;
return false;
}
cfgPath.append("/config.json");
mConfigFile = new QFile(cfgPath);
bool useDefaults = !mConfigFile->exists() || mConfigFile->size() == 0;
if (!mConfigFile->open(QIODevice::ReadWrite)) {
qWarning() << "Could not open" << cfgPath << "with read/write mode";
return false;
}
if (useDefaults)
revertToDefaults();
return true;
}
//-------------------------------------------------------------------------------------------------
void ConfigStorage::revertToDefaults()
{
Q_ASSERT(mConfigFile && mConfigFile->isOpen()); // call init first
QFile defaults(":/config/install/default.json");
Q_ASSERT(defaults.exists());
bool open = defaults.open(QIODevice::ReadOnly);
Q_ASSERT(open);
mConfigFile->write(defaults.readAll());
mConfigFile->flush();
}