From d2df4ddf1621c6f22a311f2ea1dcf64024910798 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Thu, 27 May 2021 23:48:50 +0200 Subject: [PATCH] Enhance location attachment information (accuracy, address) --- qml/pages/ChatPage.qml | 19 +++++++++- src/fernschreiberutils.cpp | 42 +++++++++++++++++++++ src/fernschreiberutils.h | 5 +++ translations/harbour-fernschreiber-de.ts | 8 ++++ translations/harbour-fernschreiber-en.ts | 8 ++++ translations/harbour-fernschreiber-es.ts | 8 ++++ translations/harbour-fernschreiber-fi.ts | 8 ++++ translations/harbour-fernschreiber-hu.ts | 8 ++++ translations/harbour-fernschreiber-it.ts | 8 ++++ translations/harbour-fernschreiber-pl.ts | 8 ++++ translations/harbour-fernschreiber-ru.ts | 8 ++++ translations/harbour-fernschreiber-sk.ts | 8 ++++ translations/harbour-fernschreiber-sv.ts | 8 ++++ translations/harbour-fernschreiber-zh_CN.ts | 8 ++++ translations/harbour-fernschreiber.ts | 8 ++++ 15 files changed, 160 insertions(+), 2 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index 484fd17..a7cbdde 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -1654,15 +1654,28 @@ Page { property bool isVoiceNote: false; property bool isLocation: false; property var locationData: null; + property var geocodedAddress: qsTr("Unknown address") property var fileProperties: null; property string attachmentDescription: ""; + function getLocationDescription() { + return qsTr("Location (%1/%2)").arg(attachmentPreviewRow.locationData.latitude).arg(attachmentPreviewRow.locationData.longitude) + " | " + + qsTr("Accuracy: %1m").arg(attachmentPreviewRow.locationData.horizontalAccuracy) + "\n" + + attachmentPreviewRow.geocodedAddress; + } + Connections { target: fernschreiberUtils onNewPositionInformation: { attachmentPreviewRow.locationData = positionInformation; if (attachmentPreviewRow.isLocation) { - attachmentPreviewRow.attachmentDescription = qsTr("Location (%1/%2)").arg(attachmentPreviewRow.locationData.latitude).arg(attachmentPreviewRow.locationData.longitude); + attachmentPreviewRow.attachmentDescription = attachmentPreviewRow.getLocationDescription(); + } + } + onNewGeocodedAddress: { + attachmentPreviewRow.geocodedAddress = geocodedAddress; + if (attachmentPreviewRow.isLocation) { + attachmentPreviewRow.attachmentDescription = attachmentPreviewRow.getLocationDescription(); } } } @@ -1695,7 +1708,9 @@ Page { text: ( attachmentPreviewRow.isVoiceNote || attachmentPreviewRow.isLocation ) ? attachmentPreviewRow.attachmentDescription : ( !!attachmentPreviewRow.fileProperties ? attachmentPreviewRow.fileProperties.fileName || "" : "" ); anchors.verticalCenter: parent.verticalCenter - maximumLineCount: 1 + width: parent.width - removeAttachmentsIconButton.width - Theme.paddingMedium + maximumLineCount: 2 + wrapMode: Text.Wrap truncationMode: TruncationMode.Fade color: Theme.secondaryColor visible: attachmentPreviewRow.isDocument || attachmentPreviewRow.isVoiceNote || attachmentPreviewRow.isLocation diff --git a/src/fernschreiberutils.cpp b/src/fernschreiberutils.cpp index 0f85cd8..4f5c568 100644 --- a/src/fernschreiberutils.cpp +++ b/src/fernschreiberutils.cpp @@ -26,10 +26,13 @@ #include #include #include +#include #include #include #include #include +#include +#include #define DEBUG_MODULE FernschreiberUtils #include "debuglog.h" @@ -65,6 +68,8 @@ FernschreiberUtils::FernschreiberUtils(QObject *parent) : QObject(parent) } else { LOG("Unable to initialize geolocation!"); } + + this->manager = new QNetworkAccessManager(this); } FernschreiberUtils::~FernschreiberUtils() @@ -252,6 +257,25 @@ QString FernschreiberUtils::getSailfishOSVersion() return QSysInfo::productVersion(); } +void FernschreiberUtils::initiateReverseGeocode(double latitude, double longitude) +{ + LOG("Initiating reverse geocode:" << latitude << longitude); + QUrl url = QUrl("https://nominatim.openstreetmap.org/reverse"); + QUrlQuery urlQuery; + urlQuery.addQueryItem("lat", QString::number(latitude)); + urlQuery.addQueryItem("lon", QString::number(longitude)); + urlQuery.addQueryItem("format", "json"); + url.setQuery(urlQuery); + QNetworkRequest request(url); + request.setHeader(QNetworkRequest::UserAgentHeader, "Fernschreiber (Sailfish OS)"); + request.setRawHeader(QByteArray("Accept"), QByteArray("application/json")); + request.setRawHeader(QByteArray("Accept-Charset"), QByteArray("utf-8")); + request.setRawHeader(QByteArray("Connection"), QByteArray("close")); + request.setRawHeader(QByteArray("Cache-Control"), QByteArray("max-age=0")); + QNetworkReply *reply = manager->get(request); + connect(reply, SIGNAL(finished()), this, SLOT(handleReverseGeocodeFinished())); +} + void FernschreiberUtils::handleAudioRecorderStatusChanged(QMediaRecorder::Status status) { LOG("Audio recorder status changed:" << status); @@ -296,10 +320,28 @@ void FernschreiberUtils::handleGeoPositionUpdated(const QGeoPositionInfo &info) positionInformation.insert("latitude", geoCoordinate.latitude()); positionInformation.insert("longitude", geoCoordinate.longitude()); + this->initiateReverseGeocode(geoCoordinate.latitude(), geoCoordinate.longitude()); emit newPositionInformation(positionInformation); } +void FernschreiberUtils::handleReverseGeocodeFinished() +{ + qDebug() << "FernschreiberUtils::handleReverseGeocodeFinished"; + QNetworkReply *reply = qobject_cast(sender()); + reply->deleteLater(); + if (reply->error() != QNetworkReply::NoError) { + return; + } + + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply->readAll()); + qDebug().noquote() << jsonDocument.toJson(QJsonDocument::Indented); + if (jsonDocument.isObject()) { + QJsonObject responseObject = jsonDocument.object(); + emit newGeocodedAddress(responseObject.value("display_name").toString()); + } +} + void FernschreiberUtils::cleanUp() { if (this->geoPositionInfoSource) { diff --git a/src/fernschreiberutils.h b/src/fernschreiberutils.h index a8ee7ea..2ab92e5 100644 --- a/src/fernschreiberutils.h +++ b/src/fernschreiberutils.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "tdlibwrapper.h" class FernschreiberUtils : public QObject @@ -53,21 +54,25 @@ public: Q_INVOKABLE void stopGeoLocationUpdates(); Q_INVOKABLE bool supportsGeoLocation(); Q_INVOKABLE QString getSailfishOSVersion(); + Q_INVOKABLE void initiateReverseGeocode(double latitude, double longitude); signals: void voiceNoteDurationChanged(qlonglong duration); void voiceNoteRecordingStateChanged(VoiceNoteRecordingState state); void newPositionInformation(const QVariantMap &positionInformation); + void newGeocodedAddress(const QString &geocodedAddress); private slots: void handleAudioRecorderStatusChanged(QMediaRecorder::Status status); void handleGeoPositionUpdated(const QGeoPositionInfo &info); + void handleReverseGeocodeFinished(); private: QAudioRecorder audioRecorder; VoiceNoteRecordingState voiceNoteRecordingState; QGeoPositionInfoSource *geoPositionInfoSource; + QNetworkAccessManager *manager; void cleanUp(); QString getTemporaryDirectoryPath(); diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index 313a050..eeec32d 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -506,6 +506,14 @@ Forward Message Nachricht weiterleiten + + Unknown address + Unbekannte Adresse + + + Accuracy: %1m + Genauigkeit: %1m + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-en.ts b/translations/harbour-fernschreiber-en.ts index 69f8bd3..69be9cb 100644 --- a/translations/harbour-fernschreiber-en.ts +++ b/translations/harbour-fernschreiber-en.ts @@ -506,6 +506,14 @@ Forward Message Forward Message + + Unknown address + Unknown address + + + Accuracy: %1m + Accuracy: %1m + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-es.ts b/translations/harbour-fernschreiber-es.ts index 10e930c..1876445 100644 --- a/translations/harbour-fernschreiber-es.ts +++ b/translations/harbour-fernschreiber-es.ts @@ -506,6 +506,14 @@ Forward Message Reenviar mensaje + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-fi.ts b/translations/harbour-fernschreiber-fi.ts index 4e85048..917d847 100644 --- a/translations/harbour-fernschreiber-fi.ts +++ b/translations/harbour-fernschreiber-fi.ts @@ -506,6 +506,14 @@ Forward Message Välitä viesti + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-hu.ts b/translations/harbour-fernschreiber-hu.ts index 98171fb..ed14a78 100644 --- a/translations/harbour-fernschreiber-hu.ts +++ b/translations/harbour-fernschreiber-hu.ts @@ -496,6 +496,14 @@ Forward Message + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-it.ts b/translations/harbour-fernschreiber-it.ts index 2e090fd..5d20e91 100644 --- a/translations/harbour-fernschreiber-it.ts +++ b/translations/harbour-fernschreiber-it.ts @@ -506,6 +506,14 @@ Forward Message + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-pl.ts b/translations/harbour-fernschreiber-pl.ts index bb48731..48b01d8 100644 --- a/translations/harbour-fernschreiber-pl.ts +++ b/translations/harbour-fernschreiber-pl.ts @@ -516,6 +516,14 @@ Forward Message Przekaż widomość + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-ru.ts b/translations/harbour-fernschreiber-ru.ts index 7e88823..2bc8dc4 100644 --- a/translations/harbour-fernschreiber-ru.ts +++ b/translations/harbour-fernschreiber-ru.ts @@ -516,6 +516,14 @@ Forward Message Переслать сообщение + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-sk.ts b/translations/harbour-fernschreiber-sk.ts index 115b94b..30c6884 100644 --- a/translations/harbour-fernschreiber-sk.ts +++ b/translations/harbour-fernschreiber-sk.ts @@ -516,6 +516,14 @@ Forward Message Postúpiť správu + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-sv.ts b/translations/harbour-fernschreiber-sv.ts index cd0c924..1b99ee7 100644 --- a/translations/harbour-fernschreiber-sv.ts +++ b/translations/harbour-fernschreiber-sv.ts @@ -506,6 +506,14 @@ Forward Message Vidarebefordra meddelande + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber-zh_CN.ts b/translations/harbour-fernschreiber-zh_CN.ts index e3296e0..32d5712 100644 --- a/translations/harbour-fernschreiber-zh_CN.ts +++ b/translations/harbour-fernschreiber-zh_CN.ts @@ -496,6 +496,14 @@ Forward Message 转发消息 + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index 3bdfb9a..2701efb 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -506,6 +506,14 @@ Forward Message + + Unknown address + + + + Accuracy: %1m + + ChatSelectionPage