Changeset View
Changeset View
Standalone View
Standalone View
dataengines/geolocation/location_ip.cpp
Show All 14 Lines | |||||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. | ||
17 | * | 17 | * | ||
18 | * You should have received a copy of the GNU General Public License | 18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "location_ip.h" | 22 | #include "location_ip.h" | ||
23 | #include <QDebug> | 23 | #include "geolocdebug.h" | ||
24 | #include <QJsonArray> | 24 | #include <QJsonArray> | ||
25 | #include <QJsonDocument> | 25 | #include <QJsonDocument> | ||
26 | #include <QJsonObject> | 26 | #include <QJsonObject> | ||
27 | #include <QUrl> | 27 | #include <QUrl> | ||
28 | #include <KJob> | 28 | #include <KJob> | ||
29 | #include <KIO/Job> | 29 | #include <KIO/Job> | ||
30 | #include <KIO/TransferJob> | 30 | #include <KIO/TransferJob> | ||
31 | #include <KSharedConfig> | 31 | #include <KSharedConfig> | ||
32 | #include <NetworkManagerQt/Manager> | 32 | #include <NetworkManagerQt/Manager> | ||
33 | #include <NetworkManagerQt/WirelessDevice> | 33 | #include <NetworkManagerQt/WirelessDevice> | ||
34 | 34 | | |||
35 | class Ip::Private : public QObject { | 35 | class Ip::Private : public QObject { | ||
36 | Q_OBJECT | 36 | Q_OBJECT | ||
37 | public: | 37 | public: | ||
38 | Private(Ip *q) : q(q) {} | 38 | Private(Ip *q) : q(q) {} | ||
39 | 39 | | |||
40 | void readGeoLocation(KJob *job) | 40 | void readGeoLocation(KJob *job) | ||
41 | { | 41 | { | ||
42 | m_geoLocationResolved = true; | 42 | m_geoLocationResolved = true; | ||
43 | if (job && job->error()) { | 43 | if (job && job->error()) { | ||
44 | qDebug() << "error" << job->errorString(); | 44 | qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString(); | ||
45 | m_geoLocationPayload.clear(); | 45 | m_geoLocationPayload.clear(); | ||
46 | checkUpdateData(); | 46 | checkUpdateData(); | ||
47 | return; | 47 | return; | ||
48 | } | 48 | } | ||
49 | const QJsonObject json = QJsonDocument::fromJson(m_geoLocationPayload).object(); | 49 | const QJsonObject json = QJsonDocument::fromJson(m_geoLocationPayload).object(); | ||
50 | m_geoLocationPayload.clear(); | 50 | m_geoLocationPayload.clear(); | ||
51 | 51 | | |||
52 | auto accuracyIt = json.find(QStringLiteral("accuracy")); | 52 | auto accuracyIt = json.find(QStringLiteral("accuracy")); | ||
Show All 36 Lines | 88 | if (data.isEmpty()) { | |||
89 | return; | 89 | return; | ||
90 | } | 90 | } | ||
91 | m_countryPayload.append(data); | 91 | m_countryPayload.append(data); | ||
92 | } | 92 | } | ||
93 | 93 | | |||
94 | void readCountry(KJob *job) { | 94 | void readCountry(KJob *job) { | ||
95 | m_countryResolved = true; | 95 | m_countryResolved = true; | ||
96 | if (job && job->error()) { | 96 | if (job && job->error()) { | ||
97 | qDebug() << "error" << job->errorString(); | 97 | qCCritical(DATAENGINE_GEOLOCATION) << "error: " << job->errorString(); | ||
98 | m_countryPayload.clear(); | 98 | m_countryPayload.clear(); | ||
99 | checkUpdateData(); | 99 | checkUpdateData(); | ||
100 | return; | 100 | return; | ||
101 | } | 101 | } | ||
102 | 102 | | |||
103 | const QJsonObject json = QJsonDocument::fromJson(m_countryPayload).object(); | 103 | const QJsonObject json = QJsonDocument::fromJson(m_countryPayload).object(); | ||
104 | m_countryPayload.clear(); | 104 | m_countryPayload.clear(); | ||
105 | 105 | | |||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Line(s) | 165 | { | |||
175 | } | 175 | } | ||
176 | const QByteArray postData = QJsonDocument(request).toJson(QJsonDocument::Compact); | 176 | const QByteArray postData = QJsonDocument(request).toJson(QJsonDocument::Compact); | ||
177 | const QString apiKey = QStringLiteral("60e8eae6-3988-4ada-ad48-2cfddddf216b"); | 177 | const QString apiKey = QStringLiteral("60e8eae6-3988-4ada-ad48-2cfddddf216b"); | ||
178 | KIO::TransferJob *datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)), | 178 | KIO::TransferJob *datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/geolocate?key=%1").arg(apiKey)), | ||
179 | postData, | 179 | postData, | ||
180 | KIO::HideProgressInfo); | 180 | KIO::HideProgressInfo); | ||
181 | datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); | 181 | datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); | ||
182 | 182 | | |||
183 | qDebug() << "Fetching https://location.services.mozilla.com/v1/geolocate"; | 183 | qCDebug(DATAENGINE_GEOLOCATION) << "Fetching https://location.services.mozilla.com/v1/geolocate"; | ||
184 | connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::geoLocationData); | 184 | connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::geoLocationData); | ||
185 | connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readGeoLocation); | 185 | connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readGeoLocation); | ||
186 | 186 | | |||
187 | datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/country?key=%1").arg(apiKey)), | 187 | datajob = KIO::http_post(QUrl(QStringLiteral("https://location.services.mozilla.com/v1/country?key=%1").arg(apiKey)), | ||
188 | postData, | 188 | postData, | ||
189 | KIO::HideProgressInfo); | 189 | KIO::HideProgressInfo); | ||
190 | datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); | 190 | datajob->addMetaData(QStringLiteral("content-type"), QStringLiteral("application/json")); | ||
191 | connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::countryData); | 191 | connect(datajob, &KIO::TransferJob::data, d, &Ip::Private::countryData); | ||
192 | connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readCountry); | 192 | connect(datajob, &KIO::TransferJob::result, d, &Ip::Private::readCountry); | ||
193 | } | 193 | } | ||
194 | 194 | | |||
195 | K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(ip, Ip) | 195 | K_EXPORT_PLASMA_GEOLOCATIONPROVIDER(ip, Ip) | ||
196 | 196 | | |||
197 | #include "location_ip.moc" | 197 | #include "location_ip.moc" |