2 // File : QAD_Config.cxx
3 // Created : Tue Sep 04 09:58:34 2001
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE 2001
12 \class QAD_Config QAD_Config.h
13 \brief Settings file management for QAD-based application.
16 #include "QAD_Config.h"
17 #include "QAD_ParserSettings.h"
20 #include <qapplication.h>
23 #include <qtextstream.h>
27 QAD_Config provides access to global settings.
29 QAD_Config* QAD_Config::theOneAndOnlyConfig=0;
33 Creates a new one on first call.
35 QAD_Config* QAD_Config::getConfig()
37 if(theOneAndOnlyConfig==0) {
38 theOneAndOnlyConfig = new QAD_Config;
39 theOneAndOnlyConfig->ini();
41 return theOneAndOnlyConfig;
49 QAD_Config::QAD_Config()
50 : QAD_Settings('=', '"', '"')
57 QAD_Config::~QAD_Config()
62 Initializes the config object (only called once).
64 void QAD_Config::ini()
70 Creates not existing config files.
72 bool QAD_Config::createConfigFile( bool overwrite )
77 setConfigDir(QDir(prgDir.absPath()));
79 setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
82 // Create config directory:
83 if(!configDir.exists()) {
84 if(!configDir.mkdir(configDir.absPath(), true)) {
85 // Can't create directory for config file!
90 // Create ".<app>.conf":
91 QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
93 if(!configFile.exists() || overwrite) {
94 if(configFile.open(IO_WriteOnly)) { // file opened successfully
95 QTextStream ts(&configFile);
99 "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
100 "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
105 QStringList sectionList; // List of all sections
107 // Collect section list:
108 for(setting=settingList.first(); setting!=0; setting=settingList.next()) {
109 sep = setting->getName().find( ':' );
110 section = setting->getName().left( sep );
112 if( sectionList.find( section ) == sectionList.end() ) {
113 sectionList += section;
119 // Write the sections:
120 for( QStringList::Iterator it = sectionList.begin(); it!=sectionList.end(); ++it ) {
121 ts << "\n[" << (*it) << "]\n";
122 for( setting=settingList.first(); setting!=0; setting=settingList.next() ) {
123 sep = setting->getName().find( ':' );
124 section = setting->getName().left( sep );
126 if( section==(*it) ) {
127 variable = setting->getName().right( setting->getName().length()-sep-1 );
128 ts << variable << "=\"" << setting->getValue() << "\"\n";
146 Reads the config file.
148 bool QAD_Config::readConfigFile()
151 setConfigDir(QDir(prgDir.absPath()));
153 setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
157 configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
159 int i=0, j, l=0; // Index, length of matching string
160 QRegExp regSection("\\[[^]]*\\]"); // Reg exp for a section including brackets
161 QRegExp regName("[^=[]*"); // Reg exp for a setting name (lvalue)
162 QRegExp regValue("\"[^\"]*\""); // Reg exp for a setting value (rvalue) including quote marks
163 QString lSectionName; // Section name excluding brackets
164 QString setName; // Setting name
165 QString setValue; // Setting value
167 // Get file contents without comments:
168 QString cont = QAD_ParserSettings::getContents(configPath, false);
171 // Read next section (name/contents):
172 i=regSection.match(cont, i, &l);
174 lSectionName = cont.mid(i+1, l-2);
177 // Read next setting:
179 j=regName.match(cont, i, &l);
181 setName = cont.mid(j, l);
182 if(setName.stripWhiteSpace().isEmpty()) break;
185 j=regValue.match(cont, i, &l);
187 setValue = cont.mid(j+1, l-2);
190 addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
196 // Add some values which were not saved in config file: