2024-08-20 16:54:05 +07:00
# include "apiServicesModel.h"
# include <QJsonObject>
# include "logger.h"
namespace
{
Logger logger ( " ApiServicesModel " ) ;
namespace configKey
{
constexpr char userCountryCode [ ] = " user_country_code " ;
constexpr char services [ ] = " services " ;
constexpr char serviceInfo [ ] = " service_info " ;
constexpr char serviceType [ ] = " service_type " ;
constexpr char serviceProtocol [ ] = " service_protocol " ;
constexpr char name [ ] = " name " ;
constexpr char price [ ] = " price " ;
constexpr char speed [ ] = " speed " ;
constexpr char timelimit [ ] = " timelimit " ;
constexpr char region [ ] = " region " ;
constexpr char availableCountries [ ] = " available_countries " ;
constexpr char storeEndpoint [ ] = " store_endpoint " ;
2024-09-09 16:27:29 +04:00
constexpr char isAvailable [ ] = " is_available " ;
2024-12-09 09:32:49 +03:00
constexpr char subscription [ ] = " subscription " ;
constexpr char endDate [ ] = " end_date " ;
2024-08-20 16:54:05 +07:00
}
namespace serviceType
{
constexpr char amneziaFree [ ] = " amnezia-free " ;
constexpr char amneziaPremium [ ] = " amnezia-premium " ;
}
}
ApiServicesModel : : ApiServicesModel ( QObject * parent ) : QAbstractListModel ( parent )
{
}
int ApiServicesModel : : rowCount ( const QModelIndex & parent ) const
{
Q_UNUSED ( parent )
return m_services . size ( ) ;
}
QVariant ApiServicesModel : : data ( const QModelIndex & index , int role ) const
{
if ( ! index . isValid ( ) | | index . row ( ) < 0 | | index . row ( ) > = static_cast < int > ( rowCount ( ) ) )
return QVariant ( ) ;
2024-12-09 09:32:49 +03:00
auto apiServiceData = m_services . at ( index . row ( ) ) ;
auto serviceType = apiServiceData . type ;
auto isServiceAvailable = apiServiceData . isServiceAvailable ;
2024-08-20 16:54:05 +07:00
switch ( role ) {
case NameRole : {
2024-12-09 09:32:49 +03:00
return apiServiceData . serviceInfo . name ;
2024-08-20 16:54:05 +07:00
}
case CardDescriptionRole : {
2024-12-09 09:32:49 +03:00
auto speed = apiServiceData . serviceInfo . speed ;
2024-08-20 16:54:05 +07:00
if ( serviceType = = serviceType : : amneziaPremium ) {
2025-03-05 06:11:31 +03:00
return tr ( " Amnezia Premium is classic VPN for seamless work, downloading large files, and watching videos. "
" Access all websites and online resources. Speeds up to %1 Mbps. " )
2024-08-20 16:54:05 +07:00
. arg ( speed ) ;
2025-01-15 12:04:48 +07:00
} else if ( serviceType = = serviceType : : amneziaFree ) {
2025-05-12 10:31:41 +03:00
QString description = tr ( " Amnezia Free provides unlimited, free access to a basic set of websites and apps, including Facebook, Instagram, Twitter (X), Discord, Telegram, and more. YouTube is not included in the free plan. " ) ;
2025-01-08 09:12:55 +03:00
if ( ! isServiceAvailable ) {
2025-01-15 12:04:48 +07:00
description + = tr ( " <p><a style= \" color: #EB5757; \" >Not available in your region. If you have VPN enabled, disable it, "
" return to the previous screen, and try again.</a> " ) ;
2024-09-09 16:27:29 +04:00
}
return description ;
2024-08-20 16:54:05 +07:00
}
}
case ServiceDescriptionRole : {
if ( serviceType = = serviceType : : amneziaPremium ) {
2025-03-05 06:11:31 +03:00
return tr ( " Amnezia Premium is classic VPN for for seamless work, downloading large files, and watching videos. "
" Access all websites and online resources. " ) ;
2024-08-20 16:54:05 +07:00
} else {
2025-05-12 10:31:41 +03:00
return tr ( " Amnezia Free provides unlimited, free access to a basic set of websites and apps, including Facebook, Instagram, Twitter (X) , Discord , Telegram , and more . YouTube is not included in the free plan . " ) ;
2024-08-20 16:54:05 +07:00
}
}
2024-09-09 16:27:29 +04:00
case IsServiceAvailableRole : {
if ( serviceType = = serviceType : : amneziaFree ) {
2025-01-08 09:12:55 +03:00
if ( ! isServiceAvailable ) {
2024-09-09 16:27:29 +04:00
return false ;
}
}
return true ;
}
2024-08-20 16:54:05 +07:00
case SpeedRole : {
2024-12-09 09:32:49 +03:00
return tr ( " %1 MBit/s " ) . arg ( apiServiceData . serviceInfo . speed ) ;
2024-08-20 16:54:05 +07:00
}
2024-12-09 09:32:49 +03:00
case TimeLimitRole : {
auto timeLimit = apiServiceData . serviceInfo . timeLimit ;
if ( timeLimit = = " 0 " ) {
2024-08-20 16:54:05 +07:00
return " " ;
}
2024-12-09 09:32:49 +03:00
return tr ( " %1 days " ) . arg ( timeLimit ) ;
2024-08-20 16:54:05 +07:00
}
case RegionRole : {
2024-12-09 09:32:49 +03:00
return apiServiceData . serviceInfo . region ;
2024-08-20 16:54:05 +07:00
}
case FeaturesRole : {
if ( serviceType = = serviceType : : amneziaPremium ) {
return tr ( " " ) ;
} else {
return tr ( " VPN will open only popular sites blocked in your region, such as Instagram, Facebook, Twitter and others. "
" Other sites will be opened from your real IP address, "
2025-08-01 06:36:30 +03:00
" <a href= \" %1 \" style= \" color: #FBB26A; \" >more details on the website.</a> " ) ;
2024-08-20 16:54:05 +07:00
}
}
case PriceRole : {
2024-12-09 09:32:49 +03:00
auto price = apiServiceData . serviceInfo . price ;
2024-08-20 16:54:05 +07:00
if ( price = = " free " ) {
return tr ( " Free " ) ;
}
return tr ( " %1 $/month " ) . arg ( price ) ;
}
2024-12-09 09:32:49 +03:00
case EndDateRole : {
return QDateTime : : fromString ( apiServiceData . subscription . endDate , Qt : : ISODate ) . toLocalTime ( ) . toString ( " d MMM yyyy " ) ;
}
2024-08-20 16:54:05 +07:00
}
return QVariant ( ) ;
}
void ApiServicesModel : : updateModel ( const QJsonObject & data )
{
beginResetModel ( ) ;
2024-12-09 09:32:49 +03:00
m_services . clear ( ) ;
2024-08-20 16:54:05 +07:00
m_countryCode = data . value ( configKey : : userCountryCode ) . toString ( ) ;
2024-12-09 09:32:49 +03:00
auto services = data . value ( configKey : : services ) . toArray ( ) ;
2024-08-20 16:54:05 +07:00
2024-12-09 09:32:49 +03:00
if ( services . isEmpty ( ) ) {
m_services . push_back ( getApiServicesData ( data ) ) ;
2024-08-20 16:54:05 +07:00
m_selectedServiceIndex = 0 ;
2024-12-09 09:32:49 +03:00
} else {
for ( const auto & service : services ) {
2025-01-15 12:04:48 +07:00
auto serviceObject = service . toObject ( ) ;
m_services . push_back ( getApiServicesData ( serviceObject ) ) ;
2024-12-09 09:32:49 +03:00
}
2024-08-20 16:54:05 +07:00
}
endResetModel ( ) ;
}
void ApiServicesModel : : setServiceIndex ( const int index )
{
m_selectedServiceIndex = index ;
}
QJsonObject ApiServicesModel : : getSelectedServiceInfo ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . serviceInfo . object ;
2024-08-20 16:54:05 +07:00
}
QString ApiServicesModel : : getSelectedServiceType ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . type ;
2024-08-20 16:54:05 +07:00
}
QString ApiServicesModel : : getSelectedServiceProtocol ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . protocol ;
2024-08-20 16:54:05 +07:00
}
QString ApiServicesModel : : getSelectedServiceName ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . serviceInfo . name ;
2024-08-20 16:54:05 +07:00
}
QJsonArray ApiServicesModel : : getSelectedServiceCountries ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . availableCountries ;
2024-08-20 16:54:05 +07:00
}
QString ApiServicesModel : : getCountryCode ( )
{
return m_countryCode ;
}
QString ApiServicesModel : : getStoreEndpoint ( )
{
2024-12-09 09:32:49 +03:00
auto service = m_services . at ( m_selectedServiceIndex ) ;
return service . storeEndpoint ;
2024-08-20 16:54:05 +07:00
}
QVariant ApiServicesModel : : getSelectedServiceData ( const QString roleString )
{
QModelIndex modelIndex = index ( m_selectedServiceIndex ) ;
auto roles = roleNames ( ) ;
for ( auto it = roles . begin ( ) ; it ! = roles . end ( ) ; it + + ) {
if ( QString ( it . value ( ) ) = = roleString ) {
return data ( modelIndex , it . key ( ) ) ;
}
}
return { } ;
}
QHash < int , QByteArray > ApiServicesModel : : roleNames ( ) const
{
QHash < int , QByteArray > roles ;
roles [ NameRole ] = " name " ;
roles [ CardDescriptionRole ] = " cardDescription " ;
roles [ ServiceDescriptionRole ] = " serviceDescription " ;
2024-09-09 16:27:29 +04:00
roles [ IsServiceAvailableRole ] = " isServiceAvailable " ;
2024-08-20 16:54:05 +07:00
roles [ SpeedRole ] = " speed " ;
2024-12-09 09:32:49 +03:00
roles [ TimeLimitRole ] = " timeLimit " ;
2024-08-20 16:54:05 +07:00
roles [ RegionRole ] = " region " ;
roles [ FeaturesRole ] = " features " ;
roles [ PriceRole ] = " price " ;
2024-12-09 09:32:49 +03:00
roles [ EndDateRole ] = " endDate " ;
2024-08-20 16:54:05 +07:00
return roles ;
}
2024-12-09 09:32:49 +03:00
ApiServicesModel : : ApiServicesData ApiServicesModel : : getApiServicesData ( const QJsonObject & data )
{
2025-01-15 12:04:48 +07:00
auto serviceInfo = data . value ( configKey : : serviceInfo ) . toObject ( ) ;
2024-12-09 09:32:49 +03:00
auto serviceType = data . value ( configKey : : serviceType ) . toString ( ) ;
auto serviceProtocol = data . value ( configKey : : serviceProtocol ) . toString ( ) ;
auto availableCountries = data . value ( configKey : : availableCountries ) . toArray ( ) ;
auto subscriptionObject = data . value ( configKey : : subscription ) . toObject ( ) ;
ApiServicesData serviceData ;
serviceData . serviceInfo . name = serviceInfo . value ( configKey : : name ) . toString ( ) ;
serviceData . serviceInfo . price = serviceInfo . value ( configKey : : price ) . toString ( ) ;
serviceData . serviceInfo . region = serviceInfo . value ( configKey : : region ) . toString ( ) ;
serviceData . serviceInfo . speed = serviceInfo . value ( configKey : : speed ) . toString ( ) ;
serviceData . serviceInfo . timeLimit = serviceInfo . value ( configKey : : timelimit ) . toString ( ) ;
serviceData . type = serviceType ;
serviceData . protocol = serviceProtocol ;
2025-01-31 23:01:53 +07:00
serviceData . storeEndpoint = data . value ( configKey : : storeEndpoint ) . toString ( ) ;
2024-12-09 09:32:49 +03:00
2025-01-15 12:04:48 +07:00
if ( data . value ( configKey : : isAvailable ) . isBool ( ) ) {
2024-12-09 09:32:49 +03:00
serviceData . isServiceAvailable = data . value ( configKey : : isAvailable ) . toBool ( ) ;
} else {
serviceData . isServiceAvailable = true ;
}
serviceData . serviceInfo . object = serviceInfo ;
serviceData . availableCountries = availableCountries ;
serviceData . subscription . endDate = subscriptionObject . value ( configKey : : endDate ) . toString ( ) ;
return serviceData ;
}