Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/kernel.git] / src / SALOMEGUI / QAD_Config.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_Config.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 /*!
30   \class QAD_Config QAD_Config.h
31   \brief Settings file management for QAD-based application.
32 */
33
34 #include "QAD_Config.h"
35 #include "QAD_ParserSettings.h"
36
37 // QT Includes
38 #include <qapplication.h>
39 #include <qfile.h>
40 #include <qregexp.h>
41 #include <qtextstream.h>
42 using namespace std;
43
44
45 /*!
46     QAD_Config provides access to global settings.
47 */
48 QAD_Config* QAD_Config::theOneAndOnlyConfig=0;
49
50
51 /*!
52     Creates a new one on first call.
53 */
54 QAD_Config* QAD_Config::getConfig()
55 {
56   if(theOneAndOnlyConfig==0) {
57     theOneAndOnlyConfig = new QAD_Config;
58     theOneAndOnlyConfig->ini();
59   }
60   return theOneAndOnlyConfig;
61 }
62
63
64
65 /*!
66     Constructor.
67 */
68 QAD_Config::QAD_Config()
69  : QAD_Settings('=', '"', '"')
70 {
71 }
72
73 /*!
74     Destructor.
75 */
76 QAD_Config::~QAD_Config()
77 {
78 }
79
80 /*!
81    Initializes the config object (only called once).
82 */
83 void QAD_Config::ini()
84 {
85 }
86
87
88 /*!
89     Creates not existing config files.
90 */
91 bool QAD_Config::createConfigFile( bool overwrite )
92 {
93   bool ret=true;
94
95 #ifdef DEF_WINDOWS
96   setConfigDir(QDir(prgDir.absPath()));
97 #else
98   setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
99 #endif
100
101   // Create config directory:
102   if(!configDir.exists()) {
103     if(!configDir.mkdir(configDir.absPath(), true)) {
104       // Can't create directory for config file!
105       return false;
106     }
107   }
108   
109   // Create ".<app>.conf":
110   QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
111
112   if(!configFile.exists() || overwrite) {
113     if(configFile.open(IO_WriteOnly)) {    // file opened successfully
114       QTextStream ts(&configFile);
115       QAD_Setting* setting;
116
117       ts <<
118         "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
119         "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
120       
121       int sep;
122       QString section;
123       QString variable;
124       QStringList sectionList;      // List of all sections
125
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 );
130
131         if( sectionList.find( section ) == sectionList.end() ) {
132           sectionList += section;
133         }
134       }
135
136       sectionList.sort();
137
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 );
144
145           if( section==(*it) ) {
146             variable = setting->getName().right( setting->getName().length()-sep-1 );
147             ts << variable << "=\"" << setting->getValue() << "\"\n";
148           }
149         }
150       }
151
152       configFile.close();
153     }
154
155     else {
156       // Can't create file
157       ret=false;
158     }
159   }
160
161   return ret;
162 }
163
164 /*!
165    Reads the config file.
166 */
167 bool QAD_Config::readConfigFile()
168 {
169 #ifdef DEF_WINDOWS
170   setConfigDir(QDir(prgDir.absPath()));
171 #else
172   setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
173 #endif
174
175   QString configPath;
176   configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
177
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
185
186   // Get file contents without comments:
187   QString cont = QAD_ParserSettings::getContents(configPath, false);
188
189   do {
190     // Read next section (name/contents):
191     i=regSection.match(cont, i, &l);
192     if(i==-1) break;
193     lSectionName = cont.mid(i+1, l-2);
194     i+=l;
195
196     // Read next setting:
197     do {
198       j=regName.match(cont, i, &l);
199       if(j==-1) break;
200       setName = cont.mid(j, l);
201       if(setName.stripWhiteSpace().isEmpty()) break;
202       i=j+l;
203
204       j=regValue.match(cont, i, &l);
205       if(j==-1) break;
206       setValue = cont.mid(j+1, l-2);
207       i=j+l;
208
209       addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
210
211     } while(true);
212
213   } while(true);
214
215   // Add some values which were not saved in config file:
216
217   return false;
218 }
219
220
221 // EOF