Added refresh button to station schedule page
[quandoparte] / application / dataprovider.cpp
index e6dfe25..81617fb 100644 (file)
@@ -21,11 +21,13 @@ Boston, MA 02110-1301, USA.
 
 #include "dataprovider.h"
 #include "settings.h"
+#include "stationschedulemodel.h"
 
 #include <QDebug>
 #include <QNetworkAccessManager>
 #include <QNetworkReply>
 #include <QNetworkRequest>
+#include <QSharedPointer>
 #include <QWebElement>
 #include <QWebFrame>
 #include <QWebPage>
@@ -35,28 +37,44 @@ static const int RECENT_STATIONS_MAX_COUNT = 10;
 
 // Class methods
 
+DataProvider *DataProvider::instance()
+{
+    static DataProvider *dataProvider = 0;
+
+    if (!dataProvider)
+        dataProvider = new DataProvider(0);
+    return dataProvider;
+}
+
 DataProvider::DataProvider(QObject *parent) :
     QObject(parent),
     accessManager(new QNetworkAccessManager(this))
 {
 }
 
-void DataProvider::stationSchedule(const QString &station)
+void DataProvider::fetchStationSchedule(const QString &station,
+                                        const QString &stationCode)
 {
     QNetworkRequest request;
     Settings *settings = Settings::instance();
-    request.setUrl(settings->queryBaseUrl());
-    const QString queryString = "stazione=" + station;
+    request.setUrl(settings->queryBaseUrl() + "stazione");
+    qDebug() << "fetching schedule for station:" << station << "code:" << stationCode;
+    const QString queryString =
+            stationCode.isEmpty() ? "stazione=" + station :
+                                    "codiceStazione=" + stationCode;
     const QByteArray query(queryString.toLocal8Bit());
     stationQueryReply = accessManager->post(request, query);
     connect(stationQueryReply, SIGNAL(finished()),
-            SLOT(onStationScheduleReady()));
-    settings->recentStations().push_front(station);
-    settings->recentStations().removeDuplicates();
-    if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
-        settings->recentStations().pop_back();
+            SLOT(onStationScheduleFetched()));
+    connect(stationQueryReply, SIGNAL(error(QNetworkReply::NetworkError)),
+            SLOT(onNetworkError(QNetworkReply::NetworkError)));
+    QStringList recentStations = settings->recentStations();
+    recentStations.push_front(station);
+    recentStations.removeDuplicates();
+    if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
+        recentStations.pop_back();
     }
-    // TODO Implement busy indication...
+    settings->setRecentStations(recentStations);
 }
 
 void DataProvider::updateStation()
@@ -65,17 +83,30 @@ void DataProvider::updateStation()
 
     qDebug() << "updating station data";
     if (!settings->recentStations().isEmpty()) {
-        stationSchedule(settings->recentStations().front());
+        fetchStationSchedule(settings->recentStations().front());
     }
 }
 
-void DataProvider::onStationScheduleReady()
+void DataProvider::onStationScheduleFetched()
 {
-    disconnect(stationQueryReply, SIGNAL(finished()),
-               this, SLOT(onStationScheduleReady()));
-    // TODO implement parsing or data returning...
-    emit stationScheduleReady(QString::fromUtf8(stationQueryReply->readAll()),
-                              stationQueryReply->url());
+    disconnect(stationQueryReply);
+
+    QString name = Settings::instance()->recentStations().front();
+
+    emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
     stationQueryReply->deleteLater();
     stationQueryReply = 0;
 }
+
+void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
+{
+    switch (errorCode) {
+    case QNetworkReply::NoError:
+        qDebug() << "No Network error" << errorCode;
+        break;
+    default:
+        qDebug() << "SNetwork error" << errorCode;
+        emit error();
+        break;
+    }
+}