source: trunk/src/network/access/qfilenetworkreply.cpp@ 774

Last change on this file since 774 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
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>
47#include <QDebug>
48
49QT_BEGIN_NAMESPACE
50
51QFileNetworkReplyPrivate::QFileNetworkReplyPrivate()
52 : QNetworkReplyPrivate(), fileEngine(0), fileSize(0), filePos(0)
53{
54}
55
56QFileNetworkReplyPrivate::~QFileNetworkReplyPrivate()
57{
58 delete fileEngine;
59}
60
61QFileNetworkReply::~QFileNetworkReply()
62{
63}
64
65QFileNetworkReply::QFileNetworkReply(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
66 : QNetworkReply(*new QFileNetworkReplyPrivate(), parent)
67{
68 setRequest(req);
69 setUrl(req.url());
70 setOperation(op);
71 QNetworkReply::open(QIODevice::ReadOnly);
72
73 qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
74
75 QFileNetworkReplyPrivate *d = (QFileNetworkReplyPrivate*) d_func();
76
77 QUrl url = req.url();
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());
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);
90 return;
91 }
92#endif
93 if (url.path().isEmpty())
94 url.setPath(QLatin1String("/"));
95 setUrl(url);
96
97
98 QString fileName = url.toLocalFile();
99 if (fileName.isEmpty()) {
100 fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
101 }
102
103 QFileInfo fi(fileName);
104 if (fi.isDir()) {
105 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString());
106 setError(QNetworkReply::ContentOperationNotPermittedError, msg);
107 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
108 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
109 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
110 return;
111 }
112
113 d->fileEngine = QAbstractFileEngine::create(fileName);
114 bool opened = d->fileEngine->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
115
116 // could we open the file?
117 if (!opened) {
118 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
119 .arg(fileName, d->fileEngine->errorString());
120
121 if (fi.exists()) {
122 setError(QNetworkReply::ContentAccessDenied, msg);
123 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
124 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
125 } else {
126 setError(QNetworkReply::ContentNotFoundError, msg);
127 QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
128 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
129 }
130 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
131 return;
132 }
133
134 d->fileSize = fi.size();
135 setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified());
136 setHeader(QNetworkRequest::ContentLengthHeader, d->fileSize);
137
138 QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
139 QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
140 Q_ARG(qint64, d->fileSize), Q_ARG(qint64, d->fileSize));
141 QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
142 QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
143}
144
145bool QFileNetworkReplyPrivate::isFinished() const
146{
147 return true;
148}
149
150void QFileNetworkReply::close()
151{
152 Q_D(QFileNetworkReply);
153 QNetworkReply::close();
154 if (d->fileEngine)
155 d->fileEngine->close();
156}
157
158void QFileNetworkReply::abort()
159{
160 Q_D(QFileNetworkReply);
161 QNetworkReply::close();
162 if (d->fileEngine)
163 d->fileEngine->close();
164}
165
166qint64 QFileNetworkReply::bytesAvailable() const
167{
168 Q_D(const QFileNetworkReply);
169 if (!d->fileEngine)
170 return 0;
171
172 return QNetworkReply::bytesAvailable() + d->fileSize - d->filePos;
173}
174
175bool QFileNetworkReply::isSequential () const
176{
177 return true;
178}
179
180qint64 QFileNetworkReply::size() const
181{
182 Q_D(const QFileNetworkReply);
183 return d->fileSize;
184}
185
186/*!
187 \internal
188*/
189qint64 QFileNetworkReply::readData(char *data, qint64 maxlen)
190{
191 Q_D(QFileNetworkReply);
192 if (!d->fileEngine)
193 return -1;
194
195 qint64 ret = d->fileEngine->read(data, maxlen);
196 if (ret == 0 && bytesAvailable() == 0) {
197 return -1; // everything had been read
198 } else if (ret > 0) {
199 d->filePos += ret;
200 }
201
202 return ret;
203}
204
205
206QT_END_NAMESPACE
207
208#include "moc_qfilenetworkreply_p.cpp"
209
Note: See TracBrowser for help on using the repository browser.