forked from cnlpete/ttrss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mynetworkmanager.cpp
142 lines (117 loc) · 4.85 KB
/
mynetworkmanager.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* 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/.
*/
#include <QtNetwork/QNetworkDiskCache>
#include <QtNetwork/QSslConfiguration>
#include <QtCore/QDebug>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QtCore/QStandardPaths>
#else
#include <QtGui/QDesktopServices>
#endif
#include "mynetworkmanager.hh"
#include "settings.hh"
QScopedPointer<MyNetworkManager> MyNetworkManager::m_instance(0);
MyNetworkManager *MyNetworkManager::instance() {
if (m_instance.isNull())
m_instance.reset(new MyNetworkManager);
m_instance->_numRequests = 0;
m_instance->_gotSSLError = false;
return m_instance.data();
}
QNetworkAccessManager* MyNetworkManager::create(QObject *parent) {
QNetworkAccessManager *nam = new MyNetworkAccessManager(parent);
connect(nam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(onSslErrors(QNetworkReply*,QList<QSslError>)));
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*)));
connect(nam, SIGNAL(started()), this, SLOT(onStarted()));
connect(nam, SIGNAL(error()), this, SLOT(onError()));
QNetworkDiskCache* diskCache = new QNetworkDiskCache(parent);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#else
QString cachePath = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
#endif
diskCache->setCacheDirectory(cachePath);
diskCache->setMaximumCacheSize(5*1024*1024); // 5Mo
nam->setCache(diskCache);
return nam;
}
QNetworkReply *MyNetworkAccessManager::createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest & req, QIODevice * outgoingData) {
this->started();
QNetworkRequest request(req);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
QNetworkReply *reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
QSslConfiguration sslConfig = request.sslConfiguration();
QSsl::SslProtocol protocol = sslConfig.protocol();
Settings* settings = Settings::instance();
if (settings->isMinSSlVersionGreaterThan(protocol)) {
sslConfig.setProtocol(settings->getMinSSLVersion());
//qDebug() << "ssl protocol is now " << sslConfig.protocol();
}
request.setSslConfiguration(sslConfig);
if (settings->ignoreSSLErrors()) {
reply->ignoreSslErrors();
}
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
return reply;
}
void MyNetworkAccessManager::onError(QNetworkReply::NetworkError e) {
qDebug() << "got network error " << (int)e;
if (e < QNetworkReply::ContentAccessDenied && e != QNetworkReply::TemporaryNetworkFailureError)
this->error();
}
void MyNetworkManager::onError() {
this->decNumRequests();
}
void MyNetworkManager::onSslErrors(QNetworkReply *reply, const QList<QSslError> &errors) {
bool alreadyGotSSLError = this->gotSSLError();
this->_gotSSLError = true;
if (alreadyGotSSLError != this->_gotSSLError) {
emit this->gotSSLErrorChanged();
}
if (Settings::instance()->ignoreSSLErrors()) {
qDebug("onSslErrors");
reply->ignoreSslErrors(errors);
}
else
qDebug("not ignoring onSslErrors, since this is not specified in settings");
}
void MyNetworkManager::onStarted() {
this->incNumRequests();
}
void MyNetworkManager::onReplyFinished(QNetworkReply *reply) {
Q_UNUSED(reply);
this->decNumRequests();
}
//void MyNetworkManager::onReplyFinished(QNetworkReply *reply) {
// Q_ASSERT(reply);
// if (!reply)
// return;
// if (reply->error() == QNetworkReply::NoError) {
// return;
// }
// const int httpStatusCode = reply->attribute(
// QNetworkRequest::HttpStatusCodeAttribute).toInt();
// qDebug(QString("Network error = %1, HTTP code = %2, error description = '%3'")
// .arg(reply->error())
// .arg(httpStatusCode)
// .arg(reply->errorString())
// .toAscii());
//}