KWeatherCore

alertmanager.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Han Young <[email protected]>
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>
17 namespace KWeatherCore
18 {
19 class AlertManager::AlertManagerPrivate
20 {
21 public:
22  AlertManagerPrivate();
23  AlertManagerPrivate(const AlertManagerPrivate &other);
24  ~AlertManagerPrivate();
25  AlertManagerPrivate &operator=(const AlertManagerPrivate &other);
26  QNetworkAccessManager *manager = nullptr;
28 };
29 AlertManager::AlertManagerPrivate::AlertManagerPrivate()
30  : manager(new QNetworkAccessManager())
31 {
32 }
33 AlertManager::AlertManagerPrivate::~AlertManagerPrivate()
34 {
35  if (manager) {
36  manager->deleteLater();
37  }
38 }
39 AlertManager::AlertManagerPrivate::AlertManagerPrivate(const AlertManagerPrivate &other)
40  : manager(new QNetworkAccessManager())
41 {
42  hash = other.hash;
43 }
44 AlertManager::AlertManagerPrivate &AlertManager::AlertManagerPrivate::operator=(const AlertManagerPrivate &other)
45 {
46  hash = other.hash;
47  return *this;
48 }
49 AlertManager::~AlertManager() = default;
50 AlertManager::AlertManager(const AlertManager &other)
51  : d(std::make_unique<AlertManagerPrivate>(*other.d))
52 {
53 }
54 AlertManager &AlertManager::operator=(const AlertManager &other)
55 {
56  *d = *other.d;
57  return *this;
58 }
59 AlertManager &AlertManager::operator=(AlertManager &&other) = default;
61 {
62  static AlertManager singleton;
63  return &singleton;
64 }
65 AlertManager::AlertManager()
66  : d(std::make_unique<AlertManagerPrivate>())
67 {
68 }
69 void 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  auto config = QJsonDocument::fromJson(file.readAll()).object();
80  QJsonValue key = config.value(QStringLiteral("country"));
81  d->hash[key.toString()] = std::make_pair(it.filePath(), config[QStringLiteral("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 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QString next()
QString writableLocation(QStandardPaths::StandardLocation type)
static AlertManager * inst()
inst return a pointer to the global instance
QList< QString > availableCountries() const
availableCountries
QString fileName() const const
bool exists() const const
QString toString() const const
virtual bool open(QIODevice::OpenMode mode) override
PendingAlerts * getAlerts(const QString &country) const
getAlerts
The AlertManager class is intened to get pending weather alerts about a location. ...
Definition: alertmanager.h:23
bool hasNext() const const
The PendingAlerts class contains the reply to an asynchronous CAP feed request.
Definition: pendingalerts.h:23
QString filePath() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.