Qt 4.8
qnetworkreplyfileimpl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 
44 #include "QtCore/qdatetime.h"
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QFileInfo>
47 #include <QDebug>
48 
50 
52  : QNetworkReplyPrivate(), realFileSize(0)
53 {
54 }
55 
57 {
58 }
59 
62 {
63  setRequest(req);
64  setUrl(req.url());
65  setOperation(op);
66  setFinished(true);
68 
70 
71  QUrl url = req.url();
72  if (url.host() == QLatin1String("localhost"))
73  url.setHost(QString());
74 
75 #if !defined(Q_OS_WIN)
76  // do not allow UNC paths on Unix
77  if (!url.host().isEmpty()) {
78  // we handle only local files
79  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString());
84  return;
85  }
86 #endif
87  if (url.path().isEmpty())
88  url.setPath(QLatin1String("/"));
89  setUrl(url);
90 
91 
93  if (fileName.isEmpty()) {
94  if (url.scheme() == QLatin1String("qrc"))
95  fileName = QLatin1Char(':') + url.path();
96  else
98  }
99 
100  QFileInfo fi(fileName);
101  if (fi.isDir()) {
102  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString());
107  return;
108  }
109 
110  d->realFile.setFileName(fileName);
112 
113  // could we open the file?
114  if (!opened) {
115  QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
117 
118  if (d->realFile.exists()) {
122  } else {
126  }
128  return;
129  }
130 
132  d->realFileSize = fi.size();
134 
135  QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
136  QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
140 }
142 {
145  d->realFile.close();
146 }
147 
149 {
152  d->realFile.close();
153 }
154 
156 {
157  Q_D(const QNetworkReplyFileImpl);
158  if (!d->realFile.isOpen())
160  return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable();
161 }
162 
164 {
165  return true;
166 }
167 
169 {
170  Q_D(const QNetworkReplyFileImpl);
171  return d->realFileSize;
172 }
173 
178 {
180  if (!d->realFile.isOpen())
181  return -1;
182  qint64 ret = d->realFile.read(data, maxlen);
183  if (bytesAvailable() == 0 && d->realFile.isOpen())
184  d->realFile.close();
185  if (ret == 0 && bytesAvailable() == 0)
186  return -1;
187  else
188  return ret;
189 }
190 
191 
193 
194 #include "moc_qnetworkreplyfileimpl_p.cpp"
195 
double d
Definition: qnumeric_p.h:62
QString fileName() const
Returns the name set by setFileName() or to the QFile constructors.
Definition: qfile.cpp:470
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setUrl(const QUrl &url)
Sets the URL being processed to be url.
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
void setError(NetworkError errorCode, const QString &errorString)
Sets the error condition to be errorCode.
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
Definition: qnetworkreply.h:65
QString errorString() const
Returns a human-readable description of the last device error that occurred.
Definition: qiodevice.cpp:1671
NetworkError
Indicates all possible error conditions found during the processing of the request.
Definition: qnetworkreply.h:70
Operation
Indicates the operation this reply is processing.
void setPath(const QString &path)
Sets the path of the URL to path.
Definition: qurl.cpp:4960
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString host() const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4837
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void setHost(const QString &host)
Sets the host of the URL to host.
Definition: qurl.cpp:4821
#define Q_D(Class)
Definition: qglobal.h:2482
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
QUrl url() const
Returns the URL this network request is referring to.
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
void setOperation(QNetworkAccessManager::Operation operation)
Sets the associated operation for this object to be operation.
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void close()
Closes this device for reading.
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise ret...
Definition: qfileinfo.cpp:990
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static const char * data(const QByteArray &arr)
virtual void close()
Closes this device for reading.
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
Sets the known header header to be of value value.
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
virtual qint64 readData(char *data, qint64 maxlen)
__int64 qint64
Definition: qglobal.h:942
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition: qurl.cpp:6412
void setRequest(const QNetworkRequest &request)
Sets the associated request for this object to be request.
virtual bool isSequential() const
qint64 size() const
For open random-access devices, this function returns the size of the device.
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
qint64 size() const
Returns the file size in bytes.
Definition: qfileinfo.cpp:1248
void setFinished(bool)
Sets the reply as finished.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
Definition: qiodevice.cpp:752
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
QObject * parent
Definition: qobject.h:92
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
QUrl url() const
Returns the URL of the content downloaded or uploaded.
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QNetworkReplyFileImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void setFileName(const QString &name)
Sets the name of the file.
Definition: qfile.cpp:494
virtual void abort()
Aborts the operation immediately and close down any network connections still open.
QDateTime lastModified() const
Returns the date and time when the file was last modified.
Definition: qfileinfo.cpp:1296