Sayonara Player
AsyncWebAccess.h
1 /* AsyncWebAccess.h */
2 
3 /* Copyright (C) 2011-2017 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 #ifndef ASYNCWEBACCESS_H_
22 #define ASYNCWEBACCESS_H_
23 
24 #include <QMap>
25 #include <QObject>
26 
27 #include "AbstractWebAccess.h"
28 #include "Helper/Pimpl.h"
29 
30 
31 class QImage;
32 class QString;
33 class QNetworkReply;
39  public QObject,
40  public AbstractWebAccess
41 {
42  Q_OBJECT
43 
44 public:
45 
49  enum class Behavior : quint8
50  {
51  AsBrowser=0,
52  AsSayonara,
53  Random,
54  None
55  };
56 
57 
58  enum class Status : quint8
59  {
60  GotData,
61  AudioStream,
62  NoHttp,
63  NoData,
64  Timeout,
65  Error
66  };
67 
68 signals:
69  void sig_finished();
70 
71 public:
72 
78  AsyncWebAccess(QObject* parent=nullptr, const QByteArray& header=QByteArray(),
79  AsyncWebAccess::Behavior behavior=AsyncWebAccess::Behavior::AsBrowser);
80 
81  ~AsyncWebAccess();
82 
87  QByteArray data() const;
88 
93  bool has_data() const;
94 
99  QImage image() const;
100 
101 
107  QString url() const;
108 
114  void set_behavior(AsyncWebAccess::Behavior behavior);
115 
116 
122  void run(const QString& url, int timeout=4000);
123 
130  void run_post(const QString& url, const QByteArray& post_data, int timeout=4000);
131 
136  void set_raw_header(const QMap<QByteArray, QByteArray>& header);
137 
142  AsyncWebAccess::Status status() const;
143 
148  bool has_error() const;
149 
150  void stop() override;
151 
152 
153 private slots:
154 
155  void data_available();
156 
161  void finished();
162 
163  void redirected(const QUrl& url);
164 
169  void timeout();
170 
171 private:
172  PIMPL(AsyncWebAccess)
173  void redirect_request(QString redirect_url);
174 };
175 
176 #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:49
QString url() const
get last called url. This url may differ from the originally called url when request has been redire...
QByteArray data() const
get fetched data
void set_raw_header(const QMap< QByteArray, QByteArray > &header)
modify header.
bool has_data() const
indicates, if data is avaialbe
AsyncWebAccess(QObject *parent=nullptr, const QByteArray &header=QByteArray(), AsyncWebAccess::Behavior behavior=AsyncWebAccess::Behavior::AsBrowser)
AsyncWebAccess constructor.
AsyncWebAccess::Status status() const
Request Status.
Asynchgronous web access class.
Definition: AsyncWebAccess.h:38
QImage image() const
get fetched data formatted as image
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...
bool has_error() const
Indicates if error.
Definition: AbstractWebAccess.h:26