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 Set default directory for config files.
91 void QAD_Config::setDefaultConfigDir()
94 setConfigDir(QDir(prgDir.absPath()));
96 QString vers = tr("INF_VERSION");
97 int first_dot = vers.find('.');
98 int blanc_before = vers.findRev(' ', first_dot) + 1;
99 int blanc_after = vers.find(' ', first_dot);
100 if (blanc_after == -1) blanc_after = vers.length();
101 int vers_len = blanc_after - blanc_before;
102 vers.truncate(blanc_after);
103 QString vers_nb = vers.right(vers_len);
104 setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") + "_" + vers_nb));
109 Creates not existing config files.
111 bool QAD_Config::createConfigFile( bool overwrite )
115 setDefaultConfigDir();
117 // Create config directory:
118 if(!configDir.exists()) {
119 if(!configDir.mkdir(configDir.absPath(), true)) {
120 // Can't create directory for config file!
125 // Create ".<app>.conf":
126 QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
128 if(!configFile.exists() || overwrite) {
129 if(configFile.open(IO_WriteOnly)) { // file opened successfully
130 QTextStream ts(&configFile);
131 QAD_Setting* setting;
134 "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
135 "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
140 QStringList sectionList; // List of all sections
142 // Collect section list:
143 for(setting=settingList.first(); setting!=0; setting=settingList.next()) {
144 sep = setting->getName().find( ':' );
145 section = setting->getName().left( sep );
147 if( sectionList.find( section ) == sectionList.end() ) {
148 sectionList += section;
154 // Write the sections:
155 for( QStringList::Iterator it = sectionList.begin(); it!=sectionList.end(); ++it ) {
156 ts << "\n[" << (*it) << "]\n";
157 for( setting=settingList.first(); setting!=0; setting=settingList.next() ) {
158 sep = setting->getName().find( ':' );
159 section = setting->getName().left( sep );
161 if( section==(*it) ) {
162 variable = setting->getName().right( setting->getName().length()-sep-1 );
163 ts << variable << "=\"" << setting->getValue() << "\"\n";
181 Reads the config file.
183 bool QAD_Config::readConfigFile()
185 setDefaultConfigDir();
188 configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
190 int i=0, j, l=0; // Index, length of matching string
191 QRegExp regSection("\\[[^]]*\\]"); // Reg exp for a section including brackets
192 QRegExp regName("[^=[]*"); // Reg exp for a setting name (lvalue)
193 QRegExp regValue("\"[^\"]*\""); // Reg exp for a setting value (rvalue) including quote marks
194 QString lSectionName; // Section name excluding brackets
195 QString setName; // Setting name
196 QString setValue; // Setting value
198 // Get file contents without comments:
199 QString cont = QAD_ParserSettings::getContents(configPath, false);
202 // Read next section (name/contents):
203 i=regSection.match(cont, i, &l);
205 lSectionName = cont.mid(i+1, l-2);
208 // Read next setting:
210 j=regName.match(cont, i, &l);
212 setName = cont.mid(j, l);
213 if(setName.stripWhiteSpace().isEmpty()) break;
216 j=regValue.match(cont, i, &l);
218 setValue = cont.mid(j+1, l-2);
221 addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
227 // Add some values which were not saved in config file: