-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTheApp.cpp
More file actions
216 lines (165 loc) · 5.55 KB
/
TheApp.cpp
File metadata and controls
216 lines (165 loc) · 5.55 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
////////////////////////////////////////////////////////////////////////////////
//! \file TheApp.cpp
//! \brief The TheApp class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "TheApp.hpp"
#include <Core/StringUtils.hpp>
#include <WCL/BusyCursor.hpp>
#include <WCL/AppConfig.hpp>
#include <Core/ConfigurationException.hpp>
#include <limits>
////////////////////////////////////////////////////////////////////////////////
// Global variables.
//! The application singleton instance.
TheApp g_app;
////////////////////////////////////////////////////////////////////////////////
// Constants.
//! The configuration data publisher name.
const tchar* PUBLISHER = TXT("Chris Oldwood");
//! The configuration data application name.
const tchar* APPLICATION = TXT("VC++ Project Compare");
//! The configuration data format version.
const tchar* CONFIG_VERSION = TXT("1.0");
////////////////////////////////////////////////////////////////////////////////
//! Constructor.
TheApp::TheApp()
: CApp(m_appWnd, m_appCmds)
, m_appWnd()
, m_appCmds(m_appWnd)
, m_modified(0)
, m_lastFolder()
, m_lastFile1()
, m_lastFile2()
, m_action(LIST_SETTINGS)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
TheApp::~TheApp()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Handle application startup.
bool TheApp::OnOpen()
{
// These cannot be initialised in the ctor.
m_lastFolder = CPath::ApplicationDir();
m_lastFile1 = CPath::ApplicationDir();
m_lastFile2 = CPath::ApplicationDir();
// Set the app title.
m_strTitle = APPLICATION;
try
{
// Load settings.
loadConfig();
}
catch (const Core::Exception& e)
{
FatalMsg(TXT("Failed to configure the application:-\n\n%s"), e.twhat());
return false;
}
// Create the main window.
if (!m_appWnd.Create())
return false;
// Show it.
if (!m_lastWndPos.Empty())
m_appWnd.Move(m_lastWndPos);
m_appWnd.Show(m_iCmdShow);
// Update UI.
m_appCmds.UpdateUI();
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Handle application shutdown.
bool TheApp::OnClose()
{
try
{
// Save settings.
saveConfig();
}
catch (const Core::Exception& e)
{
FatalMsg(TXT("Failed to save the application configuration:-\n\n%s"), e.twhat());
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Load the application settings.
void TheApp::loadConfig()
{
CBusyCursor busyCursor;
WCL::AppConfig appConfig(PUBLISHER, APPLICATION);
// Read the config data version.
tstring version = appConfig.readString(appConfig.DEFAULT_SECTION, TXT("Version"), CONFIG_VERSION);
if (version != CONFIG_VERSION)
throw Core::ConfigurationException(Core::fmt(TXT("The configuration data is incompatible - '%s'"), version.c_str()));
// Read the UI settings.
m_lastWndPos = appConfig.readValue<CRect>(TXT("UI"), TXT("MainWindow"), m_lastWndPos);
m_lastFolder = appConfig.readString(TXT("UI"), TXT("LastFolder"), m_lastFolder);
m_lastFile1 = appConfig.readString(TXT("UI"), TXT("LastFile1"), m_lastFile1);
m_lastFile2 = appConfig.readString(TXT("UI"), TXT("LastFile2"), m_lastFile2);
// Read the ignored settings list.
const size_t max = std::numeric_limits<size_t>::max();
for (size_t i = 0; i != max; ++i)
{
tstring entry = Core::fmt(TXT("%u"), i);
WCL::AppConfig::StringArray values;
appConfig.readStringList(TXT("Project Dependent"), entry, TXT(""), values);
if (values.size() != 2)
break;
m_projectDepList.insert(ToolSetting(values[0], values[1]));
}
// Read the build dependent settings list.
for (size_t i = 0; i != max; ++i)
{
tstring entry = Core::fmt(TXT("%u"), i);
WCL::AppConfig::StringArray values;
appConfig.readStringList(TXT("Build Dependent"), entry, TXT(""), values);
if (values.size() != 2)
break;
m_buildDepList.insert(ToolSetting(values[0], values[1]));
}
}
////////////////////////////////////////////////////////////////////////////////
//! Save the application settings.
void TheApp::saveConfig()
{
CBusyCursor busyCursor;
WCL::AppConfig appConfig(PUBLISHER, APPLICATION);
// Write the config data version.
appConfig.writeString(appConfig.DEFAULT_SECTION, TXT("Version"), CONFIG_VERSION);
// Write the UI settings.
appConfig.writeValue<CRect>(TXT("UI"), TXT("MainWindow"), m_lastWndPos);
appConfig.writeString(TXT("UI"), TXT("LastFolder"), m_lastFolder);
appConfig.writeString(TXT("UI"), TXT("LastFile1"), m_lastFile1);
appConfig.writeString(TXT("UI"), TXT("LastFile2"), m_lastFile2);
// Write the ignored settings list.
if (g_app.m_modified & TheApp::PROJECT_LIST)
{
appConfig.deleteSection(TXT("Project Dependent"));
size_t i = 0;
for (ToolSettings::const_iterator it = m_projectDepList.begin(); it != m_projectDepList.end(); ++it, ++i)
{
const ToolSetting& toolSetting = *it;
tstring entry = Core::fmt(TXT("%u"), i);
tstring value = toolSetting.m_tool + TXT(",") + toolSetting.m_setting;
appConfig.writeString(TXT("Project Dependent"), entry, value);
}
}
// Write the build dependent settings list.
if (g_app.m_modified & TheApp::BUILDDEP_LIST)
{
appConfig.deleteSection(TXT("Build Dependent"));
size_t i = 0;
for (ToolSettings::const_iterator it = m_buildDepList.begin(); it != m_buildDepList.end(); ++it, ++i)
{
const ToolSetting& toolSetting = *it;
tstring entry = Core::fmt(TXT("%u"), i);
tstring value = toolSetting.m_tool + TXT(",") + toolSetting.m_setting;
appConfig.writeString(TXT("Build Dependent"), entry, value);
}
}
}