diff options
Diffstat (limited to 'tests/hawd/state.cpp')
-rw-r--r-- | tests/hawd/state.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/hawd/state.cpp b/tests/hawd/state.cpp new file mode 100644 index 0000000..cfc4326 --- /dev/null +++ b/tests/hawd/state.cpp | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include "state.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | #include <QDir> | ||
24 | #include <QJsonDocument> | ||
25 | #include <QObject> | ||
26 | |||
27 | #include <iostream> | ||
28 | |||
29 | static const QString configFileName("hawd.conf"); | ||
30 | |||
31 | namespace HAWD | ||
32 | { | ||
33 | |||
34 | State::State() | ||
35 | : m_valid(true) | ||
36 | { | ||
37 | QDir dir; | ||
38 | QString configPath; | ||
39 | |||
40 | while (!dir.exists(configFileName) && dir.cdUp()) { } | ||
41 | |||
42 | if (dir.exists(configFileName)) { | ||
43 | configPath = dir.absoluteFilePath(configFileName); | ||
44 | } | ||
45 | |||
46 | if (configPath.isEmpty()) { | ||
47 | std::cerr << QObject::tr("Could not find hawd configuration. A hawd.conf file must be in the current directory or in a directory above it.").toStdString() << std::endl; | ||
48 | m_valid = false; | ||
49 | return; | ||
50 | } | ||
51 | |||
52 | QFile configFile(configPath); | ||
53 | if (configFile.open(QIODevice::ReadOnly)) { | ||
54 | QJsonParseError error; | ||
55 | QJsonDocument config = QJsonDocument::fromJson(configFile.readAll(), &error); | ||
56 | if (config.isNull()) { | ||
57 | std::cerr << QObject::tr("Error parsing config file at %1").arg(configPath).toStdString() << std::endl; | ||
58 | std::cerr << '\t' << error.errorString().toStdString(); | ||
59 | } else { | ||
60 | m_configData = config.object(); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | bool State::isValid() const | ||
66 | { | ||
67 | return m_valid; | ||
68 | } | ||
69 | |||
70 | QString tildeExpand(QString path) | ||
71 | { | ||
72 | if (path.isEmpty() || path.at(0) != '~') { | ||
73 | return path; | ||
74 | } | ||
75 | |||
76 | return path.replace('~', QDir::homePath()); | ||
77 | } | ||
78 | |||
79 | QString State::resultsPath() const | ||
80 | { | ||
81 | return tildeExpand(configValue("results").toString()); | ||
82 | } | ||
83 | |||
84 | QString State::projectPath() const | ||
85 | { | ||
86 | return tildeExpand(configValue("project").toString()); | ||
87 | } | ||
88 | |||
89 | DatasetDefinition State::datasetDefinition(const QString &name) const | ||
90 | { | ||
91 | return DatasetDefinition(projectPath() + '/' + name); | ||
92 | } | ||
93 | |||
94 | QVariant State::configValue(const QString &key) const | ||
95 | { | ||
96 | return m_configData.value(key).toVariant(); | ||
97 | } | ||
98 | |||
99 | } // namespace HAWD | ||