Use DataProvider class form the App
authorLuciano Montanaro <mikelima@cirulla.net>
Wed, 27 Jul 2011 22:35:35 +0000 (00:35 +0200)
committerLuciano Montanaro <mikelima@cirulla.net>
Tue, 27 Dec 2011 22:16:47 +0000 (23:16 +0100)
application/app.cpp
application/app.h
application/dataprovider.cpp
application/dataprovider.h

index 278d420..8c950c4 100644 (file)
@@ -20,6 +20,7 @@ Boston, MA 02110-1301, USA.
 */
 
 #include "app.h"
+#include "dataprovider.h"
 #include "stationview.h"
 #include "stationlistmodel.h"
 #include "stationlistview.h"
@@ -45,7 +46,7 @@ QTM_USE_NAMESPACE
 
 App::App(QObject *parent) :
     QObject(parent),
-    accessManager(new QNetworkAccessManager(this)),
+    dataProvider(new DataProvider(this)),
     checkingTimer(new QTimer(this)),
     stationView(new StationView()),
     stationListModel(new StationListModel(this)),
@@ -53,6 +54,9 @@ App::App(QObject *parent) :
 {
     stationListModel->load("stations:stations.qpl");
 
+    connect(dataProvider, SIGNAL(queryStationCompleted(QByteArray)),
+            SLOT(downloadFinished(QByteArray)));
+
     connect(stationListView, SIGNAL(stationSelected(const QString &)),
             SLOT(queryStation(const QString &)));
 
@@ -79,7 +83,7 @@ App::App(QObject *parent) :
     if (settings->recentStations().isEmpty() || !settings->stationViewPreferred()) {
         stationListView->show();
     } else {
-        queryStation(settings->recentStations().front());
+        updateStation();
     }
 }
 
@@ -90,14 +94,10 @@ App::~App()
     delete stationView;
 }
 
-void App::downloadFinished(void)
+void App::downloadFinished(const QByteArray &data)
 {
-    disconnect(stationQueryReply, SIGNAL(finished()),
-               this, SLOT(downloadFinished()));
-    stationView->updateView(stationQueryReply->readAll());
+    stationView->updateView(data);
     stationListView->hide();
-    stationQueryReply->deleteLater();
-    stationQueryReply = 0;
 #ifdef Q_WS_MAEMO_5
     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 #endif
@@ -105,19 +105,7 @@ void App::downloadFinished(void)
 
 void App::queryStation(const QString &station)
 {
-    QNetworkRequest request;
-    Settings *settings = Settings::instance();
-    request.setUrl(settings->queryBaseUrl());
-    const QString queryString = "stazione=" + station;
-    const QByteArray query(queryString.toLocal8Bit());
-    stationQueryReply = accessManager->post(request, query);
-    connect(stationQueryReply, SIGNAL(finished()),
-            this, SLOT(downloadFinished()));
-    settings->recentStations().push_front(station);
-    settings->recentStations().removeDuplicates();
-    if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
-        settings->recentStations().pop_back();
-    }
+    dataProvider->queryStation(station);
 #ifdef Q_WS_MAEMO_5
     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
@@ -125,11 +113,8 @@ void App::queryStation(const QString &station)
 
 void App::updateStation()
 {
-    Settings *settings = Settings::instance();
-
-    qDebug() << "updating station data";
-    if (!settings->recentStations().isEmpty() && !stationListView->isVisible()) {
-        queryStation(settings->recentStations().front());
+    if (!stationListView->isVisible()) {
+        dataProvider->updateStation();
     }
 }
 
index 93e5018..fd4c88d 100644 (file)
@@ -25,10 +25,9 @@ Boston, MA 02110-1301, USA.
 #include <QApplication>
 #include <QStringList>
 
-class QNetworkAccessManager;
-class QNetworkReply;
 class QTimer;
 
+class DataProvider;
 class StationView;
 class StationListView;
 class StationListModel;
@@ -47,7 +46,7 @@ signals:
 public slots:
     void queryStation(const QString &station);
     void updateStation();
-    void downloadFinished(void);
+    void downloadFinished(const QByteArray &data);
     void showAboutDialog(void);
     void showSettingsDialog(void);
     void showStationSelectView(void);
@@ -55,8 +54,7 @@ public:
     void saveSettings(void);
     void readSettings(void);
 private:
-    QNetworkAccessManager *accessManager;
-    QNetworkReply *stationQueryReply;
+    DataProvider *dataProvider;
     QTimer *checkingTimer;
     StationView *stationView;
     StationListModel *stationListModel;
index 5c0cede..f0fe577 100644 (file)
@@ -45,7 +45,7 @@ void DataProvider::queryStation(const QString &station)
     const QByteArray query(queryString.toLocal8Bit());
     stationQueryReply = accessManager->post(request, query);
     connect(stationQueryReply, SIGNAL(finished()),
-            this, SLOT(queryStationCompleted()));
+            SLOT(onQueryStationReply()));
     settings->recentStations().push_front(station);
     settings->recentStations().removeDuplicates();
     if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
@@ -64,11 +64,12 @@ void DataProvider::updateStation()
     }
 }
 
-void DataProvider::queryStationCompleted()
+void DataProvider::onQueryStationReply()
 {
     disconnect(stationQueryReply, SIGNAL(finished()),
                this, SLOT(downloadFinished()));
     // TODO implement parsing or data returning...
+    emit queryStationCompleted(stationQueryReply->readAll());
     stationQueryReply->deleteLater();
     stationQueryReply = 0;
 }
index f90ee0f..38990a6 100644 (file)
@@ -34,13 +34,14 @@ public:
     explicit DataProvider(QObject *parent = 0);
 
 signals:
+    void queryStationCompleted(const QByteArray &result);
 
 public slots:
     void queryStation(const QString &station);
     void updateStation();
 
 private slots:
-    void queryStationCompleted(void);
+    void onQueryStationReply(void);
 
 private:
     QNetworkAccessManager *accessManager;