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