Back to index

salome-gui  6.5.0
SUIT_FileValidator.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : SUIT_FileValidator.cxx
00024 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 //
00026 #include "SUIT_FileValidator.h"
00027 #include "SUIT_MessageBox.h"
00028 #include "SUIT_Tools.h"   
00029 #include <QFileInfo>
00030 
00043 SUIT_FileValidator::SUIT_FileValidator( QWidget* parent )
00044 : myParent( parent ) 
00045 {
00046 }
00047   
00059 bool SUIT_FileValidator::canOpen( const QString& fileName, bool checkPermission ) 
00060 {
00061   if ( !QFile::exists( fileName ) ) {
00062     if ( parent() )
00063       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00064                                  QObject::tr( "ERR_FILE_NOT_EXIST" ).arg( fileName ) );
00065     return false;
00066   }
00067   if ( checkPermission && !QFileInfo( fileName ).isReadable() ) {
00068     if ( parent() )
00069       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00070                                  QObject::tr( "ERR_OPEN_PERMISSION_DENIED" ).arg( fileName ) );
00071     return false; 
00072   }
00073   return true;
00074 }
00075 
00090 bool SUIT_FileValidator::canSave( const QString& fileName, bool checkPermission ) 
00091 {
00092   if ( QFile::exists( fileName ) ) {
00093     if ( parent() )
00094       if ( SUIT_MessageBox::question( parent(), QObject::tr( "WRN_WARNING" ),
00095                                       QObject::tr( "QUE_DOC_FILEEXISTS" ).arg( fileName ),
00096                                       SUIT_MessageBox::Yes | SUIT_MessageBox::No,
00097                                       SUIT_MessageBox::No ) != SUIT_MessageBox::Yes )
00098         return false;
00099     
00100     if ( checkPermission && !QFileInfo( fileName ).isWritable() ) {
00101       if ( parent() ) 
00102         SUIT_MessageBox::critical( myParent, QObject::tr( "ERR_ERROR" ),
00103                                    QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
00104       return false; 
00105     }
00106   }
00107   else {
00108     QString dirName = SUIT_Tools::dir( fileName );
00109     if ( !QFile::exists( dirName ) ) {
00110       SUIT_MessageBox::critical( parent(), QObject::tr( "WRN_WARNING" ),
00111                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
00112       return false;
00113     }
00114     if ( checkPermission && !QFileInfo( dirName ).isWritable() ) {
00115       if ( parent() )
00116         SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00117                                    QObject::tr( "ERR_PERMISSION_DENIED" ).arg( fileName ) );
00118       return false;
00119     }
00120   }
00121   return true;
00122 }
00123 
00135 bool SUIT_FileValidator::canReadDir( const QString& dirName, bool checkPermission )
00136 {
00137   QFileInfo info( dirName );
00138   if ( !info.exists() ) {
00139     if ( parent() )
00140       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00141                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
00142     return false;
00143   }
00144   if ( !info.isDir() ) {
00145     if ( parent() )
00146       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00147                                  QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
00148     return false;
00149   }
00150   if ( checkPermission && !info.isReadable() ) {
00151     if ( parent() )
00152       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00153                                  QObject::tr( "ERR_DIR_READ_PERMISSION_DENIED" ).arg( dirName ) );
00154     return false; 
00155   }
00156   return true;
00157 }
00158 
00170 bool SUIT_FileValidator::canWriteDir( const QString& dirName, bool checkPermission )
00171 {
00172   QFileInfo info( dirName );
00173   if ( !info.exists() ) {
00174     if ( parent() )
00175       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00176                                  QObject::tr( "ERR_DIR_NOT_EXIST" ).arg( dirName ) );
00177     return false;
00178   }
00179   if ( !info.isDir() ) {
00180     if ( parent() )
00181       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00182                                  QObject::tr( "ERR_FILE_NOT_DIR" ).arg( dirName ) );
00183     return false;
00184   }
00185   if ( checkPermission && !info.isWritable() ) {
00186     if ( parent() )
00187       SUIT_MessageBox::critical( parent(), QObject::tr( "ERR_ERROR" ),
00188                                  QObject::tr( "ERR_DIR_WRITE_PERMISSION_DENIED" ).arg( dirName ) );
00189     return false; 
00190   }
00191   return true;
00192 }
00193 
00198 QWidget* SUIT_FileValidator::parent() const
00199 { 
00200   return myParent; 
00201 }