Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_FileValidator.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
20 //
21 //  File   : SUIT_FileValidator.cxx
22 //  Module : SALOME
23 //  $Header$
24
25 #include "SUIT_FileValidator.h"
26 #include "SUIT_MessageBox.h"
27 #include "SUIT_Session.h"
28
29 #include <qfile.h>
30 #include <qfileinfo.h>
31
32 /*! constructor */
33 SUIT_FileValidator::SUIT_FileValidator(QWidget* parent) :
34 myParent(parent) 
35 {
36 }
37   
38 /*! returns false if can't open file */
39 bool SUIT_FileValidator::canOpen( const QString& file ) 
40 {
41   if ( !QFile::exists( file ) ) {
42     SUIT_MessageBox::error1( myParent,
43           QObject::tr( "ERR_ERROR" ),
44           QObject::tr( "ERR_FILE_NOT_EXIST" ).arg( file ),
45           QObject::tr( "BUT_OK" ) );
46       return false;
47     }
48   if ( !QFileInfo( file ).isReadable() ) {
49     SUIT_MessageBox::error1( myParent,
50           QObject::tr( "ERR_ERROR" ),
51           QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
52           QObject::tr( "BUT_OK" ) );
53     return false; 
54   }
55   return true;
56 }
57
58 /*! returns false if can't save file */
59 bool SUIT_FileValidator::canSave( const QString& file ) 
60 {
61   if ( QFile::exists( file ) ) {
62     // if file exists - raise warning...
63     if ( SUIT_MessageBox::warn2( myParent,
64               QObject::tr( "WRN_WARNING" ),
65               QObject::tr( "QUE_DOC_FILEEXISTS" ).arg( file ),
66               QObject::tr( "BUT_YES" ), 
67               QObject::tr( "BUT_NO" ),
68               SUIT_YES, 
69               SUIT_NO, 
70               SUIT_NO ) == SUIT_NO ) {
71       return false;
72     }
73     // ... and if user wants to overwrite file, check it for writeability
74     if ( !QFileInfo( file ).isWritable() ) {
75       SUIT_MessageBox::error1( myParent,
76             QObject::tr( "ERR_ERROR" ),
77             QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
78             QObject::tr( "BUT_OK" ) );
79       return false; 
80     }
81   }
82   else {
83     // if file doesn't exist - try to create it
84     QFile qf( file );
85     if ( !qf.open( IO_WriteOnly ) ) {
86       SUIT_MessageBox::error1( myParent,
87             QObject::tr( "ERR_ERROR" ),
88             QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
89             QObject::tr( "BUT_OK" ) );
90       return false;
91     }
92     else {
93       // remove just created file
94       qf.close();
95       qf.remove();
96     }
97   }
98   return true;
99 }
100