1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qnetworkreplydataimpl_p.h"
43 #include "private/qdataurl_p.h"
44 #include <QtCore/QCoreApplication>
45 #include <QtCore/QMetaObject>
49 QNetworkReplyDataImplPrivate::QNetworkReplyDataImplPrivate()
50 : QNetworkReplyPrivate()
54 QNetworkReplyDataImplPrivate::~QNetworkReplyDataImplPrivate()
58 QNetworkReplyDataImpl::~QNetworkReplyDataImpl()
62 QNetworkReplyDataImpl::QNetworkReplyDataImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
63 : QNetworkReply(*new QNetworkReplyDataImplPrivate(), parent)
65 Q_D(QNetworkReplyDataImpl);
70 QNetworkReply::open(QIODevice::ReadOnly);
75 if (qDecodeDataUrl(url, mimeType, payload)) {
76 qint64 size = payload.size();
77 setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
78 setHeader(QNetworkRequest::ContentLengthHeader, size);
79 QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
81 d->decodedData.setData(payload);
82 d->decodedData.open(QIODevice::ReadOnly);
84 QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
85 Q_ARG(qint64,size), Q_ARG(qint64, size));
86 QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
87 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
89 // something wrong with this URI
90 const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend",
91 "Invalid URI: %1").arg(url.toString());
92 setError(QNetworkReply::ProtocolFailure, msg);
93 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
94 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolFailure));
95 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
99 void QNetworkReplyDataImpl::close()
101 QNetworkReply::close();
104 void QNetworkReplyDataImpl::abort()
106 QNetworkReply::close();
109 qint64 QNetworkReplyDataImpl::bytesAvailable() const
111 Q_D(const QNetworkReplyDataImpl);
112 return QNetworkReply::bytesAvailable() + d->decodedData.bytesAvailable();
115 bool QNetworkReplyDataImpl::isSequential () const
120 qint64 QNetworkReplyDataImpl::size() const
122 Q_D(const QNetworkReplyDataImpl);
123 return d->decodedData.size();
129 qint64 QNetworkReplyDataImpl::readData(char *data, qint64 maxlen)
131 Q_D(QNetworkReplyDataImpl);
134 // Instead of decoding the whole data into new memory, we could decode on demand.
135 // Note that this might be tricky to do.
137 return d->decodedData.read(data, maxlen);
143 #include "moc_qnetworkreplydataimpl_p.cpp"