KWeatherCore

alertmanager.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Han Young <hanyoung@protonmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "alertmanager.h"
7#include "pendingalerts.h"
8#include <QDirIterator>
9#include <QFile>
10#include <QHash>
11#include <QJsonDocument>
12#include <QJsonObject>
13#include <QNetworkAccessManager>
14#include <QNetworkRequest>
15#include <QStandardPaths>
16#include <QUrl>
17namespace KWeatherCore
18{
19class AlertManager::AlertManagerPrivate
20{
21public:
22 AlertManagerPrivate();
23 AlertManagerPrivate(const AlertManagerPrivate &other);
24 ~AlertManagerPrivate();
25 AlertManagerPrivate &operator=(const AlertManagerPrivate &other);
26 QNetworkAccessManager *manager = nullptr;
28};
29AlertManager::AlertManagerPrivate::AlertManagerPrivate()
30 : manager(new QNetworkAccessManager())
31{
32}
33AlertManager::AlertManagerPrivate::~AlertManagerPrivate()
34{
35 if (manager) {
36 manager->deleteLater();
37 }
38}
39AlertManager::AlertManagerPrivate::AlertManagerPrivate(const AlertManagerPrivate &other)
40 : manager(new QNetworkAccessManager())
41{
42 hash = other.hash;
43}
44AlertManager::AlertManagerPrivate &AlertManager::AlertManagerPrivate::operator=(const AlertManagerPrivate &other)
45{
46 hash = other.hash;
47 return *this;
48}
49AlertManager::~AlertManager() = default;
50AlertManager::AlertManager(const AlertManager &other)
51 : d(std::make_unique<AlertManagerPrivate>(*other.d))
52{
53}
54AlertManager &AlertManager::operator=(const AlertManager &other)
55{
56 *d = *other.d;
57 return *this;
58}
59AlertManager &AlertManager::operator=(AlertManager &&other) = default;
60AlertManager *AlertManager::inst()
61{
62 static AlertManager singleton;
63 return &singleton;
64}
65AlertManager::AlertManager()
66 : d(std::make_unique<AlertManagerPrivate>())
67{
68}
69void AlertManager::loadConfigs()
70{
72 QDir dir(config + QStringLiteral("/kweathercore"));
73 if (dir.exists()) {
74 QDirIterator it(config + QStringLiteral("kweathercore"));
75 while (it.hasNext()) {
76 QFile file(it.next());
77 if ((it.fileName()).right(4) == QStringLiteral("json")) {
79 const auto config = QJsonDocument::fromJson(file.readAll()).object();
80 const QJsonValue key = config.value(QLatin1String("country"));
81 d->hash[key.toString()] = std::make_pair(it.filePath(), config[QLatin1String("url")].toString());
82 }
83 }
84 }
85}
87{
88 return d->hash.keys();
89}
91{
92 QFile file(d->hash.value(country).first);
94 QByteArray val = file.readAll();
96 QUrl url(((d->hash.value(country)).second));
97 auto reply = d->manager->get(QNetworkRequest(url));
98 return new PendingAlerts(config, reply);
99}
100}
The AlertManager class is intened to get pending weather alerts about a location.
PendingAlerts * getAlerts(const QString &country) const
getAlerts
QList< QString > availableCountries() const
availableCountries
The PendingAlerts class contains the reply to an asynchronous CAP feed request.
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
QByteArray readAll()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QString writableLocation(StandardLocation type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 12:02:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.