2 ******************************************************************************
4 * @file filenamevalidatinglineedit.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "filenamevalidatinglineedit.h"
30 #include "qtcassert.h"
32 #include <QtCore/QRegExp>
33 #include <QtCore/QDebug>
36 #define WINDOWS_DEVICES "CON|AUX|PRN|COM1|COM2|LPT1|LPT2|NUL"
38 // Naming a file like a device name will break on Windows, even if it is
39 // "com1.txt". Since we are cross-platform, we generally disallow such file
41 static const QRegExp
&windowsDeviceNoSubDirPattern()
43 static const QRegExp
rc(QLatin1String(WINDOWS_DEVICES
),
46 QTC_ASSERT(rc
.isValid(), return rc
);
50 static const QRegExp
&windowsDeviceSubDirPattern()
52 static const QRegExp
rc(QLatin1String(".*[/\\\\](" WINDOWS_DEVICES
")"), Qt::CaseInsensitive
);
54 QTC_ASSERT(rc
.isValid(), return rc
);
58 // ----------- FileNameValidatingLineEdit
59 FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget
*parent
) :
60 BaseValidatingLineEdit(parent
),
61 m_allowDirectories(false),
65 bool FileNameValidatingLineEdit::allowDirectories() const
67 return m_allowDirectories
;
70 void FileNameValidatingLineEdit::setAllowDirectories(bool v
)
72 m_allowDirectories
= v
;
75 /* Validate a file base name, check for forbidden characters/strings. */
78 # define SLASHES "/\\"
83 static const char *notAllowedCharsSubDir
= "?:&*\"|#%<> ";
84 static const char *notAllowedCharsNoSubDir
= "?:&*\"|#%<> " SLASHES
;
86 static const char *notAllowedSubStrings
[] = { ".." };
88 bool FileNameValidatingLineEdit::validateFileName(const QString
&name
,
89 bool allowDirectories
,
90 QString
*errorMessage
/* = 0*/)
94 *errorMessage
= tr("The name must not be empty");
99 const char *notAllowedChars
= allowDirectories
? notAllowedCharsSubDir
: notAllowedCharsNoSubDir
;
100 for (const char *c
= notAllowedChars
; *c
; c
++) {
101 if (name
.contains(QLatin1Char(*c
))) {
103 *errorMessage
= tr("The name must not contain any of the characters '%1'.").arg(QLatin1String(notAllowedChars
));
109 const int notAllowedSubStringCount
= sizeof(notAllowedSubStrings
) / sizeof(const char *);
110 for (int s
= 0; s
< notAllowedSubStringCount
; s
++) {
111 const QLatin1String
notAllowedSubString(notAllowedSubStrings
[s
]);
112 if (name
.contains(notAllowedSubString
)) {
114 *errorMessage
= tr("The name must not contain '%1'.").arg(QString(notAllowedSubString
));
120 bool matchesWinDevice
= windowsDeviceNoSubDirPattern().exactMatch(name
);
121 if (!matchesWinDevice
&& allowDirectories
) {
122 matchesWinDevice
= windowsDeviceSubDirPattern().exactMatch(name
);
124 if (matchesWinDevice
) {
126 *errorMessage
= tr("The name must not match that of a MS Windows device. (%1).").
127 arg(windowsDeviceNoSubDirPattern().pattern().replace(QLatin1Char('|'), QLatin1Char(',')));
134 bool FileNameValidatingLineEdit::validate(const QString
&value
, QString
*errorMessage
) const
136 return validateFileName(value
, m_allowDirectories
, errorMessage
);