-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmynetworkmanager.hh
More file actions
113 lines (95 loc) · 3.38 KB
/
mynetworkmanager.hh
File metadata and controls
113 lines (95 loc) · 3.38 KB
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
111
112
113
/*
* This file is part of TTRss, a Tiny Tiny RSS Reader App
* for MeeGo Harmattan and Sailfish OS.
* Copyright (C) 2012–2015 Hauke Schade
*
* TTRss 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.
*
* TTRss is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with TTRss; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or see
* http://www.gnu.org/licenses/.
*/
#ifndef MYNETWORKMANAGER_HH
#define MYNETWORKMANAGER_HH
#include <QtCore/QtGlobal>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QtQml/QQmlNetworkAccessManagerFactory>
#else
#include <qdeclarativenetworkaccessmanagerfactory.h>
#endif
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <QtCore/QMutex>
#include <QtCore/QDebug>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
class MyNetworkManager : public QObject, public QQmlNetworkAccessManagerFactory
#else
class MyNetworkManager : public QObject, public QDeclarativeNetworkAccessManagerFactory
#endif
{
Q_OBJECT
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
Q_PROPERTY(bool gotSSLError READ gotSSLError NOTIFY gotSSLErrorChanged)
public: // QDeclarativeNetworkAccessManagerFactory
QNetworkAccessManager *create(QObject *parent);
static MyNetworkManager *instance();
bool loading() const {
return this->_numRequests > 0;
}
bool gotSSLError() const {
return this->_gotSSLError;
}
signals:
void loadingChanged();
void gotSSLErrorChanged();
private slots:
void onSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
void onReplyFinished(QNetworkReply *reply);
void onStarted();
void onError();
private:
static QScopedPointer<MyNetworkManager> m_instance;
int _numRequests;
bool _gotSSLError;
QMutex _mutex;
void incNumRequests() {
QMutexLocker locker(&_mutex);
_numRequests++;
//qDebug() << "numReq is now " << _numRequests;
if (_numRequests == 1)
loadingChanged();
}
void decNumRequests() {
QMutexLocker locker(&_mutex);
_numRequests--;
//qDebug() << "numReq is now " << _numRequests;
// BUGFIX there are still scenarios where there is an error signal and a finished signal so this gets called twice :(
// therefor try to limit scenarios where this would be decremented one too many
if (_numRequests < 0)
_numRequests = 0;
if (_numRequests == 0)
loadingChanged();
}
};
class MyNetworkAccessManager : public QNetworkAccessManager {
Q_OBJECT
public:
MyNetworkAccessManager(QObject *parent = 0) : QNetworkAccessManager(parent) { }
private slots:
void onError(QNetworkReply::NetworkError e);
signals:
void started();
void error();
protected:
QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData = 0);
};
#endif // MYNETWORKMANAGER_HH