LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / filenamevalidatinglineedit.cpp
blob09fad9c86c475515821d2b7ffdd21ead6ceafd6e
1 /**
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.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
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
22 * for more details.
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>
35 namespace Utils {
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
40 // names.
41 static const QRegExp &windowsDeviceNoSubDirPattern()
43 static const QRegExp rc(QLatin1String(WINDOWS_DEVICES),
44 Qt::CaseInsensitive);
46 QTC_ASSERT(rc.isValid(), return rc);
47 return rc;
50 static const QRegExp &windowsDeviceSubDirPattern()
52 static const QRegExp rc(QLatin1String(".*[/\\\\](" WINDOWS_DEVICES ")"), Qt::CaseInsensitive);
54 QTC_ASSERT(rc.isValid(), return rc);
55 return rc;
58 // ----------- FileNameValidatingLineEdit
59 FileNameValidatingLineEdit::FileNameValidatingLineEdit(QWidget *parent) :
60 BaseValidatingLineEdit(parent),
61 m_allowDirectories(false),
62 m_unused(0)
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. */
77 #ifdef Q_OS_WIN
78 # define SLASHES "/\\"
79 #else
80 # define SLASHES "/"
81 #endif
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*/)
92 if (name.isEmpty()) {
93 if (errorMessage) {
94 *errorMessage = tr("The name must not be empty");
96 return false;
98 // Characters
99 const char *notAllowedChars = allowDirectories ? notAllowedCharsSubDir : notAllowedCharsNoSubDir;
100 for (const char *c = notAllowedChars; *c; c++) {
101 if (name.contains(QLatin1Char(*c))) {
102 if (errorMessage) {
103 *errorMessage = tr("The name must not contain any of the characters '%1'.").arg(QLatin1String(notAllowedChars));
105 return false;
108 // Substrings
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)) {
113 if (errorMessage) {
114 *errorMessage = tr("The name must not contain '%1'.").arg(QString(notAllowedSubString));
116 return false;
119 // Windows devices
120 bool matchesWinDevice = windowsDeviceNoSubDirPattern().exactMatch(name);
121 if (!matchesWinDevice && allowDirectories) {
122 matchesWinDevice = windowsDeviceSubDirPattern().exactMatch(name);
124 if (matchesWinDevice) {
125 if (errorMessage) {
126 *errorMessage = tr("The name must not match that of a MS Windows device. (%1).").
127 arg(windowsDeviceNoSubDirPattern().pattern().replace(QLatin1Char('|'), QLatin1Char(',')));
129 return false;
131 return true;
134 bool FileNameValidatingLineEdit::validate(const QString &value, QString *errorMessage) const
136 return validateFileName(value, m_allowDirectories, errorMessage);
138 } // namespace Utils