File indexing completed on 2024-05-12 12:43:31
0001 /* 0002 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com> 0003 * SPDX-FileCopyrightText: 2020 Devin Lin <espidev@gmail.com> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 #include "weatherforecast.h" 0008 #include <QJsonArray> 0009 namespace KWeatherCore 0010 { 0011 class WeatherForecast::WeatherForecastPrivate : public QSharedData 0012 { 0013 public: 0014 std::vector<DailyWeatherForecast> dailyWeatherForecast; 0015 QString timezone; 0016 double latitude, longitude; 0017 QDateTime createdTime = QDateTime::currentDateTime(); 0018 }; 0019 0020 WeatherForecast::WeatherForecast() 0021 : d(new WeatherForecastPrivate) 0022 { 0023 } 0024 0025 WeatherForecast::WeatherForecast(const WeatherForecast &other) 0026 : d(other.d) 0027 { 0028 } 0029 0030 WeatherForecast &WeatherForecast::operator=(const WeatherForecast &other) 0031 { 0032 if (this != &other) { 0033 d = other.d; 0034 } 0035 0036 return *this; 0037 } 0038 0039 WeatherForecast::~WeatherForecast() 0040 { 0041 } 0042 0043 QJsonObject WeatherForecast::toJson() const 0044 { 0045 QJsonObject obj; 0046 QJsonArray dayArray; 0047 for (auto d : dailyWeatherForecast()) { 0048 dayArray.append(d.toJson()); 0049 } 0050 obj[QLatin1String("day")] = dayArray; 0051 obj[QLatin1String("lat")] = latitude(); 0052 obj[QLatin1String("lon")] = longitude(); 0053 obj[QLatin1String("timezone")] = timezone(); 0054 obj[QLatin1String("createdTime")] = createdTime().toString(Qt::ISODate); 0055 return obj; 0056 } 0057 0058 WeatherForecast WeatherForecast::fromJson(const QJsonObject &obj) 0059 { 0060 WeatherForecast w; 0061 std::vector<DailyWeatherForecast> dayVec; 0062 const auto &array = obj[QLatin1String("day")].toArray(); 0063 for (const auto &d : array) { 0064 dayVec.push_back(DailyWeatherForecast::fromJson(d.toObject())); 0065 } 0066 w.setDailyWeatherForecast(dayVec); 0067 w.setCoordinate(obj[QLatin1String("lat")].toDouble(), obj[QLatin1String("lon")].toDouble()); 0068 w.setTimezone(obj[QLatin1String("timezone")].toString()); 0069 w.setCreatedTime(QDateTime::fromString(obj[QLatin1String("createdTime")].toString(), Qt::ISODate)); 0070 return w; 0071 } 0072 const std::vector<DailyWeatherForecast> &WeatherForecast::dailyWeatherForecast() const 0073 { 0074 return d->dailyWeatherForecast; 0075 } 0076 double WeatherForecast::latitude() const 0077 { 0078 return d->latitude; 0079 } 0080 double WeatherForecast::longitude() const 0081 { 0082 return d->longitude; 0083 } 0084 const QDateTime &WeatherForecast::createdTime() const 0085 { 0086 return d->createdTime; 0087 } 0088 const QString &WeatherForecast::timezone() const 0089 { 0090 return d->timezone; 0091 } 0092 void WeatherForecast::setCoordinate(double latitude, double longitude) 0093 { 0094 d->latitude = latitude; 0095 d->longitude = longitude; 0096 } 0097 void WeatherForecast::setTimezone(QString timezone) 0098 { 0099 d->timezone = std::move(timezone); 0100 } 0101 void WeatherForecast::setDailyWeatherForecast(const std::vector<DailyWeatherForecast> &forecast) 0102 { 0103 d->dailyWeatherForecast = forecast; 0104 } 0105 void WeatherForecast::setDailyWeatherForecast(std::vector<DailyWeatherForecast> &&forecast) 0106 { 0107 d->dailyWeatherForecast = std::move(forecast); 0108 } 0109 WeatherForecast &WeatherForecast::operator+=(HourlyWeatherForecast &&forecast) 0110 { 0111 for (int i = dailyWeatherForecast().size() - 1; i >= 0; --i) { 0112 if (dailyWeatherForecast().at(i).date() == forecast.date().date()) { 0113 d->dailyWeatherForecast[i] += std::move(forecast); 0114 return *this; 0115 } 0116 } 0117 0118 // if not find, append it at end 0119 auto newDay = DailyWeatherForecast(); 0120 newDay += forecast; 0121 d->dailyWeatherForecast.push_back(std::move(newDay)); 0122 return *this; 0123 } 0124 void WeatherForecast::setCreatedTime(const QDateTime &date) 0125 { 0126 d->createdTime = date; 0127 } 0128 }