Debian Codegraph @

code/apis/ProfileProxy.cpp

raw
1 /*
2 Copyright (C) 2012 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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 Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "apis/ProfileProxy.h"
20
21 #include "apis/CmdLineManager.h"
22 #include "apis/FlagListManager.h"
23 #include "apis/ProfileManager.h"
24
25 #include "controls/LightingPresets.h"
26
27 #include "global/ProfileKeys.h"
28
29 #include <wx/tokenzr.h>
30
31 LAUNCHER_DEFINE_EVENT_TYPE(EVT_PROXY_RESET);
32 LAUNCHER_DEFINE_EVENT_TYPE(EVT_PROXY_FLAG_DATA_READY);
33
34 void ProfileProxy::RegisterProxyReset(wxEvtHandler *handler) {
35 wxASSERT(ProfileProxy::IsInitialized());
36 wxASSERT_MSG(this->resetEventHandlers.IndexOf(handler) == wxNOT_FOUND,
37 wxString::Format(
38 _T("RegisterProxyReset(): Handler at %p already registered."),
39 handler));
40 this->resetEventHandlers.Append(handler);
41 }
42
43 void ProfileProxy::UnRegisterProxyReset(wxEvtHandler *handler) {
44 wxASSERT(ProfileProxy::IsInitialized());
45 wxASSERT_MSG(this->resetEventHandlers.IndexOf(handler) != wxNOT_FOUND,
46 wxString::Format(
47 _T("UnRegisterProxyReset(): Handler at %p not registered."),
48 handler));
49 this->resetEventHandlers.DeleteObject(handler);
50 }
51
52 void ProfileProxy::RegisterProxyFlagDataReady(wxEvtHandler *handler) {
53 wxASSERT(ProfileProxy::IsInitialized());
54 wxASSERT_MSG(this->readyEventHandlers.IndexOf(handler) == wxNOT_FOUND,
55 wxString::Format(
56 _T("RegisterProxyFlagDataReady(): Handler at %p already registered."),
57 handler));
58 this->readyEventHandlers.Append(handler);
59 }
60
61 void ProfileProxy::UnRegisterProxyFlagDataReady(wxEvtHandler *handler) {
62 wxASSERT(ProfileProxy::IsInitialized());
63 wxASSERT_MSG(this->readyEventHandlers.IndexOf(handler) != wxNOT_FOUND,
64 wxString::Format(
65 _T("UnRegisterProxyFlagDataReady(): Handler at %p not registered."),
66 handler));
67 this->readyEventHandlers.DeleteObject(handler);
68 }
69
70 void ProfileProxy::GenerateProxyReset() {
71 wxASSERT(ProfileProxy::IsInitialized());
72
73 wxCommandEvent event(EVT_PROXY_RESET, wxID_NONE);
74
75 wxLogDebug(_T("Generating EVT_PROXY_RESET event"));
76 for (EventHandlers::iterator
77 iter = this->resetEventHandlers.begin(),
78 end = this->resetEventHandlers.end();
79 iter != end; ++iter) {
80 wxEvtHandler* current = *iter;
81 current->AddPendingEvent(event);
82 wxLogDebug(_T(" Sent EVT_PROXY_RESET event to %p"), current);
83 }
84 }
85
86 void ProfileProxy::GenerateProxyFlagDataReady() {
87 wxASSERT(ProfileProxy::IsInitialized());
88
89 wxCommandEvent event(EVT_PROXY_FLAG_DATA_READY, wxID_NONE);
90
91 wxLogDebug(_T("Generating EVT_PROXY_FLAG_DATA_READY event"));
92 for (EventHandlers::iterator
93 iter = this->readyEventHandlers.begin(),
94 end = this->readyEventHandlers.end();
95 iter != end; ++iter) {
96 wxEvtHandler* current = *iter;
97 current->AddPendingEvent(event);
98 wxLogDebug(_T(" Sent EVT_PROXY_FLAG_DATA_READY event to %p"), current);
99 }
100 }
101
102
103 ProfileProxy* ProfileProxy::proxy = NULL;
104
105 ProfileProxy* ProfileProxy::GetProxy() {
106 wxCHECK_MSG(IsInitialized(), NULL,
107 _T("GetProxy() called when proxy wasn't initialized."));
108
109 return proxy;
110 }
111
112 bool ProfileProxy::Initialize() {
113 wxASSERT(!IsInitialized());
114
115 proxy = new ProfileProxy();
116 return true;
117 }
118
119 void ProfileProxy::DeInitialize() {
120 wxASSERT(IsInitialized());
121
122 ProfileProxy* temp = proxy;
123 proxy = NULL;
124 delete temp;
125 }
126
127 bool ProfileProxy::IsInitialized() {
128 return proxy != NULL;
129 }
130
131 ProfileProxy::ProfileProxy()
132 : isFlagDataReady(false) {
133 FlagListManager::RegisterFlagFileProcessingStatusChanged(this);
134 }
135
136 ProfileProxy::~ProfileProxy() {
137 FlagListManager::UnRegisterFlagFileProcessingStatusChanged(this);
138 }
139
140 BEGIN_EVENT_TABLE(ProfileProxy, wxEvtHandler)
141 EVT_COMMAND(wxID_NONE, EVT_FLAG_FILE_PROCESSING_STATUS_CHANGED,
142 ProfileProxy::OnFlagFileProcessingStatusChanged)
143 END_EVENT_TABLE()
144
145
146 void ProfileProxy::OnFlagFileProcessingStatusChanged(wxCommandEvent &event) {
147 wxASSERT(event.GetEventType() == EVT_FLAG_FILE_PROCESSING_STATUS_CHANGED);
148
149 const FlagListManager::FlagFileProcessingStatus status =
150 static_cast<FlagListManager::FlagFileProcessingStatus>(event.GetInt());
151
152 if (status == FlagListManager::FLAG_FILE_PROCESSING_OK) {
153 wxCHECK_RET(!this->IsFlagDataReady(),
154 _T("ProfileProxy received FLAG_FILE_PROCESSING_OK when flag data is ready."));
155
156 const ProxyFlagData* proxyData =
157 FlagListManager::GetFlagListManager()->GetProxyFlagData();
158
159 wxCHECK_RET(proxyData != NULL,
160 _T("ProfileProxy: flag file processing succeeded but proxy data was NULL."));
161
162 this->ProcessFlagData(*proxyData);
163
164 this->ProcessFlagLine();
165
166 this->isFlagDataReady = true;
167
168 GenerateProxyFlagDataReady();
169
170 delete proxyData;
171 } else if (status == FlagListManager::FLAG_FILE_PROCESSING_RESET) {
172 this->Reset();
173 this->GenerateProxyReset();
174 }
175 }
176
177 void ProfileProxy::SetFlag(const wxString& flag, const bool isChecked) {
178 wxCHECK_RET(this->IsFlagDataReady(),
179 _T("SetFlag() called when proxy flag data isn't ready."));
180 wxCHECK_RET(this->flagMap.count(flag) > 0,
181 wxString::Format(_T("SetFlag(): given unknown flag %s."), flag.c_str()));
182
183 const int flagIndex = this->flagMap.find(flag)->second;
184
185 wxASSERT_MSG(VerifyEnabledFlagsForFlag(flag, flagIndex).IsEmpty(),
186 VerifyEnabledFlagsForFlag(flag, flagIndex));
187
188 if (isChecked) {
189 this->enabledFlags[flagIndex] = flag;
190 } else {
191 this->enabledFlags.erase(flagIndex);
192 }
193
194 this->WriteFlagLineToProfile();
195 CmdLineManager::GenerateCmdLineChanged();
196 }
197
198 std::vector<wxString> ProfileProxy::GetEnabledFlags() const {
199 wxCHECK_MSG(this->IsFlagDataReady(), std::vector<wxString>(),
200 _T("GetEnabledFlags() called when proxy flag data isn't ready."));
201
202 std::vector<wxString> flags;
203
204 for (std::map<int,wxString>::const_iterator it = enabledFlags.begin(),
205 end = enabledFlags.end(); it != end; ++it) {
206 flags.push_back(it->second);
207 }
208
209 return flags;
210 }
211
212 wxString ProfileProxy::GetEnabledFlagsString() const {
213 wxCHECK_MSG(this->IsFlagDataReady(), wxEmptyString,
214 _T("GetEnabledFlagsString() called when proxy flag data isn't ready."));
215
216 std::vector<wxString> flags(this->GetEnabledFlags());
217
218 wxString flagStr;
219
220 for (std::vector<wxString>::const_iterator it = flags.begin(), end = flags.end();
221 it != end; ++it) {
222 if (!flagStr.IsEmpty()) {
223 flagStr += _T(" ");
224 }
225 flagStr += *it;
226 }
227
228 return flagStr;
229 }
230
231 // GenerateCustomFlagsChanged() is used to update the custom flags box
232 // GenerateCmdLineChanged() is used to update the current command line
233 void ProfileProxy::SetCustomFlags(const wxString& customFlags,
234 const bool updateTextCtrl) {
235 this->customFlags = customFlags;
236 this->WriteFlagLineToProfile();
237 if (updateTextCtrl) {
238 CmdLineManager::GenerateCustomFlagsChanged();
239 }
240 CmdLineManager::GenerateCmdLineChanged();
241 }
242
243 bool ProfileProxy::HasLightingPreset() const {
244 return ProMan::GetProfileManager()->ProfileExists(PRO_CFG_LIGHTING_PRESET);
245 }
246
247 wxString ProfileProxy::GetLightingPresetName() const {
248 wxString presetName;
249 ProMan::GetProfileManager()->ProfileRead(
250 PRO_CFG_LIGHTING_PRESET, &presetName, wxEmptyString);
251 return presetName;
252 }
253
254 void ProfileProxy::SetLightingPreset(const wxString& presetName) {
255 ProMan::GetProfileManager()->ProfileWrite(PRO_CFG_LIGHTING_PRESET, presetName);
256 CmdLineManager::GenerateCmdLineChanged();
257 }
258
259 void ProfileProxy::CopyPresetToCustomFlags() {
260 wxCHECK_RET(this->IsFlagDataReady(),
261 _T("CopyPresetToCustomFlags() called when proxy flag data isn't ready."));
262
263 wxCHECK_RET(this->HasLightingPreset(),
264 _T("CopyPresetToCustomFlags(): profile has no preset entry."));
265
266 wxString presetName(this->GetLightingPresetName());
267
268 wxCHECK_RET(!presetName.IsEmpty(),
269 _T("CopyPresetToCustomFlags(): preset name is empty."));
270
271 const wxString& flagSet = LightingPresets::PresetNameToPresetFlagSet(presetName);
272
273 if (!flagSet.IsEmpty()) {
274 this->SetCustomFlags(flagSet + _T(" ") + this->GetCustomFlags());
275 } else {
276 wxLogError(
277 _T("CopyPresetToCustomFlags(): retrieved flag set was empty for name %s"),
278 presetName.c_str());
279 }
280 }
281
282 bool ProfileProxy::IsProfileInitialized() const {
283 bool isProfileInitialized;
284 ProMan::GetProfileManager()->ProfileRead(
285 PRO_CFG_MAIN_INITIALIZED, &isProfileInitialized, false);
286 return isProfileInitialized;
287 }
288
289 void ProfileProxy::FinishProfileInitialization() const {
290 wxCHECK_RET(!this->IsProfileInitialized(),
291 _T("FinishProfileInitialization(): profile already initialized."));
292
293 if (!ProMan::GetProfileManager()->ProfileExists(PRO_CFG_TC_CURRENT_FLAG_LINE)) {
294 ProMan::GetProfileManager()->ProfileWrite(
295 PRO_CFG_TC_CURRENT_FLAG_LINE, wxEmptyString);
296 }
297
298 wxLogDebug(_T("Autosaving newly initialized profile '%s'."),
299 ProMan::GetProfileManager()->GetCurrentName().c_str());
300 ProMan::GetProfileManager()->ProfileWrite(PRO_CFG_MAIN_INITIALIZED, true);
301 ProMan::GetProfileManager()->SaveCurrentProfile(true);
302 }
303
304 void ProfileProxy::WriteFlagLineToProfile() const {
305 wxCHECK_RET(this->IsFlagDataReady(),
306 _T("WriteFlagLineToProfile() called when proxy flag data isn't ready."));
307
308 wxString newFlagLine(
309 this->GetEnabledFlagsString() + _T(" ") + this->GetCustomFlags());
310 ProMan::GetProfileManager()->ProfileWrite(
311 PRO_CFG_TC_CURRENT_FLAG_LINE, newFlagLine);
312 }
313
314 void ProfileProxy::ProcessFlagData(const ProxyFlagData& data) {
315 wxASSERT(!data.IsEmpty());
316
317 for (ProxyFlagData::const_iterator it = data.begin(), end = data.end();
318 it != end; ++it) {
319 const ProxyFlagDataItem& item = **it;
320
321 wxASSERT_MSG(this->flagMap.count(item.GetFlagString()) == 0,
322 wxString::Format(_T("ProcessFlagData(): attempted to add flag %s twice."),
323 item.GetFlagString().c_str()));
324
325 this->flagMap[item.GetFlagString()] = item.GetFlagIndex();
326 }
327 }
328
329 void ProfileProxy::ProcessFlagLine() {
330 wxASSERT(!this->flagMap.empty());
331 wxASSERT(this->enabledFlags.empty());
332 wxASSERT(this->customFlags.IsEmpty());
333 wxASSERT(!this->IsFlagDataReady());
334
335 wxString flagLine;
336
337 ProMan::GetProfileManager()->ProfileRead(
338 PRO_CFG_TC_CURRENT_FLAG_LINE, &flagLine, wxEmptyString);
339
340 wxStringTokenizer tokenizer(flagLine, _T(" "));
341
342 while(tokenizer.HasMoreTokens()) {
343 wxString flag(tokenizer.GetNextToken());
344
345 if (this->flagMap.count(flag) > 0) {
346 int flagIndex = this->flagMap.find(flag)->second;
347 this->enabledFlags[flagIndex] = flag;
348 } else {
349 if (!this->customFlags.IsEmpty()) {
350 this->customFlags += _T(" ");
351 }
352 this->customFlags += flag;
353 }
354 }
355 }
356
357 std::vector<int> ProfileProxy::FindInEnabledFlags(const wxString& flag) const {
358 std::vector<int> indices;
359
360 for (std::map<int, wxString>::const_iterator it = this->enabledFlags.begin();
361 it != this->enabledFlags.end();
362 ++it) {
363
364 const std::pair<int, wxString>& entry = *it;
365
366 if (entry.second == flag) {
367 indices.push_back(entry.first);
368 }
369 }
370
371 return indices;
372 }
373
374 wxString ProfileProxy::VerifyEnabledFlagsForFlag(
375 const wxString& flag, const int flagIndex) const {
376 const std::vector<int> indicesFound(this->FindInEnabledFlags(flag));
377
378 if (indicesFound.size() > 1) {
379 wxString indicesStr;
380
381 for (std::vector<int>::const_iterator it = indicesFound.begin() + 1;
382 it != indicesFound.end();
383 ++it) {
384 if (!indicesStr.IsEmpty()) {
385 indicesStr += _T(", ");
386 }
387 indicesStr += wxString::Format(_T("%d"), *it);
388 }
389
390 return wxString::Format(
391 _T("Found flag %s at indices %s."), flag.c_str(), indicesStr.c_str());
392 }
393
394 if (indicesFound.size() == 1 && indicesFound[0] != flagIndex) {
395 return wxString::Format(_T("Found flag %s at index %d instead of index %d."),
396 flag.c_str(), indicesFound[0], flagIndex);
397 }
398
399 if ((this->enabledFlags.count(flagIndex) > 1) &&
400 (this->enabledFlags.find(flagIndex)->second != flag)) {
401 return wxString::Format(_T("Found flag %s at index %d instead of flag %s."),
402 flag.c_str(),
403 flagIndex,
404 this->enabledFlags.find(flagIndex)->second.c_str());
405 }
406
407 return wxEmptyString; // no errors found
408 }
409
410 void ProfileProxy::Reset() {
411 this->flagMap.clear();
412 this->enabledFlags.clear();
413 this->customFlags.Empty();
414 this->isFlagDataReady = false;
415 }