Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_ResourceMgr.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SUIT_ResourceMgr.h"
20
21 #include <qfileinfo.h>
22 #include <qdir.h>
23
24 /*!
25     Constructor
26 */
27 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
28 : QtxResourceMgr( app, resVarTemplate )
29 {
30 }
31
32 /*!
33     Destructor
34 */
35 SUIT_ResourceMgr::~SUIT_ResourceMgr()
36 {
37 }
38
39 /*!
40     Returns the version of application
41 */
42 QString SUIT_ResourceMgr::version() const
43 {
44   return myVersion;
45 }
46
47 /*!
48     Sets the version of application
49 */
50 void SUIT_ResourceMgr::setVersion( const QString& ver )
51 {
52   myVersion = ver;
53 }
54
55 /*!
56     Loads a doc page from 'prefix' resources and indetified by 'id'
57 */
58 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
59 {
60   QString docSection = option( "doc_section_name" );
61   if ( docSection.isEmpty() )
62     docSection = QString( "docs" );
63
64   return path( docSection, prefix, id );
65 }
66
67 #include <unistd.h>
68 /*!
69     Returns the user file name for specified application
70 */
71 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
72 {
73   QString pathName = QtxResourceMgr::userFileName( appName );
74
75   if ( !version().isEmpty() )
76     pathName += QString( "." ) + version();
77
78   if( !QFileInfo( pathName ).exists() && for_load )
79   {
80     QString newName = findAppropriateUserFile( pathName );
81     if( !newName.isEmpty() )
82       pathName = newName;
83   }
84
85   return pathName;
86 }
87
88 /*!
89     Finds other the most appropriate user file instead missing one
90 */
91 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
92 {
93   QDir d( QFileInfo( fname ).dir( true ) );
94   d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
95   QStringList l = d.entryList();
96   QString appr_file;
97   int id0 = userFileId( fname ), id, appr=-1;
98   if( id0<0 )
99     return appr_file;
100
101   for( QStringList::const_iterator anIt = l.begin(), aLast = l.end(); anIt!=aLast; anIt++ )
102   {
103     id = userFileId( *anIt );
104     if( id<0 )
105       continue;
106
107     if( abs( id-id0 ) < abs( appr-id0 ) )
108     {
109       appr = id;
110       appr_file = d.absFilePath( *anIt );
111     }
112   }
113   return appr_file;
114 }
115
116 /*!
117     Calculates integer extended version number by user file name for comparing
118 */
119 int SUIT_ResourceMgr::userFileId( const QString& ) const
120 {
121   return -1;
122 }