mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-13 15:45:42 +08:00
refs #288, improved validation in some value objects
This commit is contained in:
committed by
Roland Winklmeier
parent
5ff28cb3a1
commit
d1d02d6c3c
@@ -128,5 +128,25 @@ namespace BlackMisc
|
||||
return (regexp.match(designator).hasMatch());
|
||||
}
|
||||
|
||||
/*
|
||||
* Valid combined type
|
||||
*/
|
||||
bool CAircraftIcao::isValidCombinedType(const QString &combinedType)
|
||||
{
|
||||
static QRegularExpression regexp("^[A-Z][0-9][A-Z]$");
|
||||
if (combinedType.length() != 3) return false;
|
||||
return (regexp.match(combinedType).hasMatch());
|
||||
}
|
||||
|
||||
/*
|
||||
* Valid airline designator
|
||||
*/
|
||||
bool CAircraftIcao::isValidAirlineDesignator(const QString &airline)
|
||||
{
|
||||
static QRegularExpression regexp("^[A-Z]+[A-Z0-9]*$");
|
||||
if (airline.length() < 2 || airline.length() > 5) return false;
|
||||
return (regexp.match(airline).hasMatch());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -138,6 +138,12 @@ namespace BlackMisc
|
||||
//! Valid designator?
|
||||
static bool isValidDesignator(const QString &designator);
|
||||
|
||||
//! Valid combined type
|
||||
static bool isValidCombinedType(const QString &combinedType);
|
||||
|
||||
//! Valid designator?
|
||||
static bool isValidAirlineDesignator(const QString &airline);
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
@@ -126,5 +126,15 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Valid callsign?
|
||||
*/
|
||||
bool CCallsign::isValidCallsign(const QString &callsign)
|
||||
{
|
||||
static QRegularExpression regexp("^[A-Z]+[A-Z0-9]*$");
|
||||
if (callsign.length() < 2 || callsign.length() > 10) return false;
|
||||
return (regexp.match(callsign).hasMatch());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -75,6 +75,9 @@ namespace BlackMisc
|
||||
//! \copydoc CValueObject::setPropertyByIndex(variant, index)
|
||||
virtual void setPropertyByIndex(const QVariant &variant, const BlackMisc::CPropertyIndex &index) override;
|
||||
|
||||
//! Valid callsign?
|
||||
static bool isValidCallsign(const QString &callsign);
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString()
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
@@ -38,6 +38,17 @@ namespace BlackMisc
|
||||
return this->m_user.hasValidCredentials() && this->m_port > 0 && !this->m_address.isEmpty() && this->isAcceptingConnections();
|
||||
}
|
||||
|
||||
CStatusMessageList CServer::validate() const
|
||||
{
|
||||
CStatusMessageList msgs;
|
||||
if (this->getName().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing name")); }
|
||||
if (this->getAddress().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing address")); }
|
||||
if (this->getDescription().isEmpty()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Missing description")); }
|
||||
if (this->getPort() < 1 || this->getPort() > 65535) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Wrong port")); }
|
||||
msgs.push_back(this->getUser().validate());
|
||||
return msgs;
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
|
||||
#ifndef BLACKMISC_SERVER_H
|
||||
#define BLACKMISC_SERVER_H
|
||||
|
||||
#include "nwuser.h"
|
||||
#include "statusmessagelist.h"
|
||||
#include "valueobject.h"
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -46,7 +48,7 @@ namespace BlackMisc
|
||||
const QString &getAddress() const { return m_address; }
|
||||
|
||||
//! Set address (e.g. myserver.foo.com)
|
||||
void setAddress(const QString &address) { m_address = address; }
|
||||
void setAddress(const QString &address) { m_address = address.trimmed(); }
|
||||
|
||||
//! Get user
|
||||
const CUser &getUser() const { return m_user; }
|
||||
@@ -58,13 +60,13 @@ namespace BlackMisc
|
||||
const QString &getName() const { return m_name; }
|
||||
|
||||
//! Set name
|
||||
void setName(const QString &name) { m_name = name; }
|
||||
void setName(const QString &name) { m_name = name.trimmed(); }
|
||||
|
||||
//! Get description
|
||||
const QString &getDescription() const { return m_description; }
|
||||
|
||||
//! Set description
|
||||
void setDescription(const QString &description) { m_description = description; }
|
||||
void setDescription(const QString &description) { m_description = description.trimmed().simplified(); }
|
||||
|
||||
//! Get port
|
||||
qint32 getPort() const { return m_port; }
|
||||
@@ -81,6 +83,9 @@ namespace BlackMisc
|
||||
//! Is valid for login?
|
||||
bool isValidForLogin() const;
|
||||
|
||||
//! Validate, provide details about issues
|
||||
BlackMisc::CStatusMessageList validate() const;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex(int)
|
||||
virtual QVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
|
||||
|
||||
|
||||
@@ -17,9 +17,6 @@ namespace BlackMisc
|
||||
{
|
||||
namespace Network
|
||||
{
|
||||
/*
|
||||
* Convert to string
|
||||
*/
|
||||
QString CUser::convertToQString(bool /** i18n **/) const
|
||||
{
|
||||
if (this->m_realname.isEmpty()) return "<no realname>";
|
||||
@@ -35,6 +32,16 @@ namespace BlackMisc
|
||||
return s;
|
||||
}
|
||||
|
||||
CStatusMessageList CUser::validate() const
|
||||
{
|
||||
CStatusMessageList msgs;
|
||||
// callsign optional
|
||||
if (!this->hasValidId()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Invalid id"));}
|
||||
if (!this->hasValidRealName()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Invalid real name"));}
|
||||
if (!this->hasValidCredentials()) { msgs.push_back(CStatusMessage(CStatusMessage::SeverityWarning, "Invalid credentials"));}
|
||||
return msgs;
|
||||
}
|
||||
|
||||
/*
|
||||
* Exchange data
|
||||
*/
|
||||
@@ -63,6 +70,15 @@ namespace BlackMisc
|
||||
this->setCallsign(otherUser.getCallsign());
|
||||
}
|
||||
|
||||
bool CUser::isValidVatsimId(const QString &id)
|
||||
{
|
||||
if (id.isEmpty()) { return false; }
|
||||
bool ok;
|
||||
int i = id.toInt(&ok);
|
||||
if (!ok) { return false; }
|
||||
return i >= 100000 && i <= 9999999;
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "avcallsign.h"
|
||||
#include "avairporticao.h"
|
||||
#include "propertyindex.h"
|
||||
#include "statusmessagelist.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -60,7 +61,7 @@ namespace BlackMisc
|
||||
QString getRealName() const { return m_realname; }
|
||||
|
||||
//! setRealName
|
||||
void setRealName(const QString &realname) { m_realname = realname.trimmed(); }
|
||||
void setRealName(const QString &realname) { m_realname = realname.trimmed().simplified(); }
|
||||
|
||||
//! Get password
|
||||
QString getPassword() const { return m_password; }
|
||||
@@ -83,6 +84,9 @@ namespace BlackMisc
|
||||
//! Has associated callsign?
|
||||
bool hasValidCallsign() const { return !m_callsign.isEmpty(); }
|
||||
|
||||
//! Validate, provide details about issues
|
||||
BlackMisc::CStatusMessageList validate() const;
|
||||
|
||||
//! Get email.
|
||||
QString getEmail() const { return m_email; }
|
||||
|
||||
@@ -125,6 +129,9 @@ namespace BlackMisc
|
||||
*/
|
||||
void syncronizeData(CUser &otherUser);
|
||||
|
||||
//! Valid VATSIM id
|
||||
static bool isValidVatsimId(const QString &id);
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
Reference in New Issue
Block a user