1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 
32 #include "configfile.h"
33 #include <qttoolsinterface.h>
34 #include <core/loghelper.h>
35 #include <tinyxml.h>
36 
37 #define MODUS 0711
38 
39 #ifdef _WIN32
40 #include <windows.h>
41 #include <direct.h>
42 #endif
43 
44 #include <iostream>
45 #include <sstream>
46 #include <cstdlib>
47 #include <fstream>
48 #include <set>
49 #include <algorithm>
50 
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 
54 using namespace std;
55 
56 
ConfigFile(char * argv0,bool readonly)57 ConfigFile::ConfigFile(char *argv0, bool readonly) : noWriteAccess(readonly)
58 {
59 
60 	myArgv0 = argv0;
61 
62 	myQtToolsInterface = CreateQtToolsWrapper();
63 
64 	myConfigState = OK;
65 
66 	// !!!! Revisionsnummer der Configdefaults !!!!!
67 	configRev = 104;
68 
69 	//standard defaults
70 	logOnOffDefault = "1";
71 
72 	// Pfad und Dateinamen setzen
73 #ifdef _WIN32
74 	const char *appDataPath = getenv("AppData");
75 	if (appDataPath && appDataPath[0] != 0) {
76 		configFileName = appDataPath;
77 	} else {
78 		const int MaxPathSize = 1024;
79 		char curDir[MaxPathSize + 1];
80 		curDir[0] = 0;
81 		_getcwd(curDir, MaxPathSize);
82 		curDir[MaxPathSize] = 0;
83 		configFileName = curDir;
84 		// Testen ob das Verzeichnis beschreibbar ist
85 		ofstream tmpFile;
86 		const char *tmpFileName = "pokerth_test.tmp";
87 		tmpFile.open((configFileName + "\\" + tmpFileName).c_str());
88 		if (tmpFile) {
89 			// Erfolgreich, Verzeichnis beschreibbar.
90 			// Datei wieder loeschen.
91 			tmpFile.close();
92 			remove((configFileName + "\\" + tmpFileName).c_str());
93 		} else {
94 			// Fehlgeschlagen, Verzeichnis nicht beschreibbar
95 			curDir[0] = 0;
96 			GetTempPathA(MaxPathSize, curDir);
97 			curDir[MaxPathSize] = 0;
98 			configFileName = curDir;
99 		}
100 	}
101 	//define app-dir
102 	configFileName += "\\pokerth\\";
103 	////define log-dir
104 	logDir = configFileName;
105 	logDir += "log-files\\";
106 	////define data-dir
107 	dataDir = configFileName;
108 	dataDir += "data\\";
109 	////define cache-dir
110 	cacheDir = configFileName;
111 	cacheDir += "cache\\";
112 
113 	//create directories on first start of app
114 	_mkdir(configFileName.c_str());
115 	_mkdir(logDir.c_str());
116 	_mkdir(dataDir.c_str());
117 	_mkdir(cacheDir.c_str());
118 
119 #else
120 	//define app-dir
121 	const char *homePath = getenv("HOME");
122 	if(homePath) {
123 		configFileName = homePath;
124 #ifndef ANDROID
125 		configFileName += "/.pokerth/";
126 #endif
127 		////define log-dir
128 		logDir = configFileName;
129 		logDir += "log-files/";
130 		////define data-dir
131 		dataDir = configFileName;
132 		dataDir += "data/";
133 		////define cache-dir
134 		cacheDir = configFileName;
135 		cacheDir += "cache/";
136 		//create directories on first start of app
137 		mkdir(configFileName.c_str(), MODUS) ;
138 		mkdir(logDir.c_str(), MODUS);
139 		mkdir(dataDir.c_str(), MODUS);
140 		mkdir(cacheDir.c_str(), MODUS);
141 	}
142 #endif
143 
144 	ostringstream tempIntToString;
145 	tempIntToString << configRev;
146 	configList.push_back(ConfigInfo("ConfigRevision", CONFIG_TYPE_INT, tempIntToString.str()));
147 #ifdef ANDROID
148 	configList.push_back(ConfigInfo("AppDataDir", CONFIG_TYPE_STRING, ":/android/android-data/"));
149 #else
150 	configList.push_back(ConfigInfo("AppDataDir", CONFIG_TYPE_STRING, myQtToolsInterface->getDataPathStdString(myArgv0)));
151 #endif
152 	configList.push_back(ConfigInfo("Language", CONFIG_TYPE_INT, myQtToolsInterface->getDefaultLanguage()));
153 	configList.push_back(ConfigInfo("ShowLeftToolBox", CONFIG_TYPE_INT, "1"));
154 	configList.push_back(ConfigInfo("ShowCountryFlagInAvatar", CONFIG_TYPE_INT, "1"));
155 	configList.push_back(ConfigInfo("ShowPingStateInAvatar", CONFIG_TYPE_INT, "1"));
156 	configList.push_back(ConfigInfo("ShowRightToolBox", CONFIG_TYPE_INT, "1"));
157 	configList.push_back(ConfigInfo("ShowFadeOutCardsAnimation", CONFIG_TYPE_INT, "1"));
158 	configList.push_back(ConfigInfo("ShowFlipCardsAnimation", CONFIG_TYPE_INT, "1"));
159 	configList.push_back(ConfigInfo("ShowBlindButtons", CONFIG_TYPE_INT, "1"));
160 	configList.push_back(ConfigInfo("ShowCardsChanceMonitor", CONFIG_TYPE_INT, "1"));
161 	configList.push_back(ConfigInfo("DontTranslateInternationalPokerStringsFromStyle", CONFIG_TYPE_INT, "0"));
162 	configList.push_back(ConfigInfo("DisableSplashScreenOnStartup", CONFIG_TYPE_INT, "0"));
163 	configList.push_back(ConfigInfo("AccidentallyCallBlocker", CONFIG_TYPE_INT, "1"));
164 	configList.push_back(ConfigInfo("DontHideAvatarsOfIgnored", CONFIG_TYPE_INT, "0"));
165 	configList.push_back(ConfigInfo("DisableChatEmoticons", CONFIG_TYPE_INT, "0"));
166 	configList.push_back(ConfigInfo("AntiPeekMode", CONFIG_TYPE_INT, "0"));
167 	configList.push_back(ConfigInfo("AlternateFKeysUserActionMode", CONFIG_TYPE_INT, "0"));
168 	configList.push_back(ConfigInfo("EnableBetInputFocusSwitch", CONFIG_TYPE_INT, "0"));
169 	configList.push_back(ConfigInfo("FlipsideTux", CONFIG_TYPE_INT, "1"));
170 	configList.push_back(ConfigInfo("FlipsideOwn", CONFIG_TYPE_INT, "0"));
171 	configList.push_back(ConfigInfo("FlipsideOwnFile", CONFIG_TYPE_STRING, ""));
172 	configList.push_back(ConfigInfo("GameTableStylesList", CONFIG_TYPE_STRING_LIST, "GameTableStyles"));
173 	configList.push_back(ConfigInfo("CurrentGameTableStyle", CONFIG_TYPE_STRING, ""));
174 	configList.push_back(ConfigInfo("CardDeckStylesList", CONFIG_TYPE_STRING_LIST, "CardDeckStyles"));
175 	configList.push_back(ConfigInfo("PlayerTooltips", CONFIG_TYPE_STRING_LIST, "PlayerTooltips"));
176 	configList.push_back(ConfigInfo("CurrentCardDeckStyle", CONFIG_TYPE_STRING, ""));
177 	configList.push_back(ConfigInfo("LastGameTableStyleDir", CONFIG_TYPE_STRING, ""));
178 	configList.push_back(ConfigInfo("LastCardDeckStyleDir", CONFIG_TYPE_STRING, ""));
179 	configList.push_back(ConfigInfo("PlaySoundEffects", CONFIG_TYPE_INT, "1"));
180 	configList.push_back(ConfigInfo("SoundVolume", CONFIG_TYPE_INT, "8"));
181 	configList.push_back(ConfigInfo("PlayGameActions", CONFIG_TYPE_INT, "1"));
182 	configList.push_back(ConfigInfo("PlayLobbyChatNotification", CONFIG_TYPE_INT, "1"));
183 	configList.push_back(ConfigInfo("PlayNetworkGameNotification", CONFIG_TYPE_INT, "1"));
184 	configList.push_back(ConfigInfo("PlayBlindRaiseNotification", CONFIG_TYPE_INT, "1"));
185 	configList.push_back(ConfigInfo("NumberOfPlayers", CONFIG_TYPE_INT, "10"));
186 	configList.push_back(ConfigInfo("StartCash", CONFIG_TYPE_INT, "5000"));
187 	configList.push_back(ConfigInfo("FirstSmallBlind", CONFIG_TYPE_INT, "10"));
188 	configList.push_back(ConfigInfo("RaiseBlindsAtHands", CONFIG_TYPE_INT, "1"));
189 	configList.push_back(ConfigInfo("RaiseBlindsAtMinutes", CONFIG_TYPE_INT, "0"));
190 	configList.push_back(ConfigInfo("RaiseSmallBlindEveryHands", CONFIG_TYPE_INT, "8"));
191 	configList.push_back(ConfigInfo("RaiseSmallBlindEveryMinutes", CONFIG_TYPE_INT, "5"));
192 	configList.push_back(ConfigInfo("AlwaysDoubleBlinds", CONFIG_TYPE_INT, "1"));
193 	configList.push_back(ConfigInfo("ManualBlindsOrder", CONFIG_TYPE_INT, "0"));
194 	configList.push_back(ConfigInfo("ManualBlindsList", CONFIG_TYPE_INT_LIST, "Blind"));
195 	configList.push_back(ConfigInfo("AfterMBAlwaysDoubleBlinds", CONFIG_TYPE_INT, "1"));
196 	configList.push_back(ConfigInfo("AfterMBAlwaysRaiseAbout", CONFIG_TYPE_INT, "0"));
197 	configList.push_back(ConfigInfo("AfterMBAlwaysRaiseValue", CONFIG_TYPE_INT, "0"));
198 	configList.push_back(ConfigInfo("AfterMBStayAtLastBlind", CONFIG_TYPE_INT, "0"));
199 	configList.push_back(ConfigInfo("GameSpeed", CONFIG_TYPE_INT, "4"));
200 	configList.push_back(ConfigInfo("PauseBetweenHands", CONFIG_TYPE_INT, "0"));
201 	configList.push_back(ConfigInfo("ShowGameSettingsDialogOnNewGame", CONFIG_TYPE_INT, "1"));
202 	configList.push_back(ConfigInfo("NetNumberOfPlayers", CONFIG_TYPE_INT, "10"));
203 	configList.push_back(ConfigInfo("NetStartCash", CONFIG_TYPE_INT, "3000"));
204 	configList.push_back(ConfigInfo("NetFirstSmallBlind", CONFIG_TYPE_INT, "10"));
205 	configList.push_back(ConfigInfo("NetRaiseBlindsAtHands", CONFIG_TYPE_INT, "1"));
206 	configList.push_back(ConfigInfo("NetRaiseBlindsAtMinutes", CONFIG_TYPE_INT, "0"));
207 	configList.push_back(ConfigInfo("NetRaiseSmallBlindEveryHands", CONFIG_TYPE_INT, "8"));
208 	configList.push_back(ConfigInfo("NetRaiseSmallBlindEveryMinutes", CONFIG_TYPE_INT, "5"));
209 	configList.push_back(ConfigInfo("NetAlwaysDoubleBlinds", CONFIG_TYPE_INT, "1"));
210 	configList.push_back(ConfigInfo("NetManualBlindsOrder", CONFIG_TYPE_INT, "0"));
211 	configList.push_back(ConfigInfo("NetManualBlindsList", CONFIG_TYPE_INT_LIST, "NetBlind"));
212 	configList.push_back(ConfigInfo("NetAfterMBAlwaysDoubleBlinds", CONFIG_TYPE_INT, "1"));
213 	configList.push_back(ConfigInfo("NetAfterMBAlwaysRaiseAbout", CONFIG_TYPE_INT, "0"));
214 	configList.push_back(ConfigInfo("NetAfterMBAlwaysRaiseValue", CONFIG_TYPE_INT, "0"));
215 	configList.push_back(ConfigInfo("NetAfterMBStayAtLastBlind", CONFIG_TYPE_INT, "0"));
216 	configList.push_back(ConfigInfo("NetGameSpeed", CONFIG_TYPE_INT, "4"));
217 	configList.push_back(ConfigInfo("NetDelayBetweenHands", CONFIG_TYPE_INT, "7"));
218 	configList.push_back(ConfigInfo("NetTimeOutPlayerAction", CONFIG_TYPE_INT, "20"));
219 	configList.push_back(ConfigInfo("NetAutoLeaveGameAfterFinish", CONFIG_TYPE_INT, "0"));
220 	configList.push_back(ConfigInfo("ServerPassword", CONFIG_TYPE_STRING, ""));
221 	configList.push_back(ConfigInfo("ServerUseIpv6", CONFIG_TYPE_INT, "0"));
222 	configList.push_back(ConfigInfo("ServerUseSctp", CONFIG_TYPE_INT, "0"));
223 	configList.push_back(ConfigInfo("ServerUseWebSocket", CONFIG_TYPE_INT, "0"));
224 	configList.push_back(ConfigInfo("ServerPort", CONFIG_TYPE_INT, "7234"));
225 	configList.push_back(ConfigInfo("ServerWebSocketPort", CONFIG_TYPE_INT, "7233"));
226 	configList.push_back(ConfigInfo("ServerWebSocketResource", CONFIG_TYPE_STRING, ""));
227 	configList.push_back(ConfigInfo("ServerWebSocketOrigin", CONFIG_TYPE_STRING, ""));
228 	configList.push_back(ConfigInfo("ServerUsePutAvatars", CONFIG_TYPE_INT, "0"));
229 	configList.push_back(ConfigInfo("ServerPutAvatarsAddress", CONFIG_TYPE_STRING, ""));
230 	configList.push_back(ConfigInfo("ServerPutAvatarsUser", CONFIG_TYPE_STRING, ""));
231 	configList.push_back(ConfigInfo("ServerPutAvatarsPassword", CONFIG_TYPE_STRING, ""));
232 	configList.push_back(ConfigInfo("ServerBruteForceProtection", CONFIG_TYPE_INT, "1"));
233 	configList.push_back(ConfigInfo("InternetServerConfigMode", CONFIG_TYPE_INT, "0"));
234 	configList.push_back(ConfigInfo("InternetServerListAddress", CONFIG_TYPE_STRING, "pokerth.net/serverlist.xml.z"));
235 	configList.push_back(ConfigInfo("InternetServerAddress", CONFIG_TYPE_STRING, "pokerth.6dns.org"));
236 	configList.push_back(ConfigInfo("InternetServerPort", CONFIG_TYPE_INT, "7234"));
237 	configList.push_back(ConfigInfo("InternetServerUseIpv6", CONFIG_TYPE_INT, "0"));
238 	configList.push_back(ConfigInfo("InternetServerUseSctp", CONFIG_TYPE_INT, "0"));
239 	configList.push_back(ConfigInfo("UseAvatarServer", CONFIG_TYPE_INT, "0"));
240 	configList.push_back(ConfigInfo("AvatarServerAddress", CONFIG_TYPE_STRING, ""));
241 	configList.push_back(ConfigInfo("UseInternetGamePassword", CONFIG_TYPE_INT, "0"));
242 	configList.push_back(ConfigInfo("InternetGamePassword", CONFIG_TYPE_STRING, ""));
243 	configList.push_back(ConfigInfo("InternetGameType", CONFIG_TYPE_INT, "0"));
244 	configList.push_back(ConfigInfo("InternetGameName", CONFIG_TYPE_STRING, "My Online Game"));
245 	configList.push_back(ConfigInfo("InternetGameAllowSpectators", CONFIG_TYPE_INT, "1"));
246 	configList.push_back(ConfigInfo("UseLobbyChat", CONFIG_TYPE_INT, "1"));
247 	configList.push_back(ConfigInfo("UseAdminIRC", CONFIG_TYPE_INT, "0"));
248 	configList.push_back(ConfigInfo("AdminIRCServerAddress", CONFIG_TYPE_STRING, "chat.freenode.net"));
249 	configList.push_back(ConfigInfo("AdminIRCServerPort", CONFIG_TYPE_INT, "6667"));
250 	configList.push_back(ConfigInfo("AdminIRCChannel", CONFIG_TYPE_STRING, "#test"));
251 	configList.push_back(ConfigInfo("AdminIRCChannelPassword", CONFIG_TYPE_STRING, ""));
252 	configList.push_back(ConfigInfo("AdminIRCServerUseIpv6", CONFIG_TYPE_INT, "0"));
253 	configList.push_back(ConfigInfo("AdminIRCServerNick", CONFIG_TYPE_INT, "PokerTH_Admin"));
254 	configList.push_back(ConfigInfo("UseLobbyIRC", CONFIG_TYPE_INT, "0"));
255 	configList.push_back(ConfigInfo("LobbyIRCServerAddress", CONFIG_TYPE_STRING, "chat.freenode.net"));
256 	configList.push_back(ConfigInfo("LobbyIRCServerPort", CONFIG_TYPE_INT, "6667"));
257 	configList.push_back(ConfigInfo("LobbyIRCChannel", CONFIG_TYPE_STRING, "#pokerth-lobby"));
258 	configList.push_back(ConfigInfo("LobbyIRCChannelPassword", CONFIG_TYPE_STRING, ""));
259 	configList.push_back(ConfigInfo("LobbyIRCServerUseIpv6", CONFIG_TYPE_INT, "0"));
260 	configList.push_back(ConfigInfo("LobbyIRCServerNick", CONFIG_TYPE_INT, "PokerTH_Lobby"));
261 	configList.push_back(ConfigInfo("UseChatCleaner", CONFIG_TYPE_INT, "0"));
262 	configList.push_back(ConfigInfo("ChatCleanerHostAddress", CONFIG_TYPE_STRING, "localhost"));
263 	configList.push_back(ConfigInfo("ChatCleanerPort", CONFIG_TYPE_INT, "4327"));
264 	configList.push_back(ConfigInfo("ChatCleanerClientAuth", CONFIG_TYPE_STRING, ""));
265 	configList.push_back(ConfigInfo("ChatCleanerServerAuth", CONFIG_TYPE_STRING, ""));
266 	configList.push_back(ConfigInfo("ChatCleanerUseIpv6", CONFIG_TYPE_INT, "0"));
267 	configList.push_back(ConfigInfo("MyName", CONFIG_TYPE_STRING, "Human Player"));
268 	configList.push_back(ConfigInfo("MyAvatar", CONFIG_TYPE_STRING, ""));
269 	configList.push_back(ConfigInfo("MyRememberedNameDuringGuestLogin", CONFIG_TYPE_STRING, ""));
270 	configList.push_back(ConfigInfo("Opponent1Name", CONFIG_TYPE_STRING, "Player 1"));
271 	configList.push_back(ConfigInfo("Opponent1Avatar", CONFIG_TYPE_STRING, ""));
272 	configList.push_back(ConfigInfo("Opponent2Name", CONFIG_TYPE_STRING, "Player 2"));
273 	configList.push_back(ConfigInfo("Opponent2Avatar", CONFIG_TYPE_STRING, ""));
274 	configList.push_back(ConfigInfo("Opponent3Name", CONFIG_TYPE_STRING, "Player 3"));
275 	configList.push_back(ConfigInfo("Opponent3Avatar", CONFIG_TYPE_STRING, ""));
276 	configList.push_back(ConfigInfo("Opponent4Name", CONFIG_TYPE_STRING, "Player 4"));
277 	configList.push_back(ConfigInfo("Opponent4Avatar", CONFIG_TYPE_STRING, ""));
278 	configList.push_back(ConfigInfo("Opponent5Name", CONFIG_TYPE_STRING, "Player 5"));
279 	configList.push_back(ConfigInfo("Opponent5Avatar", CONFIG_TYPE_STRING, ""));
280 	configList.push_back(ConfigInfo("Opponent6Name", CONFIG_TYPE_STRING, "Player 6"));
281 	configList.push_back(ConfigInfo("Opponent6Avatar", CONFIG_TYPE_STRING, ""));
282 	configList.push_back(ConfigInfo("Opponent7Name", CONFIG_TYPE_STRING, "Player 7"));
283 	configList.push_back(ConfigInfo("Opponent7Avatar", CONFIG_TYPE_STRING, ""));
284 	configList.push_back(ConfigInfo("Opponent8Name", CONFIG_TYPE_STRING, "Player 8"));
285 	configList.push_back(ConfigInfo("Opponent8Avatar", CONFIG_TYPE_STRING, ""));
286 	configList.push_back(ConfigInfo("Opponent9Name", CONFIG_TYPE_STRING, "Player 9"));
287 	configList.push_back(ConfigInfo("Opponent9Avatar", CONFIG_TYPE_STRING, ""));
288 	configList.push_back(ConfigInfo("LogOnOff", CONFIG_TYPE_INT, logOnOffDefault));
289 	configList.push_back(ConfigInfo("LogDir", CONFIG_TYPE_STRING, logDir));
290 	configList.push_back(ConfigInfo("LogStoreDuration", CONFIG_TYPE_INT, "2"));
291 	configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "1"));
292 	configList.push_back(ConfigInfo("UserDataDir", CONFIG_TYPE_STRING, dataDir));
293 	configList.push_back(ConfigInfo("CacheDir", CONFIG_TYPE_STRING, cacheDir));
294 	configList.push_back(ConfigInfo("CLA_NoWriteAccess", CONFIG_TYPE_INT, "0"));
295 	configList.push_back(ConfigInfo("DisableBackToLobbyWarning", CONFIG_TYPE_INT, "0"));
296 	configList.push_back(ConfigInfo("DlgGameLobbyGameListSortingSection", CONFIG_TYPE_INT, "2"));
297 	configList.push_back(ConfigInfo("DlgGameLobbyGameListSortingOrder", CONFIG_TYPE_INT, "1"));
298 	configList.push_back(ConfigInfo("DlgGameLobbyGameListFilterIndex", CONFIG_TYPE_INT, "0"));
299 	configList.push_back(ConfigInfo("DlgGameLobbyNickListSortFilterIndex", CONFIG_TYPE_INT, "0"));
300 	configList.push_back(ConfigInfo("GameTableFullScreenSave", CONFIG_TYPE_INT, "0"));
301 	configList.push_back(ConfigInfo("GameTableHeightSave", CONFIG_TYPE_INT, "600"));
302 	configList.push_back(ConfigInfo("GameTableWidthSave", CONFIG_TYPE_INT, "1024"));
303 	configList.push_back(ConfigInfo("InternetLoginMode", CONFIG_TYPE_INT, "0"));
304 	configList.push_back(ConfigInfo("InternetLoginPassword", CONFIG_TYPE_STRING, ""));
305 	configList.push_back(ConfigInfo("InternetSavePassword", CONFIG_TYPE_INT, "0"));
306 	configList.push_back(ConfigInfo("IfInfoMessageShowList", CONFIG_TYPE_STRING_LIST, "Msg"));
307 	configList.push_back(ConfigInfo("PlayerIgnoreList", CONFIG_TYPE_STRING_LIST, "Player"));
308 	configList.push_back(ConfigInfo("DBServerAddress", CONFIG_TYPE_STRING, "127.0.0.1"));
309 	configList.push_back(ConfigInfo("DBServerUser", CONFIG_TYPE_STRING, "pokerth"));
310 	configList.push_back(ConfigInfo("DBServerPassword", CONFIG_TYPE_STRING, ""));
311 	configList.push_back(ConfigInfo("DBServerDatabaseName", CONFIG_TYPE_STRING, "pokerth"));
312 	configList.push_back(ConfigInfo("DBServerEncryptionKey", CONFIG_TYPE_STRING, ""));
313 	configList.push_back(ConfigInfo("GameNameBadWordList", CONFIG_TYPE_STRING_LIST, "Regex"));
314 	configList.push_back(ConfigInfo("ServerRestrictGuestLogin", CONFIG_TYPE_INT, "0"));
315 
316 	//fill tempList firstTime
317 	configBufferList = configList;
318 
319 	// 	cout << configTempList[3].name << " " << configTempList[10].defaultValue << endl;
320 
321 	if(!noWriteAccess) {
322 		configFileName += "config.xml";
323 
324 		//Prüfen ob Configfile existiert --> sonst anlegen
325 		TiXmlDocument doc(configFileName);
326 		if(!doc.LoadFile()) {
327 			myConfigState = NONEXISTING;
328 			updateConfig(myConfigState);
329 		} else {
330 			//Check if config revision and AppDataDir is ok. Otherwise --> update()
331 			int tempRevision = 0;
332 			string tempAppDataPath ("");
333 
334 			TiXmlHandle docHandle( &doc );
335 			TiXmlElement* confRevision = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( "ConfigRevision" ).ToElement();
336 			if ( confRevision ) {
337 				confRevision->QueryIntAttribute("value", &tempRevision );
338 			}
339 			TiXmlElement* confAppDataPath = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( "AppDataDir" ).ToElement();
340 			if ( confAppDataPath ) {
341 				const char *tmpStr = confAppDataPath->Attribute("value");
342 				if (tmpStr) tempAppDataPath = tmpStr;
343 				//if appdatapath changes directly update it here not in UpdateConfig()
344 #ifdef ANDROID
345 				if(tempAppDataPath != ":/android/android-data/") {
346 					confAppDataPath->SetAttribute("value", ":/android/android-data/");
347 #else
348 				if(tempAppDataPath != myQtToolsInterface->getDataPathStdString(myArgv0)) {
349 					confAppDataPath->SetAttribute("value", myQtToolsInterface->stringToUtf8(myQtToolsInterface->getDataPathStdString(myArgv0)));
350 #endif
351 					doc.SaveFile( configFileName );
352 				}
353 			}
354 			if (tempRevision < configRev) {
355 				myConfigState = OLD;
356 				updateConfig(myConfigState) ;
357 			}
358 		}
359 
360 		fillBuffer();
361 		checkAndCorrectBuffer();
362 	}
363 }
364 
365 
366 ConfigFile::~ConfigFile()
367 {
368 	delete myQtToolsInterface;
369 	myQtToolsInterface = 0;
370 
371 }
372 
373 
374 void ConfigFile::fillBuffer()
375 {
376 
377 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
378 
379 	string tempString1("");
380 	string tempString2("");
381 
382 	TiXmlDocument doc(configFileName);
383 
384 	if(doc.LoadFile()) {
385 		TiXmlHandle docHandle( &doc );
386 
387 		for (size_t i=0; i<configBufferList.size(); i++) {
388 
389 			TiXmlElement* conf = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).ToElement();
390 
391 			if ( conf ) {
392 
393 				const char *tmpStr1 = conf->Attribute("value");
394 				if (tmpStr1) tempString1 = tmpStr1;
395 				configBufferList[i].defaultValue = tempString1;
396 
397 				const char *tmpStr2 = conf->Attribute("type");
398 				if (tmpStr2) {
399 					tempString2 = tmpStr2;
400 					if(tempString2 == "list") {
401 
402 						list<string> tempStringList2;
403 
404 						TiXmlElement* confList = docHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).FirstChild().ToElement();
405 
406 						for( ; confList; confList=confList->NextSiblingElement()) {
407 							tempStringList2.push_back(confList->Attribute("value"));
408 						}
409 
410 						configBufferList[i].defaultListValue = tempStringList2;
411 					}
412 				}
413 
414 
415 			} else {
416 				LOG_ERROR("Could not find the root element in the config file!");
417 			}
418 
419 			// 			cout << configBufferList[i].name << " " << configBufferList[i].defaultValue << endl;
420 		}
421 	}
422 }
423 
424 void ConfigFile::checkAndCorrectBuffer()
425 {
426 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
427 	// For now, only the player names are checked.
428 	checkAndCorrectPlayerNames();
429 }
430 
431 void ConfigFile::checkAndCorrectPlayerNames()
432 {
433 	// Verify that the player names are uniquely set.
434 	set<string> playerNames;
435 	playerNames.insert(readConfigString("MyName"));
436 	for(int i = 1; i <= 9; i++) {
437 		ostringstream opponentVar;
438 		opponentVar << "Opponent" << i << "Name";
439 		playerNames.insert(readConfigString(opponentVar.str()));
440 	}
441 	if (playerNames.size() < 10 || playerNames.find("") != playerNames.end()) {
442 		// The set contains less than 10 players or an empty player name.
443 		// Reset to default player names.
444 		writeConfigString("MyName", "Human Player");
445 		for(int i = 1; i <= 9; i++) {
446 			ostringstream opponentVar;
447 			ostringstream opponentName;
448 			opponentVar << "Opponent" << i << "Name";
449 			opponentName << "Player " << i;
450 			writeConfigString(opponentVar.str(), opponentName.str());
451 		}
452 	}
453 }
454 
455 void ConfigFile::writeBuffer() const
456 {
457 
458 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
459 
460 	//write buffer to disc if enabled
461 	if(!noWriteAccess) {
462 		TiXmlDocument doc;
463 		TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
464 		doc.LinkEndChild( decl );
465 
466 		TiXmlElement * root = new TiXmlElement( "PokerTH" );
467 		doc.LinkEndChild( root );
468 
469 		TiXmlElement * config;
470 		config = new TiXmlElement( "Configuration" );
471 		root->LinkEndChild( config );
472 
473 		size_t i;
474 
475 		for (i=0; i<configBufferList.size(); i++) {
476 			TiXmlElement *tmpElement = new TiXmlElement(configBufferList[i].name);
477 			config->LinkEndChild( tmpElement );
478 			tmpElement->SetAttribute("value", configBufferList[i].defaultValue);
479 
480 			if(configBufferList[i].type == CONFIG_TYPE_INT_LIST || configBufferList[i].type == CONFIG_TYPE_STRING_LIST) {
481 
482 				tmpElement->SetAttribute("type", "list");
483 				list<string> tempList = configBufferList[i].defaultListValue;
484 				list<string>::iterator it;
485 				for(it = tempList.begin(); it != tempList.end(); ++it) {
486 
487 					TiXmlElement *tmpSubElement = new TiXmlElement(configBufferList[i].defaultValue);
488 					tmpElement->LinkEndChild( tmpSubElement );
489 					tmpSubElement->SetAttribute("value", *it);
490 				}
491 
492 			}
493 		}
494 
495 		doc.SaveFile( configFileName );
496 	}
497 }
498 
499 void ConfigFile::updateConfig(ConfigState myConfigState)
500 {
501 
502 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
503 
504 	size_t i;
505 
506 	if(myConfigState == NONEXISTING) {
507 
508 		//Create a new ConfigFile!
509 		TiXmlDocument doc;
510 		TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
511 		doc.LinkEndChild( decl );
512 
513 		TiXmlElement * root = new TiXmlElement( "PokerTH" );
514 		doc.LinkEndChild( root );
515 
516 		TiXmlElement * config;
517 		config = new TiXmlElement( "Configuration" );
518 		root->LinkEndChild( config );
519 
520 		for (i=0; i<configList.size(); i++) {
521 			TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
522 			config->LinkEndChild( tmpElement );
523 			tmpElement->SetAttribute("value", myQtToolsInterface->stringToUtf8(configList[i].defaultValue));
524 
525 			if(configList[i].type == CONFIG_TYPE_INT_LIST || configBufferList[i].type == CONFIG_TYPE_STRING_LIST) {
526 
527 				tmpElement->SetAttribute("type", "list");
528 				list<string> tempList = configList[i].defaultListValue;
529 				list<string>::iterator it;
530 				for(it = tempList.begin(); it != tempList.end(); ++it) {
531 
532 					TiXmlElement *tmpSubElement = new TiXmlElement(configList[i].defaultValue);
533 					tmpElement->LinkEndChild( tmpSubElement );
534 					tmpSubElement->SetAttribute("value", *it);
535 				}
536 			}
537 		}
538 		doc.SaveFile( configFileName );
539 	}
540 
541 	if(myConfigState == OLD) {
542 
543 		TiXmlDocument oldDoc(configFileName);
544 
545 		//load the old one
546 		if(oldDoc.LoadFile()) {
547 
548 			string tempString1("");
549 			string tempString2("");
550 
551 			TiXmlDocument newDoc;
552 
553 			//Create the new one
554 			TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
555 			newDoc.LinkEndChild( decl );
556 
557 			TiXmlElement * root = new TiXmlElement( "PokerTH" );
558 			newDoc.LinkEndChild( root );
559 
560 			TiXmlElement * config;
561 			config = new TiXmlElement( "Configuration" );
562 			root->LinkEndChild( config );
563 
564 			//change configRev and AppDataPath
565 			std::list<std::string> noUpdateElemtsList;
566 
567 			TiXmlElement * confElement0 = new TiXmlElement( "ConfigRevision" );
568 			config->LinkEndChild( confElement0 );
569 			confElement0->SetAttribute("value", configRev);
570 			noUpdateElemtsList.push_back("ConfigRevision");
571 
572 			TiXmlElement * confElement1 = new TiXmlElement( "AppDataDir" );
573 			config->LinkEndChild( confElement1 );
574 			confElement1->SetAttribute("value", myQtToolsInterface->stringToUtf8(myQtToolsInterface->getDataPathStdString(myArgv0)));
575 			noUpdateElemtsList.push_back("AppDataDir");
576 
577 			///////// VERSION HACK SECTION ///////////////////////
578 			//this is the right place for special version depending config hacks:
579 			//0.9.1 - log interval needs to be set to 1 instead of 0
580 			if (configRev >= 95 && configRev <= 98) { // this means 0.9.1 or 0.9.2 or 1.0
581 				TiXmlElement * confElement2 = new TiXmlElement( "LogInterval" );
582 				config->LinkEndChild( confElement2 );
583 				confElement2->SetAttribute("value", 1);
584 				noUpdateElemtsList.push_back("LogInterval");
585 			}
586 
587 			if (configRev == 98) { // this means 1.0
588 				TiXmlElement * confElement3 = new TiXmlElement( "CurrentCardDeckStyle" );
589 				config->LinkEndChild( confElement3 );
590 				confElement3->SetAttribute("value", "");
591 				noUpdateElemtsList.push_back("CurrentCardDeckStyle");
592 			}
593 			///////// VERSION HACK SECTION ///////////////////////
594 
595 			TiXmlHandle oldDocHandle( &oldDoc );
596 
597 			for (i=0; i<configList.size(); i++) {
598 
599 				TiXmlElement* oldConf = oldDocHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).ToElement();
600 
601 				if ( oldConf ) { // if element is already there --> take over the saved values
602 
603 					// dont update ConfigRevision and AppDataDir AND possible hacked Config-Elements becaus it was already set ^^
604 					if(count(noUpdateElemtsList.begin(), noUpdateElemtsList.end(), configList[i].name) == 0) {
605 
606 						TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
607 						config->LinkEndChild( tmpElement );
608 
609 						const char *tmpStr1 = oldConf->Attribute("value");
610 						if (tmpStr1) tempString1 = tmpStr1;
611 						tmpElement->SetAttribute("value", tempString1);
612 
613 						//for lists copy elements
614 						const char *tmpStr2 = oldConf->Attribute("type");
615 						if (tmpStr2) {
616 							tempString2 = tmpStr2;
617 							if(tempString2 == "list") {
618 
619 								list<string> tempStringList2;
620 
621 								TiXmlElement* oldConfList = oldDocHandle.FirstChild( "PokerTH" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).FirstChild().ToElement();
622 
623 								for( ; oldConfList; oldConfList=oldConfList->NextSiblingElement()) {
624 									tempStringList2.push_back(oldConfList->Attribute("value"));
625 								}
626 
627 								tmpElement->SetAttribute("type", "list");
628 								list<string> tempList = tempStringList2;
629 								list<string>::iterator it;
630 								for(it = tempList.begin(); it != tempList.end(); ++it) {
631 
632 									TiXmlElement *tmpSubElement = new TiXmlElement(tempString1);
633 									tmpElement->LinkEndChild( tmpSubElement );
634 									tmpSubElement->SetAttribute("value", *it);
635 								}
636 							}
637 						}
638 					}
639 				} else {
640 					// if element is not there --> set it with defaultValue
641 					TiXmlElement *tmpElement = new TiXmlElement(configList[i].name);
642 					config->LinkEndChild( tmpElement );
643 					tmpElement->SetAttribute("value", myQtToolsInterface->stringToUtf8(configList[i].defaultValue));
644 
645 					if(configList[i].type == CONFIG_TYPE_INT_LIST || configBufferList[i].type == CONFIG_TYPE_STRING_LIST) {
646 
647 						tmpElement->SetAttribute("type", "list");
648 						list<string> tempList = configList[i].defaultListValue;
649 						list<string>::iterator it;
650 						for(it = tempList.begin(); it != tempList.end(); ++it) {
651 
652 							TiXmlElement *tmpSubElement = new TiXmlElement(configList[i].defaultValue);
653 							tmpElement->LinkEndChild( tmpSubElement );
654 							tmpSubElement->SetAttribute("value", *it);
655 						}
656 					}
657 				}
658 			}
659 			newDoc.SaveFile( configFileName );
660 		} else {
661 			LOG_ERROR("Cannot update config file: Unable to load configuration.");
662 		}
663 
664 
665 	}
666 }
667 
668 ConfigState ConfigFile::getConfigState() const
669 {
670 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
671 	return myConfigState;
672 }
673 
674 string ConfigFile::readConfigString(string varName) const
675 {
676 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
677 
678 	size_t i;
679 	string tempString("");
680 
681 	for (i=0; i<configBufferList.size(); i++) {
682 
683 		if (configBufferList[i].name == varName) {
684 			tempString = configBufferList[i].defaultValue;
685 		}
686 	}
687 
688 	return tempString;
689 }
690 
691 int ConfigFile::readConfigInt(string varName) const
692 {
693 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
694 
695 	size_t i;
696 	string tempString("");
697 	int tempInt=0;
698 
699 	for (i=0; i<configBufferList.size(); i++) {
700 
701 		if (configBufferList[i].name == varName) {
702 			tempString = configBufferList[i].defaultValue;
703 		}
704 	}
705 
706 	istringstream isst;
707 	isst.str (tempString);
708 	isst >> tempInt;
709 
710 	return tempInt;
711 }
712 
713 list<int> ConfigFile::readConfigIntList(string varName) const
714 {
715 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
716 
717 	size_t i;
718 	list<string> tempStringList;
719 	list<int> tempIntList;
720 
721 	for (i=0; i<configBufferList.size(); i++) {
722 
723 		if (configBufferList[i].name == varName) {
724 			tempStringList = configBufferList[i].defaultListValue;
725 		}
726 	}
727 
728 	istringstream isst;
729 	int tempInt;
730 	list<string>::iterator it;
731 	for(it = tempStringList.begin(); it != tempStringList.end(); ++it) {
732 
733 		isst.str(*it);
734 		isst >> tempInt;
735 		tempIntList.push_back(tempInt);
736 		isst.str("");
737 		isst.clear();
738 	}
739 
740 	return tempIntList;
741 }
742 
743 list<string> ConfigFile::readConfigStringList(string varName) const
744 {
745 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
746 
747 	size_t i;
748 	list<string> tempStringList;
749 
750 	for (i=0; i<configBufferList.size(); i++) {
751 
752 		if (configBufferList[i].name == varName) {
753 			tempStringList = configBufferList[i].defaultListValue;
754 		}
755 	}
756 
757 	return tempStringList;
758 }
759 
760 void ConfigFile::writeConfigInt(string varName, int varCont)
761 {
762 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
763 
764 	size_t i;
765 	ostringstream intToString;
766 
767 	for (i=0; i<configBufferList.size(); i++) {
768 
769 		if (configBufferList[i].name == varName) {
770 			intToString << varCont;
771 			configBufferList[i].defaultValue = intToString.str();
772 		}
773 	}
774 }
775 
776 void ConfigFile::writeConfigIntList(string varName, list<int> varCont)
777 {
778 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
779 
780 	size_t i;
781 	ostringstream intToString;
782 	list<string> stringList;
783 
784 	for (i=0; i<configBufferList.size(); i++) {
785 
786 		if (configBufferList[i].name == varName) {
787 			list<int>::iterator it;
788 			for(it = varCont.begin(); it != varCont.end(); ++it) {
789 
790 				intToString << (*it);
791 				stringList.push_back(intToString.str());
792 				intToString.str("");
793 				intToString.clear();
794 			}
795 
796 			configBufferList[i].defaultListValue = stringList;
797 		}
798 	}
799 }
800 
801 void ConfigFile::writeConfigString(string varName, string varCont)
802 {
803 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
804 
805 	size_t i;
806 	for (i=0; i<configBufferList.size(); i++) {
807 		if (configBufferList[i].name == varName) {
808 			configBufferList[i].defaultValue = varCont;
809 		}
810 	}
811 
812 }
813 
814 void ConfigFile::writeConfigStringList(string varName, list<string> varCont)
815 {
816 	boost::recursive_mutex::scoped_lock lock(m_configMutex);
817 
818 	size_t i;
819 	for (i=0; i<configBufferList.size(); i++) {
820 
821 		if (configBufferList[i].name == varName) {
822 			configBufferList[i].defaultListValue = varCont;
823 		}
824 	}
825 }
826 
827 void ConfigFile::deleteConfigFile()
828 {
829 	remove(configFileName.c_str());
830 }
831