1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : QAD_Config.cxx
25 // Author : Nicolas REJNERI
30 \class QAD_Config QAD_Config.h
31 \brief Settings file management for QAD-based application.
34 #include "QAD_Config.h"
35 #include "QAD_ParserSettings.h"
38 #include <qapplication.h>
41 #include <qtextstream.h>
46 QAD_Config provides access to global settings.
48 QAD_Config* QAD_Config::theOneAndOnlyConfig=0;
52 Creates a new one on first call.
54 QAD_Config* QAD_Config::getConfig()
56 if(theOneAndOnlyConfig==0) {
57 theOneAndOnlyConfig = new QAD_Config;
58 theOneAndOnlyConfig->ini();
60 return theOneAndOnlyConfig;
68 QAD_Config::QAD_Config()
69 : QAD_Settings('=', '"', '"')
76 QAD_Config::~QAD_Config()
81 Initializes the config object (only called once).
83 void QAD_Config::ini()
89 Creates not existing config files.
91 bool QAD_Config::createConfigFile( bool overwrite )
96 setConfigDir(QDir(prgDir.absPath()));
98 setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
101 // Create config directory:
102 if(!configDir.exists()) {
103 if(!configDir.mkdir(configDir.absPath(), true)) {
104 // Can't create directory for config file!
109 // Create ".<app>.conf":
110 QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
112 if(!configFile.exists() || overwrite) {
113 if(configFile.open(IO_WriteOnly)) { // file opened successfully
114 QTextStream ts(&configFile);
115 QAD_Setting* setting;
118 "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
119 "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
124 QStringList sectionList; // List of all sections
126 // Collect section list:
127 for(setting=settingList.first(); setting!=0; setting=settingList.next()) {
128 sep = setting->getName().find( ':' );
129 section = setting->getName().left( sep );
131 if( sectionList.find( section ) == sectionList.end() ) {
132 sectionList += section;
138 // Write the sections:
139 for( QStringList::Iterator it = sectionList.begin(); it!=sectionList.end(); ++it ) {
140 ts << "\n[" << (*it) << "]\n";
141 for( setting=settingList.first(); setting!=0; setting=settingList.next() ) {
142 sep = setting->getName().find( ':' );
143 section = setting->getName().left( sep );
145 if( section==(*it) ) {
146 variable = setting->getName().right( setting->getName().length()-sep-1 );
147 ts << variable << "=\"" << setting->getValue() << "\"\n";
165 Reads the config file.
167 bool QAD_Config::readConfigFile()
170 setConfigDir(QDir(prgDir.absPath()));
172 setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
176 configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
178 int i=0, j, l=0; // Index, length of matching string
179 QRegExp regSection("\\[[^]]*\\]"); // Reg exp for a section including brackets
180 QRegExp regName("[^=[]*"); // Reg exp for a setting name (lvalue)
181 QRegExp regValue("\"[^\"]*\""); // Reg exp for a setting value (rvalue) including quote marks
182 QString lSectionName; // Section name excluding brackets
183 QString setName; // Setting name
184 QString setValue; // Setting value
186 // Get file contents without comments:
187 QString cont = QAD_ParserSettings::getContents(configPath, false);
190 // Read next section (name/contents):
191 i=regSection.match(cont, i, &l);
193 lSectionName = cont.mid(i+1, l-2);
196 // Read next setting:
198 j=regName.match(cont, i, &l);
200 setName = cont.mid(j, l);
201 if(setName.stripWhiteSpace().isEmpty()) break;
204 j=regValue.match(cont, i, &l);
206 setValue = cont.mid(j+1, l-2);
209 addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
215 // Add some values which were not saved in config file: