Salome HOME
Initial version
[modules/gui.git] / src / SUIT / SUIT_FileValidator.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SUIT_FileValidator.cxx
8 //  Author : 
9 //  Module : SALOME
10 //  $Header$
11
12 #include "SUIT_FileValidator.h"
13 #include "SUIT_MessageBox.h"
14 #include "SUIT_Session.h"
15
16 #include <qfile.h>
17 #include <qfileinfo.h>
18
19 /* constructor */
20 SUIT_FileValidator::SUIT_FileValidator(QWidget* parent) :
21 myParent(parent) 
22 {
23 }
24   
25 /* returns false if can't open file */
26 bool SUIT_FileValidator::canOpen( const QString& file ) 
27 {
28   if ( !QFile::exists( file ) ) {
29     SUIT_MessageBox::error1( myParent,
30           QObject::tr( "ERR_ERROR" ),
31           QObject::tr( "ERR_DOC_FILENOTEXIST" ).arg( file ),
32           QObject::tr( "BUT_OK" ) );
33       return false;
34     }
35   if ( !QFileInfo( file ).isReadable() ) {
36     SUIT_MessageBox::error1( myParent,
37           QObject::tr( "ERR_ERROR" ),
38           QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
39           QObject::tr( "BUT_OK" ) );
40     return false; 
41   }
42   return true;
43 }
44
45 /* returns false if can't save file */
46 bool SUIT_FileValidator::canSave( const QString& file ) 
47 {
48   if ( QFile::exists( file ) ) {
49     // if file exists - raise warning...
50     if ( SUIT_MessageBox::warn2( myParent,
51               QObject::tr( "WRN_WARNING" ),
52               QObject::tr( "QUE_DOC_FILEEXISTS" ).arg( file ),
53               QObject::tr( "BUT_YES" ), 
54               QObject::tr( "BUT_NO" ),
55               SUIT_YES, 
56               SUIT_NO, 
57               SUIT_NO ) == SUIT_NO ) {
58       return false;
59     }
60     // ... and if user wants to overwrite file, check it for writeability
61     if ( !QFileInfo( file ).isWritable() ) {
62       SUIT_MessageBox::error1( myParent,
63             QObject::tr( "ERR_ERROR" ),
64             QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
65             QObject::tr( "BUT_OK" ) );
66       return false; 
67     }
68   }
69   else {
70     // if file doesn't exist - try to create it
71     QFile qf( file );
72     if ( !qf.open( IO_WriteOnly ) ) {
73       SUIT_MessageBox::error1( myParent,
74             QObject::tr( "ERR_ERROR" ),
75             QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
76             QObject::tr( "BUT_OK" ) );
77       return false;
78     }
79     else {
80       // remove just created file
81       qf.close();
82       qf.remove();
83     }
84   }
85   return true;
86 }
87