1 #include "webdata.h"
2 #include "globalsettings.h"
4 #include "weatherdata.h"
5 #include "webdataengine/openweathermapdataengine.h"
6 #include "webdataengine/wundergrounddataengine.h"
8 #include "forecastdata.h"
10 #include <QtScript/QScriptEngine>
11 #include <QtScript/QScriptValueIterator>
13 #include <QtDebug>
14 #include <QTime>
16 /***********************************************************************************************************
17 * WebData
18 * Class that wraps a webdataengine used for retrieving data from an online API. Handles network connection
19 * manager and also configures the proxy, first by trying none and then a known one, then failing.
20 ************************************************************************************************************/
22 WebData::WebData(QObject *parent) :
23 QObject(parent)
24 {
25 m_globalSettings = GlobalSettings::getInstance();
27 // create network access manager
28 manager = new QNetworkAccessManager(this);
30 webDataEngine = new OpenWeatherMapDataEngine(manager);
31 connect(webDataEngine, SIGNAL(dataAvailable(WeatherData*)), this, SLOT(processDataAvailable(WeatherData*)));
32 connect(webDataEngine, SIGNAL(networkTimeout()), this, SLOT(processNetworkTimeout()));
34 configureProxy();
36 }
38 //FUNCTION: configureProxy()
39 //
40 // configures the correct proxy based on the ProxyState enum defined
42 void WebData::configureProxy()
43 {
44 if(m_globalSettings->proxyHost() == "" && m_globalSettings->proxyPort() == 0)
45 configureNoProxy();
46 else
47 configureSetProxy();
48 }
50 //FUNCTION: configureNoProxy()
51 //
52 // Sets the network access manager to use no proxy at all
54 void WebData::configureNoProxy()
55 {
56 manager->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
57 }
60 //FUNCTION: configureSetProxy()
61 //
62 // Sets the network access manager to use proxy that is configured in the settings
64 void WebData::configureSetProxy()
65 {
66 QNetworkProxy proxy;
67 proxy.setType(QNetworkProxy::HttpProxy);
68 proxy.setHostName(m_globalSettings->proxyHost());
69 proxy.setPort(m_globalSettings->proxyPort());
70 manager->setProxy(proxy);
71 }
73 //FUNCTION: changeCity()
74 //
75 // Sets the webDataEngine's city variable and then dispatches a request
77 void WebData::changeCity(QString city)
78 {
79 webDataEngine->setCity(city);
80 webDataEngine->dispatchRequest();
81 }
83 //FUNCTION: loadLocalData()
84 //
85 // invokes the webdataengine's loadLocalData function to pull locally cached data
87 void WebData::loadLocalData()
88 {
89 webDataEngine->loadLocalData();
90 }
92 //FUNCTION: licenseString()
93 //
94 // Returns the defined license string in the webdataengine
96 QString WebData::licenseString()
97 {
98 return webDataEngine->licenseString();
99 }
101 //FUNCTION: processDataAvailable()
102 //
103 // Catches the dataAvailable signal from the webdataengine and sets the proxy if it worked
104 // Otherwise emits signal with either web data or cache data
106 void WebData::processDataAvailable(WeatherData *weatherData)
107 {
108 if(!weatherData->cachedData())
109 {
110 m_globalSettings->setProxyInfo(manager->proxy().hostName(), manager->proxy().port());
111 }
113 emit dataAvailable(weatherData);
114 }
116 //FUNCTION: processNetworkTimeout()
117 //
118 // Catches the networkTimeout signal form the webdataengine and tries a new proxy if
119 // still in the configuration phase. Other wise emits a normal networkTimeout signal
121 void WebData::processNetworkTimeout()
122 {
123 emit networkTimeout();
124 }