~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/network/access/qnetworkaccessfilebackend.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/****************************************************************************
2
2
**
3
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
4
5
** Contact: Nokia Corporation (qt-info@nokia.com)
5
6
**
6
7
** This file is part of the QtNetwork module of the Qt Toolkit.
7
8
**
8
9
** $QT_BEGIN_LICENSE:LGPL$
9
 
** Commercial Usage
10
 
** Licensees holding valid Qt Commercial licenses may use this file in
11
 
** accordance with the Qt Commercial License Agreement provided with the
12
 
** Software or, alternatively, in accordance with the terms contained in
13
 
** a written agreement between you and Nokia.
 
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.
14
15
**
15
16
** GNU Lesser General Public License Usage
16
17
** Alternatively, this file may be used under the terms of the GNU Lesser
20
21
** ensure the GNU Lesser General Public License version 2.1 requirements
21
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22
23
**
23
 
** In addition, as a special exception, Nokia gives you certain
24
 
** additional rights. These rights are described in the Nokia Qt LGPL
25
 
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26
 
** 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 are unsure which license is appropriate for your use, please
37
 
** contact the sales department at http://www.qtsoftware.com/contact.
 
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
38
** $QT_END_LICENSE$
39
39
**
40
40
****************************************************************************/
43
43
#include "qfileinfo.h"
44
44
#include "qurlinfo.h"
45
45
#include "qdir.h"
 
46
#include "private/qnoncontiguousbytedevice_p.h"
46
47
 
47
48
#include <QtCore/QCoreApplication>
48
49
 
77
78
}
78
79
 
79
80
QNetworkAccessFileBackend::QNetworkAccessFileBackend()
80
 
    : totalBytes(0)
 
81
    : uploadByteDevice(0), totalBytes(0), hasUploadFinished(false)
81
82
{
82
83
}
83
84
 
108
109
    QString fileName = url.toLocalFile();
109
110
    if (fileName.isEmpty()) {
110
111
        if (url.scheme() == QLatin1String("qrc"))
111
 
            fileName = QLatin1String(":") + url.path();
 
112
            fileName = QLatin1Char(':') + url.path();
112
113
        else
113
114
            fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
114
115
    }
126
127
        break;
127
128
    case QNetworkAccessManager::PutOperation:
128
129
        mode = QIODevice::WriteOnly | QIODevice::Truncate;
 
130
        uploadByteDevice = createUploadByteDevice();
 
131
        QObject::connect(uploadByteDevice, SIGNAL(readyRead()), this, SLOT(uploadReadyReadSlot()));
 
132
        QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection);
129
133
        break;
130
134
    default:
131
135
        Q_ASSERT_X(false, "QNetworkAccessFileBackend::open",
152
156
    }
153
157
}
154
158
 
 
159
void QNetworkAccessFileBackend::uploadReadyReadSlot()
 
160
{
 
161
    if (hasUploadFinished)
 
162
        return;
 
163
 
 
164
    forever {
 
165
        qint64 haveRead;
 
166
        const char *readPointer = uploadByteDevice->readPointer(-1, haveRead);
 
167
        if (haveRead == -1) {
 
168
            // EOF
 
169
            hasUploadFinished = true;
 
170
            file.flush();
 
171
            file.close();
 
172
            finished();
 
173
            break;
 
174
        } else if (haveRead == 0 || readPointer == 0) {
 
175
            // nothing to read right now, we will be called again later
 
176
            break;
 
177
        } else {
 
178
            qint64 haveWritten;
 
179
            haveWritten = file.write(readPointer, haveRead);
 
180
 
 
181
            if (haveWritten < 0) {
 
182
                // write error!
 
183
                QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Write error writing to %1: %2")
 
184
                              .arg(url().toString(), file.errorString());
 
185
                error(QNetworkReply::ProtocolFailure, msg);
 
186
 
 
187
                finished();
 
188
                return;
 
189
            } else {
 
190
                uploadByteDevice->advanceReadPointer(haveWritten);
 
191
            }
 
192
 
 
193
 
 
194
            file.flush();
 
195
        }
 
196
    }
 
197
}
 
198
 
155
199
void QNetworkAccessFileBackend::closeDownstreamChannel()
156
200
{
157
201
    if (operation() == QNetworkAccessManager::GetOperation) {
158
202
        file.close();
159
 
        //downstreamChannelClosed();
160
 
    }
161
 
}
162
 
 
163
 
void QNetworkAccessFileBackend::closeUpstreamChannel()
164
 
{
165
 
    if (operation() == QNetworkAccessManager::PutOperation) {
166
 
        file.close();
167
 
        finished();
168
203
    }
169
204
}
170
205
 
174
209
    return readMoreFromFile();
175
210
}
176
211
 
177
 
bool QNetworkAccessFileBackend::waitForUpstreamBytesWritten(int)
178
 
{
179
 
    Q_ASSERT_X(false, "QNetworkAccessFileBackend::waitForUpstreamBytesWritten",
180
 
               "This function should never have been called, since there is never anything "
181
 
               "left to be written!");
182
 
    return false;
183
 
}
184
 
 
185
 
void QNetworkAccessFileBackend::upstreamReadyRead()
186
 
{
187
 
    Q_ASSERT_X(operation() == QNetworkAccessManager::PutOperation, "QNetworkAccessFileBackend",
188
 
               "We're being told to upload data but operation isn't PUT!");
189
 
 
190
 
    // there's more data to be written to the file
191
 
    while (upstreamBytesAvailable()) {
192
 
        // write everything and let QFile handle it
193
 
        int written = file.write(readUpstream());
194
 
 
195
 
        if (written < 0) {
196
 
            // write error!
197
 
            QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Write error writing to %1: %2")
198
 
                                                    .arg(url().toString(), file.errorString());
199
 
            error(QNetworkReply::ProtocolFailure, msg);
200
 
 
201
 
            finished();
202
 
            return;
203
 
        }
204
 
 
205
 
        // successful write
206
 
        file.flush();
207
 
        upstreamBytesConsumed(written);
208
 
    }
209
 
}
210
 
 
211
212
void QNetworkAccessFileBackend::downstreamReadyWrite()
212
213
{
213
214
    Q_ASSERT_X(operation() == QNetworkAccessManager::GetOperation, "QNetworkAccessFileBackend",
262
263
 
263
264
        data.resize(actuallyRead);
264
265
        totalBytes += actuallyRead;
265
 
        writeDownstreamData(data);
 
266
 
 
267
        QByteDataBuffer list;
 
268
        list.append(data);
 
269
        data.clear(); // important because of implicit sharing!
 
270
        writeDownstreamData(list);
266
271
    }
267
272
    return true;
268
273
}