1 // File : SALOME_XmlHandler.cxx
2 // Created : Thu Dec 18 12:01:00 2002
3 // Author : Vadim SANDLER, Open CASCADE SAS (vadim.sandler@opencascade.com)
5 // Module : Installation Wizard
6 // Copyright : 2002-2007 CEA
10 #include "SALOME_XmlHandler.hxx"
11 #include "SALOME_ProductsView.hxx"
12 #include "SALOME_InstallWizard.hxx"
14 #include <qlineedit.h>
17 #include <qstringlist.h>
20 // ================================================================
22 * ::isBoolAttributeSet [ static ]
23 * Returns true if the attribute stores boolean value and
24 * corresponds to True value
26 // ================================================================
27 static bool isBoolAttributeSet( const QString& attr ) {
28 return ( attr.lower() == "true" ||
29 attr.lower() == "yes" ||
30 attr.lower() == "ok" ||
31 ( !attr.stripWhiteSpace().isEmpty() && attr.toInt() != 0 ) );
33 // ================================================================
35 * ::environmentVariable [ static ]
36 * Seraches for the environment variable and returns it's
37 * position on the given string
39 // ================================================================
40 QString environmentVariable( const QString& str, int& start, int& len ) {
41 QString varName = QString::null;
44 // Environment variable can be given in the form:
49 // The first symbol should be the letter.
50 QRegExp rx( "\\$\\{([a-zA-Z]+[a-zA-Z0-9_]*)\\}|\\$\\(([a-zA-Z]+[a-zA-Z0-9_]*)\\)|\\$([a-zA-Z]+[a-zA-Z0-9_]*)|\\%([a-zA-Z]+[a-zA-Z0-9_]*)\\%" );
52 int pos = rx.search( str, start );
56 len = rx.matchedLength();
57 QStringList caps = rx.capturedTexts();
58 for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
59 varName = *caps.at( i );
63 // ================================================================
65 * ::substituteVars [ static ]
66 * Substitutes environment variables in the given string
69 // ================================================================
70 static QString substituteVars( const QString& str ) {
73 int start( 0 ), len( 0 );
75 QString envName = environmentVariable( res, start, len );
76 if ( envName.isNull() )
79 QString newStr = QString::null;
80 if ( ::getenv( envName ) )
81 newStr = QString( ::getenv( envName ) );
83 res.replace( start, len, newStr );
89 // ================================================================
91 * StructureParser::StructureParser
94 // ================================================================
95 StructureParser::StructureParser( SALOME_InstallWizard* wizard )
103 // ================================================================
105 * StructureParser::setProductsLists
106 * Sets products list view
108 // ================================================================
109 void StructureParser::setProductsLists( ProductsView* mtree, ProductsView* ptree )
114 // ================================================================
116 * StructureParser::setTargetDir
117 * Sets target directory widget
119 // ================================================================
120 void StructureParser::setTargetDir( QLineEdit* dir )
122 QString home = QDir::homeDirPath();
124 if ( myTargetDir && !home.isEmpty() )
125 myTargetDir->setText( home + QDir::separator() + "salome" );
127 // ================================================================
129 * StructureParser::setTempDir
130 * Sets temp directory widget
132 // ================================================================
133 void StructureParser::setTempDir( QLineEdit* dir )
137 myTempDir->setText( "/tmp" );
139 // ================================================================
141 * StructureParser::getConfigInfo
142 * Parse 'config' part of the XML file
144 // ================================================================
145 void StructureParser::getConfigInfo(const QDomElement &theElem)
147 QString myVersion, myCaption, myCopyright, myLicense, myPlatforms;
148 if ( theElem.attribute( "version" ) ) {
149 myVersion = theElem.attribute( "version" ).stripWhiteSpace();
150 if ( myWizard && !myVersion.isEmpty() )
151 myWizard->setVersion( myVersion );
153 if ( theElem.attribute( "caption" ) ) {
154 myCaption = theElem.attribute( "caption" ).arg( myVersion ).stripWhiteSpace();
155 if ( myWizard && !myCaption.isEmpty() )
156 myWizard->setCaption( myCaption );
158 if ( theElem.attribute( "copyright" ) ) {
159 myCopyright = theElem.attribute( "copyright" ).stripWhiteSpace();
160 if ( myWizard && !myCopyright.isEmpty() )
161 myWizard->setCopyright( myCopyright );
163 if ( theElem.attribute( "license" ) ) {
164 myLicense = theElem.attribute( "license" ).stripWhiteSpace();
165 if ( myWizard && !myLicense.isEmpty() )
166 myWizard->setLicense( myLicense );
168 if ( theElem.attribute( "platforms" ) ) {
169 // myPlatforms = theElem.attribute( "platforms" ).stripWhiteSpace();
170 // if ( myWizard && !myPlatforms.isEmpty() )
171 // myWizard->setPlatforms( myPlatforms );
173 if ( theElem.attribute( "targetdir" ) ) {
175 myTargetDir->setText( substituteVars( theElem.attribute( "targetdir" ) ) );
177 if ( theElem.attribute( "tempdir" ) ) {
179 myTempDir->setText( substituteVars( theElem.attribute( "tempdir" ) ) );
182 // ================================================================
184 * StructureParser::getButtonsInfo
185 * Parse 'buttons' part of the XML file
187 // ================================================================
188 void StructureParser::getButtonsInfo(const QDomNode &theNode)
190 QString aLabel, aTootip, aScript;
191 for( QDomNode node = theNode.firstChild(); !node.isNull(); node = node.nextSibling() ) {
192 if ( !node.isElement() )
194 QDomElement elem = node.toElement();
195 if ( isBoolAttributeSet( elem.attribute( "disable" ) ) )
197 aLabel = ""; aTootip = ""; aScript = "";
198 if ( elem.attribute( "label" ) )
199 aLabel = elem.attribute( "label" ).stripWhiteSpace();
200 if ( elem.attribute( "tooltip" ) )
201 aTootip = elem.attribute( "tooltip" ).stripWhiteSpace();
202 if ( elem.attribute( "script" ) )
203 aScript = elem.attribute( "script" ).stripWhiteSpace();
204 if ( !aLabel.isEmpty() ) {
205 if ( node == theNode.firstChild() )
206 myWizard->addFinishButton( aLabel, aTootip, aScript, true );
208 myWizard->addFinishButton( aLabel, aTootip, aScript );
212 // ================================================================
214 * StructureParser::getProductsInfo
215 * Parse 'products' part of the XML file
217 // ================================================================
218 void StructureParser::getProductsInfo( const QDomNode &theNode )
220 MapDependencies mapDeps;
221 QCheckListItem* element;
223 QDomNode docNode = theNode.parentNode();
224 getDependenciesInfo( docNode, mapDeps );
226 for( QDomNode prodNode = theNode.firstChild(); !prodNode.isNull(); prodNode = prodNode.nextSibling() ) {
227 if ( !prodNode.isElement() )
229 for( QDomNode instNode = prodNode.firstChild(); !instNode.isNull(); instNode = instNode.nextSibling() ) {
230 if ( !instNode.isElement() )
232 QDomElement instElem = instNode.toElement();
233 if ( instElem.attribute( "os" ) != myWizard->getPlatform() && instElem.attribute( "os" ) != "all" )
235 if ( isBoolAttributeSet( instElem.attribute( "disable" ) ) )
236 break; // current product is skip
237 QDomElement prodElem = prodNode.toElement();
239 QStringList diskspace = QStringList::split( ",", instElem.attribute( "installdiskspace" ) );
240 long binreq = diskspace[ 0 ].toInt();
241 long srcreq = diskspace[ 1 ].toInt();
242 long cmplreq = diskspace[ 2 ].toInt();
244 QString name = prodElem.attribute( "name" ).stripWhiteSpace();
245 QString type = prodElem.attribute( "type" ).stripWhiteSpace().lower();
246 bool pickUp = isBoolAttributeSet( instElem.attribute( "pickupenv" ) );
247 QString script = instElem.attribute( "script" );
248 InstallationType instType = myWizard->getInstType();
249 QStringList typesList = QStringList::split( ",", type );
250 long prodSize = ( instType == Binaries ? (binreq + srcreq) :
251 instType == Sources ? srcreq :
253 if ( typesList.find( "component" ) != typesList.end() ) {
254 element = myModules->addItem( name,
255 QString::number( prodSize ) + " KB",
259 element = myPrereqs->addItem( name,
260 QString::number( prodSize ) + " KB",
262 QString descr = QString::null;
263 if ( prodElem.attribute( "description" ) != "" )
264 descr = prodElem.attribute( "description" ).stripWhiteSpace();
265 myWizard->setDependancies
272 instElem.attribute( "temporarydiskspace" ).toInt(),
273 instElem.attribute( "version" ),
280 // ================================================================
282 * StructureParser::getDependencies
283 * Parse 'dependencies' part of the XML file
285 // ================================================================
286 void StructureParser::getDependenciesInfo( QDomNode &theNode, MapDependencies &theDepends )
288 QDomNode depsNode = theNode.namedItem( "dependencies" );
289 QStringList depsList = QStringList();
290 for( QDomNode prodNode = depsNode.firstChild(); !prodNode.isNull(); prodNode = prodNode.nextSibling() ) {
291 if ( !prodNode.isElement() )
293 QDomElement prodElem = prodNode.toElement();
294 QString prodName = prodElem.attribute( "name" ).stripWhiteSpace();
295 if ( theDepends.contains( prodName ) )
298 for ( QDomNode depNode = prodNode.firstChild(); !depNode.isNull(); depNode = depNode.nextSibling() ) {
299 if ( !depNode.isElement() )
301 QDomElement depElem = depNode.toElement();
302 QString depName = depElem.text();
303 if ( depName.isEmpty() )
305 if ( !depsList.contains( depName ) )
306 depsList.append( depName );
308 theDepends[ prodName ] = depsList;
311 // ================================================================
313 * StructureParser::readXmlFile
314 * Read XML configuration file for the current platform
316 // ================================================================
317 void StructureParser::readXmlFile(QString theFileName)
319 QDomDocument doc( "xml_doc" );
320 QFile file( theFileName );
321 if ( !file.open( IO_ReadOnly ) )
323 if ( !doc.setContent( &file ) ) {
329 QDomElement docElem = doc.documentElement();
331 for( QDomNode node = docElem.firstChild(); !node.isNull(); node = node.nextSibling() ) {
332 QString nodeName = node.nodeName();
333 if ( nodeName == "config" && node.isElement() ) {
334 getConfigInfo( node.toElement() );
336 else if ( nodeName == "buttons" && node.hasChildNodes() && myModules && myWizard ) {
337 getButtonsInfo( node );
339 else if ( nodeName == "products" && node.hasChildNodes() && myModules && myWizard ) {
340 getProductsInfo( node );