-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
187 lines (148 loc) · 5.01 KB
/
Model.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
////////////////////////////////////////////////////////////////////////////////
//! \file Model.cpp
//! \brief The Model class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Model.hpp"
#include <WCL/AppConfig.hpp>
#include <WCL/Path.hpp>
#include <shlobj.h>
#include <XML/Reader.hpp>
#include <XML/Writer.hpp>
#include <WCL/File.hpp>
#include <Core/FileSystemException.hpp>
////////////////////////////////////////////////////////////////////////////////
// Constants.
namespace
{
//! The configuration data format version.
const tchar* CONFIG_VERSION = TXT("1");
//! The default config file folder.
const tchar* DEFAULT_CONFIG_FOLDER = TXT("FarmMonitor");
//! The default config file name.
const tchar* DEFAULT_CONFIG_FILE = TXT("FarmMonitor.xml");
}
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
Model::Model()
: m_hosts()
, m_tools()
, m_queries()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Model::~Model()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Query if the application data file exists.
bool Model::configFileExists()
{
const CPath configFile = CPath::SpecialDir(CSIDL_APPDATA) / DEFAULT_CONFIG_FOLDER / DEFAULT_CONFIG_FILE;
return configFile.Exists();
}
////////////////////////////////////////////////////////////////////////////////
//! Load the application settings.
void Model::loadConfig()
{
const CPath configFile = CPath::SpecialDir(CSIDL_APPDATA) / DEFAULT_CONFIG_FOLDER / DEFAULT_CONFIG_FILE;
if (configFile.Exists())
{
loadConfigFromXmlFile(configFile);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Load the application settings.
void Model::loadConfig(XML::DocumentPtr config)
{
m_hosts.load(config);
m_tools.load(config);
m_queries.load(config);
}
////////////////////////////////////////////////////////////////////////////////
//! Save the application settings.
void Model::saveConfig()
{
if (m_hosts.isModified() || m_tools.isModified() || m_queries.isModified())
{
const CPath userDataFolder = CPath::SpecialDir(CSIDL_APPDATA);
if (!userDataFolder.Exists())
{
throw Core::FileSystemException(Core::fmt(TXT("The Application Data folder is invalid - '%s'"), userDataFolder.c_str()));
}
const CPath dataFolder = userDataFolder / DEFAULT_CONFIG_FOLDER;
if (!dataFolder.Exists())
{
if (!CFile::CreateFolder(dataFolder))
{
throw Core::FileSystemException(Core::fmt(TXT("Failed to create the folder - '%s'"), dataFolder.c_str()));
}
}
const CPath configFile = dataFolder / DEFAULT_CONFIG_FILE;
saveConfigToXmlFile(configFile);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Save the application settings.
void Model::saveConfig(XML::DocumentPtr config)
{
XML::ElementNodePtr root = XML::makeElement(TXT("FarmMonitor"));
root->setAttribute(TXT("Version"), CONFIG_VERSION);
root->appendChild(XML::makeElement(TXT("Hosts")));
root->appendChild(XML::makeElement(TXT("Tools")));
root->appendChild(XML::makeElement(TXT("Queries")));
config->appendChild(root);
m_hosts.save(config);
m_tools.save(config);
m_queries.save(config);
}
////////////////////////////////////////////////////////////////////////////////
//! Load the application settings from the default XML file.
void Model::loadConfigFromXmlFile(const CPath& configFile)
{
const tstring content = CFile::ReadTextFile(configFile);
const XML::DocumentPtr appConfig = XML::Reader::readDocument(content, XML::Reader::DISCARD_WHITESPACE);
loadConfig(appConfig);
}
////////////////////////////////////////////////////////////////////////////////
//! Save the application settings to the default XML file.
void Model::saveConfigToXmlFile(const CPath& configFile)
{
XML::DocumentPtr appConfig = XML::makeDocument();
saveConfig(appConfig);
const CPath backupFile = configFile.PathWithoutExt() + TXT(".bak");
const CPath tempFile = configFile.PathWithoutExt() + TXT(".$$$");
if (tempFile.Exists())
{
if (!CFile::Delete(tempFile))
{
throw Core::FileSystemException(Core::fmt(TXT("Failed to delete previous temporary file:\n\n%s"),
tempFile.c_str()));
}
}
const tstring content = XML::Writer::writeDocument(appConfig);
CFile::WriteTextFile(tempFile, content.c_str(), ANSI_TEXT);
if (backupFile.Exists())
{
if (!CFile::Delete(backupFile))
{
throw Core::FileSystemException(Core::fmt(TXT("Failed to delete backup file:\n\n%s"),
backupFile.c_str()));
}
}
if (configFile.Exists())
{
if (!CFile::Move(configFile, backupFile))
{
throw Core::FileSystemException(Core::fmt(TXT("Failed to rename file:\n\n%s\n\nto\n\n%s"),
configFile.c_str(), backupFile.c_str()));
}
}
if (!CFile::Move(tempFile, configFile))
{
CFile::Move(backupFile, configFile);
throw Core::FileSystemException(Core::fmt(TXT("Failed to rename file:\n\n%s\n\nto\n\n%s"),
tempFile.c_str(), configFile.c_str()));
}
}