summaryrefslogtreecommitdiffstats
path: root/utils/rbutilqt/base/httpget.h
blob: 443a606e6d236fe6fdf7fcc97d5efa66a27e474e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Riebeling
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/


#ifndef HTTPGET_H
#define HTTPGET_H

#include <QtCore>
#include <QtNetwork>
#include <QNetworkAccessManager>
#include "Logger.h"

class HttpGet : public QObject
{
    Q_OBJECT

    public:
        HttpGet(QObject *parent = nullptr);

        void getFile(const QUrl &url);
        void setProxy(const QUrl &url);
        void setProxy(bool);
        QString errorString(void);
        void setFile(QFile*);
        void setCache(const QDir&);
        void setCache(bool);
        int httpResponse(void);
        QByteArray readAll(void);
        bool isCached()
            { return m_lastRequestCached; }
        QDateTime timestamp(void)
            { return m_lastServerTimestamp; }
        //< set global cache path
        static void setGlobalCache(const QDir& d)
        {
            LOG_INFO() << "Global cache set to" << d.absolutePath();
            m_globalCache = d;
        }
        //< set global proxy value
        static void setGlobalProxy(const QUrl& p)
        {
            LOG_INFO() << "setting global proxy" << p;
            if(!p.isValid() || p.isEmpty()) {
                HttpGet::m_globalProxy.setType(QNetworkProxy::NoProxy);
            }
            else {
                HttpGet::m_globalProxy.setType(QNetworkProxy::HttpProxy);
                HttpGet::m_globalProxy.setHostName(p.host());
                HttpGet::m_globalProxy.setPort(p.port());
                HttpGet::m_globalProxy.setUser(p.userName());
                HttpGet::m_globalProxy.setPassword(p.password());
            }
            QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
            QNetworkProxy::setApplicationProxy(HttpGet::m_globalProxy);
        }
        //< set global user agent string
        static void setGlobalUserAgent(const QString& u)
            { m_globalUserAgent = u; }

    public slots:
        void abort(void);

    signals:
        void done(QNetworkReply::NetworkError error);
        void dataReadProgress(int, int);
        void headerFinished(void);

    private slots:
        void requestFinished(QNetworkReply* reply);
        void startRequest(QUrl url);
        void downloadProgress(qint64 received, qint64 total);
        void networkError(QNetworkReply::NetworkError error);

    private:
        static QString m_globalUserAgent;
        static QNetworkProxy m_globalProxy;
        QNetworkAccessManager m_mgr;
        QNetworkReply *m_reply;
        QNetworkDiskCache *m_cache;
        QDir m_cachedir;
        static QDir m_globalCache; //< global cache path value
        QByteArray m_data;
        QFile *m_outputFile;
        int m_lastStatusCode;
        QString m_lastErrorString;
        QDateTime m_lastServerTimestamp;
        bool m_lastRequestCached;
        QNetworkProxy m_proxy;
};


#endif