feature: added issued configs info parsing

This commit is contained in:
vladimir.kuznetsov 2025-02-19 22:58:04 +07:00
parent eda24765e7
commit c2b17c128d
6 changed files with 87 additions and 28 deletions

View file

@ -8,14 +8,6 @@
namespace
{
Logger logger("AccountInfoModel");
namespace configKey
{
constexpr char availableCountries[] = "available_countries";
constexpr char activeDeviceCount[] = "active_device_count";
constexpr char maxDeviceCount[] = "max_device_count";
constexpr char subscriptionEndDate[] = "subscription_end_date";
}
}
ApiAccountInfoModel::ApiAccountInfoModel(QObject *parent) : QAbstractListModel(parent)
@ -77,11 +69,12 @@ void ApiAccountInfoModel::updateModel(const QJsonObject &accountInfoObject, cons
AccountInfoData accountInfoData;
m_availableCountries = accountInfoObject.value(configKey::availableCountries).toArray();
m_availableCountries = accountInfoObject.value(apiDefs::key::availableCountries).toArray();
m_issuedConfigsInfo = accountInfoObject.value(apiDefs::key::issuedConfigs).toArray();
accountInfoData.activeDeviceCount = accountInfoObject.value(configKey::activeDeviceCount).toInt();
accountInfoData.maxDeviceCount = accountInfoObject.value(configKey::maxDeviceCount).toInt();
accountInfoData.subscriptionEndDate = accountInfoObject.value(configKey::subscriptionEndDate).toString();
accountInfoData.activeDeviceCount = accountInfoObject.value(apiDefs::key::activeDeviceCount).toInt();
accountInfoData.maxDeviceCount = accountInfoObject.value(apiDefs::key::maxDeviceCount).toInt();
accountInfoData.subscriptionEndDate = accountInfoObject.value(apiDefs::key::subscriptionEndDate).toString();
accountInfoData.configType = apiUtils::getConfigType(serverConfig);
@ -108,6 +101,11 @@ QJsonArray ApiAccountInfoModel::getAvailableCountries()
return m_availableCountries;
}
QJsonArray ApiAccountInfoModel::getIssuedConfigsInfo()
{
return m_issuedConfigsInfo;
}
QString ApiAccountInfoModel::getTelegramBotLink()
{
if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) {

View file

@ -31,6 +31,7 @@ public slots:
QVariant data(const QString &roleString);
QJsonArray getAvailableCountries();
QJsonArray getIssuedConfigsInfo();
QString getTelegramBotLink();
protected:
@ -48,6 +49,7 @@ private:
AccountInfoData m_accountInfoData;
QJsonArray m_availableCountries;
QJsonArray m_issuedConfigsInfo;
};
#endif // APIACCOUNTINFOMODEL_H

View file

@ -2,17 +2,12 @@
#include <QJsonObject>
#include "core/api/apiDefs.h"
#include "logger.h"
namespace
{
Logger logger("ApiCountryModel");
namespace configKey
{
constexpr char serverCountryCode[] = "server_country_code";
constexpr char serverCountryName[] = "server_country_name";
}
}
ApiCountryModel::ApiCountryModel(QObject *parent) : QAbstractListModel(parent)
@ -30,17 +25,19 @@ QVariant ApiCountryModel::data(const QModelIndex &index, int role) const
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(rowCount()))
return QVariant();
QJsonObject countryInfo = m_countries.at(index.row()).toObject();
CountryInfo countryInfo = m_countries.at(index.row());
IssuedConfigInfo issuedConfigInfo = m_issuedConfigs.value(countryInfo.countryCode);
bool isIssued = !issuedConfigInfo.lastDownloaded.isEmpty();
switch (role) {
case CountryCodeRole: {
return countryInfo.value(configKey::serverCountryCode).toString();
return countryInfo.countryCode;
}
case CountryNameRole: {
return countryInfo.value(configKey::serverCountryName).toString();
return countryInfo.countryName;
}
case CountryImageCodeRole: {
return countryInfo.value(configKey::serverCountryCode).toString().toUpper();
return countryInfo.countryCode.toUpper();
}
}
@ -51,13 +48,39 @@ void ApiCountryModel::updateModel(const QJsonArray &countries, const QString &cu
{
beginResetModel();
m_countries = countries;
for (int i = 0; i < m_countries.size(); i++) {
if (m_countries.at(i).toObject().value(configKey::serverCountryCode).toString() == currentCountryCode) {
m_countries.clear();
for (int i = 0; i < countries.size(); i++) {
CountryInfo countryInfo;
QJsonObject countryObject = countries.at(i).toObject();
countryInfo.countryName = countryObject.value(apiDefs::key::serverCountryName).toString();
countryInfo.countryCode = countryObject.value(apiDefs::key::serverCountryCode).toString();
if (countryInfo.countryCode == currentCountryCode) {
m_currentIndex = i;
emit currentIndexChanged(m_currentIndex);
break;
}
m_countries.push_back(countryInfo);
}
endResetModel();
}
void ApiCountryModel::updateIssuedConfigsInfo(const QJsonArray &issuedConfigs)
{
beginResetModel();
for (int i = 0; i < issuedConfigs.size(); i++) {
IssuedConfigInfo issuedConfigInfo;
QJsonObject issuedConfigObject = issuedConfigs.at(i).toObject();
issuedConfigInfo.installationUuid = issuedConfigObject.value(apiDefs::key::installationUuid).toString();
issuedConfigInfo.workerLastUpdated = issuedConfigObject.value(apiDefs::key::workerLastUpdated).toString();
issuedConfigInfo.lastDownloaded = issuedConfigObject.value(apiDefs::key::lastDownloaded).toString();
issuedConfigInfo.sourceType = issuedConfigObject.value(apiDefs::key::sourceType).toString();
issuedConfigInfo.osVersion = issuedConfigObject.value(apiDefs::key::osVersion).toString();
m_issuedConfigs.insert(issuedConfigObject.value(apiDefs::key::serverCountryCode).toString(), issuedConfigInfo);
}
endResetModel();

View file

@ -2,6 +2,7 @@
#define APICOUNTRYMODEL_H
#include <QAbstractListModel>
#include <QHash>
#include <QJsonArray>
class ApiCountryModel : public QAbstractListModel
@ -12,7 +13,8 @@ public:
enum Roles {
CountryNameRole = Qt::UserRole + 1,
CountryCodeRole,
CountryImageCodeRole
CountryImageCodeRole,
IsIssuedRole
};
explicit ApiCountryModel(QObject *parent = nullptr);
@ -25,6 +27,7 @@ public:
public slots:
void updateModel(const QJsonArray &countries, const QString &currentCountryCode);
void updateIssuedConfigsInfo(const QJsonArray &issuedConfigs);
int getCurrentIndex();
void setCurrentIndex(const int i);
@ -36,7 +39,23 @@ protected:
QHash<int, QByteArray> roleNames() const override;
private:
QJsonArray m_countries;
struct IssuedConfigInfo
{
QString installationUuid;
QString workerLastUpdated;
QString lastDownloaded;
QString sourceType;
QString osVersion;
};
struct CountryInfo
{
QString countryName;
QString countryCode;
};
QVector<CountryInfo> m_countries;
QHash<QString, IssuedConfigInfo> m_issuedConfigs;
int m_currentIndex;
};