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

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Han Young <hanyoung@protonmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "alertmanager.h"
0007 #include "pendingalerts.h"
0008 #include <QDirIterator>
0009 #include <QFile>
0010 #include <QHash>
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkAccessManager>
0014 #include <QNetworkRequest>
0015 #include <QStandardPaths>
0016 #include <QUrl>
0017 namespace KWeatherCore
0018 {
0019 class AlertManager::AlertManagerPrivate
0020 {
0021 public:
0022     AlertManagerPrivate();
0023     AlertManagerPrivate(const AlertManagerPrivate &other);
0024     ~AlertManagerPrivate();
0025     AlertManagerPrivate &operator=(const AlertManagerPrivate &other);
0026     QNetworkAccessManager *manager = nullptr;
0027     QHash<QString, std::pair<QString, QString>> hash;
0028 };
0029 AlertManager::AlertManagerPrivate::AlertManagerPrivate()
0030     : manager(new QNetworkAccessManager())
0031 {
0032 }
0033 AlertManager::AlertManagerPrivate::~AlertManagerPrivate()
0034 {
0035     if (manager) {
0036         manager->deleteLater();
0037     }
0038 }
0039 AlertManager::AlertManagerPrivate::AlertManagerPrivate(const AlertManagerPrivate &other)
0040     : manager(new QNetworkAccessManager())
0041 {
0042     hash = other.hash;
0043 }
0044 AlertManager::AlertManagerPrivate &AlertManager::AlertManagerPrivate::operator=(const AlertManagerPrivate &other)
0045 {
0046     hash = other.hash;
0047     return *this;
0048 }
0049 AlertManager::~AlertManager() = default;
0050 AlertManager::AlertManager(const AlertManager &other)
0051     : d(std::make_unique<AlertManagerPrivate>(*other.d))
0052 {
0053 }
0054 AlertManager &AlertManager::operator=(const AlertManager &other)
0055 {
0056     *d = *other.d;
0057     return *this;
0058 }
0059 AlertManager &AlertManager::operator=(AlertManager &&other) = default;
0060 AlertManager *AlertManager::inst()
0061 {
0062     static AlertManager singleton;
0063     return &singleton;
0064 }
0065 AlertManager::AlertManager()
0066     : d(std::make_unique<AlertManagerPrivate>())
0067 {
0068 }
0069 void AlertManager::loadConfigs()
0070 {
0071     QString config = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
0072     QDir dir(config + QStringLiteral("/kweathercore"));
0073     if (dir.exists()) {
0074         QDirIterator it(config + QStringLiteral("kweathercore"));
0075         while (it.hasNext()) {
0076             QFile file(it.next());
0077             if ((it.fileName()).right(4) == QStringLiteral("json")) {
0078                 file.open(QIODevice::ReadOnly | QIODevice::Text);
0079                 const auto config = QJsonDocument::fromJson(file.readAll()).object();
0080                 const QJsonValue key = config.value(QLatin1String("country"));
0081                 d->hash[key.toString()] = std::make_pair(it.filePath(), config[QLatin1String("url")].toString());
0082             }
0083         }
0084     }
0085 }
0086 QList<QString> AlertManager::availableCountries() const
0087 {
0088     return d->hash.keys();
0089 }
0090 PendingAlerts *AlertManager::getAlerts(const QString &country) const
0091 {
0092     QFile file(d->hash.value(country).first);
0093     file.open(QIODevice::ReadOnly);
0094     QByteArray val = file.readAll();
0095     QJsonDocument config = QJsonDocument::fromJson(val);
0096     QUrl url(((d->hash.value(country)).second));
0097     auto reply = d->manager->get(QNetworkRequest(url));
0098     return new PendingAlerts(config, reply);
0099 }
0100 }