1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "filenamevalidatinglineedit.h"
35 #include "qtcassert.h"
37 #include <QtCore/QRegExp>
38 #include <QtCore/QDebug>
42 #define WINDOWS_DEVICES "CON|AUX|PRN|COM1|COM2|LPT1|LPT2|NUL"
44 // Naming a file like a device name will break on Windows, even if it is
45 // "com1.txt". Since we are cross-platform, we generally disallow such file
47 static const QRegExp &windowsDeviceNoSubDirPattern()
49 static const QRegExp rc(QLatin1String(WINDOWS_DEVICES),
51 QTC_ASSERT(rc.isValid(), return rc);
55 static const QRegExp &windowsDeviceSubDirPattern()
57 static const QRegExp rc(QLatin1String(".*[/\\\\](" WINDOWS_DEVICES ")"), Qt::CaseInsensitive);
58 QTC_ASSERT(rc.isValid(), return rc);
62 // ----------- FileNameValidatingLineEdit
63 FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
64 BaseValidatingLineEdit(parent),
65 m_allowDirectories(false),
70 bool FileNameValidatingLineEdit::allowDirectories() const
72 return m_allowDirectories;
75 void FileNameValidatingLineEdit::setAllowDirectories(bool v)
77 m_allowDirectories = v;
80 /* Validate a file base name, check for forbidden characters/strings. */
83 # define SLASHES "/\\"
88 static const char *notAllowedCharsSubDir = "?:&*\"|#%<> ";
89 static const char *notAllowedCharsNoSubDir = "?:&*\"|#%<> "SLASHES;
91 static const char *notAllowedSubStrings[] = {".."};
93 bool FileNameValidatingLineEdit::validateFileName(const QString &name,
94 bool allowDirectories,
95 QString *errorMessage /* = 0*/)
99 *errorMessage = tr("Name is empty.");
103 const char *notAllowedChars = allowDirectories ? notAllowedCharsSubDir : notAllowedCharsNoSubDir;
104 for (const char *c = notAllowedChars; *c; c++)
105 if (name.contains(QLatin1Char(*c))) {
107 if (QChar(*c).isSpace()) {
108 *errorMessage = tr("Name contains white space.");
110 *errorMessage = tr("Invalid character '%1'.").arg(*c);
116 const int notAllowedSubStringCount = sizeof(notAllowedSubStrings)/sizeof(const char *);
117 for (int s = 0; s < notAllowedSubStringCount; s++) {
118 const QLatin1String notAllowedSubString(notAllowedSubStrings[s]);
119 if (name.contains(notAllowedSubString)) {
121 *errorMessage = tr("Invalid characters '%1'.").arg(QString(notAllowedSubString));
126 bool matchesWinDevice = windowsDeviceNoSubDirPattern().exactMatch(name);
127 if (!matchesWinDevice && allowDirectories)
128 matchesWinDevice = windowsDeviceSubDirPattern().exactMatch(name);
129 if (matchesWinDevice) {
131 *errorMessage = tr("Name matches MS Windows device. (%1).").
132 arg(windowsDeviceNoSubDirPattern().pattern().replace(QLatin1Char('|'), QLatin1Char(',')));
138 bool FileNameValidatingLineEdit::validate(const QString &value, QString *errorMessage) const
140 return validateFileName(value, m_allowDirectories, errorMessage);