Salome HOME
bcc86ffd71ba3c31b4378145dd09f7c8692195e4
[tools/install.git] / src / SALOME_XmlHandler.cxx
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)
4 //  Project   : SALOME
5 //  Module    : Installation Wizard
6 //  Copyright : 2002-2006 CEA
7
8 #include "globals.h"
9
10 #include "SALOME_XmlHandler.hxx"
11 #include "SALOME_ProductsView.hxx"
12 #include "SALOME_InstallWizard.hxx"
13
14 #include <qlineedit.h>
15 #include <qdir.h>
16 #include <qregexp.h>
17
18 // ================================================================
19 /*!
20  *  ::isBoolAttributeSet [ static ]
21  *  Returns true if the attribute stores boolean value and 
22  *  corresponds to True value
23  */
24 // ================================================================
25 static bool isBoolAttributeSet( const QString& attr ) {
26   return ( attr.lower() == "true" || 
27            attr.lower() == "yes"  ||
28            attr.lower() == "ok"   || 
29            ( !attr.stripWhiteSpace().isEmpty() && attr.toInt() != 0 ) );
30 }
31
32 // ================================================================
33 /*!
34  *  ::environmentVariable [ static ]
35  *  Seraches for the environment variable and returns it's
36  *  position on the given string
37  */
38 // ================================================================
39 QString environmentVariable( const QString& str, int& start, int& len ) {
40   QString varName = QString::null;
41   len = 0;
42
43   // Environment variable can be given in the form:
44   // - ${VARIABLE} or
45   // - $(VARIABLE) or 
46   // - $VARIABLE   or
47   // - %VARIABLE%
48   // The first symbol should be the letter.
49   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_]*)\\%" );
50
51   int pos = rx.search( str, start );
52   if ( pos != -1 )
53   {
54     start = pos;
55     len = rx.matchedLength();
56     QStringList caps = rx.capturedTexts();
57     for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
58       varName = *caps.at( i );
59   }
60   return varName;
61 }
62 // ================================================================
63 /*!
64  *  ::substituteVars [ static ]
65  *  Substitutes environment variables in the given string
66  *  by their values
67  */
68 // ================================================================
69 static QString substituteVars( const QString& str ) {
70   QString res = str;
71
72   int start( 0 ), len( 0 );
73   while ( true ) {
74     QString envName = environmentVariable( res, start, len );
75     if ( envName.isNull() )
76       break;
77
78     QString newStr = QString::null;
79     if ( ::getenv( envName ) )
80       newStr = QString( ::getenv( envName ) );
81
82     res.replace( start, len, newStr );
83   }
84
85   return res;
86 }
87
88 // ================================================================
89 /*!
90  *  StructureParser::StructureParser
91  *  Constructor
92  */
93 // ================================================================
94 StructureParser::StructureParser( SALOME_InstallWizard* wizard )
95      : QXmlDefaultHandler(), 
96        myWizard( wizard ), 
97        myTree( 0 ), 
98        myTargetDir( 0 ), 
99        myTempDir( 0 )
100 {
101 }
102 // ================================================================
103 /*!
104  *  StructureParser::setProductsList
105  *  Sets products list view
106  */
107 // ================================================================
108 void StructureParser::setProductsList( ProductsView* tree )
109 {
110   myTree = tree;
111 }
112 // ================================================================
113 /*!
114  *  StructureParser::setTargetDir
115  *  Sets target directory widget
116  */
117 // ================================================================
118 void StructureParser::setTargetDir( QLineEdit* dir )
119 {
120   QString home = QDir::homeDirPath();
121   myTargetDir = dir;
122   if ( myTargetDir && !home.isEmpty() )
123     myTargetDir->setText( home + QDir::separator() + "salome" );
124 }
125 // ================================================================
126 /*!
127  *  StructureParser::setTempDir
128  *  Sets temp directory widget
129  */
130 // ================================================================
131 void StructureParser::setTempDir( QLineEdit* dir )
132 {
133   myTempDir = dir;
134   if ( myTempDir )
135     myTempDir->setText( "/tmp" );
136 }
137 // ================================================================
138 /*!
139  *  StructureParser::startElement
140  *  Begins parsing of the xml dom-element
141  */  
142 // ================================================================
143 bool StructureParser::startElement( const QString&        /*namespaceURI*/,
144                                     const QString&        /*localName*/,
145                                     const QString&        qName,
146                                     const QXmlAttributes& attributes)
147 {
148   ___MESSAGE___( qName );
149   ___MESSAGE___( attributes.length() );
150   QCheckListItem* element;
151   if (( qName == "config" ) && ( attributes.length() > 0 ) ) {
152     QString myVersion, myCaption, myCopyright, myLicense, myOS;
153     if ( attributes.value( "version" ) ) {
154       myVersion = attributes.value( "version" ).stripWhiteSpace();
155       if ( myWizard && !myVersion.isEmpty() ) 
156         myWizard->setVersion( myVersion );
157     }
158     if ( attributes.value( "caption" ) ) {
159       myCaption = attributes.value( "caption" ).arg( myVersion ).stripWhiteSpace();
160       if ( myWizard && !myCaption.isEmpty() ) 
161         myWizard->setCaption( myCaption );
162     }
163     if ( attributes.value( "copyright" ) ) {
164       myCopyright = attributes.value( "copyright" ).stripWhiteSpace();
165       if ( myWizard && !myCopyright.isEmpty() ) 
166         myWizard->setCopyright( myCopyright );
167     }
168     if ( attributes.value( "license" ) ) {
169       myLicense = attributes.value( "license" ).stripWhiteSpace();
170       if ( myWizard && !myLicense.isEmpty() ) 
171         myWizard->setLicense( myLicense );
172     }
173     if ( attributes.value( "os" ) ) {
174       myOS = attributes.value( "os" ).stripWhiteSpace();
175       if ( myWizard && !myOS.isEmpty() ) 
176         myWizard->setOS( myOS );
177     }
178   } 
179   else if (( qName == "button" ) && ( attributes.length() > 0 ) && myTree && myWizard ) {
180     if ( isBoolAttributeSet( attributes.value( "disable" ) ) )
181       return true;
182
183     QString myLabel, myTootip, myScript;
184     if ( attributes.value( "label" ) )
185       myLabel = attributes.value( "label" ).stripWhiteSpace();
186     if ( attributes.value( "tooltip" ) )
187       myTootip = attributes.value( "tooltip" ).stripWhiteSpace();
188     if ( attributes.value( "script" ) )
189       myScript = attributes.value( "script" ).stripWhiteSpace();
190     if ( !myLabel.isEmpty() )
191       myWizard->addFinishButton( myLabel, myTootip, myScript );
192   }
193   else if (( qName == "product" ) && ( attributes.length() > 0 ) && myTree && myWizard ) {
194     if ( isBoolAttributeSet( attributes.value( "disable" ) ) )
195       return true;
196     
197     QString install = attributes.value( "install" );
198     QStringList supported = QStringList::split( ",", attributes.value( "supported" ) );
199     QString script = attributes.value( "script" );
200     QStringList deps = QStringList();
201     if ( attributes.value( "dependancies" ) != "" )
202       deps = QStringList::split( ",", attributes.value( "dependancies" ), false );
203     element = myTree->addItem( attributes.value( "name" ), attributes.value( "version" ), install, supported, script );
204     QStringList diskspace = QStringList::split(",",attributes.value( "installdiskspace" ) );
205     QString descr = QString::null;
206     if ( attributes.value( "description" ) != "" )
207       descr = attributes.value( "description" ).stripWhiteSpace();
208     QString ctx = QString::null;
209     if ( attributes.value( "context" ) != "" )
210       ctx = attributes.value( "context" ).stripWhiteSpace().lower();
211     bool pickUp = isBoolAttributeSet( attributes.value( "pickupenv" ) );
212     myWizard->setDependancies( element, 
213                                Dependancies( attributes.value( "name" ), 
214                                              deps,
215                                              ( diskspace.count() > 0 ? diskspace[ 0 ].toInt() : 0 ), 
216                                              ( diskspace.count() > 1 ? diskspace[1].toInt() : ( diskspace.count() > 0 ? diskspace[0].toInt() : 0 ) ), 
217                                              attributes.value( "temporarydiskspace" ).toInt(),
218                                              install,
219                                              descr,
220                                              ctx,
221                                              pickUp ) );
222   }
223   else if (( qName == "path" ) && ( attributes.length() > 0 ) && myWizard ) {
224     if ( myTargetDir )
225       myTargetDir->setText( substituteVars( attributes.value( "targetdir" ) ) );
226
227     if ( myTempDir ) {
228       if ( !attributes.value( "tempdir" ).stripWhiteSpace().isEmpty() )
229         myTempDir->setText( substituteVars( attributes.value( "tempdir" ) ) );
230     }
231   }
232   return true;
233 }
234 // ================================================================
235 /*!
236  *  StructureParser::endElement
237  *  Finishes parsing of the xml dom-element
238  */
239 // ================================================================
240 bool StructureParser::endElement( const QString&, 
241                                   const QString&,
242                                   const QString& )
243 {
244   return true;
245 }
246