#include "apiAccountInfoModel.h" #include #include "core/api/apiUtils.h" #include "logger.h" 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) { } int ApiAccountInfoModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 1; } QVariant ApiAccountInfoModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() < 0 || index.row() >= static_cast(rowCount())) return QVariant(); switch (role) { case SubscriptionStatusRole: { if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) { return tr("Active"); } return apiUtils::isSubscriptionExpired(m_accountInfoData.subscriptionEndDate) ? tr("Inactive") : tr("Active"); } case EndDateRole: { if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) { return ""; } return QDateTime::fromString(m_accountInfoData.subscriptionEndDate, Qt::ISODate).toLocalTime().toString("d MMM yyyy"); } case ConnectedDevicesRole: { if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) { return ""; } return tr("%1 out of %2").arg(m_accountInfoData.activeDeviceCount).arg(m_accountInfoData.maxDeviceCount); } case ServiceDescriptionRole: { if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaPremiumV2) { return tr("Classic VPN for comfortable work, downloading large files and watching videos. Works for any sites. Speed up to 200 " "Mb/s"); } else if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) { return tr("Free unlimited access to a basic set of websites such as Facebook, Instagram, Twitter (X), Discord, Telegram and " "more. YouTube is not included in the free plan."); } } case IsComponentVisibleRole: { return m_accountInfoData.configType == apiDefs::ConfigType::AmneziaPremiumV2; } } return QVariant(); } void ApiAccountInfoModel::updateModel(const QJsonObject &accountInfoObject, const QJsonObject &serverConfig) { beginResetModel(); AccountInfoData accountInfoData; m_availableCountries = accountInfoObject.value(configKey::availableCountries).toArray(); accountInfoData.activeDeviceCount = accountInfoObject.value(configKey::activeDeviceCount).toInt(); accountInfoData.maxDeviceCount = accountInfoObject.value(configKey::maxDeviceCount).toInt(); accountInfoData.subscriptionEndDate = accountInfoObject.value(configKey::subscriptionEndDate).toString(); accountInfoData.configType = apiUtils::getConfigType(serverConfig); m_accountInfoData = accountInfoData; endResetModel(); } QVariant ApiAccountInfoModel::data(const QString &roleString) { QModelIndex modelIndex = index(0); auto roles = roleNames(); for (auto it = roles.begin(); it != roles.end(); it++) { if (QString(it.value()) == roleString) { return data(modelIndex, it.key()); } } return {}; } QJsonArray ApiAccountInfoModel::getAvailableCountries() { return m_availableCountries; } QString ApiAccountInfoModel::getTelegramBotLink() { if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaFreeV3) { return tr("amnezia_free_support_bot"); } else if (m_accountInfoData.configType == apiDefs::ConfigType::AmneziaPremiumV2) { return tr("amnezia_premium_support_bot"); } return ""; } QHash ApiAccountInfoModel::roleNames() const { QHash roles; roles[SubscriptionStatusRole] = "subscriptionStatus"; roles[EndDateRole] = "endDate"; roles[ConnectedDevicesRole] = "connectedDevices"; roles[ServiceDescriptionRole] = "serviceDescription"; roles[IsComponentVisibleRole] = "isComponentVisible"; return roles; }