fix crash problems and update the openweathermap URLs
authorEric Ruei <e-ruei1@ti.com>
Fri, 27 Oct 2017 15:54:22 +0000 (11:54 -0400)
committerEric Ruei <e-ruei1@ti.com>
Fri, 27 Oct 2017 15:54:22 +0000 (11:54 -0400)
mainwindow.cpp:
- add default proxy setting
- replace QList iterator with QList index
forecastdatawidget.cpp: add boundary check at ::updateData()
openweathermapdataengine.cpp: use the latset URLs from openweathermap.com

Signed-off-by: Eric Ruei <e-ruei1@ti.com>
forecastdatawidget.cpp
mainwindow.cpp
webdataengine/openweathermapdataengine.cpp

index 63fcbf75435cd0ce2ffd40928fad95b083e274b4..ebabc5744d91f0db325dc92e8c24ada5d38255d0 100644 (file)
@@ -80,7 +80,7 @@ void ForecastDataWidget::setData(QList<ForecastData *> data)
 //  Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards
 void ForecastDataWidget::updateData()
 {
 //  Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards
 void ForecastDataWidget::updateData()
 {
-    for(int a = 0;a<FORECAST_DAYS;a++)
+    for(int a = 0;a<FORECAST_DAYS && a<m_data.size();a++)
     {
         m_forecastDataWidgetGroup[a].highTemp->setText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat()));
         m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat()));
     {
         m_forecastDataWidgetGroup[a].highTemp->setText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat()));
         m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat()));
@@ -101,7 +101,7 @@ void ForecastDataWidget::scaleContents()
     //the total height of the widget (giving each row that proportion of the total space)
     int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height();
 
     //the total height of the widget (giving each row that proportion of the total space)
     int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height();
 
-    for(int a = 0;a<FORECAST_DAYS;a++)
+    for(int a = 0;a<FORECAST_DAYS && a<m_data.size();a++)
     {
         m_forecastDataWidgetGroup[a].highTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
         m_forecastDataWidgetGroup[a].lowTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
     {
         m_forecastDataWidgetGroup[a].highTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
         m_forecastDataWidgetGroup[a].lowTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
index b3f89946b32fb590f3b516a4f3141907c1f42b70..9b8974267a130f4c4b0ea401b32cabc58ee41107 100644 (file)
@@ -22,6 +22,9 @@
 MainWindow::MainWindow(QWidget *parent) :\r
     QWidget(parent)\r
 {\r
 MainWindow::MainWindow(QWidget *parent) :\r
     QWidget(parent)\r
 {\r
+\r
+    QString http_proxy = QString::fromLatin1(qgetenv("http_proxy"));\r
+\r
     // the MainWindow provides the container for the ThermostatWidget, WeatherWidget, and OptionsWidget\r
     // it is also provides much of the interface between WebData and the widgets that require web updates\r
     // it is set to initial hard-coded values first, and only updated from the web if internet options\r
     // the MainWindow provides the container for the ThermostatWidget, WeatherWidget, and OptionsWidget\r
     // it is also provides much of the interface between WebData and the widgets that require web updates\r
     // it is set to initial hard-coded values first, and only updated from the web if internet options\r
@@ -31,6 +34,13 @@ MainWindow::MainWindow(QWidget *parent) :
     //get a handle to the global settings singleton\r
     m_globalSettings = GlobalSettings::getInstance();\r
 \r
     //get a handle to the global settings singleton\r
     m_globalSettings = GlobalSettings::getInstance();\r
 \r
+    // Initialize the proxy setting from http_proxy if defined\r
+    if(!http_proxy.isEmpty())\r
+    {\r
+        QUrl url(http_proxy);\r
+        m_globalSettings->setProxyInfo(url.host(), url.port());\r
+    }\r
+\r
     // sets initial weather background\r
     setStyleSheet("mainwindow {border-image: url(:/Images/background-sunny-very-few-clouds.JPG)}");\r
 \r
     // sets initial weather background\r
     setStyleSheet("mainwindow {border-image: url(:/Images/background-sunny-very-few-clouds.JPG)}");\r
 \r
@@ -304,13 +314,14 @@ void MainWindow::webDataFailed()
         setBackground(weatherData->icon(), weatherData->localTime().time());\r
 \r
         //now iterate through the forecast days and do the same thing\r
         setBackground(weatherData->icon(), weatherData->localTime().time());\r
 \r
         //now iterate through the forecast days and do the same thing\r
-        QList<ForecastData* >::iterator i;\r
-        for(i=weatherData->forecastData().begin();i!=weatherData->forecastData().end();i++)\r
+        int i;\r
+        for(i = 0; i < weatherData->forecastData().size(); i++)\r
         {\r
         {\r
+            ForecastData* forecast = weatherData->forecastData().at(i);\r
 \r
 \r
-            (*i)->setLowTemp(75+(qrand()%10*qrand()%2*(-1)));\r
-            (*i)->setHighTemp((*i)->lowTemp()+qrand()%10);\r
-            (*i)->setIcon(icons[qrand()%icons.size()]);\r
+            forecast->setLowTemp(75+(qrand()%10*qrand()%2*(-1)));\r
+            forecast->setHighTemp(forecast->lowTemp()+qrand()%10);\r
+            forecast->setIcon(icons[qrand()%icons.size()]);\r
         }\r
 \r
         //set the data just to trigger an update of the widgets\r
         }\r
 \r
         //set the data just to trigger an update of the widgets\r
index 09581b358367a285eca73015c6ee376b90281a0d..b5554dd437fb18f7c3c5f7af6dc972cffcc22a8c 100644 (file)
@@ -36,7 +36,7 @@ void OpenWeatherMapDataEngine::dispatchRequest()
     m_weatherReceived = false;
 
     //for openweather map we first must find the city
     m_weatherReceived = false;
 
     //for openweather map we first must find the city
-    QString cityUrl = "http://openweathermap.org/data/2.1/find/name?q="+m_preparedCity;
+    QString cityUrl = "http://api.openweathermap.org/data/2.5/find?q="+m_preparedCity+"&appid=62da841a6b62ce48d249c6e2d00a2102";
 
     // receive document and parse it
     QNetworkRequest request;
 
     // receive document and parse it
     QNetworkRequest request;
@@ -84,7 +84,7 @@ void OpenWeatherMapDataEngine::responseReceived()
     }
     else
     {
     }
     else
     {
-        qDebug() << "Network Error: " << m_reply->errorString();
+        qDebug() << "responseReceived(): Network Error: (" << m_reply->error() << ")" << m_reply->errorString();
         loadLocalData();
         emit networkTimeout();
     }
         loadLocalData();
         emit networkTimeout();
     }
@@ -96,7 +96,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests()
     m_weatherData->setCurrentCity(m_fullCity);
 
     //first send request for current weather
     m_weatherData->setCurrentCity(m_fullCity);
 
     //first send request for current weather
-    QString currentWeatherURL = "http://openweathermap.org/data/2.1/weather/city/"+QString::number(m_cityId);
+    QString currentWeatherURL = "http://api.openweathermap.org/data/2.5/weather?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102";
 
     QNetworkRequest request;
     request.setUrl(QUrl(currentWeatherURL));
 
     QNetworkRequest request;
     request.setUrl(QUrl(currentWeatherURL));
@@ -110,7 +110,7 @@ void OpenWeatherMapDataEngine::dispatchWeatherDataRequests()
     connect(m_reply,SIGNAL(finished()),this,SLOT(currentWeatherResponseReceived()));
 
     //next send request for current weather
     connect(m_reply,SIGNAL(finished()),this,SLOT(currentWeatherResponseReceived()));
 
     //next send request for current weather
-    QString forecastWeatherURL = "http://openweathermap.org/data/2.1/forecast/city/"+QString::number(m_cityId);
+    QString forecastWeatherURL = "http://api.openweathermap.org/data/2.5/forecast?id="+QString::number(m_cityId)+"&appid=62da841a6b62ce48d249c6e2d00a2102";
 
     request.setUrl(QUrl(forecastWeatherURL));
 
 
     request.setUrl(QUrl(forecastWeatherURL));
 
@@ -128,7 +128,8 @@ void OpenWeatherMapDataEngine::currentWeatherResponseReceived()
 
     if(m_reply->error() != QNetworkReply::NoError)
     {
 
     if(m_reply->error() != QNetworkReply::NoError)
     {
-        qDebug() << "Network Error: " << m_reply->errorString();
+        qDebug() << "OpenWeatherMapDataEngine::currentWeatherResponseReceived()";
+        qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString();
         emit networkTimeout();
         m_reply->deleteLater();
         return;
         emit networkTimeout();
         m_reply->deleteLater();
         return;
@@ -148,7 +149,8 @@ void OpenWeatherMapDataEngine::forecastResponseReceived()
 {
     if(m_forecastReply->error() != QNetworkReply::NoError)
     {
 {
     if(m_forecastReply->error() != QNetworkReply::NoError)
     {
-        qDebug() << "Network Error: " << m_reply->errorString();
+        qDebug() << "OpenWeatherMapDataEngine::forecastResponseReceived()";
+        qDebug() << "Network Error: (" << m_reply->error() << ")" << m_reply->errorString();
         emit networkTimeout();
         m_forecastReply->deleteLater();
         return;
         emit networkTimeout();
         m_forecastReply->deleteLater();
         return;
@@ -291,7 +293,7 @@ qlonglong OpenWeatherMapDataEngine::parseCityInformation(QString jsonData)
     //must have an object set equal to the class data received from the web or qt throws parse error
     QScriptValue result = engine.evaluate("weatherObject="+jsonData);
 
     //must have an object set equal to the class data received from the web or qt throws parse error
     QScriptValue result = engine.evaluate("weatherObject="+jsonData);
 
-    if(result.property("message").toString() != "")
+    if(result.property("message").toString() != "accurate")
         return -1;
     else
         return result.property("list").property("0").property("id").toInteger();
         return -1;
     else
         return result.property("list").property("0").property("id").toInteger();
@@ -437,5 +439,5 @@ bool OpenWeatherMapDataEngine::readFromCache(QString alternateCacheFile)
 
 QString OpenWeatherMapDataEngine::licenseString()
 {
 
 QString OpenWeatherMapDataEngine::licenseString()
 {
-    return QString("Weather data from  openweathermap.org under CC-BY-SA");
+    return QString("Weather data from openweathermap.org under CC-BY-SA 4.0");
 }
 }