Commit 98cadd48 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

[dataengines/geolocation] Port from KIO::http_post to QNetworkAccessManager

Not only does this slightly simplify the code, it also avoids a deadlock in kded when automatic proxy detection is enabled

BUG: 449984

BUG: 457341
parent 55ea36db
Pipeline #230658 passed with stage
in 6 minutes and 15 seconds
......@@ -36,7 +36,7 @@ target_link_libraries(plasma_engine_geolocation
kcoreaddons_add_plugin(plasma-geolocation-ip SOURCES location_ip.cpp INSTALL_NAMESPACE plasma/geolocationprovider)
ecm_qt_declare_logging_category(plasma-geolocation-ip HEADER geolocdebug.h IDENTIFIER DATAENGINE_GEOLOCATION CATEGORY_NAME org.kde.plasma.dataengine.geolocation)
target_compile_definitions(plasma-geolocation-ip PRIVATE -DQT_NO_KEYWORDS)
target_link_libraries(plasma-geolocation-ip plasma-geolocation-interface KF5::KIOCore KF5::NetworkManagerQt)
target_link_libraries(plasma-geolocation-ip plasma-geolocation-interface KF5::NetworkManagerQt)
pkg_check_modules(LIBGPS libgps IMPORTED_TARGET)
......
......@@ -12,15 +12,14 @@
#include "location_ip.h"
#include "geolocdebug.h"
#include <KIO/Job>
#include <KIO/TransferJob>
#include <KJob>
#include <KSharedConfig>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/WirelessDevice>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>
class Ip::Private : public QObject
......@@ -30,19 +29,21 @@ public:
Private(Ip *q)
: q(q)
{
m_nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
m_nam.setStrictTransportSecurityEnabled(true);
m_nam.enableStrictTransportSecurityStore(true,
QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/plasmashell/hsts/"));
}
void readGeoLocation(KJob *job)
void readGeoLocation(QNetworkReply *reply)
{
m_geoLocationResolved = true;
if (job && job->error()) {
qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString();
m_geoLocationPayload.clear();
if (reply->error()) {
qCCritical(DATAENGINE_GEOLOCATION) << "error: " << reply->errorString();
checkUpdateData();
return;
}
const QJsonObject json = QJsonDocument::fromJson(m_geoLocationPayload).object();
m_geoLocationPayload.clear();
const QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
auto accuracyIt = json.find(QStringLiteral("accuracy"));
if (accuracyIt != json.end()) {
......@@ -62,52 +63,28 @@ public:
void clear()
{
m_geoLocationPayload.clear();
m_countryPayload.clear();
m_countryResolved = false;
m_geoLocationResolved = false;
m_data.clear();
}
void geoLocationData(KIO::Job *job, const QByteArray &data)
{
Q_UNUSED(job)
if (data.isEmpty()) {
return;
}
m_geoLocationPayload.append(data);
}
void countryData(KIO::Job *job, const QByteArray &data)
{
Q_UNUSED(job)
if (data.isEmpty()) {
return;
}
m_countryPayload.append(data);
}
void readCountry(KJob *job)
void readCountry(QNetworkReply *reply)
{
m_countryResolved = true;
if (job && job->error()) {
qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString();
m_countryPayload.clear();
if (reply->error()) {
qCCritical(DATAENGINE_GEOLOCATION) << "error: " << reply->errorString();
checkUpdateData();
return;
}
const QJsonObject json = QJsonDocument::fromJson(m_countryPayload).object();
m_countryPayload.clear();
const QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
m_data[QStringLiteral("country")] = json.value(QStringLiteral("country_name")).toString();
m_data[QStringLiteral("country code")] = json.value(QStringLiteral("country_code")).toString();
checkUpdateData();
}
private:
void checkUpdateData()
{
if (!m_countryResolved || !m_geoLocationResolved) {
......@@ -117,11 +94,10 @@ private:
}
Ip *q;
QByteArray m_geoLocationPayload;
QByteArray m_countryPayload;
bool m_countryResolved = false;
bool m_geoLocationResolved = false;
Plasma::DataEngine::Data m_data;
QNetworkAccessManager m_nam;
};
Ip::Ip(QObject *parent, const QVariantList &args)
......@@ -176,18 +152,26 @@ void Ip::update()
}
const QByteArray postData = QJsonDocument(request).toJson(QJsonDocument::Compact);
const QString apiKey = QStringLiteral("60e8eae6-3988-4ada-ad48-2cfddddf216b");
KIO::TransferJob *datajob =
KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)), postData, KIO::HideProgressInfo);
datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json"));
qCDebug(DATAENGINE_GEOLOCATION) << "Fetching https://location.services.mozilla.com/v1/geolocate";
connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::geoLocationData);
connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readGeoLocation);
datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/country?key=%1").arg(apiKey)), postData, KIO::HideProgressInfo);
datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json"));
connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::countryData);
connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readCountry);
QNetworkRequest locationRequest(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)));
locationRequest.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
QNetworkReply *locationReply = d->m_nam.post(locationRequest, postData);
connect(locationReply, &QNetworkReply::finished, this, [this, locationReply] {
locationReply->deleteLater();
d->readGeoLocation(locationReply);
});
qCDebug(DATAENGINE_GEOLOCATION) << "Fetching https://location.services.mozilla.com/v1/country";
QNetworkRequest countryRequest(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/country?key=%1").arg(apiKey)));
countryRequest.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
QNetworkReply *countryReply = d->m_nam.post(countryRequest, postData);
connect(countryReply, &QNetworkReply::finished, this, [this, countryReply] {
countryReply->deleteLater();
d->readCountry(countryReply);
});
}
K_PLUGIN_CLASS_WITH_JSON(Ip, "plasma-geolocation-ip.json")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment