X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=application%2Fapp.cpp;h=26f0076c11065b0e39371a443b3b51b40c33a76c;hb=10b758aa403704b12f1f9deafcfd03095eb73b52;hp=e9dab123ac8110c7d2f5e7daf77e3b5a049b5c6f;hpb=bd8c6791c3d70ef1f12ed05c459eca2b81be1cc4;p=quandoparte diff --git a/application/app.cpp b/application/app.cpp index e9dab12..26f0076 100644 --- a/application/app.cpp +++ b/application/app.cpp @@ -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 #include @@ -32,6 +34,7 @@ Boston, MA 02110-1301, USA. #include #include #include +#include #include #include @@ -43,20 +46,19 @@ QTM_USE_NAMESPACE App::App(QObject *parent) : QObject(parent), - accessManager(new QNetworkAccessManager(this)), - positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)), + dataProvider(new DataProvider(this)), + checkingTimer(new QTimer(this)), stationView(new StationView()), stationListModel(new StationListModel(this)), stationListView(new StationListView(stationListModel, stationView)) { - stationListModel->load(dataDir() + "stations/stations.qpl"); + stationListModel->load("stations:stations.qpl"); - if (positionInfoSource) { - connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)), - stationListView, SLOT(updatePosition(QGeoPositionInfo))); - } - 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,33 +76,28 @@ App::App(QObject *parent) : readSettings(); qDebug() << "found" << stationListModel->rowCount() << "stations"; + + 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()); - } - - // Testing only: start updates rigt away. - if (positionInfoSource) { - positionInfoSource->startUpdates(); + updateStation(); } } App::~App() { - delete stationView; saveSettings(); + disconnect(); + 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 @@ -108,23 +105,19 @@ 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 } +void App::updateStation() +{ + if (!stationListView->isVisible()) { + dataProvider->updateStation(); + } +} + void App::showSettingsDialog() { qDebug() << "Settings Dialog called"; @@ -142,10 +135,18 @@ void App::showAboutDialog() QString name = QApplication::instance()->applicationName(); QString version = QApplication::instance()->applicationVersion(); QString aboutText = QString( - tr("

%1 version %2

" + tr("

" + "%1 version %2" + "

" "

Copyright (c) 2010, 2011

" "

Luciano Montanaro (mikelima@cirulla.net)

" - "

Licensed under the GNU Public License v2 or above

")).arg(name).arg(version); + "

Licensed under the GNU Public License v2 or above

" + "

Station geolocation data from " + "OpenStreetMap" + "

" + "

Realtime train data from " + "Viaggiatreno" + "

")).arg(name).arg(version); QMessageBox::about(stationView, name, aboutText); } @@ -156,33 +157,30 @@ 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(","); - checkingInterval = settings.value("CheckInterval", 2000).toInt(); - stationViewPreferred = settings.value("StationViewPreferred", false).toBool(); + 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 + seconds timeout which is way too short. + As a workaround I consider anything less than 30 seconds as too short + and disable the timer. + */ + if (settings->checkingInterval() > 30000) { + checkingTimer->setInterval(settings->checkingInterval()); + checkingTimer->start(); + } else { + checkingTimer->setInterval(-1); + checkingTimer->stop(); + } } 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) { -#ifdef Q_WS_MAEMO_5 - return QString("/opt/usr/share/apps/quandoparte/"); -#else - return QString("/usr/share/apps/quandoparte/"); -#endif + return QString(DATADIR); }