choke uploadProgress signals
[profile/ivi/qtbase.git] / src / network / access / qnetworkreplyfileimpl.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 "qnetworkreplyfileimpl_p.h"
43
44 #include "QtCore/qdatetime.h"
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QFileInfo>
47 #include <QDebug>
48
49 QT_BEGIN_NAMESPACE
50
51 QNetworkReplyFileImplPrivate::QNetworkReplyFileImplPrivate()
52     : QNetworkReplyPrivate(), realFileSize(0)
53 {
54 }
55
56 QNetworkReplyFileImpl::~QNetworkReplyFileImpl()
57 {
58 }
59
60 QNetworkReplyFileImpl::QNetworkReplyFileImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op)
61     : QNetworkReply(*new QNetworkReplyFileImplPrivate(), parent)
62 {
63     setRequest(req);
64     setUrl(req.url());
65     setOperation(op);
66     setFinished(true);
67     QNetworkReply::open(QIODevice::ReadOnly);
68
69     QNetworkReplyFileImplPrivate *d = (QNetworkReplyFileImplPrivate*) d_func();
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());
80         setError(QNetworkReply::ProtocolInvalidOperationError, msg);
81         QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
82             Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError));
83         QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
84         return;
85     }
86 #endif
87     if (url.path().isEmpty())
88         url.setPath(QLatin1String("/"));
89     setUrl(url);
90
91
92     QString fileName = url.toLocalFile();
93     if (fileName.isEmpty()) {
94         if (url.scheme() == QLatin1String("qrc"))
95             fileName = QLatin1Char(':') + url.path();
96         else
97             fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
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());
103         setError(QNetworkReply::ContentOperationNotPermittedError, msg);
104         QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
105             Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError));
106         QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
107         return;
108     }
109
110     d->realFile.setFileName(fileName);
111     bool opened = d->realFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered);
112
113     // could we open the file?
114     if (!opened) {
115         QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2")
116                 .arg(d->realFile.fileName(), d->realFile.errorString());
117
118         if (d->realFile.exists()) {
119             setError(QNetworkReply::ContentAccessDenied, msg);
120             QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
121                 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied));
122         } else {
123             setError(QNetworkReply::ContentNotFoundError, msg);
124             QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
125                 Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError));
126         }
127         QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
128         return;
129     }
130
131     setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified());
132     d->realFileSize = fi.size();
133     setHeader(QNetworkRequest::ContentLengthHeader, d->realFileSize);
134
135     QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection);
136     QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection,
137         Q_ARG(qint64, d->realFileSize), Q_ARG(qint64, d->realFileSize));
138     QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
139     QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
140 }
141 void QNetworkReplyFileImpl::close()
142 {
143     Q_D(QNetworkReplyFileImpl);
144     QNetworkReply::close();
145     d->realFile.close();
146 }
147
148 void QNetworkReplyFileImpl::abort()
149 {
150     Q_D(QNetworkReplyFileImpl);
151     QNetworkReply::close();
152     d->realFile.close();
153 }
154
155 qint64 QNetworkReplyFileImpl::bytesAvailable() const
156 {
157     Q_D(const QNetworkReplyFileImpl);
158     return QNetworkReply::bytesAvailable() + d->realFile.bytesAvailable();
159 }
160
161 bool QNetworkReplyFileImpl::isSequential () const
162 {
163     return true;
164 }
165
166 qint64 QNetworkReplyFileImpl::size() const
167 {
168     Q_D(const QNetworkReplyFileImpl);
169     return d->realFileSize;
170 }
171
172 /*!
173     \internal
174 */
175 qint64 QNetworkReplyFileImpl::readData(char *data, qint64 maxlen)
176 {
177     Q_D(QNetworkReplyFileImpl);
178     qint64 ret = d->realFile.read(data, maxlen);
179     if (ret == 0 && bytesAvailable() == 0)
180         return -1;
181     else
182         return ret;
183 }
184
185
186 QT_END_NAMESPACE
187
188 #include "moc_qnetworkreplyfileimpl_p.cpp"
189