Sayonara Player
AsyncWebAccess.h
1 /* AsyncWebAccess.h */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (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 SAYONARA_ASYNC_WEB_ACCESS_H
22 #define SAYONARA_ASYNC_WEB_ACCESS_H
23 
24 #include "AbstractWebAccess.h"
25 #include "Utils/Pimpl.h"
26 
27 #include <QObject>
28 
29 class QImage;
30 
36  public QObject,
37  public AbstractWebAccess
38 {
39  Q_OBJECT
40  PIMPL(AsyncWebAccess)
41 
42  public:
43  enum class Behavior :
44  uint8_t
45  {
46  AsBrowser = 0,
47  AsSayonara,
48  Random,
49  None
50  };
51 
52  enum class Status :
53  uint8_t
54  {
55  NoError = 0,
56  GotData,
57  AudioStream,
58  NoData,
59  NoHttp,
60  NotFound,
61  Timeout,
62  Error
63  };
64 
65  signals:
66  void sigFinished();
67  void sigStopped();
68 
69  public:
70  explicit AsyncWebAccess(QObject* parent = nullptr,
71  AsyncWebAccess::Behavior behavior = AsyncWebAccess::Behavior::AsBrowser);
72 
73  virtual ~AsyncWebAccess() override;
74 
75  QByteArray data() const;
76  bool hasData() const;
77 
78  QImage image() const;
79  QString url() const;
80 
81  AsyncWebAccess::Status status() const;
82  bool hasError() const;
83 
84  void setBehavior(AsyncWebAccess::Behavior behavior);
85  void setRawHeader(const QMap<QByteArray, QByteArray>& header);
86 
87  void run(const QString& url, int timeout = 4000);
88  void runPost(const QString& url, const QByteArray& postData, int timeout = 4000);
89 
90  public slots:
91  void stop() override;
92 
93  private slots:
94  void dataAvailable();
95  void finished();
96  void timeout();
97 };
98 
100  public QObject
101 {
102  friend class AsyncWebAccess;
103 
104  Q_OBJECT
105  PIMPL(AsyncWebAccessStopper)
106 
107  signals:
108  void sigFinished();
109  void sigTimeout();
110  void sigStopped();
111 
112  private:
113  explicit AsyncWebAccessStopper(QObject* parent);
114  ~AsyncWebAccessStopper() noexcept;
115 
116  private slots:
117  void startTimer(int timeout);
118  void stopTimer();
119  void timeout();
120  void stop();
121 };
122 
123 #endif // SAYONARA_ASYNC_WEB_ACCESS_H
AsyncWebAccessStopper
Definition: AsyncWebAccess.h:101
AsyncWebAccess
Asynchgronous web access class.
Definition: AsyncWebAccess.h:38
QMap
Definition: org_mpris_media_player2_adaptor.h:21
AbstractWebAccess
Definition: AbstractWebAccess.h:27