choke uploadProgress signals
[profile/ivi/qtbase.git] / src / network / access / qnetworkreplydataimpl.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qnetworkreplydataimpl_p.h"
43 #include "private/qdataurl_p.h"
44 #include <QtCore/QCoreApplication>
45 #include <QtCore/QMetaObject>
46
47 QT_BEGIN_NAMESPACE
48
49 QNetworkReplyDataImplPrivate::QNetworkReplyDataImplPrivate()
50     : QNetworkReplyPrivate()
51 {
52 }
53
54 QNetworkReplyDataImplPrivate::~QNetworkReplyDataImplPrivate()
55 {
56 }
57
58 QNetworkReplyDataImpl::~QNetworkReplyDataImpl()
59 {
60 }
61
62 QNetworkReplyDataImpl::QNetworkReplyDataImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
63     : QNetworkReply(*new QNetworkReplyDataImplPrivate(), parent)
64 {
65     Q_D(QNetworkReplyDataImpl);
66     setRequest(req);
67     setUrl(req.url());
68     setOperation(op);
69     setFinished(true);
70     QNetworkReply::open(QIODevice::ReadOnly);
71
72     QUrl url = req.url();
73     QString mimeType;
74     QByteArray payload;
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);
80
81         d->decodedData.setData(payload);
82         d->decodedData.open(QIODevice::ReadOnly);
83
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);
88     } else {
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);
96     }
97 }
98
99 void QNetworkReplyDataImpl::close()
100 {
101     QNetworkReply::close();
102 }
103
104 void QNetworkReplyDataImpl::abort()
105 {
106     QNetworkReply::close();
107 }
108
109 qint64 QNetworkReplyDataImpl::bytesAvailable() const
110 {
111     Q_D(const QNetworkReplyDataImpl);
112     return QNetworkReply::bytesAvailable() + d->decodedData.bytesAvailable();
113 }
114
115 bool QNetworkReplyDataImpl::isSequential () const
116 {
117     return true;
118 }
119
120 qint64 QNetworkReplyDataImpl::size() const
121 {
122     Q_D(const QNetworkReplyDataImpl);
123     return d->decodedData.size();
124 }
125
126 /*!
127     \internal
128 */
129 qint64 QNetworkReplyDataImpl::readData(char *data, qint64 maxlen)
130 {
131     Q_D(QNetworkReplyDataImpl);
132
133     // TODO idea:
134     // Instead of decoding the whole data into new memory, we could decode on demand.
135     // Note that this might be tricky to do.
136
137     return d->decodedData.read(data, maxlen);
138 }
139
140
141 QT_END_NAMESPACE
142
143 #include "moc_qnetworkreplydataimpl_p.cpp"
144