Salome HOME
c524905bbe2f1647f701ee7ee791773f0be3276f
[modules/gui.git] / src / SUIT / SUIT_ResourceMgr.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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
23 #include "SUIT_ResourceMgr.h"
24 #include "SUIT_Session.h"
25
26 #include <QDir>
27 #include <QFileInfo>
28 #include <QApplication>
29 #include <QRegExp>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #endif
34
35 /*!
36     Constructor
37 */
38 SUIT_ResourceMgr::SUIT_ResourceMgr( const QString& app, const QString& resVarTemplate )
39 : QtxResourceMgr( app, resVarTemplate )
40 {
41 }
42
43 /*!
44     Destructor
45 */
46 SUIT_ResourceMgr::~SUIT_ResourceMgr()
47 {
48 }
49
50 /*!
51     Returns the version of application
52 */
53 QString SUIT_ResourceMgr::version() const
54 {
55   return myVersion;
56 }
57
58 /*!
59     Sets the version of application
60 */
61 void SUIT_ResourceMgr::setVersion( const QString& ver )
62 {
63   myVersion = ver;
64 }
65
66 /*!
67     Loads a doc page from 'prefix' resources and indetified by 'id'
68 */
69 QString SUIT_ResourceMgr::loadDoc( const QString& prefix, const QString& id ) const
70 {
71   QString docSection = option( "doc_section_name" );
72   if ( docSection.isEmpty() )
73     docSection = QString( "docs" );
74
75   return path( docSection, prefix, id );
76 }
77
78 /*!
79     Returns the user file name for specified application
80 */
81 QString SUIT_ResourceMgr::userFileName( const QString& appName, const bool for_load ) const
82 {
83   QString pathName;
84
85   // Try config file, given in arguments
86   QStringList args = QApplication::arguments();
87   for (int i = 1; i < args.count(); i++) {
88     QRegExp rx ("--resources=(.+)");
89     if ( rx.indexIn( args[i] ) >= 0 && rx.captureCount() > 0 ) {
90       QString file = rx.cap(1);
91       QFileInfo fi (file);
92       pathName = fi.absoluteFilePath();
93     }
94   }
95
96   if (!pathName.isEmpty())
97     return pathName;
98
99   // QtxResourceMgr::userFileName() + '.' + version()
100   pathName = QtxResourceMgr::userFileName( appName );
101
102   if ( !version().isEmpty() )
103     pathName += QString( "." ) + version();
104
105   if ( !QFileInfo( pathName ).exists() && for_load )
106   {
107     QString newName = findAppropriateUserFile( pathName );
108     if ( !newName.isEmpty() )
109       pathName = newName;
110   }
111
112   return pathName;
113 }
114
115 /*!
116     Finds other the most appropriate user file instead missing one
117 */
118 QString SUIT_ResourceMgr::findAppropriateUserFile( const QString& fname ) const
119 {
120   QString appr_file;
121
122   // calculate default file id from user file name
123   long id0 = userFileId( fname );
124   if ( id0 < 0 ) // can't calculate file id from user file name, no further processing
125     return appr_file;
126
127   long id, appr = -1;
128
129   QStringList all_files;
130
131   // get all files from the same dir where use file is (should be) situated
132   QDir d( QFileInfo( fname ).dir() );
133   if ( d.exists() ) {
134     d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
135     QStringList l = d.entryList();
136     foreach( QString ll, l )
137       all_files << d.absoluteFilePath( ll );
138   }
139   // backward compatibility: check also user's home directory (if it differs from above one)
140   QDir home = QDir::home();
141   if ( home.exists() && d.canonicalPath() != home.canonicalPath() ) {
142     home.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
143     QStringList l = home.entryList();
144     foreach( QString ll, l )
145       all_files << home.absoluteFilePath( ll );
146   }
147
148   for( QStringList::const_iterator anIt = all_files.begin(), aLast = all_files.end(); anIt!=aLast; anIt++ )
149   {
150     id = userFileId( *anIt );
151     if ( id < 0 )
152       continue;
153
154     if( appr < 0 || qAbs( id-id0 ) < qAbs( appr-id0 ) )
155     {
156       appr = id;
157       appr_file = d.absoluteFilePath( *anIt );
158     }
159     else if ( qAbs( id-id0 ) == qAbs( appr-id0 ) ) {
160       // appr == id that means that another file with equal version id is detected
161       // this can happen, e.g. if one file begins with "." and other one - not
162       // ...
163       // VSR 24/09/2012: issue 0021781: since version 6.6.0 user filename is not prepended with "."
164       // when it is stored in the ~/.config/<appname> directory;
165       // for backward compatibility we also check files prepended with "." with lower priority
166       if ( !QFileInfo( *anIt ).fileName().startsWith(".") )
167         appr_file = home.absoluteFilePath( *anIt );
168     }
169   }
170   
171   return appr_file;
172 }
173
174 /*!
175     Calculates integer extended version number by user file name for comparing
176 */
177 long SUIT_ResourceMgr::userFileId( const QString& ) const
178 {
179   return -1;
180 }
181
182 /*!
183   \brief Specify default language for the application.
184 */
185 QString SUIT_ResourceMgr::defaultLanguage() const
186 {
187   QString language;
188
189   // Try language, given in arguments
190   QStringList args = QApplication::arguments();
191   for (int i = 1; i < args.count(); i++) {
192     QRegExp rx ("--language=(.+)");
193     if ( rx.indexIn( args[i] ) >= 0 && rx.captureCount() > 0 ) {
194       language = rx.cap(1);
195     }
196   }
197
198   return language;
199 }