void AppModel::handleForecastNetworkData(QObject *replyObj) { QNetworkReply *networkReply = qobject_cast<QNetworkReply*>(replyObj); if (!networkReply) return; if (!networkReply->error()) { QJsonDocument document = QJsonDocument::fromJson(networkReply->readAll()); QJsonObject jo; QJsonValue jv; QJsonObject root = document.object(); jv = root.value(QStringLiteral("list")); if (!jv.isArray()) qWarning() << "Invalid forecast object"; QJsonArray ja = jv.toArray(); //we need 4 days of forecast -> first entry is today if (ja.count() != 5) qWarning() << "Invalid forecast object"; QString data; for (int i = 1; i<ja.count(); i++) { WeatherData *forecastEntry = new WeatherData(); //min/max temperature QJsonObject subtree = ja.at(i).toObject(); jo = subtree.value(QStringLiteral("temp")).toObject(); jv = jo.value(QStringLiteral("min")); data.clear(); data += niceTemperatureString(jv.toDouble()); data += QChar('/'); jv = jo.value(QStringLiteral("max")); data += niceTemperatureString(jv.toDouble()); forecastEntry->setTemperature(data); //get date jv = subtree.value(QStringLiteral("dt")); QDateTime dt = QDateTime::fromMSecsSinceEpoch((qint64)jv.toDouble()*1000); forecastEntry->setDayOfWeek(dt.date().toString(QStringLiteral("ddd"))); //get icon QJsonArray weatherArray = subtree.value(QStringLiteral("weather")).toArray(); jo = weatherArray.at(0).toObject(); forecastEntry->setWeatherIcon(jo.value(QStringLiteral("icon")).toString()); //get description forecastEntry->setWeatherDescription(jo.value(QStringLiteral("description")).toString()); d->forecast.append(forecastEntry); } if (!(d->ready)) { d->ready = true; emit readyChanged(); } emit weatherChanged(); } networkReply->deleteLater(); }
void FieldProxy::setWeather(int weather) { if (mWeather == weather) { return; } mWeather = weather; emit weatherChanged(); }
void AppModel::setUseGps(bool value) { d->useGps = value; if (value) { d->city = ""; emit cityChanged(); emit weatherChanged(); } emit useGpsChanged(); }
void AppModel::setUseGps(bool value) { d->useGps = value; if (value) { d->city = ""; d->throttle.invalidate(); emit cityChanged(); emit weatherChanged(); } emit useGpsChanged(); }
void WeatherDataSource::onSqlConnectorReply(const bb::data::DataAccessReply& reply) { if (reply.hasError()) { qWarning() << "WeatherModel: Database error, " << reply.errorType() << " " << reply.errorMessage() << " id: " << reply.id(); } else { if (reply.id() == 0) { emit weatherChanged(mRevision); } } }
void AppModel::handleWeatherNetworkData(QObject *replyObj) { QNetworkReply *networkReply = qobject_cast<QNetworkReply*>(replyObj); if (!networkReply) return; if (!networkReply->error()) { QString xmlData = QString::fromUtf8(networkReply->readAll()); foreach (WeatherData *inf, d->forecast) delete inf; d->forecast.clear(); QXmlStreamReader xml(xmlData); while (!xml.atEnd()) { xml.readNext(); if (xml.name() == "current_conditions") { while (!xml.atEnd()) { xml.readNext(); if (xml.name() == "current_conditions") break; if (xml.tokenType() == QXmlStreamReader::StartElement) { if (xml.name() == "condition") { d->now.setWeatherDesc(GET_DATA_ATTR); } else if (xml.name() == "icon") { d->now.setWeather(google2name(GET_DATA_ATTR)); } else if (xml.name() == "temp_f") { d->now.setTempString(GET_DATA_ATTR + QChar(176)); } } } } if (xml.name() == "forecast_conditions") { WeatherData *cur = NULL; while (!xml.atEnd()) { xml.readNext(); if (xml.name() == "forecast_conditions") { if (cur) { d->forecast.append(cur); } break; } else if (xml.tokenType() == QXmlStreamReader::StartElement) { if (!cur) cur = new WeatherData(); if (xml.name() == "day_of_week") { cur->setDayOfWeek(GET_DATA_ATTR); } else if (xml.name() == "icon") { cur->setWeather(google2name(GET_DATA_ATTR)); } else if (xml.name() == "low") { QString v = cur->tempString(); QStringList parts = v.split("/"); if (parts.size() >= 1) parts.replace(0, GET_DATA_ATTR + QChar(176)); if (parts.size() == 0) parts.append(GET_DATA_ATTR + QChar(176)); cur->setTempString(parts.join("/")); } else if (xml.name() == "high") { QString v = cur->tempString(); QStringList parts = v.split("/"); if (parts.size() == 2) parts.replace(1, GET_DATA_ATTR + QChar(176)); if (parts.size() == 0) parts.append(""); if (parts.size() == 1) parts.append(GET_DATA_ATTR + QChar(176)); cur->setTempString(parts.join("/")); } } } } } if (!(d->ready)) { d->ready = true; emit readyChanged(); } emit weatherChanged(); } networkReply->deleteLater(); }