File indexing completed on 2024-12-22 05:30:44
0001 /* 0002 SPDX-FileCopyrightText: 2020 HanY <hanyoung@protonmail.com> 0003 SPDX-License-Identifier: LGPL-2.1-or-later 0004 */ 0005 #include "kweather_1x4.h" 0006 0007 #include <QQmlApplicationEngine> 0008 #include <QTimer> 0009 0010 #include <KSharedConfig> 0011 0012 #include "hourlymodel.h" 0013 #include "kweathersettings.h" 0014 0015 KWeather_1x4::KWeather_1x4(QObject *parent, const KPluginMetaData &md, const QVariantList &args) 0016 : Plasma::Applet(parent, md, args) 0017 , m_hourlyModel(new HourlyModel()) 0018 , m_timer(new QTimer(this)) 0019 { 0020 qmlRegisterAnonymousType<HourlyModel>("HourlyModel", 1); 0021 auto config = KSharedConfig::openConfig(QStringLiteral("kweather/plasmoid")); 0022 auto group = config->group(QStringLiteral("general")); 0023 const QString locationID = group.readEntry("locationID"); 0024 if (!locationID.isEmpty()) { 0025 auto m_config = KWeatherSettings::self()->config()->group(QStringLiteral("WeatherLocations")); 0026 auto m_group = m_config.group(locationID); 0027 m_location = m_group.readEntry("locationName"); 0028 m_latitude = m_group.readEntry("latitude").toDouble(); 0029 m_longitude = m_group.readEntry("longitude").toDouble(); 0030 m_needLocation = false; 0031 update(); 0032 m_timer->setInterval(3600 * 1000); 0033 m_timer->start(); 0034 connect(m_timer, &QTimer::timeout, this, [this] { 0035 update(); 0036 }); 0037 } 0038 } 0039 0040 void KWeather_1x4::update() 0041 { 0042 auto pendingForecast = m_source.requestData(m_latitude, m_longitude); 0043 connect(pendingForecast, &KWeatherCore::PendingWeatherForecast::finished, this, [this, pendingForecast] { 0044 m_forecast = pendingForecast->value(); 0045 pendingForecast->deleteLater(); 0046 m_hourlyModel->loadForecast(m_forecast); 0047 Q_EMIT updated(); 0048 }); 0049 } 0050 0051 QStringList KWeather_1x4::locationsInSystem() 0052 { 0053 auto m_config = KWeatherSettings::self()->config()->group(QStringLiteral("WeatherLocations")); 0054 const QStringList groups = m_config.groupList(); 0055 QStringList list; 0056 list.reserve(groups.size()); 0057 0058 std::transform(groups.begin(), groups.end(), std::back_inserter(list), [&m_config](const QString &loc) { 0059 return m_config.group(loc).readEntry("locationName"); 0060 }); 0061 return list; 0062 } 0063 void KWeather_1x4::setLocation(const QString &location) 0064 { 0065 auto m_config = KWeatherSettings::self()->config()->group(QStringLiteral("WeatherLocations")); 0066 const auto groups = m_config.groupList(); 0067 for (const auto &loc : groups) { 0068 auto m_group = m_config.group(loc); 0069 if (location == m_group.readEntry("locationName")) { 0070 m_location = location; 0071 auto config = KSharedConfig::openConfig(QStringLiteral("kweather/plasmoid")); 0072 auto group = config->group(QStringLiteral("general")); 0073 group.writeEntry("locationID", loc); 0074 m_latitude = m_group.readEntry("latitude").toDouble(); 0075 m_longitude = m_group.readEntry("longitude").toDouble(); 0076 update(); 0077 m_needLocation = false; 0078 Q_EMIT needLocationChanged(); 0079 Q_EMIT locationChanged(); 0080 0081 group.sync(); 0082 break; 0083 } 0084 } 0085 } 0086 bool KWeather_1x4::hasForecast() const 0087 { 0088 return !m_forecast.dailyWeatherForecast().empty() && !m_forecast.dailyWeatherForecast().front().hourlyWeatherForecast().empty(); 0089 } 0090 const KWeatherCore::HourlyWeatherForecast &KWeather_1x4::getFirst() const 0091 { 0092 return m_forecast.dailyWeatherForecast().front().hourlyWeatherForecast().front(); 0093 } 0094 QString KWeather_1x4::location() const 0095 { 0096 return m_location; 0097 } 0098 qreal KWeather_1x4::humidity() const 0099 { 0100 if (hasForecast()) 0101 return getFirst().humidity(); 0102 else 0103 return 0; 0104 } 0105 QString KWeather_1x4::weatherIcon() const 0106 { 0107 if (hasForecast()) 0108 return getFirst().weatherIcon(); 0109 else 0110 return QStringLiteral("unknown"); 0111 } 0112 QString KWeather_1x4::desc() const 0113 { 0114 if (hasForecast()) 0115 return getFirst().weatherDescription(); 0116 else 0117 return {}; 0118 } 0119 qreal KWeather_1x4::temp() const 0120 { 0121 if (hasForecast()) 0122 return getFirst().temperature(); 0123 else 0124 return 0; 0125 } 0126 qreal KWeather_1x4::precipitation() const 0127 { 0128 if (hasForecast()) 0129 return getFirst().precipitationAmount(); 0130 else 0131 return 0; 0132 } 0133 0134 K_PLUGIN_CLASS_WITH_JSON(KWeather_1x4, "package/metadata.json") 0135 0136 #include "kweather_1x4.moc" 0137 0138 #include "moc_kweather_1x4.cpp"