-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cpp
160 lines (128 loc) · 4.82 KB
/
Settings.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* SEGS - Super Entity Game Server
* http://www.segs.io/
* Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
* This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
*/
/*!
* @addtogroup Components
* @{
*/
#include "Settings.h"
#include "Logging.h"
#include <QFileInfo>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QRegularExpression>
QString Settings::s_segs_dir;
QString Settings::s_settings_path = QStringLiteral("settings.cfg"); // default path 'settings.cfg' from args
QString Settings::s_default_tpl_dir = QStringLiteral("default_setup"); // default folder 'default_setup'
QString Settings::s_default_settings_path = Settings::s_default_tpl_dir + QDir::separator() + QStringLiteral("settings_template.cfg"); // default template from folder 'default_setup'
bool fileExists(const QString &path)
{
QFileInfo check_file(path);
// check if file exists and if yes: Is it really a file and not a directory?
return check_file.exists() && check_file.isFile() && check_file.size() > 0;
}
Settings::Settings()
{
if(!fileExists(getSettingsPath()))
qCritical() << "Settings path not defined? This is unpossible!";
}
void Settings::setSettingsPath(const QString &path)
{
if(path.isEmpty())
qCritical() << "Settings path not defined? This is unpossible!";
s_settings_path = getSEGSDir() + QDir::separator() + path;
if(!fileExists(s_settings_path))
createSettingsFile(s_settings_path);
qCDebug(logSettings) << "Settings Path" << s_settings_path;
}
QString Settings::getSettingsPath()
{
if(s_settings_path.isEmpty())
setSettingsPath("settings.cfg"); // set default path to "settings.cfg"
return s_settings_path;
}
void Settings::setSEGSDir()
{
// Get the current SEGS directory. This library is shared
// by dbtool and others, so make sure we're in the correct
// working directory
QDir curdir(QDir::current());
qCDebug(logSettings) << "Current Active Dir" << curdir.absolutePath();
// if called from utilities, move up one directory
if(curdir.absolutePath().endsWith("utilities", Qt::CaseInsensitive))
{
curdir.cdUp();
qCDebug(logSettings) << "Root Dir" << curdir.absolutePath();
}
if(-1 == curdir.entryList().indexOf(QRegularExpression("segs_server.*")))
qWarning() << "Cannot find SEGS Server at" << curdir.absolutePath();
s_segs_dir = curdir.absolutePath();
}
QString Settings::getSEGSDir()
{
// if m_segs_dir is not empty, we've set it, return that instead
if(s_segs_dir.isEmpty())
setSEGSDir();
return s_segs_dir;
}
QString Settings::getSettingsTplPath()
{
QDir curdir(getSEGSDir()); // Get the SEGS working directory
if(!fileExists(curdir.absolutePath() + QDir::separator() + s_default_settings_path))
qWarning() << "Cannot find" << s_default_settings_path;
return curdir.absolutePath() + QDir::separator() + s_default_settings_path;
}
QString Settings::getTemplateDirPath()
{
QDir curdir(getSEGSDir()); // Get the SEGS working directory
if(!QDir(curdir.absolutePath() + QDir::separator() + s_default_tpl_dir).exists())
qWarning() << "Cannot find directory" << s_default_tpl_dir;
return curdir.absolutePath() + QDir::separator() + s_default_tpl_dir;
}
void Settings::createSettingsFile(const QString &new_file_path)
{
qCDebug(logSettings) << "Creating Settings file" << new_file_path;
QFile tpl_file(Settings::getSettingsTplPath());
QFile new_file(new_file_path);
if(!tpl_file.open(QIODevice::ReadOnly))
{
qWarning() << "Unable to read" << tpl_file.fileName() << "Check folder permissions.";
return;
}
// QSettings setValue() methods delete all file comments, it's better to
// simply copy the template over to our destination directory.
// Unfortunately QFile::copy() has some sort of bug and doesn't work
// so instead let's open the new file, and copy the contents from template
if(!new_file.open(QIODevice::WriteOnly) || !new_file.write(tpl_file.readAll()))
{
qWarning() << "Unable to create" << new_file_path << "Check folder permissions.";
return;
}
new_file.close();
tpl_file.close();
}
void settingsDump()
{
QSettings config(Settings::getSettingsPath(),QSettings::IniFormat,nullptr);
settingsDump(&config);
}
void settingsDump(QSettings *s)
{
QString output = "Settings File Dump\n";
for(const QString &group : s->childGroups())
{
QString groupString = QString("===== %1 =====\n").arg(group);
s->beginGroup(group);
for(const QString &key : s->allKeys())
groupString.append(QString(" %1\t\t %2\n").arg(key, s->value(key).toString()));
s->endGroup();
groupString.append("\n");
output.append(groupString);
}
qDebug().noquote() << output;
}
//! @}