1 | /* |
2 | * Copyright (C) 2012 Cutehacks AS. All rights reserved. |
3 | * info@cutehacks.com |
4 | * http://cutehacks.com |
5 | * |
6 | * This file is part of Fly. |
7 | * |
8 | * Fly is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * Fly is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with Fly. If not, see <http://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | #ifndef RECEIVER_H |
23 | #define RECEIVER_H |
24 | |
25 | #include <QtCore> |
26 | #include <QtNetwork> |
27 | #include <QtGui> |
28 | #include "node.h" |
29 | |
30 | class Receiver; |
31 | |
32 | class RequestQueue : public QObject |
33 | { |
34 | Q_OBJECT |
35 | public: |
36 | RequestQueue(QObject *parent = 0); |
37 | ~RequestQueue(); |
38 | |
39 | public Q_SLOTS: |
40 | void enableCaching(bool enable); |
41 | void setCacheSize(int size); |
42 | void setCacheMechanism(QNetworkRequest::CacheLoadControl mechanism); |
43 | void setCacheExpiration(int seconds); |
44 | void sendRequest(const QUrl &url, Receiver *receiver, QNetworkAccessManager::Operation operation, const QByteArray &data); |
45 | void setCookies(const QList<QNetworkCookie> &cookies); |
46 | void setUserAgent(const QByteArray &userAgent); |
47 | void (const QByteArray &, const QByteArray &); |
48 | void abort(Receiver *receiver); |
49 | |
50 | protected Q_SLOTS: |
51 | void replyFinished(QNetworkReply *reply); |
52 | void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator); |
53 | |
54 | Q_SIGNALS: |
55 | void error(Receiver *receiver, const QString &errorString); |
56 | void replyFinished(Receiver *receiver, const QByteArray &reply, const QString &errorString); |
57 | void authenticationRequired(Receiver *receiver, const QByteArray &reply, QAuthenticator *authenticator); |
58 | |
59 | protected: |
60 | QNetworkAccessManager *manager() const; |
61 | void setManager(QNetworkAccessManager *manager); |
62 | |
63 | private: |
64 | QHash<QNetworkReply*, Receiver*> m_receivers; |
65 | QNetworkAccessManager *m_manager; |
66 | QList<QNetworkCookie> m_cookies; |
67 | QHash <QByteArray, QByteArray> ; |
68 | int m_cacheMaxSize; |
69 | QNetworkRequest::CacheLoadControl m_cacheMechanism; |
70 | int m_cacheExpirationSeconds; |
71 | friend class RequestQueueThread; // ### WARNING!!! Use with caution! |
72 | }; |
73 | |
74 | Q_DECLARE_METATYPE(QNetworkAccessManager::Operation) |
75 | |
76 | |
77 | class RequestQueueThread : public QThread |
78 | { |
79 | Q_OBJECT |
80 | public: |
81 | RequestQueueThread(QObject *parent=0); |
82 | ~RequestQueueThread(); |
83 | |
84 | static RequestQueueThread *instance(); |
85 | static QNetworkAccessManager *manager(); // ### WARNING!!! Use with caution! |
86 | static void setManager(QNetworkAccessManager *manager); // ### WARNING!!! Use with caution! |
87 | |
88 | void enableCaching(bool enable); |
89 | void setCacheSize(int size); |
90 | void setCacheMechanism(QNetworkRequest::CacheLoadControl mechanism); |
91 | void setCacheExpiration(int seconds); |
92 | void sendRequest(const QUrl &url, Receiver *receiver, |
93 | QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, |
94 | const QByteArray &data = QByteArray()); |
95 | void setCookies(const QList<QNetworkCookie> &cookies); |
96 | void setUserAgent(const QByteArray &userAgent); |
97 | void (const QByteArray &, const QByteArray &); |
98 | void abort(Receiver *receiver); |
99 | |
100 | protected Q_SLOTS: |
101 | void error(Receiver *receiver, const QString &errorString); |
102 | void replyFinished(Receiver *receiver, const QByteArray &reply, const QString &errorString); |
103 | |
104 | Q_SIGNALS: |
105 | void enableCachingSignal(bool enable); |
106 | void setCacheSizeSignal(int size); |
107 | void setCacheMechanismSignal(QNetworkRequest::CacheLoadControl mechanism); |
108 | void setCacheExpirationSignal(int seconds); |
109 | void sendRequestSignal(const QUrl &url, Receiver *receiver, QNetworkAccessManager::Operation operation, const QByteArray &data); |
110 | void setCookiesSignal(const QList<QNetworkCookie> &cookies); |
111 | void setUserAgentSignal(const QByteArray &userAgent); |
112 | void (const QByteArray &, const QByteArray &); |
113 | void abortSignal(Receiver *receiver); |
114 | |
115 | protected: |
116 | void run(); |
117 | void connectToQueue(RequestQueue *queue); |
118 | |
119 | private: |
120 | static QMutex m_mutex; |
121 | static QWaitCondition m_condition; |
122 | static bool m_connected; |
123 | static bool m_threaded; |
124 | static RequestQueue *m_queue; // ### WARNING!!! Use with caution! |
125 | }; |
126 | |
127 | class Receiver : public QObject |
128 | { |
129 | friend class RequestQueueThread; |
130 | Q_OBJECT |
131 | public: |
132 | Receiver(QObject *parent = 0); |
133 | ~Receiver(); |
134 | |
135 | Q_SIGNALS: |
136 | void finished(); |
137 | void error(const QString &error); |
138 | |
139 | public Q_SLOTS: |
140 | virtual void request(const QUrl &url); |
141 | void abortRequest(); |
142 | |
143 | protected Q_SLOTS: |
144 | virtual void replyFinished(const QByteArray &reply, const QString &errorString); |
145 | }; |
146 | |
147 | class NodeReceiver : public Receiver |
148 | { |
149 | Q_OBJECT |
150 | public: |
151 | NodeReceiver(QObject *parent = 0); |
152 | ~NodeReceiver(); |
153 | |
154 | void clear(); |
155 | Node *rootNode() const; |
156 | void setRootNode(Node *node); |
157 | |
158 | private: |
159 | Node *m_root; |
160 | }; |
161 | |
162 | class XmlReceiver : public NodeReceiver |
163 | { |
164 | Q_OBJECT |
165 | public: |
166 | XmlReceiver(QObject *parent = 0); |
167 | ~XmlReceiver(); |
168 | |
169 | protected: |
170 | void replyFinished(const QByteArray &reply, const QString &errorString); |
171 | }; |
172 | |
173 | class JsonReceiver : public NodeReceiver |
174 | { |
175 | Q_OBJECT |
176 | public: |
177 | JsonReceiver(QObject *parent = 0); |
178 | ~JsonReceiver(); |
179 | |
180 | protected: |
181 | void replyFinished(const QByteArray &reply, const QString &errorString); |
182 | }; |
183 | |
184 | class PixmapReceiver : public Receiver |
185 | { |
186 | Q_OBJECT |
187 | Q_PROPERTY(QPixmap pixmap READ pixmap) |
188 | public: |
189 | PixmapReceiver(QObject *parent = 0); |
190 | QPixmap pixmap() const; |
191 | #if 0 |
192 | void request(const QUrl &url); |
193 | #endif |
194 | Q_SIGNALS: |
195 | void finished(const QPixmap &pixmap); |
196 | |
197 | protected: |
198 | void replyFinished(const QByteArray &reply, const QString &errorString); |
199 | |
200 | private: |
201 | QPixmap m_pixmap; |
202 | #if 0 |
203 | QString m_path; |
204 | #endif |
205 | }; |
206 | |
207 | #endif |
208 | |