src/network/access/qnetworkreplyimpl.cpp
changeset 23 89e065397ea6
parent 18 2f34d5167611
child 30 5dc02b23752f
--- a/src/network/access/qnetworkreplyimpl.cpp	Fri May 14 16:40:13 2010 +0300
+++ b/src/network/access/qnetworkreplyimpl.cpp	Thu May 27 13:40:48 2010 +0300
@@ -403,6 +403,37 @@
     return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount());
 }
 
+void QNetworkReplyImplPrivate::initCacheSaveDevice()
+{
+    Q_Q(QNetworkReplyImpl);
+
+    // save the meta data
+    QNetworkCacheMetaData metaData;
+    metaData.setUrl(url);
+    metaData = backend->fetchCacheMetaData(metaData);
+
+    // save the redirect request also in the cache
+    QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute);
+    if (redirectionTarget.isValid()) {
+        QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes();
+        attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
+        metaData.setAttributes(attributes);
+    }
+
+    cacheSaveDevice = networkCache()->prepare(metaData);
+
+    if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) {
+        if (cacheSaveDevice && !cacheSaveDevice->isOpen())
+            qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- "
+                  "class %s probably needs to be fixed",
+                  networkCache()->metaObject()->className());
+
+        networkCache()->remove(url);
+        cacheSaveDevice = 0;
+        cacheEnabled = false;
+    }
+}
+
 // we received downstream data and send this to the cache
 // and to our readBuffer (which in turn gets read by the user of QNetworkReply)
 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data)
@@ -412,36 +443,12 @@
         return;
 
     if (cacheEnabled && !cacheSaveDevice) {
-        // save the meta data
-        QNetworkCacheMetaData metaData;
-        metaData.setUrl(url);
-        metaData = backend->fetchCacheMetaData(metaData);
-
-        // save the redirect request also in the cache
-        QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute);
-        if (redirectionTarget.isValid()) {
-            QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes();
-            attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
-            metaData.setAttributes(attributes);
-        }
-
-        cacheSaveDevice = networkCache()->prepare(metaData);
-
-        if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) {
-            if (cacheSaveDevice && !cacheSaveDevice->isOpen())
-                qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- "
-                      "class %s probably needs to be fixed",
-                      networkCache()->metaObject()->className());
-
-            networkCache()->remove(url);
-            cacheSaveDevice = 0;
-            cacheEnabled = false;
-        }
+        initCacheSaveDevice();
     }
 
     qint64 bytesWritten = 0;
     for (int i = 0; i < data.bufferCount(); i++) {
-        QByteArray item = data[i];
+        QByteArray const &item = data[i];
 
         if (cacheSaveDevice)
             cacheSaveDevice->write(item.constData(), item.size());
@@ -454,6 +461,13 @@
     bytesDownloaded += bytesWritten;
     lastBytesDownloaded = bytesDownloaded;
 
+    appendDownstreamDataSignalEmissions();
+}
+
+void QNetworkReplyImplPrivate::appendDownstreamDataSignalEmissions()
+{
+    Q_Q(QNetworkReplyImpl);
+
     QPointer<QNetworkReplyImpl> qq = q;
 
     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
@@ -495,6 +509,15 @@
     _q_copyReadyRead();
 }
 
+void QNetworkReplyImplPrivate::appendDownstreamData(const QByteArray &data)
+{
+    // TODO implement
+
+    // TODO call
+
+    qFatal("QNetworkReplyImplPrivate::appendDownstreamData not implemented");
+}
+
 void QNetworkReplyImplPrivate::finished()
 {
     Q_Q(QNetworkReplyImpl);
@@ -580,8 +603,13 @@
 QNetworkReplyImpl::~QNetworkReplyImpl()
 {
     Q_D(QNetworkReplyImpl);
+
+    // This code removes the data from the cache if it was prematurely aborted.
+    // See QNetworkReplyImplPrivate::completeCacheSave(), we disable caching there after the cache
+    // save had been properly finished. So if it is still enabled it means we got deleted/aborted.
     if (d->isCachingEnabled())
         d->networkCache()->remove(url());
+
     if (d->outgoingDataBuffer)
         delete d->outgoingDataBuffer;
 }