3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 #include "stationview.h"
24 #include "stationlistmodel.h"
25 #include "stationlistview.h"
26 #include "settingsdialog.h"
30 #include <QMessageBox>
31 #include <QNetworkAccessManager>
32 #include <QNetworkReply>
33 #include <QNetworkRequest>
39 #include <QGeoPositionInfoSource>
42 static const int RECENT_STATIONS_MAX_COUNT = 10;
46 App::App(QObject *parent) :
48 accessManager(new QNetworkAccessManager(this)),
49 checkingTimer(new QTimer(this)),
50 stationView(new StationView()),
51 stationListModel(new StationListModel(this)),
52 stationListView(new StationListView(stationListModel, stationView))
54 stationListModel->load("stations:stations.qpl");
56 connect(stationListView, SIGNAL(stationSelected(const QString &)),
57 SLOT(queryStation(const QString &)));
59 connect(stationListView, SIGNAL(aboutTriggered()),
60 SLOT(showAboutDialog()));
61 connect(stationView, SIGNAL(aboutTriggered()),
62 SLOT(showAboutDialog()));
64 connect(stationListView, SIGNAL(settingsChangeRequested()),
65 SLOT(showSettingsDialog()));
66 connect(stationView, SIGNAL(settingsChangeRequested()),
67 SLOT(showSettingsDialog()));
69 connect(stationView, SIGNAL(stationListSelectTriggered()),
70 SLOT(showStationSelectView()));
74 qDebug() << "found" << stationListModel->rowCount() << "stations";
76 connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
78 Settings *settings = Settings::instance();
79 if (settings->recentStations().isEmpty() || !settings->stationViewPreferred()) {
80 stationListView->show();
82 queryStation(settings->recentStations().front());
93 void App::downloadFinished(void)
95 disconnect(stationQueryReply, SIGNAL(finished()),
96 this, SLOT(downloadFinished()));
97 stationView->updateView(stationQueryReply->readAll());
98 stationListView->hide();
99 stationQueryReply->deleteLater();
100 stationQueryReply = 0;
102 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
106 void App::queryStation(const QString &station)
108 QNetworkRequest request;
109 Settings *settings = Settings::instance();
110 request.setUrl(settings->queryBaseUrl());
111 const QString queryString = "stazione=" + station;
112 const QByteArray query(queryString.toLocal8Bit());
113 stationQueryReply = accessManager->post(request, query);
114 connect(stationQueryReply, SIGNAL(finished()),
115 this, SLOT(downloadFinished()));
116 settings->recentStations().push_front(station);
117 settings->recentStations().removeDuplicates();
118 if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
119 settings->recentStations().pop_back();
122 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
126 void App::updateStation()
128 Settings *settings = Settings::instance();
130 qDebug() << "updating station data";
131 if (!settings->recentStations().isEmpty() && !stationListView->isVisible()) {
132 queryStation(settings->recentStations().front());
136 void App::showSettingsDialog()
138 qDebug() << "Settings Dialog called";
140 SettingsDialog *dialog = new SettingsDialog(stationView);
141 if (dialog->exec() == QDialog::Accepted) {
147 void App::showAboutDialog()
149 qDebug() << "About Dialog called";
150 QString name = QApplication::instance()->applicationName();
151 QString version = QApplication::instance()->applicationVersion();
152 QString aboutText = QString(
154 "<a href='http://quandoparte.garage.maemo.org'>%1</a> version %2"
156 "<p>Copyright (c) 2010, 2011</p>"
157 "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
158 "<p>Licensed under the GNU Public License v2 or above</p>"
159 "<p>Station geolocation data from "
160 "<a href='http://www.openstreetmap.org'>OpenStreetMap</a>"
162 "<p>Realtime train data from "
163 "<a href='http://mobile.viaggiatreno.it'>Viaggiatreno</a>"
164 "</p>")).arg(name).arg(version);
165 QMessageBox::about(stationView, name, aboutText);
168 void App::showStationSelectView(void)
170 stationListView->show();
173 void App::readSettings(void)
175 Settings *settings = Settings::instance();
176 stationView->setBaseUrl(settings->queryBaseUrl());
179 I would use > 0 here, but people may have an old settings file with a 2
180 seconds timeout which is way too short.
181 As a workaround I consider anything less than 30 seconds as too short
182 and disable the timer.
184 if (settings->checkingInterval() > 30000) {
185 checkingTimer->setInterval(settings->checkingInterval());
186 checkingTimer->start();
188 checkingTimer->setInterval(-1);
189 checkingTimer->stop();
193 void App::saveSettings(void)
195 Settings::instance()->save();
198 QString App::dataDir(void)
200 return QString(DATADIR);