diff --git a/article_netmgr.cc b/article_netmgr.cc index 83fbb2c9..3ac70254 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -82,6 +82,8 @@ using std::string; connect( baseReply, SIGNAL( error( QNetworkReply::NetworkError) ), this, SLOT( applyError( QNetworkReply::NetworkError ) ) ); + connect( baseReply, SIGNAL( readyRead() ), this, SLOT( readDataFromBase() ) ); + // Redirect QNetworkReply signals connect( baseReply, SIGNAL( downloadProgress( qint64, qint64 ) ), @@ -110,9 +112,6 @@ using std::string; connect( baseReply, SIGNAL( readChannelFinished() ), this, SIGNAL( readChannelFinished() ) ); - connect( baseReply, SIGNAL( readyRead() ), this, SIGNAL( readyRead() ) ); - - setOpenMode( QIODevice::ReadOnly ); } @@ -167,12 +166,38 @@ using std::string; emit metaDataChanged(); } + void AllowFrameReply::setReadBufferSize( qint64 size ) + { + QNetworkReply::setReadBufferSize( size ); + baseReply->setReadBufferSize( size ); + } + + qint64 AllowFrameReply::bytesAvailable() const + { + return buffer.size() + QNetworkReply::bytesAvailable(); + } + void AllowFrameReply::applyError( QNetworkReply::NetworkError code ) { setError( code, baseReply->errorString() ); emit error( code ); } + void AllowFrameReply::readDataFromBase() + { + QByteArray data = baseReply->readAll(); + buffer += data; + emit readyRead(); + } + + qint64 AllowFrameReply::readData( char * data, qint64 maxSize ) + { + qint64 size = qMin( maxSize, qint64( buffer.size() ) ); + memcpy( data, buffer.data(), size ); + buffer.remove( 0, size ); + return size; + } + #endif namespace @@ -331,7 +356,8 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op, } #if QT_VERSION >= 0x050200 // Qt 5.2+ - return new AllowFrameReply( reply ); + return op == QNetworkAccessManager::GetOperation + || op == QNetworkAccessManager::HeadOperation ? new AllowFrameReply( reply ) : reply; #else return reply; #endif diff --git a/article_netmgr.hh b/article_netmgr.hh index 64b38a59..b99242f0 100644 --- a/article_netmgr.hh +++ b/article_netmgr.hh @@ -59,6 +59,7 @@ class AllowFrameReply : public QNetworkReply Q_OBJECT private: QNetworkReply * baseReply; + QByteArray buffer; AllowFrameReply(); AllowFrameReply( AllowFrameReply const & ); @@ -69,16 +70,14 @@ public: { delete baseReply; } // QNetworkReply virtual functions - void setReadBufferSize( qint64 size ) - { baseReply->setReadBufferSize( size ); } + void setReadBufferSize( qint64 size ); void close() { baseReply->close(); } // QIODevice virtual functions + qint64 bytesAvailable() const; bool atEnd() const { return baseReply->atEnd(); } - qint64 bytesAvailable() const - { return baseReply->bytesAvailable(); } qint64 bytesToWrite() const { return baseReply->bytesToWrite(); } bool canReadLine() const @@ -97,6 +96,7 @@ public slots: // Own AllowFrameReply slots void applyMetaData(); void applyError( QNetworkReply::NetworkError code ); + void readDataFromBase(); // Redirect QNetworkReply slots virtual void abort() @@ -114,8 +114,7 @@ protected: { configuration = baseReply->sslConfiguration(); } // QIODevice virtual functions - qint64 readData( char * data, qint64 maxSize ) - { return baseReply->read( data, maxSize ); } + qint64 readData( char * data, qint64 maxSize ); qint64 readLineData( char * data, qint64 maxSize ) { return baseReply->readLine( data, maxSize ); } qint64 writeData( const char * data, qint64 maxSize )