Bump version to 0.9.0
[quandoparte] / application / app.cpp
index 82a322b..26f0076 100644 (file)
@@ -20,10 +20,12 @@ Boston, MA 02110-1301, USA.
 */
 
 #include "app.h"
+#include "dataprovider.h"
 #include "stationview.h"
 #include "stationlistmodel.h"
 #include "stationlistview.h"
 #include "settingsdialog.h"
+#include "settings.h"
 
 #include <QDebug>
 #include <QMessageBox>
@@ -44,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)),
@@ -52,8 +54,11 @@ App::App(QObject *parent) :
 {
     stationListModel->load("stations:stations.qpl");
 
-    connect(stationListView, SIGNAL(stationSelected(const QString &)),
-            SLOT(queryStation(const QString &)));
+    connect(dataProvider, SIGNAL(stationScheduleReady(QByteArray, QUrl)),
+            SLOT(downloadFinished(QByteArray)));
+
+    connect(stationListView, SIGNAL(stationSelected(QString)),
+            SLOT(queryStation(QString)));
 
     connect(stationListView, SIGNAL(aboutTriggered()),
             SLOT(showAboutDialog()));
@@ -74,10 +79,11 @@ App::App(QObject *parent) :
 
     connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
     stationView->show();
-    if (recentStations.isEmpty() || !stationViewPreferred) {
+    Settings *settings = Settings::instance();
+    if (settings->recentStations().isEmpty() || !settings->stationViewPreferred()) {
         stationListView->show();
     } else {
-        queryStation(recentStations.front());
+        updateStation();
     }
 }
 
@@ -88,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
@@ -103,18 +105,7 @@ void App::downloadFinished(void)
 
 void App::queryStation(const QString &station)
 {
-    QNetworkRequest request;
-    request.setUrl(queryBaseUrl);
-    const QString queryString = "stazione=" + station;
-    const QByteArray query(queryString.toLocal8Bit());
-    stationQueryReply = accessManager->post(request, query);
-    connect(stationQueryReply, SIGNAL(finished()),
-            this, SLOT(downloadFinished()));
-    recentStations.push_front(station);
-    recentStations.removeDuplicates();
-    if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
-        recentStations.pop_back();
-    }
+    dataProvider->stationSchedule(station);
 #ifdef Q_WS_MAEMO_5
     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
@@ -122,9 +113,8 @@ void App::queryStation(const QString &station)
 
 void App::updateStation()
 {
-    qDebug() << "updating station data";
-    if (!recentStations.isEmpty() && !stationListView->isVisible()) {
-        queryStation(recentStations.front());
+    if (!stationListView->isVisible()) {
+        dataProvider->updateStation();
     }
 }
 
@@ -167,19 +157,8 @@ void App::showStationSelectView(void)
 
 void App::readSettings(void)
 {
-    QSettings settings;
-    queryBaseUrl = settings.value("QueryURL",
-                                  "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
-    stationView->setBaseUrl(queryBaseUrl);
-
-    recentStations = settings.value("RecentStations").toString().split(",");
-    qDebug() << "RecentStations:" << recentStations;
-
-    stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
-    qDebug() << "StationsViewPreferred:" << stationViewPreferred;
-
-    checkingInterval = settings.value("CheckInterval", 0).toInt();
-    qDebug() << "CheckInterval:" << checkingInterval;
+    Settings *settings = Settings::instance();
+    stationView->setBaseUrl(settings->queryBaseUrl() + "stazione");
 
     /*
        I would use > 0 here, but people may have an old settings file with a 2
@@ -187,8 +166,8 @@ void App::readSettings(void)
        As a workaround I consider anything less than 30 seconds as too short
        and disable the timer.
     */
-    if (checkingInterval > 30000) {
-        checkingTimer->setInterval(checkingInterval);
+    if (settings->checkingInterval() > 30000) {
+        checkingTimer->setInterval(settings->checkingInterval());
         checkingTimer->start();
     } else {
         checkingTimer->setInterval(-1);
@@ -198,14 +177,7 @@ void App::readSettings(void)
 
 void App::saveSettings(void)
 {
-    QSettings settings;
-
-    qDebug() << "Saving Settings to" << settings.fileName();
-
-    settings.setValue("QueryURL", queryBaseUrl);
-    settings.setValue("RecentStations", recentStations.join(","));
-    settings.setValue("CheckInterval", checkingInterval);
-    settings.setValue("StationViewPreferred", stationViewPreferred);
+    Settings::instance()->save();
 }
 
 QString App::dataDir(void)