1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 // File : SUIT_FileValidator.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 #include "SUIT_FileValidator.h"
27 #include "SUIT_MessageBox.h"
28 #include "SUIT_Tools.h"
32 \class SUIT_FileValidator
33 \brief Provides functionality to check the file or directory
34 existance and permissions.
35 \sa SUIT_FileDlg class
40 \param parent parent widget (used as parent when displaying
41 information message boxes)
43 SUIT_FileValidator::SUIT_FileValidator( QWidget* parent )
49 \brief Check if the specified file exists and (optionally) can be read.
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.
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)
59 bool SUIT_FileValidator::canOpen( const QString& fileName, bool checkPermission )
61 if ( !QFile::exists( fileName ) ) {
63 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
64 QObject::tr( "ERR_FILE_NOT_EXIST" ).arg( fileName ) );
67 if ( checkPermission && !QFileInfo( fileName ).isReadable() ) {
69 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
70 QObject::tr( "ERR_OPEN_PERMISSION_DENIED" ).arg( fileName ) );
77 \brief Check if the specified file can be written.
79 If file already exists and parent() is not null, prompts
80 question message box to the user to confirm file overwriting.
82 If file can not be written (if \a checkPermission is \c true)
83 and parent() is not null, shows error message box.
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)
90 bool SUIT_FileValidator::canSave( const QString& fileName, bool checkPermission )
92 if ( QFile::exists( fileName ) ) {
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 )
100 if ( checkPermission && !QFileInfo( fileName ).isWritable() ) {
102 SUIT_MessageBox::critical( myParent, QObject::tr( "ERR_ERROR" ),
103 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
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 ) );
114 if ( checkPermission && !QFileInfo( dirName ).isWritable() ) {
116 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
117 QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
125 \brief Check if the specified directory exists and (optionally) can be read.
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.
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)
135 bool SUIT_FileValidator::canReadDir( const QString& dirName, bool checkPermission )
137 QFileInfo info( dirName );
138 if ( !info.exists() ) {
140 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
141 QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
144 if ( !info.isDir() ) {
146 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
147 QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
150 if ( checkPermission && !info.isReadable() ) {
152 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
153 QObject::tr( "ERR_DIR_READ_PERMISSION_DENIED" ).arg( dirName ) );
160 \brief Check if the specified directory can be written.
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.
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)
170 bool SUIT_FileValidator::canWriteDir( const QString& dirName, bool checkPermission )
172 QFileInfo info( dirName );
173 if ( !info.exists() ) {
175 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
176 QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
179 if ( !info.isDir() ) {
181 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
182 QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
185 if ( checkPermission && !info.isWritable() ) {
187 SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
188 QObject::tr( "ERR_DIR_WRITE_PERMISSION_DENIED" ).arg( dirName ) );
195 \brief Get parent widget.
196 \return parent widget
198 QWidget* SUIT_FileValidator::parent() const