]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_ResourceMgr.cxx
Salome HOME
f134e733279e4edfa421c3faa0cadf05885088b1
[modules/gui.git] / src / SUIT / SUIT_ResourceMgr.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SUIT_ResourceMgr.h"
23
24 #include <QDir>
25 #include <QFileInfo>
26 #include <QApplication>
27 #include <QRegExp>
28
29 #ifndef WIN32
30 #include <unistd.h>
31 #endif
32
33 /*!
34     Constructor
35 */
36 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
37 : QtxResourceMgr( app, resVarTemplate )
38 {
39 }
40
41 /*!
42     Destructor
43 */
44 SUIT_ResourceMgr::~SUIT_ResourceMgr()
45 {
46 }
47
48 /*!
49     Returns the version of application
50 */
51 QString SUIT_ResourceMgr::version() const
52 {
53   return myVersion;
54 }
55
56 /*!
57     Sets the version of application
58 */
59 void SUIT_ResourceMgr::setVersion( const QString& ver )
60 {
61   myVersion = ver;
62 }
63
64 /*!
65     Loads a doc page from 'prefix' resources and indetified by 'id'
66 */
67 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
68 {
69   QString docSection = option( "doc_section_name" );
70   if ( docSection.isEmpty() )
71     docSection = QString( "docs" );
72
73   return path( docSection, prefix, id );
74 }
75
76 /*!
77     Returns the user file name for specified application
78 */
79 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
80 {
81   QString pathName;
82
83   QStringList arguments = QApplication::arguments();
84   // Try config file, given in arguments
85   for (int i = 1; i < arguments.count(); i++) {
86     QRegExp rx ("--resources=(.+)");
87     if ( rx.indexIn( arguments[i] ) >= 0 && rx.numCaptures() > 1 ) {
88       QString file = rx.cap(1);
89       QFileInfo fi (file);
90       pathName = fi.absoluteFilePath();
91     }
92   }
93
94   if (!pathName.isEmpty())
95     return pathName;
96
97   // QtxResourceMgr::userFileName() + '.' + version()
98   pathName = QtxResourceMgr::userFileName( appName );
99
100   if ( !version().isEmpty() )
101     pathName += QString( "." ) + version();
102
103   if ( !QFileInfo( pathName ).exists() && for_load )
104   {
105     QString newName = findAppropriateUserFile( pathName );
106     if ( !newName.isEmpty() )
107       pathName = newName;
108   }
109
110   return pathName;
111 }
112
113 /*!
114     Finds other the most appropriate user file instead missing one
115 */
116 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
117 {
118   QDir d( QFileInfo( fname ).dir() );
119   d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
120   QStringList l = d.entryList();
121   QString appr_file;
122   int id0 = userFileId( fname ), id, appr=-1;
123   if( id0<0 )
124     return appr_file;
125
126   for( QStringList::const_iterator anIt = l.begin(), aLast = l.end(); anIt!=aLast; anIt++ )
127   {
128     id = userFileId( *anIt );
129     if( id<0 )
130       continue;
131
132     if( appr < 0 || abs( id-id0 ) < abs( appr-id0 ) )
133     {
134       appr = id;
135       appr_file = d.absoluteFilePath( *anIt );
136     }
137   }
138   return appr_file;
139 }
140
141 /*!
142     Calculates integer extended version number by user file name for comparing
143 */
144 int SUIT_ResourceMgr::userFileId( const QString& ) const
145 {
146   return -1;
147 }