Salome HOME
NRI : Update in order to search MODULES documentation and open active MODULE document...
[modules/kernel.git] / src / SALOMEGUI / QAD_Config.cxx
1 using namespace std;
2 //  File      : QAD_Config.cxx
3 //  Created   : Tue Sep 04 09:58:34 2001
4 //  Author    : Nicolas REJNERI
5 //  Project   : SALOME
6 //  Module    : SALOMEGUI
7 //  Copyright : Open CASCADE 2001
8 //  $Header$
9
10
11 /*!
12   \class QAD_Config QAD_Config.h
13   \brief Settings file management for QAD-based application.
14 */
15
16 #include "QAD_Config.h"
17 #include "QAD_ParserSettings.h"
18
19 // QT Includes
20 #include <qapplication.h>
21 #include <qfile.h>
22 #include <qregexp.h>
23 #include <qtextstream.h>
24
25
26 /*!
27     QAD_Config provides access to global settings.
28 */
29 QAD_Config* QAD_Config::theOneAndOnlyConfig=0;
30
31
32 /*!
33     Creates a new one on first call.
34 */
35 QAD_Config* QAD_Config::getConfig()
36 {
37   if(theOneAndOnlyConfig==0) {
38     theOneAndOnlyConfig = new QAD_Config;
39     theOneAndOnlyConfig->ini();
40   }
41   return theOneAndOnlyConfig;
42 }
43
44
45
46 /*!
47     Constructor.
48 */
49 QAD_Config::QAD_Config()
50  : QAD_Settings('=', '"', '"')
51 {
52 }
53
54 /*!
55     Destructor.
56 */
57 QAD_Config::~QAD_Config()
58 {
59 }
60
61 /*!
62    Initializes the config object (only called once).
63 */
64 void QAD_Config::ini()
65 {
66 }
67
68
69 /*!
70     Creates not existing config files.
71 */
72 bool QAD_Config::createConfigFile( bool overwrite )
73 {
74   bool ret=true;
75
76 #ifdef DEF_WINDOWS
77   setConfigDir(QDir(prgDir.absPath()));
78 #else
79   setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
80 #endif
81
82   // Create config directory:
83   if(!configDir.exists()) {
84     if(!configDir.mkdir(configDir.absPath(), true)) {
85       // Can't create directory for config file!
86       return false;
87     }
88   }
89   
90   // Create ".<app>.conf":
91   QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
92
93   if(!configFile.exists() || overwrite) {
94     if(configFile.open(IO_WriteOnly)) {    // file opened successfully
95       QTextStream ts(&configFile);
96       QAD_Setting* setting;
97
98       ts <<
99         "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
100         "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
101       
102       int sep;
103       QString section;
104       QString variable;
105       QStringList sectionList;      // List of all sections
106
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 );
111
112         if( sectionList.find( section ) == sectionList.end() ) {
113           sectionList += section;
114         }
115       }
116
117       sectionList.sort();
118
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 );
125
126           if( section==(*it) ) {
127             variable = setting->getName().right( setting->getName().length()-sep-1 );
128             ts << variable << "=\"" << setting->getValue() << "\"\n";
129           }
130         }
131       }
132
133       configFile.close();
134     }
135
136     else {
137       // Can't create file
138       ret=false;
139     }
140   }
141
142   return ret;
143 }
144
145 /*!
146    Reads the config file.
147 */
148 bool QAD_Config::readConfigFile()
149 {
150 #ifdef DEF_WINDOWS
151   setConfigDir(QDir(prgDir.absPath()));
152 #else
153   setConfigDir(QDir(QDir::home().absPath() + "/." + tr("MEN_APPNAME") ));
154 #endif
155
156   QString configPath;
157   configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
158
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
166
167   // Get file contents without comments:
168   QString cont = QAD_ParserSettings::getContents(configPath, false);
169
170   do {
171     // Read next section (name/contents):
172     i=regSection.match(cont, i, &l);
173     if(i==-1) break;
174     lSectionName = cont.mid(i+1, l-2);
175     i+=l;
176
177     // Read next setting:
178     do {
179       j=regName.match(cont, i, &l);
180       if(j==-1) break;
181       setName = cont.mid(j, l);
182       if(setName.stripWhiteSpace().isEmpty()) break;
183       i=j+l;
184
185       j=regValue.match(cont, i, &l);
186       if(j==-1) break;
187       setValue = cont.mid(j+1, l-2);
188       i=j+l;
189
190       addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
191
192     } while(true);
193
194   } while(true);
195
196   // Add some values which were not saved in config file:
197
198   return false;
199 }
200
201
202 // EOF