1/*
2 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#ifndef QtFileDownloader_h
22#define QtFileDownloader_h
23
24#include <QNetworkReply>
25#include <QNetworkRequest>
26#include <wtf/OwnPtr.h>
27#include <wtf/PassOwnPtr.h>
28
29QT_BEGIN_NAMESPACE
30class QFile;
31class QNetworkAccessManager;
32class QNetworkRequest;
33QT_END_NAMESPACE
34
35namespace WebCore {
36class ResourceError;
37}
38
39namespace WebKit {
40class Download;
41
42class QtFileDownloader : public QObject {
43    Q_OBJECT
44public:
45    QtFileDownloader(Download*, PassOwnPtr<QNetworkReply>);
46    virtual ~QtFileDownloader();
47    void cancel();
48    void init();
49    void startTransfer(const QString& destination);
50
51    enum DownloadError {
52        DownloadErrorAborted = 0,
53        DownloadErrorCannotWriteToFile,
54        DownloadErrorCannotOpenFile,
55        DownloadErrorDestinationAlreadyExists,
56        DownloadErrorCancelled,
57        DownloadErrorCannotDetermineFilename,
58        DownloadErrorNetworkFailure
59    };
60
61private Q_SLOTS:
62    void onReadyRead();
63    void onFinished();
64    void onError(QNetworkReply::NetworkError);
65
66private:
67    void abortDownloadWritingAndEmitError(QtFileDownloader::DownloadError);
68    QString determineFilename();
69    void handleDownloadResponse();
70
71    Download* m_download;
72    OwnPtr<QNetworkReply> m_reply;
73    OwnPtr<QFile> m_destinationFile;
74    QNetworkReply::NetworkError m_error;
75    bool m_headersRead;
76};
77
78} // namespace WebKit
79
80#endif
81