1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SUIT_FileValidator.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25 #include "SUIT_FileValidator.h"
26 #include "SUIT_MessageBox.h"
27 #include "SUIT_Tools.h"
31 \class SUIT_FileValidator
32 \brief Provides functionality to check the file or directory
33 existance and permissions.
34 \sa SUIT_FileDlg class
39 \param parent parent widget (used as parent when displaying
40 information message boxes)
42 SUIT_FileValidator::SUIT_FileValidator( QWidget* parent )
48 \brief Check if the specified file exists and (optionally) can be read.
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.
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)
58 bool SUIT_FileValidator::canOpen( const QString& fileName, bool checkPermission )
60 if ( !QFile::exists( fileName ) ) {
62 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
63 QObject::tr( "ERR_FILE_NOT_EXIST" ).arg( fileName ) );
66 if ( checkPermission && !QFileInfo( fileName ).isReadable() ) {
68 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
69 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
76 \brief Check if the specified file can be written.
78 If file already exists and parent() is not null, prompts
79 question message box to the user to confirm file overwriting.
81 If file can not be written (if \a checkPermission is \c true)
82 and parent() is not null, shows error message box.
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)
89 bool SUIT_FileValidator::canSave( const QString& fileName, bool checkPermission )
91 if ( QFile::exists( fileName ) ) {
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 )
99 if ( checkPermission && !QFileInfo( fileName ).isWritable() ) {
101 SUIT_MessageBox::critical( myParent, QObject::tr( "ERR_ERROR" ),
102 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
107 QString dirName = SUIT_Tools::dir( fileName );
108 if ( checkPermission && !QFileInfo( dirName ).isWritable() ) {
110 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
111 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
119 \brief Check if the specified directory exists and (optionally) can be read.
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.
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)
129 bool SUIT_FileValidator::canReadDir( const QString& dirName, bool checkPermission )
131 QFileInfo info( dirName );
132 if ( !info.exists() ) {
134 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
135 QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
138 if ( !info.isDir() ) {
140 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
141 QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
144 if ( checkPermission && !info.isReadable() ) {
146 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
147 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( dirName ) );
154 \brief Check if the specified directory can be written.
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.
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)
164 bool SUIT_FileValidator::canWriteDir( const QString& dirName, bool checkPermission )
166 QFileInfo info( dirName );
167 if ( !info.exists() ) {
169 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
170 QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
173 if ( !info.isDir() ) {
175 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
176 QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
179 if ( checkPermission && !info.isWritable() ) {
181 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
182 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( dirName ) );
189 \brief Get parent widget.
190 \return parent widget
192 QWidget* SUIT_FileValidator::parent() const