|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 |
|
48 QT_BEGIN_NAMESPACE |
|
49 |
|
50 QFileNetworkReplyPrivate::QFileNetworkReplyPrivate() |
|
51 : QNetworkReplyPrivate(), realFileSize(0), finished(false) |
|
52 { |
|
53 } |
|
54 |
|
55 QFileNetworkReply::QFileNetworkReply(QObject *parent, const QNetworkRequest &req) |
|
56 : QNetworkReply(*new QFileNetworkReplyPrivate(), parent) |
|
57 { |
|
58 setRequest(req); |
|
59 setUrl(req.url()); |
|
60 setOperation(QNetworkAccessManager::GetOperation); |
|
61 QMetaObject::invokeMethod(this, "_q_startOperation", Qt::QueuedConnection); |
|
62 QNetworkReply::open(QIODevice::ReadOnly); |
|
63 } |
|
64 |
|
65 QFileNetworkReply::~QFileNetworkReply() |
|
66 { |
|
67 } |
|
68 |
|
69 // This code is mostly inspired by QNetworkAccessFileBackend |
|
70 // We also use its translation context for error messages |
|
71 void QFileNetworkReplyPrivate::_q_startOperation() |
|
72 { |
|
73 Q_Q(QFileNetworkReply); |
|
74 |
|
75 QUrl url = q->url(); |
|
76 if (url.host() == QLatin1String("localhost")) |
|
77 url.setHost(QString()); |
|
78 |
|
79 #if !defined(Q_OS_WIN) |
|
80 // do not allow UNC paths on Unix |
|
81 if (!url.host().isEmpty()) { |
|
82 // we handle only local files |
|
83 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString()); |
|
84 q->setError(QNetworkReply::ProtocolInvalidOperationError, msg); |
|
85 emit q->error(QNetworkReply::ProtocolInvalidOperationError); |
|
86 doFinished(); |
|
87 return; |
|
88 } |
|
89 #endif |
|
90 if (url.path().isEmpty()) |
|
91 url.setPath(QLatin1String("/")); |
|
92 q->setUrl(url); |
|
93 |
|
94 |
|
95 QString fileName = url.toLocalFile(); |
|
96 if (fileName.isEmpty()) { |
|
97 fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); |
|
98 } |
|
99 realFile.setFileName(fileName); |
|
100 |
|
101 QFileInfo fi(realFile); |
|
102 if (fi.isDir()) { |
|
103 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString()); |
|
104 q->setError(QNetworkReply::ContentOperationNotPermittedError, msg); |
|
105 emit q->error(QNetworkReply::ContentOperationNotPermittedError); |
|
106 doFinished(); |
|
107 return; |
|
108 } |
|
109 |
|
110 bool opened = realFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered); |
|
111 |
|
112 // could we open the file? |
|
113 if (!opened) { |
|
114 QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2") |
|
115 .arg(realFile.fileName(), realFile.errorString()); |
|
116 |
|
117 if (realFile.exists()) { |
|
118 q->setError(QNetworkReply::ContentAccessDenied, msg); |
|
119 emit q->error(QNetworkReply::ContentAccessDenied); |
|
120 } else { |
|
121 q->setError(QNetworkReply::ContentNotFoundError, msg); |
|
122 emit q->error(QNetworkReply::ContentNotFoundError); |
|
123 } |
|
124 doFinished(); |
|
125 return; |
|
126 } |
|
127 |
|
128 realFileSize = fi.size(); |
|
129 q->setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified()); |
|
130 q->setHeader(QNetworkRequest::ContentLengthHeader, realFileSize); |
|
131 |
|
132 emit q->metaDataChanged(); |
|
133 emit q->downloadProgress(realFileSize, realFileSize); |
|
134 emit q->readyRead(); |
|
135 doFinished(); |
|
136 } |
|
137 |
|
138 bool QFileNetworkReplyPrivate::isFinished() const |
|
139 { |
|
140 return finished; |
|
141 } |
|
142 |
|
143 void QFileNetworkReplyPrivate::doFinished() |
|
144 { |
|
145 Q_Q(QFileNetworkReply); |
|
146 finished = true; |
|
147 emit q->finished(); |
|
148 } |
|
149 |
|
150 |
|
151 void QFileNetworkReply::close() |
|
152 { |
|
153 Q_D(QFileNetworkReply); |
|
154 QNetworkReply::close(); |
|
155 d->realFile.close(); |
|
156 |
|
157 if (!d->finished) |
|
158 d->doFinished(); |
|
159 } |
|
160 |
|
161 void QFileNetworkReply::abort() |
|
162 { |
|
163 Q_D(QFileNetworkReply); |
|
164 QNetworkReply::close(); |
|
165 d->realFile.close(); |
|
166 |
|
167 if (!d->finished) |
|
168 d->doFinished(); |
|
169 } |
|
170 |
|
171 qint64 QFileNetworkReply::bytesAvailable() const |
|
172 { |
|
173 Q_D(const QFileNetworkReply); |
|
174 return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable(); |
|
175 } |
|
176 |
|
177 bool QFileNetworkReply::isSequential () const |
|
178 { |
|
179 return true; |
|
180 } |
|
181 |
|
182 qint64 QFileNetworkReply::size() const |
|
183 { |
|
184 Q_D(const QFileNetworkReply); |
|
185 return d->realFileSize; |
|
186 } |
|
187 |
|
188 /*! |
|
189 \internal |
|
190 */ |
|
191 qint64 QFileNetworkReply::readData(char *data, qint64 maxlen) |
|
192 { |
|
193 Q_D(QFileNetworkReply); |
|
194 qint64 ret = d->realFile.read(data, maxlen); |
|
195 if (ret == 0 && bytesAvailable() == 0) |
|
196 return -1; // everything had been read |
|
197 else |
|
198 return ret; |
|
199 } |
|
200 |
|
201 |
|
202 QT_END_NAMESPACE |
|
203 |
|
204 #include "moc_qfilenetworkreply_p.cpp" |
|
205 |