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"
29 #include <QMessageBox>
30 #include <QNetworkAccessManager>
31 #include <QNetworkReply>
32 #include <QNetworkRequest>
38 #include <QGeoPositionInfoSource>
41 static const int RECENT_STATIONS_MAX_COUNT = 10;
45 App::App(QObject *parent) :
47 accessManager(new QNetworkAccessManager(this)),
48 checkingTimer(new QTimer(this)),
49 stationView(new StationView()),
50 stationListModel(new StationListModel(this)),
51 stationListView(new StationListView(stationListModel, stationView))
53 stationListModel->load("stations:stations.qpl");
55 connect(stationListView, SIGNAL(stationSelected(const QString &)),
56 SLOT(queryStation(const QString &)));
58 connect(stationListView, SIGNAL(aboutTriggered()),
59 SLOT(showAboutDialog()));
60 connect(stationView, SIGNAL(aboutTriggered()),
61 SLOT(showAboutDialog()));
63 connect(stationListView, SIGNAL(settingsChangeRequested()),
64 SLOT(showSettingsDialog()));
65 connect(stationView, SIGNAL(settingsChangeRequested()),
66 SLOT(showSettingsDialog()));
68 connect(stationView, SIGNAL(stationListSelectTriggered()),
69 SLOT(showStationSelectView()));
73 qDebug() << "found" << stationListModel->rowCount() << "stations";
75 connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
77 if (recentStations.isEmpty() || !stationViewPreferred) {
78 stationListView->show();
80 queryStation(recentStations.front());
88 delete stationListView;
92 void App::downloadFinished(void)
94 disconnect(stationQueryReply, SIGNAL(finished()),
95 this, SLOT(downloadFinished()));
96 stationView->updateView(stationQueryReply->readAll());
97 stationListView->hide();
98 stationQueryReply->deleteLater();
99 stationQueryReply = 0;
101 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
105 void App::queryStation(const QString &station)
107 QNetworkRequest request;
108 request.setUrl(queryBaseUrl);
109 const QString queryString = "stazione=" + station;
110 const QByteArray query(queryString.toLocal8Bit());
111 stationQueryReply = accessManager->post(request, query);
112 connect(stationQueryReply, SIGNAL(finished()),
113 this, SLOT(downloadFinished()));
114 recentStations.push_front(station);
115 recentStations.removeDuplicates();
116 if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
117 recentStations.pop_back();
120 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
124 void App::updateStation()
126 qDebug() << "updating station data";
127 if (!recentStations.isEmpty() && !stationListView->isVisible()) {
128 queryStation(recentStations.front());
132 void App::showSettingsDialog()
134 qDebug() << "Settings Dialog called";
136 SettingsDialog *dialog = new SettingsDialog(stationView);
137 if (dialog->exec() == QDialog::Accepted) {
143 void App::showAboutDialog()
145 qDebug() << "About Dialog called";
146 QString name = QApplication::instance()->applicationName();
147 QString version = QApplication::instance()->applicationVersion();
148 QString aboutText = QString(
149 tr("<p>%1 version %2</p>"
150 "<p>Copyright (c) 2010, 2011</p>"
151 "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
152 "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
153 QMessageBox::about(stationView, name, aboutText);
156 void App::showStationSelectView(void)
158 stationListView->show();
161 void App::readSettings(void)
164 queryBaseUrl = settings.value("QueryURL",
165 "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
166 stationView->setBaseUrl(queryBaseUrl);
168 recentStations = settings.value("RecentStations").toString().split(",");
169 qDebug() << "RecentStations:" << recentStations;
171 stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
172 qDebug() << "StationsViewPreferred:" << stationViewPreferred;
174 checkingInterval = settings.value("CheckInterval", 0).toInt();
175 qDebug() << "CheckInterval:" << checkingInterval;
178 I would use > 0 here, but people may have an old settings file with a 2
179 seconds timeout which is way too short.
180 As a workaround I consider anything less than 30 seconds as too short
181 and disable the timer.
183 if (checkingInterval > 30000) {
184 checkingTimer->setInterval(checkingInterval);
185 checkingTimer->start();
187 checkingTimer->setInterval(-1);
188 checkingTimer->stop();
192 void App::saveSettings(void)
196 qDebug() << "Saving Settings to" << settings.fileName();
198 settings.setValue("QueryURL", queryBaseUrl);
199 settings.setValue("RecentStations", recentStations.join(","));
200 settings.setValue("CheckInterval", checkingInterval);
201 settings.setValue("StationViewPreferred", stationViewPreferred);
204 QString App::dataDir(void)
206 return QString(DATADIR);