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