File indexing completed on 2024-05-05 16:49:20
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 AlertManager::AlertManagerPrivate::AlertManagerPrivate( 0039 const AlertManagerPrivate &other) 0040 : manager(new QNetworkAccessManager()) 0041 { 0042 hash = other.hash; 0043 } 0044 AlertManager::AlertManagerPrivate & 0045 AlertManager::AlertManagerPrivate::operator=(const AlertManagerPrivate &other) 0046 { 0047 hash = other.hash; 0048 return *this; 0049 } 0050 AlertManager::~AlertManager() = default; 0051 AlertManager::AlertManager(const AlertManager &other) 0052 : d(std::make_unique<AlertManagerPrivate>(*other.d)) 0053 { 0054 } 0055 AlertManager &AlertManager::operator=(const AlertManager &other) 0056 { 0057 *d = *other.d; 0058 return *this; 0059 } 0060 AlertManager &AlertManager::operator=(AlertManager &&other) = default; 0061 AlertManager *AlertManager::inst() 0062 { 0063 static AlertManager singleton; 0064 return &singleton; 0065 } 0066 AlertManager::AlertManager() 0067 : d(std::make_unique<AlertManagerPrivate>()) 0068 { 0069 } 0070 void AlertManager::loadConfigs() 0071 { 0072 QString config = 0073 QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); 0074 QDir dir(config + QStringLiteral("/kweathercore")); 0075 if (dir.exists()) { 0076 QDirIterator it(config + QStringLiteral("kweathercore")); 0077 while (it.hasNext()) { 0078 QFile file(it.next()); 0079 if ((it.fileName()).right(4) == QStringLiteral("json")) { 0080 file.open(QIODevice::ReadOnly | QIODevice::Text); 0081 auto config = QJsonDocument::fromJson(file.readAll()).object(); 0082 QJsonValue key = config.value(QStringLiteral("country")); 0083 d->hash[key.toString()] = std::make_pair( 0084 it.filePath(), config[QStringLiteral("url")].toString()); 0085 } 0086 } 0087 } 0088 } 0089 QList<QString> AlertManager::availableCountries() const 0090 { 0091 return d->hash.keys(); 0092 } 0093 PendingAlerts *AlertManager::getAlerts(const QString &country) const 0094 { 0095 QFile file(d->hash.value(country).first); 0096 file.open(QIODevice::ReadOnly); 0097 QByteArray val = file.readAll(); 0098 QJsonDocument config = QJsonDocument::fromJson(val); 0099 QUrl url(((d->hash.value(country)).second)); 0100 auto reply = d->manager->get(QNetworkRequest(url)); 0101 return new PendingAlerts(config, reply); 0102 } 0103 }