[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qfilenetworkreply_p.h"
|
---|
| 43 |
|
---|
| 44 | #include "QtCore/qdatetime.h"
|
---|
| 45 | #include <QtCore/QCoreApplication>
|
---|
| 46 | #include <QtCore/QFileInfo>
|
---|
[651] | 47 | #include <QDebug>
|
---|
[556] | 48 |
|
---|
| 49 | QT_BEGIN_NAMESPACE
|
---|
| 50 |
|
---|
| 51 | QFileNetworkReplyPrivate::QFileNetworkReplyPrivate()
|
---|
[769] | 52 | : QNetworkReplyPrivate(), fileEngine(0), fileSize(0), filePos(0)
|
---|
[556] | 53 | {
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[769] | 56 | QFileNetworkReplyPrivate::~QFileNetworkReplyPrivate()
|
---|
| 57 | {
|
---|
| 58 | delete fileEngine;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[651] | 61 | QFileNetworkReply::~QFileNetworkReply()
|
---|
| 62 | {
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | QFileNetworkReply::QFileNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
|
---|
[556] | 66 | : QNetworkReply(*new QFileNetworkReplyPrivate(), parent)
|
---|
| 67 | {
|
---|
| 68 | setRequest(req);
|
---|
| 69 | setUrl(req.url());
|
---|
[651] | 70 | setOperation(op);
|
---|
[556] | 71 | QNetworkReply::open(QIODevice::ReadOnly);
|
---|
| 72 |
|
---|
[651] | 73 | qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
|
---|
[556] | 74 |
|
---|
[651] | 75 | QFileNetworkReplyPrivate *d = (QFileNetworkReplyPrivate*) d_func();
|
---|
[556] | 76 |
|
---|
[651] | 77 | QUrl url = req.url();
|
---|
[556] | 78 | if (url.host() == QLatin1String("localhost"))
|
---|
| 79 | url.setHost(QString());
|
---|
| 80 |
|
---|
| 81 | #if !defined(Q_OS_WIN)
|
---|
| 82 | // do not allow UNC paths on Unix
|
---|
| 83 | if (!url.host().isEmpty()) {
|
---|
| 84 | // we handle only local files
|
---|
| 85 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString());
|
---|
[651] | 86 | setError(QNetworkReply::ProtocolInvalidOperationError, msg);
|
---|
| 87 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
---|
| 88 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
|
---|
| 89 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
---|
[556] | 90 | return;
|
---|
| 91 | }
|
---|
| 92 | #endif
|
---|
| 93 | if (url.path().isEmpty())
|
---|
| 94 | url.setPath(QLatin1String("/"));
|
---|
[651] | 95 | setUrl(url);
|
---|
[556] | 96 |
|
---|
| 97 |
|
---|
| 98 | QString fileName = url.toLocalFile();
|
---|
| 99 | if (fileName.isEmpty()) {
|
---|
[846] | 100 | if (url.scheme() == QLatin1String("qrc"))
|
---|
| 101 | fileName = QLatin1Char(':') + url.path();
|
---|
| 102 | else
|
---|
| 103 | fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
|
---|
[556] | 104 | }
|
---|
| 105 |
|
---|
[769] | 106 | QFileInfo fi(fileName);
|
---|
[556] | 107 | if (fi.isDir()) {
|
---|
| 108 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString());
|
---|
[651] | 109 | setError(QNetworkReply::ContentOperationNotPermittedError, msg);
|
---|
| 110 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
---|
| 111 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
|
---|
| 112 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
---|
[556] | 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[769] | 116 | d->fileEngine = QAbstractFileEngine::create(fileName);
|
---|
| 117 | bool opened = d->fileEngine->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
|
---|
[556] | 118 |
|
---|
| 119 | // could we open the file?
|
---|
| 120 | if (!opened) {
|
---|
| 121 | QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
|
---|
[769] | 122 | .arg(fileName, d->fileEngine->errorString());
|
---|
[556] | 123 |
|
---|
[769] | 124 | if (fi.exists()) {
|
---|
[651] | 125 | setError(QNetworkReply::ContentAccessDenied, msg);
|
---|
| 126 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
---|
| 127 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
|
---|
[556] | 128 | } else {
|
---|
[651] | 129 | setError(QNetworkReply::ContentNotFoundError, msg);
|
---|
| 130 | QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
|
---|
| 131 | Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
|
---|
[556] | 132 | }
|
---|
[651] | 133 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
---|
[556] | 134 | return;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[769] | 137 | d->fileSize = fi.size();
|
---|
[651] | 138 | setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified());
|
---|
[769] | 139 | setHeader(QNetworkRequest::ContentLengthHeader, d->fileSize);
|
---|
[556] | 140 |
|
---|
[651] | 141 | QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
|
---|
| 142 | QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
|
---|
[769] | 143 | Q_ARG(qint64, d->fileSize), Q_ARG(qint64, d->fileSize));
|
---|
[651] | 144 | QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
|
---|
| 145 | QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
|
---|
[556] | 146 | }
|
---|
| 147 |
|
---|
| 148 | bool QFileNetworkReplyPrivate::isFinished() const
|
---|
| 149 | {
|
---|
[651] | 150 | return true;
|
---|
[556] | 151 | }
|
---|
| 152 |
|
---|
| 153 | void QFileNetworkReply::close()
|
---|
| 154 | {
|
---|
| 155 | Q_D(QFileNetworkReply);
|
---|
| 156 | QNetworkReply::close();
|
---|
[769] | 157 | if (d->fileEngine)
|
---|
| 158 | d->fileEngine->close();
|
---|
[556] | 159 | }
|
---|
| 160 |
|
---|
| 161 | void QFileNetworkReply::abort()
|
---|
| 162 | {
|
---|
| 163 | Q_D(QFileNetworkReply);
|
---|
| 164 | QNetworkReply::close();
|
---|
[769] | 165 | if (d->fileEngine)
|
---|
| 166 | d->fileEngine->close();
|
---|
[556] | 167 | }
|
---|
| 168 |
|
---|
| 169 | qint64 QFileNetworkReply::bytesAvailable() const
|
---|
| 170 | {
|
---|
| 171 | Q_D(const QFileNetworkReply);
|
---|
[769] | 172 | if (!d->fileEngine)
|
---|
| 173 | return 0;
|
---|
| 174 |
|
---|
| 175 | return QNetworkReply::bytesAvailable() + d->fileSize - d->filePos;
|
---|
[556] | 176 | }
|
---|
| 177 |
|
---|
| 178 | bool QFileNetworkReply::isSequential () const
|
---|
| 179 | {
|
---|
| 180 | return true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | qint64 QFileNetworkReply::size() const
|
---|
| 184 | {
|
---|
| 185 | Q_D(const QFileNetworkReply);
|
---|
[769] | 186 | return d->fileSize;
|
---|
[556] | 187 | }
|
---|
| 188 |
|
---|
| 189 | /*!
|
---|
| 190 | \internal
|
---|
| 191 | */
|
---|
| 192 | qint64 QFileNetworkReply::readData(char *data, qint64 maxlen)
|
---|
| 193 | {
|
---|
| 194 | Q_D(QFileNetworkReply);
|
---|
[769] | 195 | if (!d->fileEngine)
|
---|
| 196 | return -1;
|
---|
| 197 |
|
---|
| 198 | qint64 ret = d->fileEngine->read(data, maxlen);
|
---|
| 199 | if (ret == 0 && bytesAvailable() == 0) {
|
---|
[556] | 200 | return -1; // everything had been read
|
---|
[769] | 201 | } else if (ret > 0) {
|
---|
| 202 | d->filePos += ret;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return ret;
|
---|
[556] | 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | QT_END_NAMESPACE
|
---|
| 210 |
|
---|
| 211 | #include "moc_qfilenetworkreply_p.cpp"
|
---|
| 212 |
|
---|