mirror of
https://github.com/clementine-player/Clementine
synced 2025-02-02 20:36:44 +01:00
Add geolocator.
This commit is contained in:
parent
ba95cc7788
commit
86cced782e
@ -158,6 +158,7 @@ set(SOURCES
|
||||
internet/digitallyimportedservicebase.cpp
|
||||
internet/digitallyimportedsettingspage.cpp
|
||||
internet/digitallyimportedurlhandler.cpp
|
||||
internet/geolocator.cpp
|
||||
internet/groovesharkradio.cpp
|
||||
internet/groovesharksearchplaylisttype.cpp
|
||||
internet/groovesharkservice.cpp
|
||||
@ -427,6 +428,7 @@ set(HEADERS
|
||||
internet/digitallyimportedclient.h
|
||||
internet/digitallyimportedservicebase.h
|
||||
internet/digitallyimportedsettingspage.h
|
||||
internet/geolocator.h
|
||||
internet/groovesharkservice.h
|
||||
internet/groovesharksettingspage.h
|
||||
internet/groovesharkurlhandler.h
|
||||
|
69
src/internet/geolocator.cpp
Normal file
69
src/internet/geolocator.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "geolocator.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <qjson/parser.h>
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include "core/closure.h"
|
||||
|
||||
const char* Geolocator::kUrl = "https://data.clementine-player.org/geolocate";
|
||||
|
||||
using std::numeric_limits;
|
||||
|
||||
Geolocator::LatLng::LatLng()
|
||||
: lat_e6_(numeric_limits<int>::min()),
|
||||
lng_e6_(numeric_limits<int>::min()) {
|
||||
}
|
||||
|
||||
Geolocator::LatLng::LatLng(int lat_e6, int lng_e6)
|
||||
: lat_e6_(lat_e6),
|
||||
lng_e6_(lng_e6) {
|
||||
}
|
||||
|
||||
bool Geolocator::LatLng::IsValid() const {
|
||||
return lat_e6_ != numeric_limits<int>::min() &&
|
||||
lng_e6_ != numeric_limits<int>::min();
|
||||
}
|
||||
|
||||
Geolocator::Geolocator(QObject* parent)
|
||||
: QObject(parent) {
|
||||
}
|
||||
|
||||
void Geolocator::Geolocate() {
|
||||
QNetworkRequest req = QNetworkRequest(QUrl(kUrl));
|
||||
QNetworkReply* reply = network_.get(req);
|
||||
NewClosure(reply, SIGNAL(finished()), this, SLOT(RequestFinished()));
|
||||
}
|
||||
|
||||
void Geolocator::RequestFinished(QNetworkReply* reply) {
|
||||
reply->deleteLater();
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) != 200) {
|
||||
emit Finished(LatLng());
|
||||
return;
|
||||
}
|
||||
|
||||
QJson::Parser parser;
|
||||
bool ok = false;
|
||||
QVariant result = parser.parse(reply, &ok);
|
||||
if (!ok) {
|
||||
emit Finished(LatLng());
|
||||
return;
|
||||
}
|
||||
|
||||
QVariantMap map = result.toMap();
|
||||
QString latlng = map["latlng"].toString();
|
||||
QStringList split = latlng.split(",");
|
||||
if (split.length() != 2) {
|
||||
emit Finished(LatLng());
|
||||
return;
|
||||
}
|
||||
|
||||
double lat = split[0].toDouble();
|
||||
double lng = split[1].toDouble();
|
||||
|
||||
emit Finished(
|
||||
LatLng(static_cast<int>(lat * 1e6),
|
||||
static_cast<int>(lng * 1e6)));
|
||||
}
|
42
src/internet/geolocator.h
Normal file
42
src/internet/geolocator.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef GEOLOCATOR_H
|
||||
#define GEOLOCATOR_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "core/network.h"
|
||||
|
||||
class Geolocator : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Geolocator(QObject* parent = 0);
|
||||
|
||||
void Geolocate();
|
||||
|
||||
class LatLng {
|
||||
public:
|
||||
LatLng();
|
||||
LatLng(int lat_e6, int lng_e6);
|
||||
|
||||
int lat_e6() const { return lat_e6_; }
|
||||
int lng_e6() const { return lng_e6_; }
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
private:
|
||||
const int lat_e6_;
|
||||
const int lng_e6_;
|
||||
};
|
||||
|
||||
signals:
|
||||
void Finished(LatLng latlng);
|
||||
|
||||
private slots:
|
||||
void RequestFinished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
NetworkAccessManager network_;
|
||||
|
||||
static const char* kUrl;
|
||||
};
|
||||
|
||||
#endif // GEOLOCATOR_H
|
Loading…
x
Reference in New Issue
Block a user