Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 #include <qapplication.h>
24 #include <qregexp.h>
25
26 /*!
27     Constructor
28 */
29 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
30 : QtxResourceMgr( app, resVarTemplate )
31 {
32 }
33
34 /*!
35     Destructor
36 */
37 SUIT_ResourceMgr::~SUIT_ResourceMgr()
38 {
39 }
40
41 /*!
42     Returns the version of application
43 */
44 QString SUIT_ResourceMgr::version() const
45 {
46   return myVersion;
47 }
48
49 /*!
50     Sets the version of application
51 */
52 void SUIT_ResourceMgr::setVersion( const QString& ver )
53 {
54   myVersion = ver;
55 }
56
57 /*!
58     Loads a doc page from 'prefix' resources and indetified by 'id'
59 */
60 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
61 {
62   QString docSection = option( "doc_section_name" );
63   if ( docSection.isEmpty() )
64     docSection = QString( "docs" );
65
66   return path( docSection, prefix, id );
67 }
68
69 #ifndef WIN32
70 #include <unistd.h>
71 #endif
72 /*!
73     Returns the user file name for specified application
74 */
75 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
76 {
77   QString pathName;
78
79   // Try config file, given in arguments
80   for (int i = 1; i < qApp->argc(); i++) {
81     QRegExp rx ("--resources=(.+)");
82     if ( rx.search( QString(qApp->argv()[i]) ) >= 0 && rx.capturedTexts().count() > 1 ) {
83       QString file = rx.capturedTexts()[1];
84       QFileInfo fi (file);
85       pathName = fi.absFilePath();
86     }
87   }
88
89   if (!pathName.isEmpty())
90     return pathName;
91
92   // QtxResourceMgr::userFileName() + '.' + version()
93   pathName = QtxResourceMgr::userFileName( appName );
94
95   if ( !version().isEmpty() )
96     pathName += QString( "." ) + version();
97
98   if ( !QFileInfo( pathName ).exists() && for_load )
99   {
100     QString newName = findAppropriateUserFile( pathName );
101     if ( !newName.isEmpty() )
102       pathName = newName;
103   }
104
105   return pathName;
106 }
107
108 /*!
109     Finds other the most appropriate user file instead missing one
110 */
111 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
112 {
113   QDir d( QFileInfo( fname ).dir( true ) );
114   d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
115   QStringList l = d.entryList();
116   QString appr_file;
117   int id0 = userFileId( fname ), id, appr=-1;
118   if( id0<0 )
119     return appr_file;
120
121   for( QStringList::const_iterator anIt = l.begin(), aLast = l.end(); anIt!=aLast; anIt++ )
122   {
123     id = userFileId( *anIt );
124     if( id<0 )
125       continue;
126
127     if( appr < 0 || abs( id-id0 ) < abs( appr-id0 ) )
128     {
129       appr = id;
130       appr_file = d.absFilePath( *anIt );
131     }
132   }
133   return appr_file;
134 }
135
136 /*!
137     Calculates integer extended version number by user file name for comparing
138 */
139 int SUIT_ResourceMgr::userFileId( const QString& ) const
140 {
141   return -1;
142 }