Implemented minimal settings dialog
[quandoparte] / application / app.cpp
index d248f10..83a4aa7 100644 (file)
@@ -21,6 +21,7 @@ Boston, MA 02110-1301, USA.
 
 #include "app.h"
 #include "stationview.h"
+#include "stationlistmodel.h"
 #include "stationlistview.h"
 #include "settingsdialog.h"
 
@@ -33,42 +34,67 @@ Boston, MA 02110-1301, USA.
 #include <QSettings>
 #include <QUrl>
 
+#include <QGeoPositionInfoSource>
+
+// Constants
+static const int RECENT_STATIONS_MAX_COUNT = 10;
+
+QTM_USE_NAMESPACE
+
 App::App(QObject *parent) :
     QObject(parent),
     accessManager(new QNetworkAccessManager(this)),
-    stationView(new StationView(NULL)),
-    stationListView(new StationListView(stationView))
+    positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
+    stationView(new StationView()),
+    stationListModel(new StationListModel(this)),
+    stationListView(new StationListView(stationListModel, stationView))
 {
+    stationListModel->load(dataDir() + "stations/stations.qpl");
+
+    connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
+            stationListView, SLOT(updatePosition(QGeoPositionInfo)));
     connect(stationListView, SIGNAL(stationSelected(const QString &)),
             SLOT(queryStation(const QString &)));
+
     connect(stationListView, SIGNAL(aboutTriggered()),
             SLOT(showAboutDialog()));
     connect(stationView, SIGNAL(aboutTriggered()),
             SLOT(showAboutDialog()));
+
+    connect(stationListView, SIGNAL(settingsChangeRequested()),
+            SLOT(showSettingsDialog()));
+    connect(stationView, SIGNAL(settingsChangeRequested()),
+            SLOT(showSettingsDialog()));
+
     connect(stationView, SIGNAL(stationListSelectTriggered()),
             SLOT(showStationSelectView()));
 
-    connect(stationView, SIGNAL(showingArrivalsChanged(bool)),
-            SLOT(setShowingArrivals(bool)));
     readSettings();
 
+    qDebug() << "found" << stationListModel->rowCount() << "stations";
 #if defined(Q_WS_S60)
     stationView->showMaximized();
 #else
     stationView->show();
 #endif
 
-    if (stationName.isEmpty()) {
+    if (recentStations.isEmpty()) {
 #if defined(Q_WS_S60)
         stationListView->showMaximized();
 #else
         stationListView->show();
 #endif
+    } else {
+        queryStation(recentStations.front());
     }
+
+    // Testing only: start updates rigt away.
+    positionInfoSource->startUpdates();
 }
 
 App::~App()
 {
+    delete stationView;
     saveSettings();
 }
 
@@ -94,6 +120,11 @@ void App::queryStation(const QString &station)
     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();
+    }
 #ifdef Q_WS_MAEMO_5
     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
 #endif
@@ -101,6 +132,13 @@ void App::queryStation(const QString &station)
 
 void App::showSettingsDialog()
 {
+    qDebug() << "Settings Dialog called";
+
+    SettingsDialog *dialog = new SettingsDialog(stationView);
+    if (dialog->exec() == QDialog::Accepted) {
+        // TODO Use new settings
+    }
+    delete dialog;
 }
 
 void App::showAboutDialog()
@@ -110,7 +148,7 @@ void App::showAboutDialog()
     QString version = QApplication::instance()->applicationVersion();
     QString aboutText = QString(
                 tr("<p>%1 version %2</p>"
-                   "<p>Copyright (c) 2010</p>"
+                   "<p>Copyright (c) 2010, 2011</p>"
                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
     QMessageBox::about(stationView, name, aboutText);
@@ -128,21 +166,26 @@ void App::readSettings(void)
                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
     stationView->setBaseUrl(queryBaseUrl);
 
-    stationName = settings.value("CurrentStation").toString();
-    showingArrivals = settings.value("ShowingArrivals", false).toBool();
+    recentStations = settings.value("RecentStations").toString().split(",");
     checkingInterval = settings.value("CheckInterval", 2000).toInt();
 }
 
 void App::saveSettings(void)
 {
     QSettings settings;
+
+    qDebug() << "Saving Settings to" << settings.fileName();
+
     settings.setValue("QueryURL", queryBaseUrl);
-    settings.value("CurrentStation", stationName);
-    settings.value("ShowingArrivals", showingArrivals);
-    settings.value("CheckInterval", checkingInterval);
+    settings.setValue("RecentStations", recentStations.join(","));
+    settings.setValue("CheckInterval", checkingInterval);
 }
 
-void App::setShowingArrivals(bool showArrivals)
+QString App::dataDir(void)
 {
-    showingArrivals = showArrivals;
+#ifdef Q_WS_MAEMO_5
+    return QString("/opt/usr/share/apps/quandoparte/");
+#else
+    return QString("/usr/share/apps/quandoparte/");
+#endif
 }