File indexing completed on 2024-05-05 05:40:32

0001 #include "network/connectionprofile.h"
0002 
0003 #include <QCryptographicHash>
0004 #include <QDebug>
0005 
0006 ConnectionProfile::ConnectionProfile()
0007     : m_title(QObject::tr("Unknown")), m_playerName(QObject::tr("Player")), m_playerColor(Qt::red)
0008 {
0009     // binding
0010 
0011     auto updateValid= [this]()
0012     {
0013         // qDebug() << "connection info" << m_validConnectionInfo << "player info" << m_validPlayerInfo << "character:"
0014         // << m_validCharacter << "campaign:"<<m_validCampaign;
0015         setValid(m_validConnectionInfo && m_validPlayerInfo && m_validCharacter && m_validCampaign
0016                  && !m_title.isEmpty());
0017     };
0018 
0019     connect(this, &ConnectionProfile::playerInfoValidChanged, this, updateValid);
0020     connect(this, &ConnectionProfile::charactersValidChanged, this, updateValid);
0021     connect(this, &ConnectionProfile::campaignInfoValidChanged, this, updateValid);
0022     connect(this, &ConnectionProfile::connectionInfoValidChanged, this, updateValid);
0023 
0024     auto updateConnectionInfo
0025         = [this]() { setConnectionInfoValid(m_port > 0 && m_server ? m_server : !m_address.isEmpty()); };
0026 
0027     connect(this, &ConnectionProfile::serverChanged, this, updateConnectionInfo);
0028     connect(this, &ConnectionProfile::portChanged, this, updateConnectionInfo);
0029     connect(this, &ConnectionProfile::addressChanged, this, updateConnectionInfo);
0030 
0031     updateConnectionInfo();
0032     updateValid();
0033 
0034     // Signals
0035     /*m_handlers.push_back(std::make_optional(m_valid.onValueChanged(StdFunc([this]() { emit validChanged(); }))));
0036     m_handlers.push_back(std::make_optional(
0037         m_validConnectionInfo.onValueChanged(StdFunc([this]() { emit connectionInfoValidChanged(); }))));
0038     m_handlers.push_back(
0039         std::make_optional(m_validPlayerInfo.onValueChanged(StdFunc([this]() { emit playerInfoValidChanged(); }))));
0040     m_handlers.push_back(
0041         std::make_optional(m_validCharacter.onValueChanged(StdFunc([this]() { emit charactersValidChanged(); }))));
0042     m_handlers.push_back(
0043         std::make_optional(m_validCampaign.onValueChanged(StdFunc([this]() { emit campaignInfoValidChanged(); }))));*/
0044 }
0045 ConnectionProfile::~ConnectionProfile() {}
0046 void ConnectionProfile::setProfileTitle(const QString& str)
0047 {
0048     if(str == m_title)
0049         return;
0050     m_title= str;
0051     emit titleChanged();
0052 }
0053 void ConnectionProfile::setPlayerName(const QString& str)
0054 {
0055     if(str == m_playerName)
0056         return;
0057     m_playerName= str;
0058     emit playerNameChanged();
0059 }
0060 void ConnectionProfile::setAddress(const QString& str)
0061 {
0062     if(str == m_address)
0063         return;
0064     m_address= str;
0065     emit addressChanged();
0066 }
0067 void ConnectionProfile::setPort(quint16 i)
0068 {
0069     if(m_port == i)
0070         return;
0071     m_port= i;
0072     emit portChanged();
0073 }
0074 void ConnectionProfile::setServerMode(bool b)
0075 {
0076     if(b == m_server)
0077         return;
0078     m_server= b;
0079     emit serverChanged();
0080 }
0081 
0082 void ConnectionProfile::setGm(bool b)
0083 {
0084     if(m_isGM == b)
0085         return;
0086     m_isGM= b;
0087     emit gmChanged();
0088 }
0089 QString ConnectionProfile::profileTitle() const
0090 {
0091     return m_title;
0092 }
0093 QString ConnectionProfile::playerName() const
0094 {
0095     return m_playerName;
0096 }
0097 
0098 void ConnectionProfile::setPlayerColor(const QColor& color)
0099 {
0100     if(color == m_playerColor)
0101         return;
0102     m_playerColor= color;
0103     emit playerColorChanged();
0104 }
0105 
0106 QColor ConnectionProfile::playerColor() const
0107 {
0108     return m_playerColor;
0109 }
0110 
0111 void ConnectionProfile::setPlayerAvatar(const QByteArray& data)
0112 {
0113     if(data == m_playerAvatar)
0114         return;
0115     m_playerAvatar= data;
0116     emit playerAvatarChanged();
0117 }
0118 
0119 QByteArray ConnectionProfile::playerAvatar() const
0120 {
0121     return m_playerAvatar;
0122 }
0123 QString ConnectionProfile::address() const
0124 {
0125     return m_address;
0126 }
0127 quint16 ConnectionProfile::port() const
0128 {
0129     return m_port;
0130 }
0131 bool ConnectionProfile::isServer() const
0132 {
0133     return m_server;
0134 }
0135 
0136 bool ConnectionProfile::isGM() const
0137 {
0138     return m_isGM;
0139 }
0140 
0141 QString ConnectionProfile::campaignPath() const
0142 {
0143     return m_campaignDir;
0144 }
0145 
0146 void ConnectionProfile::setCampaignPath(const QString& id)
0147 {
0148     if(m_campaignDir == id)
0149         return;
0150     m_campaignDir= id;
0151     emit campaignPathChanged();
0152 }
0153 
0154 QByteArray ConnectionProfile::password() const
0155 {
0156     return m_password;
0157 }
0158 
0159 bool ConnectionProfile::valid() const
0160 {
0161     return m_valid;
0162 }
0163 
0164 QString ConnectionProfile::playerId() const
0165 {
0166     return m_playerId;
0167 }
0168 
0169 void ConnectionProfile::setPlayerId(const QString& playerId)
0170 {
0171     if(playerId == m_playerId)
0172         return;
0173     m_playerId= playerId;
0174     emit playerIdChanged();
0175 }
0176 
0177 void ConnectionProfile::editPassword(const QByteArray& password)
0178 {
0179     if(password.isEmpty())
0180     {
0181         setHash({});
0182     }
0183     else
0184     {
0185         setHash(QCryptographicHash::hash(password, QCryptographicHash::Sha3_512));
0186     }
0187 }
0188 void ConnectionProfile::setHash(const QByteArray& password)
0189 {
0190     if(password == m_password)
0191         return;
0192     m_password= password;
0193     emit passwordChanged();
0194 }
0195 
0196 const std::vector<connection::CharacterData>& ConnectionProfile::characters()
0197 {
0198     return m_characters;
0199 }
0200 void ConnectionProfile::addCharacter(const connection::CharacterData& data)
0201 {
0202     m_characters.push_back(data);
0203     emit characterCountChanged();
0204 }
0205 
0206 connection::CharacterData& ConnectionProfile::character(int i)
0207 {
0208     return m_characters[static_cast<std::size_t>(i)];
0209 }
0210 
0211 int ConnectionProfile::characterCount()
0212 {
0213     return static_cast<int>(m_characters.size());
0214 }
0215 
0216 void ConnectionProfile::removeCharacter(int index)
0217 {
0218     m_characters.erase(m_characters.begin() + index);
0219     emit characterCountChanged();
0220 }
0221 
0222 void ConnectionProfile::clearCharacter()
0223 {
0224     m_characters.clear();
0225     emit characterCountChanged();
0226 }
0227 
0228 void ConnectionProfile::cloneProfile(const ConnectionProfile* src)
0229 {
0230     m_password= src->password();
0231     setGm(src->isGM());
0232     setPort(src->port());
0233     setProfileTitle(src->profileTitle());
0234     setPlayerName(src->playerName());
0235     setPlayerAvatar(src->playerAvatar());
0236     setPlayerColor(src->playerColor());
0237     setAddress(src->address());
0238     setCampaignPath(src->campaignPath());
0239     setServerMode(src->isServer());
0240 }
0241 
0242 void ConnectionProfile::setCharactersValid(bool val)
0243 {
0244     if(m_validCharacter == val)
0245         return;
0246     m_validCharacter= val;
0247     emit charactersValidChanged();
0248 }
0249 
0250 void ConnectionProfile::setCampaignInfoValid(bool val)
0251 {
0252     if(m_validCampaign == val)
0253         return;
0254     m_validCampaign= val;
0255     emit campaignInfoValidChanged();
0256 }
0257 
0258 void ConnectionProfile::setPlayerInfoValid(bool val)
0259 {
0260     if(val == m_validPlayerInfo)
0261         return;
0262     m_validPlayerInfo= val;
0263     emit playerInfoValidChanged();
0264 }
0265 
0266 void ConnectionProfile::characterHasChanged()
0267 {
0268     emit characterChanged();
0269 }
0270 
0271 void ConnectionProfile::setValid(bool b)
0272 {
0273     if(m_valid == b)
0274         return;
0275     m_valid= b;
0276     emit validChanged();
0277 }
0278 
0279 void ConnectionProfile::setConnectionInfoValid(bool b)
0280 {
0281     if(m_validConnectionInfo == b)
0282         return;
0283     m_validConnectionInfo= b;
0284     emit connectionInfoValidChanged();
0285 }
0286 
0287 bool ConnectionProfile::connectionInfoValid() const
0288 {
0289     return m_validConnectionInfo;
0290 }
0291 bool ConnectionProfile::playerInfoValid() const
0292 {
0293     return m_validPlayerInfo;
0294 }
0295 bool ConnectionProfile::charactersValid() const
0296 {
0297     return m_validCharacter;
0298 }
0299 bool ConnectionProfile::campaignInfoValid() const
0300 {
0301     return m_validCampaign;
0302 }
0303 void ConnectionProfile::setCharacters(const std::vector<connection::CharacterData>& characters)
0304 {
0305     m_characters= characters;
0306 }