-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzimreply.h
116 lines (100 loc) · 3.75 KB
/
zimreply.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <zimfilewrapper.h>
#include <QNetworkReply>
#include <QBuffer>
#include <QPainter>
#include <QImage>
#include <QColor>
#include <QThread>
#include <asynchronouszimreader.h>
class ZimReply : public QNetworkReply
{
Q_OBJECT
private:
static ZimFileWrapper* zimFileWrapper;
public:
ZimReply(QObject* object, const QNetworkRequest& request)
: QNetworkReply(object)
, position(0)
{
setRequest(request);
setOperation(QNetworkAccessManager::GetOperation);
open(ReadOnly|Unbuffered);
setUrl(request.url());
qDebug() <<"Creating AsynchronousZimReader";
//TODO: check whether can be optimized reusing thread.
// (Would require that requests are strictly sequential,
// which is doubtful)
AsynchronousZimReader *zimReader = new AsynchronousZimReader(this,ZimReply::zimFileWrapper);
connect(zimReader, SIGNAL(readDone(QByteArray, QString)),
SLOT(readFromZimFileDone(QByteArray, QString)));
zimReader->readAsync(request.url());
}
qint64 readData(char* data, qint64 maxSize)
{
qDebug() << Q_FUNC_INFO << " for url. " << this->url();
const qint64 readSize = qMin(maxSize, (qint64)(buffer.size() - position));
memcpy(data, buffer.constData() + position, readSize);
position += readSize;
qDebug() << Q_FUNC_INFO << readSize << " bytes read.";
return readSize;
}
virtual qint64 bytesAvailable() const
{
return buffer.size() - position;
}
virtual qint64 pos () const
{
return position;
}
virtual bool seek( qint64 pos )
{
if (pos < 0 || pos >= buffer.size())
return false;
position = pos;
return true;
}
virtual qint64 size () const
{
return buffer.size();
}
static void setZimFileWrapper(ZimFileWrapper* zimFileWrapper) {
ZimReply::zimFileWrapper = zimFileWrapper;
}
static ZimFileWrapper* getZimFileWrapper() {
return zimFileWrapper;
}
public slots:
void readFromZimFileDone(const QByteArray& data, const QString& mimeType)
{
qDebug() << Q_FUNC_INFO << " url: " << this->url() << ", mimeType: " <<mimeType;
if (mimeType.startsWith(QLatin1String("image/png"))) {
//TODO: If mimeType is correct (image/png) pngs are not displayed.
// Replacing mimeType is just image (or even image/jpeg) pngs are displayed.
//Note: Detected with kiwix-qt-android. With WikiOnBoard android not tested.
// (Except behavior with all set to text/html -> images displayed, but css not loaded)
qDebug() << "Workaround for android: mimeType is "<< mimeType << ". -> set MimeType to image";
setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("image"));
} else if (mimeType.startsWith(QLatin1String("text"))){
// At least in wikipedia-de.zim else non-latin characters not shown correctly,
// while newer zim files seem to work without specifying the charset here.
setHeader(QNetworkRequest::ContentTypeHeader, mimeType+QLatin1String("; charset=UTF-8)"));
} else {
setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
}
//TODO: consider removal of javascript here. (I.p. for symbian check performance impact)
qDebug() << Q_FUNC_INFO << " ContentTypeHeader set to:"<< header(QNetworkRequest::ContentTypeHeader);
setHeader(QNetworkRequest::ContentLengthHeader,data.length());
position = 0;
buffer = data;
emit readyRead();
emit finished();
}
void abort()
{
}
public:
QNetworkReply* rawReply;
private:
QByteArray buffer;
int position;
};