mirror of
https://github.com/AskDavis/Casinotest.git
synced 2026-01-10 06:39:46 -08:00
Adverts widget supports population from web and from local files.
This commit is contained in:
@@ -13,22 +13,22 @@ CasinoCoinWebAPI::CasinoCoinWebAPI( QObject*a_pParent )
|
||||
|
||||
void CasinoCoinWebAPI::GetActivePromotions()
|
||||
{
|
||||
Get( s_strServerAddress + "/" + s_strServerEndpoint + "/ActivePromotions" );
|
||||
Get( s_strServerAddress + s_strServerEndpoint + "/ActivePromotions" );
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPI::GetActiveCasinos()
|
||||
{
|
||||
Get( s_strServerAddress + "/" + s_strServerEndpoint + "/ActiveCasinos" );
|
||||
Get( s_strServerAddress + s_strServerEndpoint + "/ActiveCasinos" );
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPI::GetActiveNewsItems()
|
||||
{
|
||||
Get( s_strServerAddress + "/" + s_strServerEndpoint + "/ActiveNewsItems" );
|
||||
Get( s_strServerAddress + s_strServerEndpoint + "/ActiveNewsItems" );
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPI::GetActiveExchanges()
|
||||
{
|
||||
Get( s_strServerAddress + "/" + s_strServerEndpoint + "/ActiveExchanges" );
|
||||
Get( s_strServerAddress + s_strServerEndpoint + "/ActiveExchanges" );
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPI::Get( const QString& a_rUrl )
|
||||
@@ -43,10 +43,11 @@ void CasinoCoinWebAPI::slotParseNetworkResponse( QNetworkReply *finished )
|
||||
if ( finished->error() != QNetworkReply::NoError )
|
||||
{
|
||||
// A communication error has occurred
|
||||
emit signalNetworkError( finished->error() );
|
||||
qDebug() << finished->request().url();
|
||||
emit signalNetworkError( finished->error(), finished->request().url() );
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray data = finished->readAll();
|
||||
QByteArray data = finished->readAll();
|
||||
emit signalResponseReady( data );
|
||||
}
|
||||
|
||||
@@ -23,7 +23,10 @@ public:
|
||||
|
||||
signals:
|
||||
void signalResponseReady( const QByteArray& a_rJsonFile );
|
||||
void signalNetworkError( QNetworkReply::NetworkError a_eError );
|
||||
void signalNetworkError
|
||||
( QNetworkReply::NetworkError a_eError
|
||||
, const QUrl a_rFailedUrl
|
||||
);
|
||||
|
||||
public slots:
|
||||
void slotParseNetworkResponse( QNetworkReply *finished );
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <QJsonValue>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
||||
CasinoCoinWebAPIParser::CasinoCoinWebAPIParser( QObject* a_pParent )
|
||||
: QObject(a_pParent)
|
||||
@@ -23,24 +25,37 @@ void CasinoCoinWebAPIParser::slotParseAnswer( const QByteArray& a_rJsonFile )
|
||||
QJsonObject jsonObjectResult = docAsObject.find( "Result" ).value().toObject();
|
||||
if ( jsonObjectResult.find( "ActivePromotions" ).value().isArray() )
|
||||
{
|
||||
StoreFile( "ActivePromotions", a_rJsonFile );
|
||||
ParsePromotions( docAsObject );
|
||||
}
|
||||
else if ( jsonObjectResult.find( "ActiveNewsItems" ).value().isArray() )
|
||||
{
|
||||
StoreFile( "ActiveNewsItems", a_rJsonFile );
|
||||
ParseNewsItems( docAsObject );
|
||||
}
|
||||
else if ( jsonObjectResult.find( "ActiveCasinos" ).value().isArray() )
|
||||
{
|
||||
StoreFile( "ActiveCasinos", a_rJsonFile );
|
||||
ParseCasinos( docAsObject );
|
||||
}
|
||||
else if ( jsonObjectResult.find( "ActiveExchanges" ).value().isArray() )
|
||||
{
|
||||
StoreFile( "ActiveExchanges", a_rJsonFile );
|
||||
ParseExchanges( docAsObject );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPIParser::slotNetworkError( QNetworkReply::NetworkError a_eError
|
||||
, const QUrl a_rFailedUrl
|
||||
)
|
||||
{
|
||||
qDebug() << "network error: " << a_eError;
|
||||
QString strAccessedUrl = a_rFailedUrl.toString().split("/").last();
|
||||
slotParseAnswer( ReadFile( strAccessedUrl ) );
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPIParser::ParsePromotions( const QJsonObject& a_rJsonPromotions )
|
||||
{
|
||||
qDebug() << "ParsePromotions";
|
||||
@@ -64,3 +79,41 @@ void CasinoCoinWebAPIParser::ParseNewsItems( const QJsonObject& a_rJsonNewsItems
|
||||
qDebug() << "Coming soon - ParseNewsItems";
|
||||
qDebug() << a_rJsonNewsItems;
|
||||
}
|
||||
|
||||
QByteArray CasinoCoinWebAPIParser::ReadFile( QString a_strSourcePath )
|
||||
{
|
||||
QByteArray strAnswer;
|
||||
if ( !QDir( "offlineData" ).exists() )
|
||||
{
|
||||
QDir().mkdir( "offlineData" );
|
||||
}
|
||||
QFile fileOutput( QDir( "offlineData" ).absoluteFilePath( a_strSourcePath ) );
|
||||
if ( !fileOutput.open( QIODevice::ReadOnly ) )
|
||||
{
|
||||
qWarning() << "cannot open file to read: " << QDir::current().relativeFilePath( a_strSourcePath );
|
||||
}
|
||||
else
|
||||
{
|
||||
strAnswer = fileOutput.readAll();
|
||||
fileOutput.close();
|
||||
}
|
||||
return strAnswer;
|
||||
}
|
||||
|
||||
void CasinoCoinWebAPIParser::StoreFile( QString a_strDestinationPath, const QByteArray& a_rJsonFile )
|
||||
{
|
||||
if ( !QDir( "offlineData" ).exists() )
|
||||
{
|
||||
QDir().mkdir( "offlineData" );
|
||||
}
|
||||
QFile fileOutput( QDir( "offlineData" ).absoluteFilePath( a_strDestinationPath ) );
|
||||
if ( !fileOutput.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
|
||||
{
|
||||
qWarning() << "cannot open file to write: " << QDir::current().relativeFilePath( a_strDestinationPath );
|
||||
}
|
||||
else
|
||||
{
|
||||
fileOutput.write( a_rJsonFile );
|
||||
fileOutput.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "jsonactivepromotionsparser.h"
|
||||
#include "jsonsingleactivepromotion.h"
|
||||
|
||||
#include <QNetworkReply>
|
||||
|
||||
class CasinoCoinWebAPIParser : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -21,12 +23,18 @@ signals:
|
||||
|
||||
public slots:
|
||||
void slotParseAnswer( const QByteArray& a_rJsonFile );
|
||||
|
||||
void slotNetworkError
|
||||
( QNetworkReply::NetworkError a_eError
|
||||
, const QUrl a_rFailedUrl
|
||||
);
|
||||
private:
|
||||
void ParsePromotions( const QJsonObject& a_rJsonPromotions );
|
||||
void ParseCasinos ( const QJsonObject& a_rJsonCasinos );
|
||||
void ParseExchanges ( const QJsonObject& a_rJsonExchanges );
|
||||
void ParseNewsItems ( const QJsonObject& a_rJsonNewsItems );
|
||||
|
||||
QByteArray ReadFile( QString a_strSourcePath );
|
||||
void StoreFile( QString a_strDestinationPath, const QByteArray& a_rJsonFile );
|
||||
};
|
||||
|
||||
#endif // CASINOCOINWEBAPIPARSER_H
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "jsonactivepromotionsparser.h"
|
||||
#include "../qtquick_controls/cpp/qmlimageprovider.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
@@ -24,7 +25,7 @@ void JsonActivePromotionsParser::ResolvePromotionsArray()
|
||||
{
|
||||
if ( singleCasinoDescriptor.isObject() )
|
||||
{
|
||||
m_aActiveCasinos.append( JsonSingleActivePromotion( singleCasinoDescriptor.toObject() ) );
|
||||
m_aActivePromotions.append( JsonSingleActivePromotion( singleCasinoDescriptor.toObject() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,10 +34,21 @@ void JsonActivePromotionsParser::ResolvePromotionsArray()
|
||||
|
||||
const QList<JsonSingleActivePromotion>& JsonActivePromotionsParser::GetPromotions() const
|
||||
{
|
||||
return m_aActiveCasinos;
|
||||
return m_aActivePromotions;
|
||||
}
|
||||
|
||||
QList<JsonSingleActivePromotion>& JsonActivePromotionsParser::GetPromotions()
|
||||
{
|
||||
return m_aActiveCasinos;
|
||||
return m_aActivePromotions;
|
||||
}
|
||||
|
||||
void JsonActivePromotionsParser::AddImagesToPool( QmlImageProvider* a_pImageProvider )
|
||||
{
|
||||
if ( a_pImageProvider )
|
||||
{
|
||||
foreach( JsonSingleActivePromotion oPromotion, m_aActivePromotions )
|
||||
{
|
||||
a_pImageProvider->AddToImagePool( oPromotion.GetImageName(), oPromotion.GetAdvertImage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
#include "jsonsingleactivepromotion.h"
|
||||
|
||||
class QmlImageProvider;
|
||||
|
||||
class JsonActivePromotionsParser : public QJsonObject
|
||||
{
|
||||
public:
|
||||
@@ -17,11 +19,13 @@ public:
|
||||
|
||||
const QList<JsonSingleActivePromotion>& GetPromotions() const;
|
||||
QList<JsonSingleActivePromotion>& GetPromotions();
|
||||
private:
|
||||
|
||||
void AddImagesToPool( QmlImageProvider* a_pImageProvider );
|
||||
|
||||
private:
|
||||
void ResolvePromotionsArray();
|
||||
|
||||
QList<JsonSingleActivePromotion> m_aActiveCasinos;
|
||||
QList<JsonSingleActivePromotion> m_aActivePromotions;
|
||||
};
|
||||
|
||||
#endif // JSONACTIVEPROMOTIONSPARSER_H
|
||||
|
||||
@@ -14,40 +14,21 @@ JsonSingleActivePromotion::JsonSingleActivePromotion()
|
||||
JsonSingleActivePromotion::JsonSingleActivePromotion( const QJsonObject& a_rOther )
|
||||
: QJsonObject( a_rOther )
|
||||
{
|
||||
m_strImageName = find( "promotion_title" ).value().toString().remove( GetRestrictedCharacters() );
|
||||
StoreImage();
|
||||
}
|
||||
|
||||
QString JsonSingleActivePromotion::GetImagePath() const
|
||||
QRegExp JsonSingleActivePromotion::GetRestrictedCharacters()
|
||||
{
|
||||
return QString( "file://" + QDir::currentPath() + "/" + GetImageRelativePath() );
|
||||
return QRegExp( "[<,>|\\:()&;#?*% ]" );
|
||||
}
|
||||
|
||||
QString JsonSingleActivePromotion::GetImageRelativePath() const
|
||||
void JsonSingleActivePromotion::StoreImage()
|
||||
{
|
||||
return QString( "adverts/" + find( "promotion_title" ).value().toString().remove( " " ) + "." + find( "image_mime_type" ).value().toString().split( "/" ).last() );
|
||||
}
|
||||
|
||||
QString JsonSingleActivePromotion::StoreImage()
|
||||
{
|
||||
QString strReturn = QString();
|
||||
if ( find( "image_mime_type" ).value().toString().split( "/" ).first().contains( "image" ) )
|
||||
{
|
||||
QString strFileName = GetImageRelativePath();
|
||||
QByteArray binaryData = QByteArray::fromBase64( find( "promotion_image" ).value().toString().toLocal8Bit() );
|
||||
|
||||
if ( !QDir( "adverts" ).exists() )
|
||||
{
|
||||
QDir().mkdir( "adverts" );
|
||||
}
|
||||
QFile imageOutputFile( strFileName );
|
||||
QImage outputImage = QImage::fromData( binaryData, "JPEG" );
|
||||
if ( imageOutputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
|
||||
{
|
||||
outputImage.save( &imageOutputFile, 0 );
|
||||
imageOutputFile.close();
|
||||
strReturn = strFileName;
|
||||
}
|
||||
m_oAdvertImage = QImage::fromData( binaryData );
|
||||
}
|
||||
return strReturn;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <QJsonValue>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include <QImage>
|
||||
|
||||
class JsonSingleActivePromotion : public QJsonObject
|
||||
{
|
||||
public:
|
||||
@@ -12,11 +14,16 @@ public:
|
||||
JsonSingleActivePromotion( const QJsonObject& a_rOther );
|
||||
virtual ~JsonSingleActivePromotion(){}
|
||||
|
||||
QString GetImagePath() const;
|
||||
|
||||
QString GetImageName() const{ return m_strImageName; }
|
||||
QImage GetAdvertImage() const { return m_oAdvertImage; }
|
||||
private:
|
||||
QString GetImageRelativePath() const;
|
||||
QString StoreImage();
|
||||
void StoreImage();
|
||||
|
||||
static QRegExp GetRestrictedCharacters();
|
||||
|
||||
QString m_strImageName;
|
||||
QImage m_oAdvertImage;
|
||||
|
||||
};
|
||||
|
||||
#endif // JSONSINGLEACTIVEPROMOTION_H
|
||||
|
||||
Reference in New Issue
Block a user