OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / filenamevalidatinglineedit.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "filenamevalidatinglineedit.h"
35 #include "qtcassert.h"
36
37 #include <QtCore/QRegExp>
38 #include <QtCore/QDebug>
39
40 namespace Utils {
41
42 #define WINDOWS_DEVICES "CON|AUX|PRN|COM1|COM2|LPT1|LPT2|NUL"
43
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
46 //  names.
47 static const QRegExp &windowsDeviceNoSubDirPattern()
48 {
49     static const QRegExp rc(QLatin1String(WINDOWS_DEVICES),
50                       Qt::CaseInsensitive);
51     QTC_ASSERT(rc.isValid(), return rc);
52     return rc;
53 }
54
55 static const QRegExp &windowsDeviceSubDirPattern()
56 {
57     static const QRegExp rc(QLatin1String(".*[/\\\\](" WINDOWS_DEVICES ")"), Qt::CaseInsensitive);
58     QTC_ASSERT(rc.isValid(), return rc);
59     return rc;
60 }
61
62 // ----------- FileNameValidatingLineEdit
63 FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
64     BaseValidatingLineEdit(parent),
65     m_allowDirectories(false),
66     m_unused(0)
67 {
68 }
69
70 bool FileNameValidatingLineEdit::allowDirectories() const
71 {
72     return m_allowDirectories;
73 }
74
75 void FileNameValidatingLineEdit::setAllowDirectories(bool v)
76 {
77     m_allowDirectories = v;
78 }
79
80 /* Validate a file base name, check for forbidden characters/strings. */
81
82 #ifdef Q_OS_WIN
83 #  define SLASHES "/\\"
84 #else
85 #  define SLASHES "/"
86 #endif
87
88 static const char *notAllowedCharsSubDir   = "?:&*\"|#%<> ";
89 static const char *notAllowedCharsNoSubDir = "?:&*\"|#%<> "SLASHES;
90
91 static const char *notAllowedSubStrings[] = {".."};
92
93 bool FileNameValidatingLineEdit::validateFileName(const QString &name,
94                                                   bool allowDirectories,
95                                                   QString *errorMessage /* = 0*/)
96 {
97     if (name.isEmpty()) {
98         if (errorMessage)
99             *errorMessage = tr("Name is empty.");
100         return false;
101     }
102     // Characters
103     const char *notAllowedChars = allowDirectories ? notAllowedCharsSubDir : notAllowedCharsNoSubDir;
104     for (const char *c = notAllowedChars; *c; c++)
105         if (name.contains(QLatin1Char(*c))) {
106             if (errorMessage) {
107                 if (QChar(*c).isSpace()) {
108                     *errorMessage = tr("Name contains white space.");
109                 } else {
110                     *errorMessage = tr("Invalid character '%1'.").arg(*c);
111                 }
112             }
113             return false;
114         }
115     // Substrings
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)) {
120             if (errorMessage)
121                 *errorMessage = tr("Invalid characters '%1'.").arg(QString(notAllowedSubString));
122             return false;
123         }
124     }
125     // Windows devices
126     bool matchesWinDevice = windowsDeviceNoSubDirPattern().exactMatch(name);
127     if (!matchesWinDevice && allowDirectories)
128         matchesWinDevice = windowsDeviceSubDirPattern().exactMatch(name);
129     if (matchesWinDevice) {
130         if (errorMessage)
131             *errorMessage = tr("Name matches MS Windows device. (%1).").
132                             arg(windowsDeviceNoSubDirPattern().pattern().replace(QLatin1Char('|'), QLatin1Char(',')));
133         return false;
134     }
135     return true;
136 }
137
138 bool  FileNameValidatingLineEdit::validate(const QString &value, QString *errorMessage) const
139 {
140     return validateFileName(value, m_allowDirectories, errorMessage);
141 }
142
143 } // namespace Utils