1/****************************************************************************
2**
3** Copyright (C) 2005 Ralf Habacker. All rights reserved.
4**
5** This file is part of the KDE installer for windows
6**
7** This file may be used under the terms of the GNU General Public
8** License version 2.0 as published by the Free Software Foundation
9** and appearing in the file LICENSE.GPL included in the packaging of
10** this file. Please review the following information to ensure GNU
11** General Public Licensing requirements will be met:
12** http://www.trolltech.com/products/qt/opensource.html
13**
14** If you are unsure which license is appropriate for your use, please
15** review the following information:
16** http://www.trolltech.com/products/qt/licensing.html or contact the
17** sales department at sales@trolltech.com.
18**
19** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21**
22****************************************************************************/
23
24#ifndef DOWNLOADER_H
25#define DOWNLOADER_H
26
27#include "hash.h"
28#include "downloaderprogress.h"
29
30#include <QObject>
31#include <QString>
32#include <QUrl>
33
34class QEventLoop;
35class DownloaderProgress;
36class Downloader : public QObject
37{
38 Q_OBJECT
39public:
40 typedef enum { Undefined, Finished, Failed, Aborted } ResultType;
41public:
42 // dtor
43 virtual ~Downloader();
44 // singleton
45 static Downloader *instance();
46 // for user interaction
47 void setProgress ( DownloaderProgress *progress );
48 DownloaderProgress *progress();
49 // start download
50 bool fetch ( const QUrl &url, const QString &fileName = QString() );
51 bool fetch ( const QUrl &url, QByteArray &ba );
52
53 /// cancel started download
54 void cancel();
55 /// return result string
56 QString resultString() const {
57 return m_resultString;
58 }
59 /// return download result
60 ResultType result() const {
61 return m_result;
62 }
63 /// return real used url for downloading
64 QUrl usedURL() const {
65 return m_usedURL;
66 }
67 // set checksum type
68 void setCheckSumType(Hash::Type type);
69
70 // calculated checksum
71 QByteArray checkSum() const;
72
73Q_SIGNALS:
74 void done ( bool error );
75 void error ( const QString &error );
76protected Q_SLOTS:
77 void threadFinished ();
78 int progressCallback ( double ultotal, double ulnow );
79protected:
80 void setError ( const QString &errStr );
81 bool fetchInternal ( const QUrl &url );
82 size_t curlWrite ( const char * data, size_t size );
83 static size_t curlWriteCallback ( void *ptr, size_t size, size_t nmemb, void *stream );
84 static int curlProgressCallback ( void *clientp, double dltotal, double dlnow,
85 double ultotal, double ulnow );
86protected:
87 ResultType m_result;
88 QString m_resultString;
89 QUrl m_usedURL;
90 QByteArray m_checkSum;
91 QEventLoop *m_loop;
92 class Private;
93 Private * const d;
94
95 friend QDebug &operator<< ( QDebug &, const Downloader & );
96private:
97 Downloader ();
98
99 friend class DownloaderSingleton;
100};
101
102#endif
103