forked from Duckbox-Developers/neutrino-mp-ddt-saved
-
Notifications
You must be signed in to change notification settings - Fork 2
/
weather_setup.cpp
220 lines (174 loc) · 6.28 KB
/
weather_setup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright (C) 2020 TangoCash
License: GPLv2
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "weather_setup.h"
#include <global.h>
#include <neutrino.h>
#include <mymenu.h>
#include <gui/widget/icons.h>
#include <gui/widget/msgbox.h>
#include <gui/widget/stringinput.h>
#include <gui/widget/keyboard_input.h>
#include <gui/weather.h>
#include <driver/screen_max.h>
#include <system/debug.h>
CWeatherSetup::CWeatherSetup()
{
width = 40;
selected = -1;
locations.clear();
loadLocations(CONFIGDIR "/weather-favorites.xml");
loadLocations(WEATHERDIR "/weather-locations.xml");
}
CWeatherSetup::~CWeatherSetup()
{
}
int CWeatherSetup::exec(CMenuTarget *parent, const std::string &actionKey)
{
dprintf(DEBUG_DEBUG, "init weather setup menu\n");
int res = menu_return::RETURN_REPAINT;
if (parent)
parent->hide();
if (actionKey == "select_location")
{
return showSelectWeatherLocation();
}
else if (actionKey == "find_location")
{
return findLocation();
}
res = showWeatherSetup();
return res;
}
int CWeatherSetup::showWeatherSetup()
{
CMenuWidget *ms_oservices = new CMenuWidget(LOCALE_MISCSETTINGS_HEAD, NEUTRINO_ICON_SETTINGS, width, MN_WIDGET_ID_MISCSETUP_ONLINESERVICES);
ms_oservices->addIntroItems(LOCALE_MISCSETTINGS_ONLINESERVICES);
weather_onoff = new CMenuOptionChooser(LOCALE_WEATHER_ENABLED, &g_settings.weather_enabled, OPTIONS_OFF0_ON1_OPTIONS, OPTIONS_OFF0_ON1_OPTION_COUNT, CApiKey::check_weather_api_key());
weather_onoff->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_ENABLED);
ms_oservices->addItem(weather_onoff);
#if ENABLE_WEATHER_KEY_MANAGE
changeNotify(LOCALE_WEATHER_API_KEY, NULL);
CKeyboardInput weather_api_key_input(LOCALE_WEATHER_API_KEY, &g_settings.weather_api_key, 32, this);
CMenuForwarder *mf_we = new CMenuForwarder(LOCALE_WEATHER_API_KEY, true, weather_api_key_short, &weather_api_key_input);
mf_we->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_API_KEY);
ms_oservices->addItem(mf_we);
#endif
CMenuForwarder *mf_wl = new CMenuForwarder(LOCALE_WEATHER_LOCATION, g_settings.weather_enabled, g_settings.weather_city, this, "select_location");
mf_wl->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_LOCATION);
ms_oservices->addItem(mf_wl);
CMenuForwarder *mf_zip = new CMenuForwarder(LOCALE_WEATHER_LOCATION_POSTCODE, g_settings.weather_enabled, NULL, this, "find_location");
mf_zip->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_LOCATION_POSTCODE);
ms_oservices->addItem(mf_zip);
int res = ms_oservices->exec(NULL, "");
selected = ms_oservices->getSelected();
delete ms_oservices;
return res;
}
int CWeatherSetup::showSelectWeatherLocation()
{
int select = 0;
int res = 0;
if (locations.empty())
{
// TODO: localize hint
ShowHint("Warning", "Failed to load weather-favorites.xml or weather-locations.xml\nPlease press any key or wait some seconds! ...", 700, 10, NULL, NEUTRINO_ICON_HINT_IMAGEINFO, CComponentsHeader::CC_BTN_EXIT);
g_settings.weather_location = "52.52,13.40";
g_settings.weather_city = "Berlin";
CWeather::getInstance()->setCoords(g_settings.weather_location, g_settings.weather_city);
return menu_return::RETURN_REPAINT;
}
if (locations.size() > 0)
{
CMenuWidget *m = new CMenuWidget(LOCALE_WEATHER_LOCATION, NEUTRINO_ICON_LANGUAGE);
CMenuSelectorTarget *selector = new CMenuSelectorTarget(&select);
m->addItem(GenericMenuSeparator);
CMenuForwarder *mf;
for (size_t i = 0; i < locations.size(); i++)
{
std::string hint = locations[i].country;
hint += ": ";
hint += locations[i].coords.c_str();
mf = new CMenuForwarder(locations[i].city, true, NULL, selector, std::to_string(i).c_str());
mf->setHint(NEUTRINO_ICON_HINT_SETTINGS, hint);
m->addItem(mf);
}
m->enableSaveScreen();
res = m->exec(NULL, "");
if (!m->gotAction())
return res;
delete selector;
}
g_settings.weather_location = locations[select].coords;
g_settings.weather_city = std::string(locations[select].city);
CWeather::getInstance()->setCoords(g_settings.weather_location, g_settings.weather_city);
return res;
}
int CWeatherSetup::findLocation()
{
int ret = menu_return::RETURN_REPAINT;
int postcode = 10178;
CIntInput zipcode(LOCALE_WEATHER_LOCATION_POSTCODE, &postcode, (unsigned int)5);
ret = zipcode.exec(NULL, "");
zipcode.hide();
if (CWeather::getInstance()->FindCoords(postcode))
{
CWeather::getInstance()->setCoords(g_settings.weather_location, g_settings.weather_city);
}
return ret;
}
bool CWeatherSetup::changeNotify(const neutrino_locale_t OptionName, void */*data*/)
{
int ret = menu_return::RETURN_NONE;
if (ARE_LOCALES_EQUAL(OptionName, LOCALE_WEATHER_API_KEY))
{
g_settings.weather_enabled = g_settings.weather_enabled && CApiKey::check_weather_api_key();
if (g_settings.weather_enabled)
weather_api_key_short = g_settings.weather_api_key.substr(0, 8) + "...";
else
weather_api_key_short.clear();
weather_onoff->setActive(CApiKey::check_weather_api_key());
}
return ret;
}
void CWeatherSetup::loadLocations(std::string filename)
{
xmlDocPtr parser = parseXmlFile(filename.c_str());
if (parser == NULL)
{
dprintf(DEBUG_INFO, "failed to load %s\n", filename.c_str());
return;
}
xmlNodePtr l0 = xmlDocGetRootElement(parser);
xmlNodePtr l1 = xmlChildrenNode(l0);
if (l1)
{
while ((xmlGetNextOccurence(l1, "location")))
{
const char *country = xmlGetAttribute(l1, "country");
const char *city = xmlGetAttribute(l1, "city");
const char *latitude = xmlGetAttribute(l1, "latitude");
const char *longitude = xmlGetAttribute(l1, "longitude");
weather_loc loc;
loc.country = strdup(country);
loc.city = strdup(city);
loc.coords = std::string(latitude) + "," + std::string(longitude);
locations.push_back(loc);
l1 = xmlNextNode(l1);
}
}
xmlFreeDoc(parser);
}