Back to index

salome-gui  6.5.0
SUIT_ResourceMgr.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SUIT_ResourceMgr.h"
00024 
00025 #include <QDir>
00026 #include <QFileInfo>
00027 #include <QApplication>
00028 #include <QRegExp>
00029 
00030 #ifndef WIN32
00031 #include <unistd.h>
00032 #endif
00033 
00037 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
00038 : QtxResourceMgr( app, resVarTemplate )
00039 {
00040 }
00041 
00045 SUIT_ResourceMgr::~SUIT_ResourceMgr()
00046 {
00047 }
00048 
00052 QString SUIT_ResourceMgr::version() const
00053 {
00054   return myVersion;
00055 }
00056 
00060 void SUIT_ResourceMgr::setVersion( const QString& ver )
00061 {
00062   myVersion = ver;
00063 }
00064 
00068 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
00069 {
00070   QString docSection = option( "doc_section_name" );
00071   if ( docSection.isEmpty() )
00072     docSection = QString( "docs" );
00073 
00074   return path( docSection, prefix, id );
00075 }
00076 
00080 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
00081 {
00082   QString pathName;
00083 
00084   QStringList arguments = QApplication::arguments();
00085   // Try config file, given in arguments
00086   for (int i = 1; i < arguments.count(); i++) {
00087     QRegExp rx ("--resources=(.+)");
00088     if ( rx.indexIn( arguments[i] ) >= 0 && rx.numCaptures() > 1 ) {
00089       QString file = rx.cap(1);
00090       QFileInfo fi (file);
00091       pathName = fi.absoluteFilePath();
00092     }
00093   }
00094 
00095   if (!pathName.isEmpty())
00096     return pathName;
00097 
00098   // QtxResourceMgr::userFileName() + '.' + version()
00099   pathName = QtxResourceMgr::userFileName( appName );
00100 
00101   if ( !version().isEmpty() )
00102     pathName += QString( "." ) + version();
00103 
00104   if ( !QFileInfo( pathName ).exists() && for_load )
00105   {
00106     QString newName = findAppropriateUserFile( pathName );
00107     if ( !newName.isEmpty() )
00108       pathName = newName;
00109   }
00110 
00111   return pathName;
00112 }
00113 
00117 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
00118 {
00119   QString appr_file;
00120 
00121   // calculate default file id from user file name
00122   long id0 = userFileId( fname );
00123   if ( id0 < 0 ) // can't calculate file id from user file name, no further processing
00124     return appr_file;
00125 
00126   long id, appr = -1;
00127 
00128   // get all files from the same dir where use file is (should be) situated
00129   QDir d( QFileInfo( fname ).dir() );
00130   if ( d.exists() ) {
00131     d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
00132     QStringList l = d.entryList();
00133     for( QStringList::const_iterator anIt = l.begin(), aLast = l.end(); anIt!=aLast; anIt++ )
00134     {
00135       id = userFileId( *anIt );
00136       if ( id < 0 )
00137        continue;
00138       if( appr < 0 || qAbs( id-id0 ) < qAbs( appr-id0 ) )
00139       {
00140        appr = id;
00141        appr_file = d.absoluteFilePath( *anIt );
00142       }
00143     }
00144   }
00145 
00146   // backward compatibility: check also user's home directory (if it differs from above one)
00147   QDir home = QDir::home();
00148   if ( home.exists() && d.canonicalPath() != home.canonicalPath() ) {
00149     home.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
00150     QStringList l = home.entryList();
00151 
00152     for( QStringList::const_iterator anIt = l.begin(), aLast = l.end(); anIt!=aLast; anIt++ )
00153     {
00154       id = userFileId( *anIt );
00155       if ( id < 0 )
00156        continue;
00157       if( appr < 0 || qAbs( id-id0 ) < qAbs( appr-id0 ) )
00158       {
00159        appr = id;
00160        appr_file = home.absoluteFilePath( *anIt );
00161       }
00162     }
00163   }
00164   
00165   return appr_file;
00166 }
00167 
00171 long SUIT_ResourceMgr::userFileId( const QString& ) const
00172 {
00173   return -1;
00174 }