Debian Codegraph @

xbmc/weather/GUIWindowWeather.cpp

raw
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 "GUIWindowWeather.h"
10
11 #include "GUIUserMessages.h"
12 #include "LangInfo.h"
13 #include "ServiceBroker.h"
14 #include "guilib/WindowIDs.h"
15 #include "utils/StringUtils.h"
16 #include "utils/URIUtils.h"
17 #include "utils/Variant.h"
18 #include "weather/WeatherManager.h"
19
20 #include <utility>
21
22 using namespace ADDON;
23
24 #define CONTROL_BTNREFRESH 2
25 #define CONTROL_SELECTLOCATION 3
26 #define CONTROL_LABELUPDATED 11
27
28 #define CONTROL_STATICTEMP 223
29 #define CONTROL_STATICFEEL 224
30 #define CONTROL_STATICUVID 225
31 #define CONTROL_STATICWIND 226
32 #define CONTROL_STATICDEWP 227
33 #define CONTROL_STATICHUMI 228
34
35 #define CONTROL_LABELD0DAY 31
36 #define CONTROL_LABELD0HI 32
37 #define CONTROL_LABELD0LOW 33
38 #define CONTROL_LABELD0GEN 34
39 #define CONTROL_IMAGED0IMG 35
40
41 #define LOCALIZED_TOKEN_FIRSTID 370
42 #define LOCALIZED_TOKEN_LASTID 395
43
44 /*
45 FIXME'S
46 >strings are not centered
47 */
48
49 CGUIWindowWeather::CGUIWindowWeather(void)
50 : CGUIWindow(WINDOW_WEATHER, "MyWeather.xml")
51 {
52 m_loadType = KEEP_IN_MEMORY;
53 }
54
55 CGUIWindowWeather::~CGUIWindowWeather(void) = default;
56
57 bool CGUIWindowWeather::OnMessage(CGUIMessage& message)
58 {
59 switch ( message.GetMessage() )
60 {
61 case GUI_MSG_CLICKED:
62 {
63 int iControl = message.GetSenderId();
64 if (iControl == CONTROL_BTNREFRESH)
65 {
66 CServiceBroker::GetWeatherManager().Refresh(); // Refresh clicked so do a complete update
67 }
68 else if (iControl == CONTROL_SELECTLOCATION)
69 {
70 CGUIMessage msg(GUI_MSG_ITEM_SELECTED,GetID(),CONTROL_SELECTLOCATION);
71 OnMessage(msg);
72
73 SetLocation(msg.GetParam1());
74 }
75 }
76 break;
77 case GUI_MSG_NOTIFY_ALL:
78 if (message.GetParam1() == GUI_MSG_WINDOW_RESET)
79 {
80 CServiceBroker::GetWeatherManager().Reset();
81 return true;
82 }
83 else if (message.GetParam1() == GUI_MSG_WEATHER_FETCHED)
84 {
85 UpdateLocations();
86 SetProperties();
87 }
88 break;
89 case GUI_MSG_ITEM_SELECT:
90 {
91 if (message.GetSenderId() == 0) //handle only message from builtin
92 {
93 SetLocation(message.GetParam1());
94 return true;
95 }
96 }
97 break;
98 case GUI_MSG_MOVE_OFFSET:
99 {
100 if (message.GetSenderId() == 0 && m_maxLocation > 0) //handle only message from builtin
101 {
102 // Clamp location between 1 and m_maxLocation
103 int v = (CServiceBroker::GetWeatherManager().GetArea() + message.GetParam1() - 1) % m_maxLocation + 1;
104 if (v < 1) v += m_maxLocation;
105 SetLocation(v);
106 return true;
107 }
108 }
109 break;
110 default:
111 break;
112 }
113
114 return CGUIWindow::OnMessage(message);
115 }
116
117 void CGUIWindowWeather::OnInitWindow()
118 {
119 // call UpdateButtons() so that we start with our initial stuff already present
120 UpdateButtons();
121 UpdateLocations();
122 CGUIWindow::OnInitWindow();
123 }
124
125 void CGUIWindowWeather::UpdateLocations()
126 {
127 if (!IsActive()) return;
128 m_maxLocation = strtol(GetProperty("Locations").asString().c_str(),0,10);
129 if (m_maxLocation < 1) return;
130
131 std::vector< std::pair<std::string, int> > labels;
132
133 unsigned int iCurWeather = CServiceBroker::GetWeatherManager().GetArea();
134
135 if (iCurWeather > m_maxLocation)
136 {
137 CServiceBroker::GetWeatherManager().SetArea(m_maxLocation);
138 iCurWeather = m_maxLocation;
139 ClearProperties();
140 CServiceBroker::GetWeatherManager().Refresh();
141 }
142
143 for (unsigned int i = 1; i <= m_maxLocation; i++)
144 {
145 std::string strLabel = CServiceBroker::GetWeatherManager().GetLocation(i);
146 if (strLabel.size() > 1) //got the location string yet?
147 {
148 size_t iPos = strLabel.rfind(", ");
149 if (iPos != std::string::npos)
150 {
151 std::string strLabel2(strLabel);
152 strLabel = strLabel2.substr(0,iPos);
153 }
154 labels.emplace_back(strLabel, i);
155 }
156 else
157 {
158 strLabel = StringUtils::Format("AreaCode {}", i);
159 labels.emplace_back(strLabel, i);
160 }
161 // in case it's a button, set the label
162 if (i == iCurWeather)
163 SET_CONTROL_LABEL(CONTROL_SELECTLOCATION,strLabel);
164 }
165
166 SET_CONTROL_LABELS(CONTROL_SELECTLOCATION, iCurWeather, &labels);
167 }
168
169 void CGUIWindowWeather::UpdateButtons()
170 {
171 CONTROL_ENABLE(CONTROL_BTNREFRESH);
172
173 SET_CONTROL_LABEL(CONTROL_BTNREFRESH, 184); //Refresh
174
175 SET_CONTROL_LABEL(WEATHER_LABEL_LOCATION, CServiceBroker::GetWeatherManager().GetLocation(CServiceBroker::GetWeatherManager().GetArea()));
176 SET_CONTROL_LABEL(CONTROL_LABELUPDATED, CServiceBroker::GetWeatherManager().GetLastUpdateTime());
177
178 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_COND, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_COND));
179 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_TEMP, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_TEMP) + g_langInfo.GetTemperatureUnitString());
180 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_FEEL, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_FEEL) + g_langInfo.GetTemperatureUnitString());
181 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_UVID, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_UVID));
182 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_WIND, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_WIND));
183 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_DEWP, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_DEWP) + g_langInfo.GetTemperatureUnitString());
184 SET_CONTROL_LABEL(WEATHER_LABEL_CURRENT_HUMI, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_HUMI));
185 SET_CONTROL_FILENAME(WEATHER_IMAGE_CURRENT_ICON, CServiceBroker::GetWeatherManager().GetInfo(WEATHER_IMAGE_CURRENT_ICON));
186
187 //static labels
188 SET_CONTROL_LABEL(CONTROL_STATICTEMP, 401); //Temperature
189 SET_CONTROL_LABEL(CONTROL_STATICFEEL, 402); //Feels Like
190 SET_CONTROL_LABEL(CONTROL_STATICUVID, 403); //UV Index
191 SET_CONTROL_LABEL(CONTROL_STATICWIND, 404); //Wind
192 SET_CONTROL_LABEL(CONTROL_STATICDEWP, 405); //Dew Point
193 SET_CONTROL_LABEL(CONTROL_STATICHUMI, 406); //Humidity
194
195 for (int i = 0; i < NUM_DAYS; i++)
196 {
197 SET_CONTROL_LABEL(CONTROL_LABELD0DAY + (i*10), CServiceBroker::GetWeatherManager().GetForecast(i).m_day);
198 SET_CONTROL_LABEL(CONTROL_LABELD0HI + (i*10), CServiceBroker::GetWeatherManager().GetForecast(i).m_high + g_langInfo.GetTemperatureUnitString());
199 SET_CONTROL_LABEL(CONTROL_LABELD0LOW + (i*10), CServiceBroker::GetWeatherManager().GetForecast(i).m_low + g_langInfo.GetTemperatureUnitString());
200 SET_CONTROL_LABEL(CONTROL_LABELD0GEN + (i*10), CServiceBroker::GetWeatherManager().GetForecast(i).m_overview);
201 SET_CONTROL_FILENAME(CONTROL_IMAGED0IMG + (i*10), CServiceBroker::GetWeatherManager().GetForecast(i).m_icon);
202 }
203 }
204
205 void CGUIWindowWeather::FrameMove()
206 {
207 // update our controls
208 UpdateButtons();
209
210 CGUIWindow::FrameMove();
211 }
212
213 /*!
214 \brief Sets the location to the specified index and refreshes the weather
215 \param loc the location index (in the range [1..MAXLOCATION])
216 */
217 void CGUIWindowWeather::SetLocation(int loc)
218 {
219 if (loc < 1 || loc > (int)m_maxLocation)
220 return;
221 // Avoid a settings write if old location == new location
222 if (CServiceBroker::GetWeatherManager().GetArea() != loc)
223 {
224 ClearProperties();
225 CServiceBroker::GetWeatherManager().SetArea(loc);
226 std::string strLabel = CServiceBroker::GetWeatherManager().GetLocation(loc);
227 size_t iPos = strLabel.rfind(", ");
228 if (iPos != std::string::npos)
229 strLabel.resize(iPos);
230 SET_CONTROL_LABEL(CONTROL_SELECTLOCATION, strLabel);
231 }
232 CServiceBroker::GetWeatherManager().Refresh();
233 }
234
235 void CGUIWindowWeather::SetProperties()
236 {
237 // Current weather
238 int iCurWeather = CServiceBroker::GetWeatherManager().GetArea();
239 SetProperty("Location", CServiceBroker::GetWeatherManager().GetLocation(iCurWeather));
240 SetProperty("LocationIndex", iCurWeather);
241 SetProperty("Updated", CServiceBroker::GetWeatherManager().GetLastUpdateTime());
242 SetProperty("Current.ConditionIcon", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_IMAGE_CURRENT_ICON));
243 SetProperty("Current.Condition", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_COND));
244 SetProperty("Current.Temperature", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_TEMP));
245 SetProperty("Current.FeelsLike", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_FEEL));
246 SetProperty("Current.UVIndex", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_UVID));
247 SetProperty("Current.Wind", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_WIND));
248 SetProperty("Current.DewPoint", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_DEWP));
249 SetProperty("Current.Humidity", CServiceBroker::GetWeatherManager().GetInfo(WEATHER_LABEL_CURRENT_HUMI));
250 // we use the icons code number for fanart as it's the safest way
251 std::string fanartcode = URIUtils::GetFileName(CServiceBroker::GetWeatherManager().GetInfo(WEATHER_IMAGE_CURRENT_ICON));
252 URIUtils::RemoveExtension(fanartcode);
253 SetProperty("Current.FanartCode", fanartcode);
254
255 // Future weather
256 std::string day;
257 for (int i = 0; i < NUM_DAYS; i++)
258 {
259 day = StringUtils::Format("Day{}.", i);
260 SetProperty(day + "Title", CServiceBroker::GetWeatherManager().GetForecast(i).m_day);
261 SetProperty(day + "HighTemp", CServiceBroker::GetWeatherManager().GetForecast(i).m_high);
262 SetProperty(day + "LowTemp", CServiceBroker::GetWeatherManager().GetForecast(i).m_low);
263 SetProperty(day + "Outlook", CServiceBroker::GetWeatherManager().GetForecast(i).m_overview);
264 SetProperty(day + "OutlookIcon", CServiceBroker::GetWeatherManager().GetForecast(i).m_icon);
265 fanartcode = URIUtils::GetFileName(CServiceBroker::GetWeatherManager().GetForecast(i).m_icon);
266 URIUtils::RemoveExtension(fanartcode);
267 SetProperty(day + "FanartCode", fanartcode);
268 }
269 }
270
271 void CGUIWindowWeather::ClearProperties()
272 {
273 // Current weather
274 SetProperty("Location", "");
275 SetProperty("LocationIndex", "");
276 SetProperty("Updated", "");
277 SetProperty("Current.ConditionIcon", "");
278 SetProperty("Current.Condition", "");
279 SetProperty("Current.Temperature", "");
280 SetProperty("Current.FeelsLike", "");
281 SetProperty("Current.UVIndex", "");
282 SetProperty("Current.Wind", "");
283 SetProperty("Current.DewPoint", "");
284 SetProperty("Current.Humidity", "");
285 SetProperty("Current.FanartCode", "");
286
287 // Future weather
288 std::string day;
289 for (int i = 0; i < NUM_DAYS; i++)
290 {
291 day = StringUtils::Format("Day{}.", i);
292 SetProperty(day + "Title", "");
293 SetProperty(day + "HighTemp", "");
294 SetProperty(day + "LowTemp", "");
295 SetProperty(day + "Outlook", "");
296 SetProperty(day + "OutlookIcon", "");
297 SetProperty(day + "FanartCode", "");
298 }
299 }