From 6df42a1fe3702217dc5342ce8baba68f4c087b77 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 20 Feb 2023 12:32:29 +0100 Subject: [PATCH] Add PrivacyPolicyHelper --- nymea-app/main.cpp | 3 + nymea-app/nymea-app.pro | 6 +- nymea-app/utils/privacypolicyhelper.cpp | 74 +++++++++++++++++++++++++ nymea-app/utils/privacypolicyhelper.h | 26 +++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 nymea-app/utils/privacypolicyhelper.cpp create mode 100644 nymea-app/utils/privacypolicyhelper.h diff --git a/nymea-app/main.cpp b/nymea-app/main.cpp index c883c0b4..bf6e87c5 100644 --- a/nymea-app/main.cpp +++ b/nymea-app/main.cpp @@ -52,6 +52,7 @@ #include "mouseobserver.h" #include "configuredhostsmodel.h" #include "../config.h" +#include "utils/privacypolicyhelper.h" #include "logging.h" @@ -185,6 +186,8 @@ int main(int argc, char *argv[]) qmlRegisterUncreatableType("Nymea", 1, 0, "DashboardSceneItem", ""); qmlRegisterUncreatableType("Nymea", 1, 0, "DashboardWebViewItem", ""); + qmlRegisterSingletonType("NymeaApp.Utils", 1, 0, "PrivacyPolicyHelper", PrivacyPolicyHelper::qmlProvider); + qmlRegisterType("Nymea", 1, 0, "MouseObserver"); qmlRegisterType("Nymea", 1, 0, "ConfiguredHostsModel"); diff --git a/nymea-app/nymea-app.pro b/nymea-app/nymea-app.pro index d9e7edd2..839a6057 100644 --- a/nymea-app/nymea-app.pro +++ b/nymea-app/nymea-app.pro @@ -41,7 +41,8 @@ HEADERS += \ stylecontroller.h \ pushnotifications.h \ platformhelper.h \ - ruletemplates/messages.h + ruletemplates/messages.h \ + utils/privacypolicyhelper.h SOURCES += main.cpp \ configuredhostsmodel.cpp \ @@ -54,7 +55,8 @@ SOURCES += main.cpp \ stylecontroller.cpp \ pushnotifications.cpp \ platformhelper.cpp \ - platformintegration/generic/screenhelper.cpp + platformintegration/generic/screenhelper.cpp \ + utils/privacypolicyhelper.cpp RESOURCES += resources.qrc \ ruletemplates.qrc \ diff --git a/nymea-app/utils/privacypolicyhelper.cpp b/nymea-app/utils/privacypolicyhelper.cpp new file mode 100644 index 00000000..64bf0373 --- /dev/null +++ b/nymea-app/utils/privacypolicyhelper.cpp @@ -0,0 +1,74 @@ +#include "privacypolicyhelper.h" + +#include +#include +#include +#include +#include +#include + +#include "platformhelper.h" + +Q_DECLARE_LOGGING_CATEGORY(dcApplication) + +PrivacyPolicyHelper::PrivacyPolicyHelper(QObject *parent) : QObject(parent) +{ + QDir dir(QString(":/privacypolicy/")); + foreach (const QString &versionString, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) { + bool ok; + int version = versionString.toInt(&ok); + if (ok && version > m_version) { + m_version = version; + } + } + if (m_version < 0) { + qCWarning(dcApplication()) << "Privacy policy directory not found. :/privacypolicy// expected."; + return; + } + + qCDebug(dcApplication()) << "Using privacy policy version" << m_version; +} + +QObject* PrivacyPolicyHelper::qmlProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + static PrivacyPolicyHelper* qmlInstance = nullptr; + if (!qmlInstance) { + qmlInstance = new PrivacyPolicyHelper(); + } + return qmlInstance; +} + +int PrivacyPolicyHelper::version() const +{ + return m_version; +} + +QString PrivacyPolicyHelper::text() const +{ + QFile f(findFile()); + if (!f.open(QFile::ReadOnly)) { + qWarning() << "Cannot open privacy policy file for reading:" << f.fileName(); + return QString(); + } + return f.readAll(); +} + +QString PrivacyPolicyHelper::findFile() const +{ + QString privacyPolicyFile = QString(":/privacypolicy/%1/privacypolicy-%2.md") + .arg(m_version); + QStringList languages = { + QLocale().name(), + QLocale().name().split('_').at(0), + "en_US" + }; + foreach (const QString &lang, languages) { + qCDebug(dcApplication) << "Trying Privacy policy at" << privacyPolicyFile.arg(lang); + if (QFile::exists(privacyPolicyFile.arg(lang))) { + return privacyPolicyFile.arg(lang); + } + } + return QString(); +} diff --git a/nymea-app/utils/privacypolicyhelper.h b/nymea-app/utils/privacypolicyhelper.h new file mode 100644 index 00000000..60f028b2 --- /dev/null +++ b/nymea-app/utils/privacypolicyhelper.h @@ -0,0 +1,26 @@ +#ifndef PRIVACYPOLICYHELPER_H +#define PRIVACYPOLICYHELPER_H + +#include +#include + +class PrivacyPolicyHelper : public QObject +{ + Q_OBJECT + Q_PROPERTY(int version READ version CONSTANT) + Q_PROPERTY(QString text READ text CONSTANT) +public: + explicit PrivacyPolicyHelper(QObject *parent = nullptr); + static QObject *qmlProvider(QQmlEngine *engine, QJSEngine *scriptEngine); + + + int version() const; + QString text() const; + +private: + QString findFile() const; + + int m_version = -1; +}; + +#endif // PRIVACYPOLICYHELPER_H