diff --git a/src/blackgui/listmodelbase.cpp b/src/blackgui/listmodelbase.cpp index 9e7263ff5..fc58df6f6 100644 --- a/src/blackgui/listmodelbase.cpp +++ b/src/blackgui/listmodelbase.cpp @@ -9,6 +9,7 @@ #include "blackmisc/avaircraftlist.h" #include "blackmisc/nwserverlist.h" #include "blackmisc/nwuserlist.h" +#include "blackmisc/nwclientlist.h" #include "blackmisc/hwkeyboardkeylist.h" #include "blackmisc/blackmiscfreefunctions.h" @@ -210,6 +211,7 @@ namespace BlackGui template class CListModelBase; template class CListModelBase; template class CListModelBase; + template class CListModelBase; template class CListModelBase; } // namespace diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 3d1550b8c..4b8d14aa6 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -92,6 +92,9 @@ void BlackMisc::Network::registerMetadata() CServerList::registerMetadata(); CTextMessage::registerMetadata(); CTextMessageList::registerMetadata(); + CClient::registerMetadata(); + CClientList::registerMetadata(); + CAircraftModel::registerMetadata(); } /* diff --git a/src/blackmisc/networkallclasses.h b/src/blackmisc/networkallclasses.h index db92ae735..4fe9bc817 100644 --- a/src/blackmisc/networkallclasses.h +++ b/src/blackmisc/networkallclasses.h @@ -10,5 +10,8 @@ #include "blackmisc/nwuserlist.h" #include "blackmisc/nwserverlist.h" #include "blackmisc/nwtextmessagelist.h" +#include "blackmisc/nwclient.h" +#include "blackmisc/nwclientlist.h" +#include "blackmisc/nwaircraftmodel.h" #endif // guard diff --git a/src/blackmisc/nwaircraftmodel.cpp b/src/blackmisc/nwaircraftmodel.cpp new file mode 100644 index 000000000..b745c1d37 --- /dev/null +++ b/src/blackmisc/nwaircraftmodel.cpp @@ -0,0 +1,152 @@ +#include "nwaircraftmodel.h" +#include + +namespace BlackMisc +{ + namespace Network + { + /* + * Convert to string + */ + QString CAircraftModel::convertToQString(bool /** i18n **/) const + { + QString s = this->m_queriedModelString; + return s; + } + + /* + * Compare + */ + int CAircraftModel::compareImpl(const CValueObject &otherBase) const + { + const auto &other = static_cast(otherBase); + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); + } + + /* + * Marshall to DBus + */ + void CAircraftModel::marshallToDbus(QDBusArgument &argument) const + { + argument << TupleConverter::toTuple(*this); + } + + /* + * Unmarshall from DBus + */ + void CAircraftModel::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> TupleConverter::toTuple(*this); + } + + /* + * Hash + */ + uint CAircraftModel::getValueHash() const + { + return qHash(TupleConverter::toTuple(*this)); + } + + /* + * Equal? + */ + bool CAircraftModel::operator ==(const CAircraftModel &other) const + { + if (this == &other) return true; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); + } + + /* + * Unequal? + */ + bool CAircraftModel::operator !=(const CAircraftModel &other) const + { + return !((*this) == other); + } + + /* + * metaTypeId + */ + int CAircraftModel::getMetaTypeId() const + { + return qMetaTypeId(); + } + + /* + * is a + */ + bool CAircraftModel::isA(int metaTypeId) const + { + if (metaTypeId == qMetaTypeId()) { return true; } + return this->CValueObject::isA(metaTypeId); + } + + /* + * Register metadata + */ + void CAircraftModel::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + /* + * Members + */ + const QStringList &CAircraftModel::jsonMembers() + { + return TupleConverter::jsonMembers(); + } + + /* + * To JSON + */ + QJsonObject CAircraftModel::toJson() const + { + return BlackMisc::serializeJson(CAircraftModel::jsonMembers(), TupleConverter::toTuple(*this)); + } + + /* + * From Json + */ + void CAircraftModel::fromJson(const QJsonObject &json) + { + BlackMisc::deserializeJson(json, CAircraftModel::jsonMembers(), TupleConverter::toTuple(*this)); + } + + /* + * Property by index + */ + QVariant CAircraftModel::propertyByIndex(int index) const + { + switch (index) + { + case IndexQueriedModelString: + return QVariant(this->m_queriedModelString); + break; + default: + break; + } + Q_ASSERT_X(false, "CAircraftModel", "index unknown"); + QString m = QString("no property, index ").append(QString::number(index)); + return QVariant::fromValue(m); + } + + /* + * Set property as index + */ + void CAircraftModel::setPropertyByIndex(const QVariant &variant, int index) + { + switch (index) + { + case IndexQueriedModelString: + this->m_queriedModelString = variant.toString(); + break; + default: + Q_ASSERT_X(false, "CAircraftModel", "index unknown"); + break; + } + } + + } // namespace +} // namespace diff --git a/src/blackmisc/nwaircraftmodel.h b/src/blackmisc/nwaircraftmodel.h new file mode 100644 index 000000000..e5d62b9fb --- /dev/null +++ b/src/blackmisc/nwaircraftmodel.h @@ -0,0 +1,99 @@ +#ifndef BLACKMISC_AIRCRAFTMODEL_H +#define BLACKMISC_AIRCRAFTMODEL_H + +#include "nwuser.h" +#include "valueobject.h" + +namespace BlackMisc +{ + namespace Network + { + /*! + * Another pilot's aircraft model + */ + class CAircraftModel : public BlackMisc::CValueObject + { + + public: + + //! Indexes + enum ColumnIndex : uint + { + IndexQueriedModelString = 100 + }; + + //! \brief Default constructor. + CAircraftModel() {} + + //! \brief Constructor. + CAircraftModel(const QString &directModel) : m_queriedModelString(directModel) {} + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); } + + //! \brief Equal operator == + bool operator ==(const CAircraftModel &other) const; + + //! \brief Unequal operator != + bool operator !=(const CAircraftModel &other) const; + + //! \copydoc CValueObject::getValueHash + virtual uint getValueHash() const override; + + //! \copydoc CValueObject::toJson + virtual QJsonObject toJson() const override; + + //! \copydoc CValueObject::fromJson + void fromJson(const QJsonObject &json) override; + + //! \copydoc CValueObject::propertyByIndex(int) + virtual QVariant propertyByIndex(int index) const override; + + //! \copydoc CValueObject::setPropertyByIndex(const QVariant, int) + virtual void setPropertyByIndex(const QVariant &variant, int index) override; + + //! Queried model string + const QString &getQueriedModelString() const { return this->m_queriedModelString; } + + //! Set queried model string + void setQueriedModelString(const QString &model) { this->m_queriedModelString = model; } + + //! Queried model string? + bool hasQueriedModelString() const { return !this->m_queriedModelString.isEmpty(); } + + //! \brief Register metadata + static void registerMetadata(); + + //! \copydoc TupleConverter<>::jsonMembers() + static const QStringList &jsonMembers(); + + protected: + //! \copydoc CValueObject::convertToQString + virtual QString convertToQString(bool i18n = false) const override; + + //! \copydoc CValueObject::getMetaTypeId + virtual int getMetaTypeId() const override; + + //! \copydoc CValueObject::isA + virtual bool isA(int metaTypeId) const override; + + //! \copydoc CValueObject::compareImpl + virtual int compareImpl(const CValueObject &other) const override; + + //! \copydoc CValueObject::marshallToDbus + virtual void marshallToDbus(QDBusArgument &argument) const override; + + //! \copydoc CValueObject::unmarshallFromDbus + virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CAircraftModel) + QString m_queriedModelString; + }; + } // namespace +} // namespace + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CAircraftModel, (o.m_queriedModelString)) +Q_DECLARE_METATYPE(BlackMisc::Network::CAircraftModel) + +#endif // guard diff --git a/src/blackmisc/nwclient.cpp b/src/blackmisc/nwclient.cpp new file mode 100644 index 000000000..349cfb3a7 --- /dev/null +++ b/src/blackmisc/nwclient.cpp @@ -0,0 +1,226 @@ +#include "nwclient.h" +#include + +namespace BlackMisc +{ + namespace Network + { + /* + * Convert to string + */ + QString CClient::convertToQString(bool i18n) const + { + QString s = this->m_user.toQString(i18n); + return s; + } + + /* + * Compare + */ + int CClient::compareImpl(const CValueObject &otherBase) const + { + const auto &other = static_cast(otherBase); + return compare(TupleConverter::toTuple(*this), TupleConverter::toTuple(other)); + } + + /* + * Marshall to DBus + */ + void CClient::marshallToDbus(QDBusArgument &argument) const + { + argument << TupleConverter::toTuple(*this); + } + + /* + * Unmarshall from DBus + */ + void CClient::unmarshallFromDbus(const QDBusArgument &argument) + { + argument >> TupleConverter::toTuple(*this); + } + + /* + * Hash + */ + uint CClient::getValueHash() const + { + return qHash(TupleConverter::toTuple(*this)); + } + + /* + * Equal? + */ + bool CClient::operator ==(const CClient &other) const + { + if (this == &other) return true; + return TupleConverter::toTuple(*this) == TupleConverter::toTuple(other); + } + + /* + * Unequal? + */ + bool CClient::operator !=(const CClient &other) const + { + return !((*this) == other); + } + + /* + * metaTypeId + */ + int CClient::getMetaTypeId() const + { + return qMetaTypeId(); + } + + /* + * is a + */ + bool CClient::isA(int metaTypeId) const + { + if (metaTypeId == qMetaTypeId()) { return true; } + return this->CValueObject::isA(metaTypeId); + } + + /* + * Register metadata + */ + void CClient::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + /* + * Members + */ + const QStringList &CClient::jsonMembers() + { + return TupleConverter::jsonMembers(); + } + + /* + * To JSON + */ + QJsonObject CClient::toJson() const + { + return BlackMisc::serializeJson(CClient::jsonMembers(), TupleConverter::toTuple(*this)); + } + + /* + * From Json + */ + void CClient::fromJson(const QJsonObject &json) + { + BlackMisc::deserializeJson(json, CClient::jsonMembers(), TupleConverter::toTuple(*this)); + } + + /* + * Capability + */ + void CClient::setCapability(bool hasCapability, CClient::Capabilities capability) + { + this->m_capabilities.setPropertyByIndex(QVariant(hasCapability), capability); + } + + /* + * Capabilities + */ + void CClient::setCapabilities(const CIndexVariantMap &capabilities) + { + this->m_capabilities = capabilities; + } + + /* + * Capabilities + */ + QString CClient::getCapabilitiesAsString() const + { + QStringList sl; + if (this->hasCapability(FsdAtisCanBeReceived)) sl << "ATIS"; + if (this->hasCapability(FsdWithInterimPositions)) sl << "interim pos."; + if (this->hasCapability(FsdWithModelDescription)) sl << "model"; + if (sl.isEmpty()) return ""; + return sl.join(", "); + } + + /* + * Capability + */ + bool CClient::hasCapability(CClient::Capabilities capability) const + { + if (this->m_capabilities.contains(capability)) + return this->m_capabilities.value(capability).toBool(); + else + return false; + } + + /* + * Property by index + */ + QVariant CClient::propertyByIndex(int index) const + { + if (index < static_cast(IndexQueriedModelString)) + { + return this->m_user.propertyByIndex(index); + } + else if (index < static_cast(IndexCapabilities)) + { + return this->m_model.propertyByIndex(index); + } + + switch (index) + { + case IndexCapabilities: + return this->m_capabilities.toQVariant(); + break; + case IndexCapabilitiesString: + return QVariant(this->getCapabilitiesAsString()); + break; + case IndexModel: + return QVariant(this->m_model.toQVariant()); + break; + case IndexHost: + return QVariant(this->m_host); + break; + default: + break; + } + Q_ASSERT_X(false, "CClient", "index unknown"); + QString m = QString("no property, index ").append(QString::number(index)); + return QVariant::fromValue(m); + } + + /* + * Set property as index + */ + void CClient::setPropertyByIndex(const QVariant &variant, int index) + { + if (index < static_cast(IndexQueriedModelString)) + { + this->m_user.setPropertyByIndex(variant, index); + return; + } + else if (index < static_cast(IndexCapabilities)) + { + this->m_model.setPropertyByIndex(variant, index); + return; + } + + switch (index) + { + case IndexCapabilities: + this->m_capabilities = variant.value(); + break; + case IndexModel: + this->m_model = variant.value(); + break; + case IndexHost: + this->m_host = variant.toString(); + break; + default: + Q_ASSERT_X(false, "CClient", "index unknown"); + break; + } + } + } // namespace +} // namespace diff --git a/src/blackmisc/nwclient.h b/src/blackmisc/nwclient.h new file mode 100644 index 000000000..5ffb87b09 --- /dev/null +++ b/src/blackmisc/nwclient.h @@ -0,0 +1,153 @@ +#ifndef BLACKMISC_CLIENT_H +#define BLACKMISC_CLIENT_H + +#include "nwuser.h" +#include "nwaircraftmodel.h" +#include "valueobject.h" +#include "indexvariantmap.h" + +namespace BlackMisc +{ + namespace Network + { + /*! + * Another client software. + */ + class CClient : public BlackMisc::CValueObject + { + + public: + /*! + * \brief Properties by index + */ + enum ColumnIndex : uint + { + IndexEmail = 0, + IndexId, + IndexPassword, + IndexRealName, + IndexCallsign, + IndexCallsignIcon, + IndexQueriedModelString = 100, + IndexCapabilities = 1000, + IndexCapabilitiesString, + IndexModel, // own indexes + IndexHost + }; + + /*! + * \brief The Capabilities enum + */ + enum Capabilities + { + FsdWithInterimPositions, + FsdWithModelDescription, + FsdAtisCanBeReceived + }; + + //! Default constructor. + CClient() {} + + //! Construct by callsign + CClient(const BlackMisc::Aviation::CCallsign &callsign) : m_user(CUser(callsign)) {} + + //! Constructor. + CClient(const CUser &user) : m_user(user) {} + + //! \copydoc CValueObject::toQVariant + virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); } + + //! Equal operator == + bool operator ==(const CClient &other) const; + + //! Unequal operator != + bool operator !=(const CClient &other) const; + + //! \copydoc CValueObject::getValueHash + virtual uint getValueHash() const override; + + //! \copydoc CValueObject::toJson + virtual QJsonObject toJson() const override; + + //! Callsign used with other client + const BlackMisc::Aviation::CCallsign &getCallsign() const { return this->m_user.getCallsign(); } + + //! \copydoc CValueObject::fromJson + void fromJson(const QJsonObject &json) override; + + + //! Get capabilities + CIndexVariantMap getCapabilities() const { return this->m_capabilities; } + + //! Set capability + void setCapability(bool hasCapability, Capabilities capability); + + //! Set capabilities + void setCapabilities(const CIndexVariantMap &capabilities); + + //! Get capabilities + QString getCapabilitiesAsString() const; + + //! Has capability? + bool hasCapability(Capabilities capability) const; + + //! Host + const QString &getHost() const { return this->m_host; } + + //! Host + void setHost(const QString &host) { this->m_host = host;} + + //! Model + const CAircraftModel &getAircraftModel() const { return this->m_model; } + + //! Set model + void setAircraftModel(const CAircraftModel &model) { this->m_model = model; } + + //! \copydoc CValueObject::toIcon() + virtual const QPixmap &toIcon() const override { return this->m_user.toIcon(); } + + //! Register metadata + static void registerMetadata(); + + //! \copydoc TupleConverter<>::jsonMembers() + static const QStringList &jsonMembers(); + + //! \copydoc CValueObject::propertyByIndex(int) + virtual QVariant propertyByIndex(int index) const override; + + //! \copydoc CValueObject::setPropertyByIndex(const QVariant, int) + virtual void setPropertyByIndex(const QVariant &variant, int index) override; + + protected: + //! \copydoc CValueObject::convertToQString + virtual QString convertToQString(bool i18n = false) const override; + + //! \copydoc CValueObject::getMetaTypeId + virtual int getMetaTypeId() const override; + + //! \copydoc CValueObject::isA + virtual bool isA(int metaTypeId) const override; + + //! \copydoc CValueObject::compareImpl + virtual int compareImpl(const CValueObject &other) const override; + + //! \copydoc CValueObject::marshallToDbus + virtual void marshallToDbus(QDBusArgument &argument) const override; + + //! \copydoc CValueObject::unmarshallFromDbus + virtual void unmarshallFromDbus(const QDBusArgument &argument) override; + + private: + BLACK_ENABLE_TUPLE_CONVERSION(CClient) + CUser m_user; + CAircraftModel m_model; + CIndexVariantMap m_capabilities; + QString m_host; + }; + } // namespace +} // namespace + +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Network::CClient, (o.m_user, o.m_model, o.m_capabilities)) +Q_DECLARE_METATYPE(BlackMisc::Network::CClient) + +#endif // guard diff --git a/src/blackmisc/nwclientlist.cpp b/src/blackmisc/nwclientlist.cpp new file mode 100644 index 000000000..c1f287a1e --- /dev/null +++ b/src/blackmisc/nwclientlist.cpp @@ -0,0 +1,34 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nwclientlist.h" +#include "predicates.h" + +namespace BlackMisc +{ + namespace Network + { + /* + * Default constructor + */ + CClientList::CClientList() { } + + /* + * Construct from base class object + */ + CClientList::CClientList(const CSequence &other) : CSequence(other) + { } + + /* + * Register metadata + */ + void CClientList::registerMetadata() + { + qRegisterMetaType(); + qDBusRegisterMetaType(); + } + + } // namespace +} // namespace diff --git a/src/blackmisc/nwclientlist.h b/src/blackmisc/nwclientlist.h new file mode 100644 index 000000000..0f309a112 --- /dev/null +++ b/src/blackmisc/nwclientlist.h @@ -0,0 +1,45 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKMISC_CLIENTLIST_H +#define BLACKMISC_CLIENTLIST_H + +#include "nwclient.h" +#include "sequence.h" +#include "collection.h" +#include +#include +#include + +namespace BlackMisc +{ + namespace Network + { + //! Value object encapsulating a list of voice rooms. + class CClientList : public CSequence + { + public: + //! \brief Default constructor. + CClientList(); + + //! Construct from a base class object. + CClientList(const CSequence &other); + + //! QVariant, required for DBus QVariant lists + virtual QVariant asQVariant() const { return QVariant::fromValue(*this); } + + //! Register metadata + static void registerMetadata(); + + }; + + } //namespace +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Network::CClientList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard