Salome HOME
PAL8065
[modules/kernel.git] / src / SALOMEGUI / QAD_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   : QAD_FileValidator.cxx
8 //  Author : 
9 //  Module : SALOME
10 //  $Header$
11
12 #include "QAD_FileValidator.h"
13 #include "QAD_Application.h"
14 #include "QAD_Desktop.h"
15 #include "QAD_MessageBox.h"
16
17 #include "utilities.h"
18
19 #include <qfile.h>
20 #include <qfileinfo.h>
21
22 using namespace std;
23
24 /* constructor */
25 QAD_FileValidator::QAD_FileValidator(QWidget* parent) :
26 myParent(parent) 
27 {
28   if (!myParent) {
29     myParent = QAD_Application::getDesktop();
30   }
31 }
32   
33 /* returns false if can't open file */
34 bool QAD_FileValidator::canOpen( const QString& file ) 
35 {
36 MESSAGE ( "QAD_FileValidator::canOpen" )
37   if ( !QFile::exists( file ) ) {
38     QAD_MessageBox::error1( myParent,
39                             QObject::tr( "ERR_ERROR" ),
40                             QObject::tr( "ERR_DOC_FILENOTEXIST" ).arg( file ),
41                             QObject::tr( "BUT_OK" ) );
42       return false;
43     }
44   if ( !QFileInfo( file ).isReadable() ) {
45     QAD_MessageBox::error1( myParent,
46                             QObject::tr( "ERR_ERROR" ),
47                             QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
48                             QObject::tr( "BUT_OK" ) );
49     return false; 
50   }
51   return true;
52 }
53
54 /* returns false if can't save file */
55 bool QAD_FileValidator::canSave( const QString& file ) 
56 {
57 MESSAGE ( "QAD_FileValidator::canSave" )
58   if ( QFile::exists( file ) ) {
59     // if file exists - raise warning...
60     if ( QAD_MessageBox::warn2( myParent,
61                                 QObject::tr( "WRN_WARNING" ),
62                                 QObject::tr( "QUE_DOC_FILEEXISTS" ).arg( file ),
63                                 QObject::tr( "BUT_YES" ), 
64                                 QObject::tr( "BUT_NO" ),
65                                 QAD_YES, 
66                                 QAD_NO, 
67                                 QAD_NO ) == QAD_NO ) {
68       return false;
69     }
70     // ... and if user wants to overwrite file, check it for writeability
71     if ( !QFileInfo( file ).isWritable() ) {
72       QAD_MessageBox::error1( myParent,
73                               QObject::tr( "ERR_ERROR" ),
74                               QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
75                               QObject::tr( "BUT_OK" ) );
76       return false; 
77     }
78   }
79   else {
80     // if file doesn't exist - try to create it
81     QFile qf( file );
82     if ( !qf.open( IO_WriteOnly ) ) {
83       QAD_MessageBox::error1( myParent,
84                               QObject::tr( "ERR_ERROR" ),
85                               QObject::tr( "ERR_PERMISSION_DENIED" ).arg( file ),
86                               QObject::tr( "BUT_OK" ) );
87       return false;
88     }
89     else {
90       // remove just created file
91       qf.close();
92       qf.remove();
93     }
94   }
95   return true;
96 }
97