Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / tools / client / client_config_qt / src / config.cpp
blob93e67284510f62bba0a8e2ca2054a60aed51dac7
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // 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 Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdpch.h"
18 #include "config.h"
20 #include "nel/misc/common.h"
21 #include "nel/misc/i18n.h"
22 #include "nel/misc/path.h"
24 CConfig::CConfig()
28 CConfig::~CConfig()
32 bool CConfig::create(const std::string &configFileName, const std::string &defaultFileName)
34 NLMISC::CFile::createDirectoryTree(NLMISC::CFile::getPath(configFileName));
36 // create the basic .cfg
37 FILE *fp = NLMISC::nlfopen(configFileName, "w");
39 if (fp == NULL) return false;
41 // store full path to default config file
42 fprintf(fp, "RootConfigFilename = \"%s\";\n", defaultFileName.c_str());
44 // get current locale
45 std::string lang = NLMISC::CI18N::getSystemLanguageCode();
47 const std::vector<std::string> &languages = NLMISC::CI18N::getLanguageCodes();
49 // search if current locale is defined in language codes
50 for(uint i = 0; i < languages.size(); ++i)
52 if (lang == languages[i])
54 // store the language code in the config file
55 fprintf(fp, "LanguageCode = \"%s\";\n", lang.c_str());
56 break;
60 fclose(fp);
62 return true;
65 bool CConfig::load(const std::string &fileName)
67 try
69 cf.load(fileName);
71 std::string def = getString("RootConfigFilename");
72 if (!def.empty())
73 dcf.load(def);
75 catch (const NLMISC::Exception &e)
77 nlwarning( "%s", e.what() );
78 return false;
81 return true;
84 bool CConfig::reload()
86 try
88 cf.clear();
89 cf.reparse();
91 catch (const NLMISC::Exception &e)
93 nlwarning( "%s", e.what() );
94 return false;
96 return true;
99 void CConfig::revertToDefault()
101 // If there's no default config, all we can do is revert the current changes
102 if( !dcf.loaded() )
104 reload();
105 return;
108 // If there is a default config, we can however revert to the default!
109 // Code taken from the original config tool
110 uint32 count = cf.getNumVar();
111 uint32 i = 0;
112 for( i = 0; i < count; i++ )
114 NLMISC::CConfigFile::CVar *dst = cf.getVar( i );
116 // Comment from the original
117 // Temp: avoid changing this variable (debug: binded to the actual texture set installed)
118 if( dst->Name.compare( "HDTextureInstalled" ) == 0 )
119 continue;
121 NLMISC::CConfigFile::CVar *src = dcf.getVarPtr( dst->Name );
122 if( ( src != NULL ) && !dst->Root &&
123 ( (( src->Type == NLMISC::CConfigFile::CVar::T_INT ) && ( dst->Type == NLMISC::CConfigFile::CVar::T_INT )) ||
124 (( src->Type == NLMISC::CConfigFile::CVar::T_REAL ) && ( dst->Type == NLMISC::CConfigFile::CVar::T_INT )) ||
125 (( src->Type == NLMISC::CConfigFile::CVar::T_INT ) && ( dst->Type == NLMISC::CConfigFile::CVar::T_REAL )) ||
126 (( src->Type == NLMISC::CConfigFile::CVar::T_REAL ) && ( dst->Type == NLMISC::CConfigFile::CVar::T_REAL )) ||
127 (( src->Type == NLMISC::CConfigFile::CVar::T_STRING ) && ( dst->Type == NLMISC::CConfigFile::CVar::T_STRING )) ) )
130 if( src->Type == NLMISC::CConfigFile::CVar::T_INT )
132 setInt( src->Name.c_str(), src->asInt() );
134 else if( src->Type == NLMISC::CConfigFile::CVar::T_REAL )
136 setFloat( src->Name.c_str(), src->asFloat() );
138 else if( src->Type == NLMISC::CConfigFile::CVar::T_STRING )
140 setString( src->Name.c_str(), src->asString() );
146 bool CConfig::save()
150 cf.save();
152 catch (const NLMISC::Exception &e)
154 nlwarning( "%s", e.what() );
155 return false;
157 return true;
160 bool CConfig::getBool( const char *key )
162 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
164 if( var != NULL )
166 return var->asBool();
168 else
170 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
171 return false;
175 sint32 CConfig::getInt( const char *key )
177 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
179 if( var != NULL )
181 return var->asInt();
183 else
185 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
186 return 0;
190 float CConfig::getFloat( const char *key )
192 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
194 if( var != NULL )
196 return var->asFloat();
198 else
200 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
201 return 0.0f;
205 std::string CConfig::getString( const char *key )
207 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
209 if( var != NULL )
211 return var->asString();
213 else
215 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
216 return "";
220 void CConfig::setBool( const char *key, bool value )
222 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
224 if( var != NULL )
226 if( var->Type == NLMISC::CConfigFile::CVar::T_BOOL )
228 if( value )
229 var->setAsString( "true" );
230 else
231 var->setAsString( "false" );
233 else
235 nlwarning( "Key %s in %s is not a boolean.", key, cf.getFilename().c_str() );
238 else
240 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
244 void CConfig::setInt(const char *key, sint32 value)
246 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
248 if( var != NULL )
250 if( var->Type == NLMISC::CConfigFile::CVar::T_INT )
251 var->setAsInt( value );
252 else
253 nlwarning( "Key %s in %s is not an integer.", key, cf.getFilename().c_str() );
255 else
257 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
261 void CConfig::setFloat( const char *key, float value )
263 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
265 if( var != NULL )
267 if( var->Type == NLMISC::CConfigFile::CVar::T_REAL )
268 var->setAsFloat( value );
269 else
270 nlwarning( "Key %s in %s is not a float.", key, cf.getFilename().c_str() );
272 else
274 nlwarning( "Couldn't find key %s in %s.", key, cf.getFilename().c_str() );
278 void CConfig::setString( const char *key, const std::string &value )
280 NLMISC::CConfigFile::CVar *var = cf.getVarPtr( key );
282 if( var != NULL )
284 if( var->Type == NLMISC::CConfigFile::CVar::T_STRING )
285 var->setAsString( value );
286 else
287 nlwarning( "Key %s in %s is not a string.", key, cf.getFilename().c_str() );
289 else
291 NLMISC::CConfigFile::CVar var;
292 var.forceAsString(value);
293 cf.insertVar(key, var);