goldendict-ng/article_netmgr.hh
2010-05-15 16:42:18 +04:00

119 lines
3 KiB
C++

/* This file is (c) 2008-2010 Konstantin Isakov <ikm@users.berlios.de>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __ARTICLE_NETMGR_HH_INCLUDED__
#define __ARTICLE_NETMGR_HH_INCLUDED__
#include <QtNetwork>
#include "dictionary.hh"
#include "article_maker.hh"
using std::vector;
/// A custom QNetworkAccessManager version which fetches images from the
/// dictionaries when requested.
class ArticleNetworkAccessManager: public QNetworkAccessManager
{
vector< sptr< Dictionary::Class > > const & dictionaries;
ArticleMaker const & articleMaker;
bool const & disallowContentFromOtherSites;
public:
ArticleNetworkAccessManager( QObject * parent,
vector< sptr< Dictionary::Class > > const &
dictionaries_,
ArticleMaker const & articleMaker_,
bool const & disallowContentFromOtherSites_ ):
QNetworkAccessManager( parent ), dictionaries( dictionaries_ ),
articleMaker( articleMaker_ ),
disallowContentFromOtherSites( disallowContentFromOtherSites_ )
{}
/// Tries handling any kind of internal resources referenced by dictionaries.
/// If it succeeds, the result is a dictionary request object. Otherwise, an
/// empty pointer is returned.
/// The function can optionally set the Content-Type header correspondingly.
sptr< Dictionary::DataRequest > getResource( QUrl const & url,
QString & contentType );
protected:
virtual QNetworkReply * createRequest( Operation op,
QNetworkRequest const & req,
QIODevice * outgoingData );
};
class ArticleResourceReply: public QNetworkReply
{
Q_OBJECT
sptr< Dictionary::DataRequest > req;
size_t alreadyRead;
public:
ArticleResourceReply( QObject * parent,
QNetworkRequest const &,
sptr< Dictionary::DataRequest > const &,
QString const & contentType );
~ArticleResourceReply();
protected:
virtual qint64 bytesAvailable() const;
virtual void abort()
{}
virtual qint64 readData( char * data, qint64 maxSize );
// We use the hackery below to work around the fact that we need to emit
// ready/finish signals after we've been constructed.
signals:
void readyReadSignal();
void finishedSignal();
private slots:
void reqUpdated();
void reqFinished();
void readyReadSlot();
void finishedSlot();
};
class BlockedNetworkReply: public QNetworkReply
{
Q_OBJECT
public:
BlockedNetworkReply( QObject * parent );
virtual qint64 readData( char *, qint64 )
{
return -1;
}
virtual void abort()
{}
protected:
// We use the hackery below to work around the fact that we need to emit
// ready/finish signals after we've been constructed.
signals:
void finishedSignal();
private slots:
void finishedSlot();
};
#endif