Salome HOME
Copyright update 2022
[modules/gui.git] / src / SUIT / SUIT_FileValidator.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 // File   : SUIT_FileValidator.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 //
26 #include "SUIT_FileValidator.h"
27 #include "SUIT_MessageBox.h"
28 #include "SUIT_Tools.h"   
29 #include <QFileInfo>
30
31 /*!
32   \class SUIT_FileValidator
33   \brief Provides functionality to check the file or directory
34   existance and permissions.
35   \sa SUIT_FileDlg class
36 */
37
38 /*!
39   \brief Constructor.
40   \param parent parent widget (used as parent when displaying 
41   information message boxes)
42 */
43 SUIT_FileValidator::SUIT_FileValidator( QWidget* parent )
44 : myParent( parent ) 
45 {
46 }
47   
48 /*!
49   \brief Check if the specified file exists and (optionally) can be read.
50
51   If file does not exists or can not be read (if \a checkPermission is \c true)
52   and parent() is not null, shows error message box.
53
54   \param fileName file path
55   \param checkPermission if \c true (default) check also file permissions
56   \return \c false if file does not exist or if it does not have 
57   read permissions (if \a checkPermission is \c true)
58 */
59 bool SUIT_FileValidator::canOpen( const QString& fileName, bool checkPermission ) 
60 {
61   if ( !QFile::exists( fileName ) ) {
62     if ( parent() )
63       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
64                                  QObject::tr( "ERR_FILE_NOT_EXIST" ).arg( fileName ) );
65     return false;
66   }
67   if ( checkPermission && !QFileInfo( fileName ).isReadable() ) {
68     if ( parent() )
69       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
70                                  QObject::tr( "ERR_OPEN_PERMISSION_DENIED" ).arg( fileName ) );
71     return false; 
72   }
73   return true;
74 }
75
76 /*!
77   \brief Check if the specified file can be written.
78
79   If file already exists and parent() is not null, prompts
80   question message box to the user to confirm file overwriting.
81
82   If file can not be written (if \a checkPermission is \c true)
83   and parent() is not null, shows error message box.
84
85   \param fileName file path
86   \param checkPermission if \c true (default) check also file permissions
87   \return \c false if file exists and user rejects file overwriting 
88   or if file does not have write permissions (if \a checkPermission is \c true)
89 */
90 bool SUIT_FileValidator::canSave( const QString& fileName, bool checkPermission ) 
91 {
92   if ( QFile::exists( fileName ) ) {
93     if ( parent() )
94       if ( SUIT_MessageBox::question( parent(), QObject::tr( "WRN_WARNING" ),
95                                       QObject::tr( "QUE_DOC_FILEEXISTS" ).arg( fileName ),
96                                       SUIT_MessageBox::Yes | SUIT_MessageBox::No,
97                                       SUIT_MessageBox::No ) != SUIT_MessageBox::Yes )
98         return false;
99     
100     if ( checkPermission && !QFileInfo( fileName ).isWritable() ) {
101       if ( parent() ) 
102         SUIT_MessageBox::critical( myParent, QObject::tr( "ERR_ERROR" ),
103                                    QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
104       return false; 
105     }
106   }
107   else {
108     QString dirName = SUIT_Tools::dir( fileName );
109     if ( !QFile::exists( dirName ) ) {
110       SUIT_MessageBox::critical( parent(), QObject::tr( "WRN_WARNING" ),
111                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
112       return false;
113     }
114     if ( checkPermission && !QFileInfo( dirName ).isWritable() ) {
115       if ( parent() )
116         SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
117                                    QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
118       return false;
119     }
120   }
121   return true;
122 }
123
124 /*!
125   \brief Check if the specified directory exists and (optionally) can be read.
126
127   If directory does not exists or can not be read (if \a checkPermission is \c true)
128   and parent() is not null, shows error message box.
129
130   \param dirName directory path
131   \param checkPermission if \c true (default) check also directory permissions
132   \return \c false if directory does not exist or if it does not have 
133   read permissions (if \a checkPermission is \c true)
134 */
135 bool SUIT_FileValidator::canReadDir( const QString& dirName, bool checkPermission )
136 {
137   QFileInfo info( dirName );
138   if ( !info.exists() ) {
139     if ( parent() )
140       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
141                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
142     return false;
143   }
144   if ( !info.isDir() ) {
145     if ( parent() )
146       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
147                                  QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
148     return false;
149   }
150   if ( checkPermission && !info.isReadable() ) {
151     if ( parent() )
152       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
153                                  QObject::tr( "ERR_DIR_READ_PERMISSION_DENIED" ).arg( dirName ) );
154     return false; 
155   }
156   return true;
157 }
158
159 /*!
160   \brief Check if the specified directory can be written.
161
162   If directory does not exists or can not be modified (if \a checkPermission is \c true)
163   and parent() is not null, shows error message box.
164
165   \param dirName directory path
166   \param checkPermission if \c true (default) check also directory permissions
167   \return \c false if directory does not exist or if it does not have 
168   write permissions (if \a checkPermission is \c true)
169 */
170 bool SUIT_FileValidator::canWriteDir( const QString& dirName, bool checkPermission )
171 {
172   QFileInfo info( dirName );
173   if ( !info.exists() ) {
174     if ( parent() )
175       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
176                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
177     return false;
178   }
179   if ( !info.isDir() ) {
180     if ( parent() )
181       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
182                                  QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
183     return false;
184   }
185   if ( checkPermission && !info.isWritable() ) {
186     if ( parent() )
187       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
188                                  QObject::tr( "ERR_DIR_WRITE_PERMISSION_DENIED" ).arg( dirName ) );
189     return false; 
190   }
191   return true;
192 }
193
194 /*!
195   \brief Get parent widget.
196   \return parent widget
197 */
198 QWidget* SUIT_FileValidator::parent() const
199
200   return myParent; 
201 }