[weather] Avoid some repeated table-lookups

parent 5956f5ad
......@@ -328,14 +328,14 @@ void UKMETIon::findPlace(const QString& place, const QString& source)
void UKMETIon::getFiveDayForecast(const QString& source)
{
QUrl xmlMap(m_place[source].forecastHTMLUrl);
XMLMapInfo& place = m_place[source];
QUrl xmlMap(place.forecastHTMLUrl);
const QString stationID = xmlMap.path().section(QLatin1Char('/'), -1);
m_place[source].XMLforecastURL = QStringLiteral("http://open.live.bbc.co.uk/weather/feeds/en/") + stationID + QStringLiteral("/3dayforecast.rss") + xmlMap.query();
place.XMLforecastURL = QStringLiteral("http://open.live.bbc.co.uk/weather/feeds/en/") + stationID + QStringLiteral("/3dayforecast.rss") + xmlMap.query();
const QUrl url(m_place[source].XMLforecastURL);
const QUrl url(place.XMLforecastURL);
KIO::TransferJob* getJob = KIO::get(url, KIO::Reload, KIO::HideProgressInfo);
getJob->addMetaData(QStringLiteral("cookies"), QStringLiteral("none")); // Disable displaying cookies
......@@ -372,8 +372,9 @@ void UKMETIon::readSearchHTMLData(const QString& source, const QByteArray& html)
tmp += QStringLiteral(" (#") + QString::number(counter) + QLatin1Char(')');
counter++;
}
m_place[tmp].XMLurl = url;
m_place[tmp].place = fullName;
XMLMapInfo& place = m_place[tmp];
place.XMLurl = url;
place.place = fullName;
m_locations.append(tmp);
}
}
......@@ -759,8 +760,10 @@ void UKMETIon::parseFiveDayForecast(const QString& source, QXmlStreamReader& xml
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("item"));
QVector<WeatherData::ForecastInfo*>& forecasts = m_weatherData[source].forecasts;
// Flush out the old forecasts when updating.
m_weatherData[source].forecasts.clear();
forecasts.clear();
WeatherData::ForecastInfo *forecast = new WeatherData::ForecastInfo;
QString line;
......@@ -795,7 +798,7 @@ void UKMETIon::parseFiveDayForecast(const QString& source, QXmlStreamReader& xml
const QString summaryTranslated = i18nc("weather forecast", summaryLC.toUtf8().data());
forecast->summary = (summaryTranslated != summaryLC) ? summaryTranslated : summary;
qCDebug(IONENGINE_BBCUKMET) << "i18n summary string: " << forecast->summary;
m_weatherData[source].forecasts.append(forecast);
forecasts.append(forecast);
// prepare next
forecast = new WeatherData::ForecastInfo;
}
......@@ -842,10 +845,12 @@ void UKMETIon::validate(const QString& source)
void UKMETIon::updateWeather(const QString& source)
{
const XMLMapInfo& place = m_place[source];
QString weatherSource = source;
// TODO: why the replacement here instead of just a new string?
weatherSource.replace(QStringLiteral("bbcukmet|"), QStringLiteral("bbcukmet|weather|"));
weatherSource.append(QLatin1Char('|') + m_place[source].XMLurl);
weatherSource.append(QLatin1Char('|') + place.XMLurl);
const WeatherData& weatherData = m_weatherData[source];
......@@ -957,7 +962,7 @@ void UKMETIon::updateWeather(const QString& source)
}
data.insert(QStringLiteral("Credit"), i18nc("credit line, keep string short", "Data from BBC\302\240Weather"));
data.insert(QStringLiteral("Credit Url"), m_place[source].forecastHTMLUrl);
data.insert(QStringLiteral("Credit Url"), place.forecastHTMLUrl);
setData(weatherSource, data);
}
......
......@@ -539,12 +539,13 @@ void EnvCanadaIon::getXMLData(const QString& source)
// Demunge source name for key only.
QString dataKey = source;
dataKey.remove(QStringLiteral("envcan|weather|"));
const XMLMapInfo& place = m_places[dataKey];
const QUrl url(QLatin1String("http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/") + m_places[dataKey].territoryName + QLatin1Char('/') + m_places[dataKey].cityCode + QStringLiteral("_e.xml"));
const QUrl url(QLatin1String("http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/") + place.territoryName + QLatin1Char('/') + place.cityCode + QStringLiteral("_e.xml"));
//url="file:///home/spstarr/Desktop/s0000649_e.xml";
//qCDebug(IONENGINE_ENVCAN) << "Will Try URL: " << url;
if (m_places[dataKey].territoryName.isEmpty() && m_places[dataKey].cityCode.isEmpty()) {
if (place.territoryName.isEmpty() && place.cityCode.isEmpty()) {
setData(source, QStringLiteral("validate"), QStringLiteral("envcan|malformed"));
return;
}
......
......@@ -386,9 +386,10 @@ void WetterComIon::parseSearchResults(const QString& source, QXmlStreamReader& x
qCDebug(IONENGINE_WETTERCOM) << "Storing place data for place:" << placeName;
m_place[placeName].name = placeName;
m_place[placeName].displayName = name;
m_place[placeName].placeCode = code;
PlaceInfo& place = m_place[placeName];
place.name = placeName;
place.displayName = name;
place.placeCode = code;
m_locations.append(placeName);
name.clear();
......@@ -531,8 +532,10 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
{
qCDebug(IONENGINE_WETTERCOM) << "About to parse forecast for source:" << source;
WeatherData& weatherData = m_weatherData[source];
// Clear old forecasts when updating
m_weatherData[source].forecasts.clear();
weatherData.forecasts.clear();
WeatherData::ForecastPeriod *forecastPeriod = new WeatherData::ForecastPeriod;
WeatherData::ForecastInfo *forecast = new WeatherData::ForecastInfo;
......@@ -541,7 +544,7 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
uint summaryUtcTime = 0, utcTime = 0, localTime = 0;
QString date, time;
m_weatherData[source].place = source;
weatherData.place = source;
while (!xml.atEnd()) {
xml.readNext();
......@@ -565,7 +568,7 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
weatherString);
forecastPeriod->probability = summaryProbability;
m_weatherData[source].forecasts.append(forecastPeriod);
weatherData.forecasts.append(forecastPeriod);
forecastPeriod = new WeatherData::ForecastPeriod;
date.clear();
......@@ -578,7 +581,7 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
qCDebug(IONENGINE_WETTERCOM) << "Parsed a forecast interval:" << date << time;
// yep, that field is written to more often than needed...
m_weatherData[source].timeDifference = localTime - utcTime;
weatherData.timeDifference = localTime - utcTime;
forecast->period = QDateTime::fromSecsSinceEpoch(utcTime, Qt::LocalTime);
QString weatherString = QString::number(weather);
......@@ -587,7 +590,7 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
forecast->probability = probability;
QTime localWeatherTime = QDateTime::fromSecsSinceEpoch(utcTime, Qt::LocalTime).time();
localWeatherTime = localWeatherTime.addSecs(m_weatherData[source].timeDifference);
localWeatherTime = localWeatherTime.addSecs(weatherData.timeDifference);
qCDebug(IONENGINE_WETTERCOM) << "localWeatherTime =" << localWeatherTime;
......@@ -639,8 +642,8 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
qCDebug(IONENGINE_WETTERCOM) << "parsed weather condition:" << tmp;
} else if (elementName == QLatin1String("name")) {
m_weatherData[source].stationName = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed station name:" << m_weatherData[source].stationName;
weatherData.stationName = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed station name:" << weatherData.stationName;
} else if (elementName == QLatin1String("pc")) {
int tmp = xml.readElementText().toInt();
......@@ -651,11 +654,11 @@ void WetterComIon::parseWeatherForecast(const QString& source, QXmlStreamReader&
qCDebug(IONENGINE_WETTERCOM) << "parsed probability:" << probability;
} else if (elementName == QLatin1String("text")) {
m_weatherData[source].credits = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed credits:" << m_weatherData[source].credits;
weatherData.credits = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed credits:" << weatherData.credits;
} else if (elementName == QLatin1String("link")) {
m_weatherData[source].creditsUrl = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed credits url:" << m_weatherData[source].creditsUrl;
weatherData.creditsUrl = xml.readElementText();
qCDebug(IONENGINE_WETTERCOM) << "parsed credits url:" << weatherData.creditsUrl;
} else if (elementName == QLatin1String("d")) {
localTime = xml.readElementText().toInt();
qCDebug(IONENGINE_WETTERCOM) << "parsed local time:" << localTime;
......@@ -682,22 +685,26 @@ void WetterComIon::updateWeather(const QString& source, bool parseError)
{
qCDebug(IONENGINE_WETTERCOM) << "Source:" << source;
const PlaceInfo& placeInfo = m_place[source];
QString weatherSource = QStringLiteral("wettercom|weather|%1|%2;%3")
.arg(source,
m_place[source].placeCode,
m_place[source].displayName);
placeInfo.placeCode,
placeInfo.displayName);
const WeatherData& weatherData = m_weatherData[source];
Plasma::DataEngine::Data data;
data.insert(QStringLiteral("Place"), m_place[source].displayName);
data.insert(QStringLiteral("Place"), placeInfo.displayName);
if (!parseError && !m_weatherData[source].forecasts.isEmpty()) {
data.insert(QStringLiteral("Station"), m_place[source].displayName);
if (!parseError && !weatherData.forecasts.isEmpty()) {
data.insert(QStringLiteral("Station"), placeInfo.displayName);
//data.insert("Condition Icon", "N/A");
//data.insert("Temperature", "N/A");
data.insert(QStringLiteral("Temperature Unit"), KUnitConversion::Celsius);
int i = 0;
foreach(WeatherData::ForecastPeriod * forecastPeriod, m_weatherData[source].forecasts) {
foreach(WeatherData::ForecastPeriod * forecastPeriod, weatherData.forecasts) {
if (i > 0) {
WeatherData::ForecastInfo weather = forecastPeriod->getWeather();
......@@ -741,8 +748,8 @@ void WetterComIon::updateWeather(const QString& source, bool parseError)
// Set number of forecasts per day/night supported
data.insert(QStringLiteral("Total Weather Days"), i);
data.insert(QStringLiteral("Credit"), m_weatherData[source].credits); // FIXME i18n?
data.insert(QStringLiteral("Credit Url"), m_weatherData[source].creditsUrl);
data.insert(QStringLiteral("Credit"), weatherData.credits); // FIXME i18n?
data.insert(QStringLiteral("Credit Url"), weatherData.creditsUrl);
qCDebug(IONENGINE_WETTERCOM) << "updated weather data:" << weatherSource << data;
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment