1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SUIT_ResourceMgr.h"
24 #include "SUIT_Session.h"
28 #include <QApplication>
38 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
39 : QtxResourceMgr( app, resVarTemplate )
46 SUIT_ResourceMgr::~SUIT_ResourceMgr()
51 Returns the version of application
53 QString SUIT_ResourceMgr::version() const
59 Sets the version of application
61 void SUIT_ResourceMgr::setVersion( const QString& ver )
67 Loads a doc page from 'prefix' resources and indetified by 'id'
69 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
71 QString docSection = option( "doc_section_name" );
72 if ( docSection.isEmpty() )
73 docSection = QString( "docs" );
75 return path( docSection, prefix, id );
79 Returns the user file name for specified application
81 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
85 QStringList arguments;
86 if ( SUIT_Session::session() ) arguments = SUIT_Session::session()->arguments();
87 // Try config file, given in arguments
88 for (int i = 1; i < arguments.count(); i++) {
89 QRegExp rx ("--resources=(.+)");
90 if ( rx.indexIn( arguments[i] ) >= 0 && rx.numCaptures() > 0 ) {
91 QString file = rx.cap(1);
93 pathName = fi.absoluteFilePath();
97 if (!pathName.isEmpty())
100 // QtxResourceMgr::userFileName() + '.' + version()
101 pathName = QtxResourceMgr::userFileName( appName );
103 if ( !version().isEmpty() )
104 pathName += QString( "." ) + version();
106 if ( !QFileInfo( pathName ).exists() && for_load )
108 QString newName = findAppropriateUserFile( pathName );
109 if ( !newName.isEmpty() )
117 Finds other the most appropriate user file instead missing one
119 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
123 // calculate default file id from user file name
124 long id0 = userFileId( fname );
125 if ( id0 < 0 ) // can't calculate file id from user file name, no further processing
130 QStringList all_files;
132 // get all files from the same dir where use file is (should be) situated
133 QDir d( QFileInfo( fname ).dir() );
135 d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
136 QStringList l = d.entryList();
137 foreach( QString ll, l )
138 all_files << d.absoluteFilePath( ll );
140 // backward compatibility: check also user's home directory (if it differs from above one)
141 QDir home = QDir::home();
142 if ( home.exists() && d.canonicalPath() != home.canonicalPath() ) {
143 home.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
144 QStringList l = home.entryList();
145 foreach( QString ll, l )
146 all_files << home.absoluteFilePath( ll );
149 for( QStringList::const_iterator anIt = all_files.begin(), aLast = all_files.end(); anIt!=aLast; anIt++ )
151 id = userFileId( *anIt );
155 if( appr < 0 || qAbs( id-id0 ) < qAbs( appr-id0 ) )
158 appr_file = d.absoluteFilePath( *anIt );
160 else if ( qAbs( id-id0 ) == qAbs( appr-id0 ) ) {
161 // appr == id that means that another file with equal version id is detected
162 // this can happen, e.g. if one file begins with "." and other one - not
164 // VSR 24/09/2012: issue 0021781: since version 6.6.0 user filename is not prepended with "."
165 // when it is stored in the ~/.config/<appname> directory;
166 // for backward compatibility we also check files prepended with "." with lower priority
167 if ( !QFileInfo( *anIt ).fileName().startsWith(".") )
168 appr_file = home.absoluteFilePath( *anIt );
176 Calculates integer extended version number by user file name for comparing
178 long SUIT_ResourceMgr::userFileId( const QString& ) const