Salome HOME
PAL8065
[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     Set default directory for config files.
90 */
91 void QAD_Config::setDefaultConfigDir()
92 {
93 #ifdef DEF_WINDOWS
94   setConfigDir(QDir(prgDir.absPath()));
95 #else
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));
105 #endif
106 }
107
108 /*!
109     Creates not existing config files.
110 */
111 bool QAD_Config::createConfigFile( bool overwrite )
112 {
113   bool ret = true;
114
115   setDefaultConfigDir();
116
117   // Create config directory:
118   if(!configDir.exists()) {
119     if(!configDir.mkdir(configDir.absPath(), true)) {
120       // Can't create directory for config file!
121       return false;
122     }
123   }
124   
125   // Create ".<app>.conf":
126   QFile configFile(configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf");
127
128   if(!configFile.exists() || overwrite) {
129     if(configFile.open(IO_WriteOnly)) {    // file opened successfully
130       QTextStream ts(&configFile);
131       QAD_Setting* setting;
132
133       ts <<
134         "# This file is automatically generated by " << tr("MEN_APPNAME") << ".\n"
135         "# Please edit only if " << tr("MEN_APPNAME") << " is not running.\n";
136       
137       int sep;
138       QString section;
139       QString variable;
140       QStringList sectionList;      // List of all sections
141
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 );
146
147         if( sectionList.find( section ) == sectionList.end() ) {
148           sectionList += section;
149         }
150       }
151
152       sectionList.sort();
153
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 );
160
161           if( section==(*it) ) {
162             variable = setting->getName().right( setting->getName().length()-sep-1 );
163             ts << variable << "=\"" << setting->getValue() << "\"\n";
164           }
165         }
166       }
167
168       configFile.close();
169     }
170
171     else {
172       // Can't create file
173       ret=false;
174     }
175   }
176
177   return ret;
178 }
179
180 /*!
181    Reads the config file.
182 */
183 bool QAD_Config::readConfigFile()
184 {
185   setDefaultConfigDir();
186
187   QString configPath;
188   configPath = configDir.absPath() + "/" + tr("MEN_APPNAME") + ".conf";
189
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
197
198   // Get file contents without comments:
199   QString cont = QAD_ParserSettings::getContents(configPath, false);
200
201   do {
202     // Read next section (name/contents):
203     i=regSection.match(cont, i, &l);
204     if(i==-1) break;
205     lSectionName = cont.mid(i+1, l-2);
206     i+=l;
207
208     // Read next setting:
209     do {
210       j=regName.match(cont, i, &l);
211       if(j==-1) break;
212       setName = cont.mid(j, l);
213       if(setName.stripWhiteSpace().isEmpty()) break;
214       i=j+l;
215
216       j=regValue.match(cont, i, &l);
217       if(j==-1) break;
218       setValue = cont.mid(j+1, l-2);
219       i=j+l;
220
221       addSetting(lSectionName + ":" + setName.stripWhiteSpace(), setValue);
222
223     } while(true);
224
225   } while(true);
226
227   // Add some values which were not saved in config file:
228
229   return false;
230 }
231
232
233 // EOF