| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WeatherManager.h" |
| 10 |
|
| 11 |
#include "LangInfo.h" |
| 12 |
#include "ServiceBroker.h" |
| 13 |
#include "WeatherJob.h" |
| 14 |
#include "addons/AddonManager.h" |
| 15 |
#include "addons/addoninfo/AddonType.h" |
| 16 |
#include "addons/gui/GUIDialogAddonSettings.h" |
| 17 |
#include "guilib/GUIComponent.h" |
| 18 |
#include "guilib/GUIWindowManager.h" |
| 19 |
#include "guilib/WindowIDs.h" |
| 20 |
#include "settings/Settings.h" |
| 21 |
#include "settings/SettingsComponent.h" |
| 22 |
#include "settings/lib/Setting.h" |
| 23 |
#include "settings/lib/SettingsManager.h" |
| 24 |
#include "utils/StringUtils.h" |
| 25 |
#include "utils/URIUtils.h" |
| 26 |
#include "utils/Variant.h" |
| 27 |
#include "utils/XMLUtils.h" |
| 28 |
|
| 29 |
using namespace ADDON; |
| 30 |
|
| 31 |
CWeatherManager::CWeatherManager(void) : CInfoLoader(30 * 60 * 1000) // 30 minutes |
| 32 |
{ |
| 33 |
CServiceBroker::GetSettingsComponent()->GetSettings()->GetSettingsManager()->RegisterCallback(this, { |
| 34 |
CSettings::SETTING_WEATHER_ADDON, |
| 35 |
CSettings::SETTING_WEATHER_ADDONSETTINGS |
| 36 |
}); |
| 37 |
|
| 38 |
Reset(); |
| 39 |
} |
| 40 |
|
| 41 |
CWeatherManager::~CWeatherManager(void) |
| 42 |
{ |
| 43 |
const auto settingsComponent = CServiceBroker::GetSettingsComponent(); |
| 44 |
if (!settingsComponent) |
| 45 |
return; |
| 46 |
|
| 47 |
const std::shared_ptr<CSettings> settings = settingsComponent->GetSettings(); |
| 48 |
if (!settings) |
| 49 |
return; |
| 50 |
|
| 51 |
settings->GetSettingsManager()->UnregisterCallback(this); |
| 52 |
} |
| 53 |
|
| 54 |
std::string CWeatherManager::BusyInfo(int info) const |
| 55 |
{ |
| 56 |
if (info == WEATHER_IMAGE_CURRENT_ICON) |
| 57 |
return URIUtils::AddFileToFolder(ICON_ADDON_PATH, "na.png"); |
| 58 |
|
| 59 |
return CInfoLoader::BusyInfo(info); |
| 60 |
} |
| 61 |
|
| 62 |
std::string CWeatherManager::TranslateInfo(int info) const |
| 63 |
{ |
| 64 |
switch (info) { |
| 65 |
case WEATHER_LABEL_CURRENT_COND: |
| 66 |
return m_info.currentConditions; |
| 67 |
case WEATHER_IMAGE_CURRENT_ICON: |
| 68 |
return m_info.currentIcon; |
| 69 |
case WEATHER_LABEL_CURRENT_TEMP: |
| 70 |
return m_info.currentTemperature; |
| 71 |
case WEATHER_LABEL_CURRENT_FEEL: |
| 72 |
return m_info.currentFeelsLike; |
| 73 |
case WEATHER_LABEL_CURRENT_UVID: |
| 74 |
return m_info.currentUVIndex; |
| 75 |
case WEATHER_LABEL_CURRENT_WIND: |
| 76 |
return m_info.currentWind; |
| 77 |
case WEATHER_LABEL_CURRENT_DEWP: |
| 78 |
return m_info.currentDewPoint; |
| 79 |
case WEATHER_LABEL_CURRENT_HUMI: |
| 80 |
return m_info.currentHumidity; |
| 81 |
case WEATHER_LABEL_LOCATION: |
| 82 |
return m_info.location; |
| 83 |
default: |
| 84 |
return ""; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/*! |
| 89 |
\brief Retrieve the city name for the specified location from the settings |
| 90 |
\param iLocation the location index (can be in the range [1..MAXLOCATION]) |
| 91 |
\return the city name (without the accompanying region area code) |
| 92 |
*/ |
| 93 |
std::string CWeatherManager::GetLocation(int iLocation) |
| 94 |
{ |
| 95 |
CGUIWindow* window = CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_WEATHER); |
| 96 |
if (window) |
| 97 |
{ |
| 98 |
std::string setting = StringUtils::Format("Location{}", iLocation); |
| 99 |
return window->GetProperty(setting).asString(); |
| 100 |
} |
| 101 |
return ""; |
| 102 |
} |
| 103 |
|
| 104 |
void CWeatherManager::Reset() |
| 105 |
{ |
| 106 |
m_info.Reset(); |
| 107 |
} |
| 108 |
|
| 109 |
bool CWeatherManager::IsFetched() |
| 110 |
{ |
| 111 |
// call GetInfo() to make sure that we actually start up |
| 112 |
GetInfo(0); |
| 113 |
return !m_info.lastUpdateTime.empty(); |
| 114 |
} |
| 115 |
|
| 116 |
const ForecastDay &CWeatherManager::GetForecast(int day) const |
| 117 |
{ |
| 118 |
return m_info.forecast[day]; |
| 119 |
} |
| 120 |
|
| 121 |
/*! |
| 122 |
\brief Saves the specified location index to the settings. Call Refresh() |
| 123 |
afterwards to update weather info for the new location. |
| 124 |
\param iLocation the new location index (can be in the range [1..MAXLOCATION]) |
| 125 |
*/ |
| 126 |
void CWeatherManager::SetArea(int iLocation) |
| 127 |
{ |
| 128 |
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 129 |
settings->SetInt(CSettings::SETTING_WEATHER_CURRENTLOCATION, iLocation); |
| 130 |
settings->Save(); |
| 131 |
} |
| 132 |
|
| 133 |
/*! |
| 134 |
\brief Retrieves the current location index from the settings |
| 135 |
\return the active location index (will be in the range [1..MAXLOCATION]) |
| 136 |
*/ |
| 137 |
int CWeatherManager::GetArea() const |
| 138 |
{ |
| 139 |
return CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_WEATHER_CURRENTLOCATION); |
| 140 |
} |
| 141 |
|
| 142 |
CJob *CWeatherManager::GetJob() const |
| 143 |
{ |
| 144 |
return new CWeatherJob(GetArea()); |
| 145 |
} |
| 146 |
|
| 147 |
void CWeatherManager::OnJobComplete(unsigned int jobID, bool success, CJob *job) |
| 148 |
{ |
| 149 |
m_info = static_cast<CWeatherJob*>(job)->GetInfo(); |
| 150 |
CInfoLoader::OnJobComplete(jobID, success, job); |
| 151 |
} |
| 152 |
|
| 153 |
void CWeatherManager::OnSettingChanged(const std::shared_ptr<const CSetting>& setting) |
| 154 |
{ |
| 155 |
if (setting == NULL) |
| 156 |
return; |
| 157 |
|
| 158 |
const std::string settingId = setting->GetId(); |
| 159 |
if (settingId == CSettings::SETTING_WEATHER_ADDON) |
| 160 |
{ |
| 161 |
// clear "WeatherProviderLogo" property that some weather addons set |
| 162 |
CGUIWindow* window = CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_WEATHER); |
| 163 |
if (window != nullptr) |
| 164 |
window->SetProperty("WeatherProviderLogo", ""); |
| 165 |
Refresh(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void CWeatherManager::OnSettingAction(const std::shared_ptr<const CSetting>& setting) |
| 170 |
{ |
| 171 |
if (setting == NULL) |
| 172 |
return; |
| 173 |
|
| 174 |
const std::string settingId = setting->GetId(); |
| 175 |
if (settingId == CSettings::SETTING_WEATHER_ADDONSETTINGS) |
| 176 |
{ |
| 177 |
AddonPtr addon; |
| 178 |
if (CServiceBroker::GetAddonMgr().GetAddon( |
| 179 |
CServiceBroker::GetSettingsComponent()->GetSettings()->GetString( |
| 180 |
CSettings::SETTING_WEATHER_ADDON), |
| 181 |
addon, AddonType::SCRIPT_WEATHER, OnlyEnabled::CHOICE_YES) && |
| 182 |
addon != NULL) |
| 183 |
{ //! @todo maybe have ShowAndGetInput return a bool if settings changed, then only reset weather if true. |
| 184 |
CGUIDialogAddonSettings::ShowForAddon(addon); |
| 185 |
Refresh(); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|