Salome HOME
bc1043187bfa80f639715715e50c537ffb2fadc4
[modules/gui.git] / src / LightApp / LightApp_FileValidator.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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
20 // File   : LightApp_FileValidator.cxx
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22
23 #include "LightApp_FileValidator.h"
24 #include "SUIT_MessageBox.h"
25
26 #include <QFileInfo>
27
28 /*!
29   \class LightApp_PyFileValidator
30   \brief Validator for Python scripts file names. 
31 */
32
33 /*!
34   \brief Constructor
35   \param parent parent widget
36 */
37 LightApp_PyFileValidator::LightApp_PyFileValidator( QWidget* parent )
38   : SUIT_FileValidator ( parent )
39 {
40 }
41
42 /*!
43   \brief Check if the specified file can be written.
44
45   Checks if file name is valid for Python script.
46
47   \param fileName file path
48   \param checkPermission if \c true (default) check also file permissions
49   \return \c false if file exists and user rejects file overwriting
50   or if file does not have write permissions (if \a checkPermission is \c true)
51   \sa SUIT_FileValidator
52 */
53 bool LightApp_PyFileValidator::canSave( const QString& fileName, bool checkPermission )
54 {
55   QFileInfo fi( fileName );
56   if ( !QRegExp( "[A-Za-z_][A-Za-z0-9_]*" ).exactMatch( fi.completeBaseName() ) ) {
57     SUIT_MessageBox::critical( parent(),
58                                QObject::tr( "WRN_WARNING" ),
59                                QObject::tr( "WRN_PYTHON_FILE_NAME_BAD" ) );
60     return false;
61   }
62   return SUIT_FileValidator::canSave( fileName, checkPermission );
63 }