Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / SALOMEGUI / QAD_ResourceMgr.cxx
index 19ce185d33ba9e676a5cf81f75270d3c4e5ef193..576c7475c1c094eab92d59636d7da4a33b64715e 100644 (file)
@@ -1,11 +1,29 @@
-using namespace std;
-//  File      : QAD_ResourceMgr.cxx
-//  Created   : UI team, 22.10.00
-//  Descr     : ResourceMgr QAD-based application
-//  Author    : UI team
-//  Project   : SALOME
-//  Module    : SALOMEGUI
-//  Copyright : Open CASCADE
+//  SALOME SALOMEGUI : implementation of desktop and GUI kernel
+//
+//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
+// 
+//  This library is free software; you can redistribute it and/or 
+//  modify it under the terms of the GNU Lesser General Public 
+//  License as published by the Free Software Foundation; either 
+//  version 2.1 of the License. 
+// 
+//  This library is distributed in the hope that it will be useful, 
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+//  Lesser General Public License for more details. 
+// 
+//  You should have received a copy of the GNU Lesser General Public 
+//  License along with this library; if not, write to the Free Software 
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
+// 
+//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//
+//
+//
+//  File   : QAD_ResourceMgr.cxx
+//  Author : UI team
+//  Module : SALOME
 //  $Header$
 
 /*!
@@ -27,6 +45,7 @@ using namespace std;
 #include <qfileinfo.h>
 
 #include <Standard.hxx>
+using namespace std;
 
 /* configuration file */
 static const char* CONFIG_FILE = "config";
@@ -37,6 +56,7 @@ static const char* RES_DOCS     = "docs";
 static const char* RES_PIXMAPS  = "icons";
 static const char* RES_STRINGS  = "strings";
 static const char* RES_LANGUAGE = "language";
+static const char* RES_FILES    = "resources";
 
 static const char* SEPARATOR    = ":";
 
@@ -106,8 +126,11 @@ bool QAD_ResourceMgr::loadResources( const char* prefix, QString &msg )
     */
     QString resDirs = collectDirs( prefix );
     conf.insert( RES_DIR, new QString( resDirs ) );
-    QString fileConfig = path( CONFIG_FILE, prefix, 0 ) ;
+    QString fileConfig = QString( prefix ) + QString( "." ) + QString( CONFIG_FILE );
+    fileConfig = path( fileConfig, prefix, 0 ) ;
     //MESSAGE("QAD_ResourceMgr::loadresources : config : "<<fileConfig);
+    if ( fileConfig.isEmpty() )
+      fileConfig = path( CONFIG_FILE, prefix, 0 ) ;
     if ( !fileConfig.isEmpty() ) {
       QFile configFile( fileConfig );
       if ( !configFile.exists() || !configFile.open( IO_ReadOnly ) ) {
@@ -149,20 +172,37 @@ bool QAD_ResourceMgr::loadResources( const char* prefix, QString &msg )
     bool bLoadString = false;
     bool bLoadImages = false;
 
-    QStringList dirList = QStringList::split( SEPARATOR, resDirs, false ); // skip empty entries
-    for ( int i = dirList.count()-1; i >= 0; i-- ) {
-      QString dir = dirList[ i ];
-      QString fileString = QAD_Tools::addSlash( dir ) + stFile;
-      QString fileImage  = QAD_Tools::addSlash( dir ) + imagesFile;
+    QStringList resFiles = conf[ RES_FILES ] ? QStringList::split( SEPARATOR, *( conf[ RES_FILES ] ), false ) : QStringList();
+    
+    // first load main module's resources
+    QString fileString = path( stFile, prefix, 0 );
+    QString fileImage  = path( imagesFile, prefix, 0 );
+    
+    if ( !fileString.isEmpty() && settings->load( fileString ) ) {
+      bLoadString = true;
+    }
+    if ( !fileImage.isEmpty() && settings->load( fileImage ) ) {
+      bLoadImages = true;
+    }
+
+    // then load additional module's resources if any are given
+    for ( int j = 0; j < resFiles.count(); j++ ) {
+      QString stFileAdd = resFiles[j] + "_msg_" +  *( conf[ RES_LANGUAGE ] ) + ".qm" ;
+      QString imagesFileAdd = resFiles[j] + "_" + RES_PIXMAPS + ".qm";
+      if ( conf[ RES_STRINGS ] && !conf[ RES_STRINGS ]->isEmpty() )
+       stFileAdd = QAD_Tools::addSlash( *conf[ RES_STRINGS ] ) + stFileAdd;
+      if ( conf[ RES_PIXMAPS ] && !conf[ RES_PIXMAPS ]->isEmpty() )
+       imagesFileAdd = QAD_Tools::addSlash( *conf[ RES_PIXMAPS ] ) + imagesFileAdd;
       
-      if ( settings->load( fileString ) ) {
-       bLoadString = true;
-      }
-      if ( settings->load( fileImage ) ) {
-       bLoadImages = true;
-      }
+      QString fileStringAdd = path( stFileAdd, prefix, 0 );
+      QString fileImageAdd  = path( imagesFileAdd, prefix, 0 );
+      
+      if ( !fileStringAdd.isEmpty() )
+       settings->load( fileStringAdd );
+      if ( !fileImageAdd.isEmpty() )
+       settings->load( fileImageAdd );
     }
-    
+
     if ( !bLoadString ) {
       QString warnMsg;
       warnMsg.sprintf( "String resources for module %s not found.\n"
@@ -221,35 +261,46 @@ QString QAD_ResourceMgr::resources( const char* prefix ) const
   Collects list of directories, separated by ';' where resources for module 'prefix'
   can be situated
   The order is following : 
-  - CSF_<prefix>Resources env.var directory ( or directory list )
-  - CSF_ResourcesDefaults env.var directory ( or directory list )
+  - <prefix>_ROOT_DIR/share/salome/resources directory
+  - SALOME_<prefix>Resources env.var directory ( or directory list )
   - ${HOME}/.salome/resources directory
-  - ${SALOME_SITE_DIR}/share/salome/resources directory
-  - ${SALOME_ROOT_DIR}/share/salome/resources directory
+  - ${SALOME_SITE_DIR}/share/${SALOME_SITE_NAME}/resources directory (for SALOME-based applications)
+  - ${KERNEL_ROOT_DIR}/share/salome/resources directory
 */
 QString QAD_ResourceMgr::collectDirs( const QString& prefix ) const
 {
   QString dirList;
-  QCString envVar( "CSF_" );
+  QCString envVar;
   QString dir;
   char* cenv;
   
-  // Try CSF_<prefix>Resources env.var directory ( or directory list )
   if ( !prefix.isEmpty() ) {
-    envVar = QCString( "CSF_" ) + prefix.latin1() + QCString( "Resources" );
+    envVar = prefix.latin1() + QCString( "_ROOT_DIR" );
     cenv = getenv( ( const char* ) envVar );
     if ( cenv ) {
       dir.sprintf( "%s", cenv );
-      if ( !dir.isEmpty() )
+      if ( !dir.isEmpty() ) {
+       dir = QAD_Tools::addSlash(dir) ;
+       dir = dir + "share" ;
+       dir = QAD_Tools::addSlash(dir) ;
+       dir = dir + "salome" ;
+       dir = QAD_Tools::addSlash(dir) ;
+       dir = dir + "resources" ;
+       dir = QAD_Tools::addSlash(dir) ;
        dirList.append( dirList.isEmpty() ? dir : ( QString( SEPARATOR ) + dir ) );
+      }
     }
   }
-  // Try CSF_ResourcesDefaults env.var directory ( or directory list )
-  cenv = getenv( "CSF_ResourcesDefaults" );
-  if ( cenv ) {
-    dir.sprintf( "%s", cenv );
-    if ( !dir.isEmpty() )
-      dirList.append( dirList.isEmpty() ? dir : ( QString( SEPARATOR ) + dir ) );
+
+  // Try SALOME_<prefix>Resources env.var directory ( or directory list )
+  if ( !prefix.isEmpty() ) {
+    envVar = QCString( "SALOME_" ) + prefix.latin1() + QCString( "Resources" );
+    cenv = getenv( ( const char* ) envVar );
+    if ( cenv ) {
+      dir.sprintf( "%s", cenv );
+      if ( !dir.isEmpty() )
+       dirList.append( dirList.isEmpty() ? dir : ( QString( SEPARATOR ) + dir ) );
+    }
   }
   // Try ${HOME}/.salome/resources directory
   cenv = getenv( "HOME" );
@@ -264,7 +315,7 @@ QString QAD_ResourceMgr::collectDirs( const QString& prefix ) const
       dirList.append( dirList.isEmpty() ? dir : ( QString( SEPARATOR ) + dir ) );
     }
   }
-  // Try ${SALOME_SITE_DIR}/share/salome/resources directory
+  // Try ${SALOME_SITE_DIR}/share/${SALOME_SITE_NAME}/resources directory
   cenv = getenv( "SALOME_SITE_DIR" );
   if ( cenv ) {
     dir.sprintf( "%s", cenv );
@@ -272,15 +323,19 @@ QString QAD_ResourceMgr::collectDirs( const QString& prefix ) const
       dir = QAD_Tools::addSlash(dir) ;
       dir = dir + "share" ;
       dir = QAD_Tools::addSlash(dir) ;
-      dir = dir + "salome" ;
+      cenv = getenv( "SALOME_SITE_NAME" );
+      if ( cenv ) 
+       dir = dir + cenv ;
+      else
+       dir = dir + "salome" ;
       dir = QAD_Tools::addSlash(dir) ;
       dir = dir + "resources" ;
       dir = QAD_Tools::addSlash(dir) ;
       dirList.append( dirList.isEmpty() ? dir : ( QString( SEPARATOR ) + dir ) );
     }
   }
-  // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
-  cenv = getenv( "SALOME_ROOT_DIR" );
+  // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
+  cenv = getenv( "KERNEL_ROOT_DIR" );
   if ( cenv ) {
     dir.sprintf( "%s", cenv );
     if ( !dir.isEmpty() ) {
@@ -342,8 +397,6 @@ QString QAD_ResourceMgr::findFile( const QString& filename, const char* prefix )
 */
 QString QAD_ResourceMgr::path( const QString& filename, const char* prefix, const char* key ) const
 {   
-  QString filePath;
-
   ResourceSettings* rs = myRes[ prefix ];
   if ( rs ) {
     StringDict& conf = rs->config();
@@ -354,22 +407,20 @@ QString QAD_ResourceMgr::path( const QString& filename, const char* prefix, cons
       for ( int i = 0; i < dirList.count(); i++ ) {
        QString dir = dirList[ i ];
        dir = QAD_Tools::addSlash( dir );
-       if ( key ) {
-         QString* where = conf[ key ];
-         if ( where )
-           dir = dir + QAD_Tools::addSlash( *where );
+       if ( key && conf[ key ] ) {
+         dir = dir + QString( *( conf[ key ] ) );
+         dir = QAD_Tools::addSlash( dir );
        }
        dir = dir + filename;
        QFileInfo fileInfo( dir );
        if ( fileInfo.isFile() && fileInfo.exists() ) {
-         filePath = fileInfo.filePath();
-         break;
+         return fileInfo.filePath();
        }
       }
     }
   }
   //MESSAGE("QAD_ResourceMgr::path : <"<<filename.latin1()<<"> : "<<filePath.latin1()) ;
-  return filePath;
+  return QString::null;
 }
 
 /*!