Salome HOME
New version of doxygen (1.4.6) is introduced
[tools/install.git] / src / SALOME_XmlHandler.cxx
index 0be7084a7e36bc7a5cf33ae895733bad0b81b242..dc6533aecc89a8800889fddf04a0bd44a60c189e 100644 (file)
 #include "SALOME_InstallWizard.hxx"
 
 #include <qlineedit.h>
+#include <qdir.h>
+#include <qregexp.h>
 
-bool isBoolAttributeSet( const QString& attr ) {
-  return ( attr == "true" || attr == "yes" || attr == "ok" || ( !attr.stripWhiteSpace().isEmpty() && attr.toInt() != 0 ) );
+// ================================================================
+/*!
+ *  ::isBoolAttributeSet [ static ]
+ *  Returns true if the attribute stores boolean value and 
+ *  corresponds to True value
+ */
+// ================================================================
+static bool isBoolAttributeSet( const QString& attr ) {
+  return ( attr.lower() == "true" || 
+          attr.lower() == "yes"  ||
+          attr.lower() == "ok"   || 
+          ( !attr.stripWhiteSpace().isEmpty() && attr.toInt() != 0 ) );
+}
+
+// ================================================================
+/*!
+ *  ::environmentVariable [ static ]
+ *  Seraches for the environment variable and returns it's
+ *  position on the given string
+ */
+// ================================================================
+QString environmentVariable( const QString& str, int& start, int& len ) {
+  QString varName = QString::null;
+  len = 0;
+
+  // Environment variable can be given in the form:
+  // - ${VARIABLE} or
+  // - $(VARIABLE) or 
+  // - $VARIABLE   or
+  // - %VARIABLE%
+  // The first symbol should be the letter.
+  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_]*)\\%" );
+
+  int pos = rx.search( str, start );
+  if ( pos != -1 )
+  {
+    start = pos;
+    len = rx.matchedLength();
+    QStringList caps = rx.capturedTexts();
+    for ( uint i = 1; i <= caps.count() && varName.isEmpty(); i++ )
+      varName = *caps.at( i );
+  }
+  return varName;
+}
+// ================================================================
+/*!
+ *  ::substituteVars [ static ]
+ *  Substitutes environment variables in the given string
+ *  by their values
+ */
+// ================================================================
+static QString substituteVars( const QString& str ) {
+  QString res = str;
+
+  int start( 0 ), len( 0 );
+  while ( true ) {
+    QString envName = environmentVariable( res, start, len );
+    if ( envName.isNull() )
+      break;
+
+    QString newStr = QString::null;
+    if ( ::getenv( envName ) )
+      newStr = QString( ::getenv( envName ) );
+
+    res.replace( start, len, newStr );
+  }
+
+  return res;
 }
 
 // ================================================================
@@ -49,7 +117,10 @@ void StructureParser::setProductsList( ProductsView* tree )
 // ================================================================
 void StructureParser::setTargetDir( QLineEdit* dir )
 {
+  QString home = QDir::homeDirPath();
   myTargetDir = dir;
+  if ( myTargetDir && !home.isEmpty() )
+    myTargetDir->setText( home + QDir::separator() + "salome" );
 }
 // ================================================================
 /*!
@@ -60,6 +131,8 @@ void StructureParser::setTargetDir( QLineEdit* dir )
 void StructureParser::setTempDir( QLineEdit* dir )
 {
   myTempDir = dir;
+  if ( myTempDir )
+    myTempDir->setText( "/tmp" );
 }
 // ================================================================
 /*!
@@ -135,13 +208,11 @@ bool StructureParser::startElement( const QString&        /*namespaceURI*/,
   }
   else if (( qName == "path" ) && ( attributes.length() > 0 ) && myWizard ) {
     if ( myTargetDir )
-      myTargetDir->setText( attributes.value( "targetdir" ) );
+      myTargetDir->setText( substituteVars( attributes.value( "targetdir" ) ) );
 
     if ( myTempDir ) {
-      if ( attributes.value( "tempdir" ) == "" )
-       myTempDir->setText( "/tmp" );
-      else
-       myTempDir->setText( attributes.value( "tempdir" ) );
+      if ( !attributes.value( "tempdir" ).stripWhiteSpace().isEmpty() )
+       myTempDir->setText( substituteVars( attributes.value( "tempdir" ) ) );
     }
   }
   return true;