From c2b17c128d6c9efa23f46161e0aa5aea0ff81c32 Mon Sep 17 00:00:00 2001 From: "vladimir.kuznetsov" Date: Wed, 19 Feb 2025 22:58:04 +0700 Subject: [PATCH] feature: added issued configs info parsing --- client/core/api/apiDefs.h | 16 ++++++ .../controllers/api/apiSettingsController.cpp | 1 + client/ui/models/api/apiAccountInfoModel.cpp | 22 ++++---- client/ui/models/api/apiAccountInfoModel.h | 2 + client/ui/models/api/apiCountryModel.cpp | 51 ++++++++++++++----- client/ui/models/api/apiCountryModel.h | 23 ++++++++- 6 files changed, 87 insertions(+), 28 deletions(-) diff --git a/client/core/api/apiDefs.h b/client/core/api/apiDefs.h index b01832ce..2892f90b 100644 --- a/client/core/api/apiDefs.h +++ b/client/core/api/apiDefs.h @@ -26,6 +26,22 @@ namespace apiDefs constexpr QLatin1String stackType("stack_type"); constexpr QLatin1String vpnKey("vpn_key"); + + constexpr QLatin1String installationUuid("installation_uuid"); + constexpr QLatin1String workerLastUpdated("worker_last_updated"); + constexpr QLatin1String lastDownloaded("last_downloaded"); + constexpr QLatin1String sourceType("source_type"); + + constexpr QLatin1String serverCountryCode("server_country_code"); + constexpr QLatin1String serverCountryName("server_country_name"); + + constexpr QLatin1String osVersion("os_version"); + + constexpr QLatin1String availableCountries("available_countries"); + constexpr QLatin1String activeDeviceCount("active_device_count"); + constexpr QLatin1String maxDeviceCount("max_device_count"); + constexpr QLatin1String subscriptionEndDate("subscription_end_date"); + constexpr QLatin1String issuedConfigs("issued_configs"); } const int requestTimeoutMsecs = 12 * 1000; // 12 secs diff --git a/client/ui/controllers/api/apiSettingsController.cpp b/client/ui/controllers/api/apiSettingsController.cpp index 0bebf19e..5f436436 100644 --- a/client/ui/controllers/api/apiSettingsController.cpp +++ b/client/ui/controllers/api/apiSettingsController.cpp @@ -68,4 +68,5 @@ bool ApiSettingsController::getAccountInfo() void ApiSettingsController::updateApiCountryModel() { m_apiCountryModel->updateModel(m_apiAccountInfoModel->getAvailableCountries(), ""); + m_apiCountryModel->updateIssuedConfigsInfo(m_apiAccountInfoModel->getIssuedConfigsInfo()); } diff --git a/client/ui/models/api/apiAccountInfoModel.cpp b/client/ui/models/api/apiAccountInfoModel.cpp index 254097bc..055c2347 100644 --- a/client/ui/models/api/apiAccountInfoModel.cpp +++ b/client/ui/models/api/apiAccountInfoModel.cpp @@ -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) { diff --git a/client/ui/models/api/apiAccountInfoModel.h b/client/ui/models/api/apiAccountInfoModel.h index 9dfdc508..e50ea5f5 100644 --- a/client/ui/models/api/apiAccountInfoModel.h +++ b/client/ui/models/api/apiAccountInfoModel.h @@ -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 diff --git a/client/ui/models/api/apiCountryModel.cpp b/client/ui/models/api/apiCountryModel.cpp index 0043efd2..d8d74a41 100644 --- a/client/ui/models/api/apiCountryModel.cpp +++ b/client/ui/models/api/apiCountryModel.cpp @@ -2,17 +2,12 @@ #include +#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(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(); diff --git a/client/ui/models/api/apiCountryModel.h b/client/ui/models/api/apiCountryModel.h index c935ebce..e57ec0dd 100644 --- a/client/ui/models/api/apiCountryModel.h +++ b/client/ui/models/api/apiCountryModel.h @@ -2,6 +2,7 @@ #define APICOUNTRYMODEL_H #include +#include #include 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 ¤tCountryCode); + void updateIssuedConfigsInfo(const QJsonArray &issuedConfigs); int getCurrentIndex(); void setCurrentIndex(const int i); @@ -36,7 +39,23 @@ protected: QHash 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 m_countries; + QHash m_issuedConfigs; int m_currentIndex; };