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