Sayonara Player
AsyncWebAccess.h
1 /* AsyncWebAccess.h */
2 
3 /* Copyright (C) 2011-2016 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 #ifndef ASYNCWEBACCESS_H_
24 #define ASYNCWEBACCESS_H_
25 
26 #include <QNetworkAccessManager>
27 #include <QNetworkReply>
28 #include <QString>
29 #include <QByteArray>
30 #include <QImage>
31 #include <QTimer>
32 
37 class AsyncWebAccess : public QObject{
38 
39  Q_OBJECT
40 
41 signals:
42 
43  void sig_finished(bool success);
44 
45 public:
46 
50  enum class Behavior : quint8 {
51  AsBrowser=0,
52  AsSayonara
53  };
54 
60  AsyncWebAccess(QObject* parent=nullptr, const QByteArray& header=QByteArray(),
61  AsyncWebAccess::Behavior behavior=AsyncWebAccess::Behavior::AsBrowser);
62 
63  virtual ~AsyncWebAccess();
64 
69  QByteArray get_data() const;
70 
75  QImage get_image() const;
76 
82  QString get_url() const;
83 
90 
91 
97  void run(const QString& url, int timeout=4000);
98 
105  void run_post(const QString& url, const QByteArray& post_data, int timeout=4000);
106 
111  void set_raw_header(const QMap<QByteArray, QByteArray>& header);
112 
113 
114 private slots:
115 
120  void finished(QNetworkReply* reply);
121 
126  void timeout();
127 
128 private:
129 
130  QNetworkAccessManager* _nam=nullptr;
131  QString _url;
132  QByteArray _data;
133  AsyncWebAccess::Behavior _behavior;
134 
135 
136  QTimer* _timer=nullptr;
137  QNetworkReply* _reply=nullptr;
138 
140 
141 };
142 
143 
144 #endif
void run_post(const QString &url, const QByteArray &post_data, int timeout=4000)
starts a POST request
Behavior
The Behavior enum. Responsible for the user-agent variable in the HTTP header.
Definition: AsyncWebAccess.h:50
QImage get_image() const
get fetched data formatted as image
void set_raw_header(const QMap< QByteArray, QByteArray > &header)
modify header.
AsyncWebAccess(QObject *parent=nullptr, const QByteArray &header=QByteArray(), AsyncWebAccess::Behavior behavior=AsyncWebAccess::Behavior::AsBrowser)
AsyncWebAccess constructor.
QByteArray get_data() const
get fetched data
QString get_url() const
get last called url. This url may differ from the originally called url when request has been redire...
Asynchgronous web access class.
Definition: AsyncWebAccess.h:37
void run(const QString &url, int timeout=4000)
starts a GET request
void set_behavior(AsyncWebAccess::Behavior behavior)
Set the behaviour how sayonara should be recognized by the server. This variable will set the user-ag...