File indexing completed on 2024-04-28 04:42:42

0001 /*
0002  * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #include "locationquery.h"
0008 #include "kweathercore_p.h"
0009 #include "locationqueryreply.h"
0010 
0011 #include <QGeoPositionInfoSource>
0012 #include <QNetworkAccessManager>
0013 #include <QStandardPaths>
0014 
0015 namespace KWeatherCore
0016 {
0017 class LocationQueryPrivate
0018 {
0019 public:
0020     LocationQueryPrivate(LocationQuery *parent);
0021     void positionUpdated(const QGeoPositionInfo &update);
0022     QNetworkAccessManager *networkAccessManager();
0023 
0024     LocationQuery *q = nullptr;
0025     QNetworkAccessManager *manager = nullptr;
0026     QGeoPositionInfoSource *locationSource = nullptr;
0027 };
0028 
0029 LocationQueryPrivate::LocationQueryPrivate(LocationQuery *parent)
0030     : q(parent)
0031     , locationSource(QGeoPositionInfoSource::createDefaultSource(q))
0032 {
0033     if (locationSource) {
0034         locationSource->stopUpdates();
0035     }
0036 }
0037 
0038 QNetworkAccessManager *LocationQueryPrivate::networkAccessManager()
0039 {
0040     if (!manager) {
0041         manager = new QNetworkAccessManager(q);
0042         manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
0043         manager->setStrictTransportSecurityEnabled(true);
0044         manager->enableStrictTransportSecurityStore(true,
0045                                                     QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)
0046                                                         + QLatin1String("/org.kde.kweathercore/hsts/"));
0047     }
0048     return manager;
0049 }
0050 
0051 LocationQuery::LocationQuery(QObject *parent)
0052     : QObject(parent)
0053     , d(new LocationQueryPrivate(this))
0054 {
0055 }
0056 
0057 LocationQuery::~LocationQuery() = default;
0058 
0059 LocationQueryReply *LocationQuery::query(const QString &name, int number)
0060 {
0061     return new LocationQueryReply(name, number, d->networkAccessManager(), this);
0062 }
0063 
0064 LocationQueryReply *LocationQuery::locate()
0065 {
0066     return new LocationQueryReply(d->locationSource, d->networkAccessManager(), this);
0067 }
0068 
0069 void LocationQuery::setNetworkAccessManager(QNetworkAccessManager *nam)
0070 {
0071     if (d->manager == nam) {
0072         return;
0073     }
0074 
0075     if (d->manager->parent() == this) {
0076         delete d->manager;
0077     }
0078     d->manager = nam;
0079 }
0080 }
0081 
0082 #include "moc_locationquery.cpp"