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>
37 #include <QGeoPositionInfoSource>
40 static const int RECENT_STATIONS_MAX_COUNT = 10;
44 App::App(QObject *parent) :
46 accessManager(new QNetworkAccessManager(this)),
47 positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
48 stationView(new StationView()),
49 stationListModel(new StationListModel(this)),
50 stationListView(new StationListView(stationListModel, stationView))
52 stationListModel->load(dataDir() + "stations/stations.qpl");
54 if (positionInfoSource) {
55 connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
56 stationListView, SLOT(updatePosition(QGeoPositionInfo)));
58 connect(stationListView, SIGNAL(stationSelected(const QString &)),
59 SLOT(queryStation(const QString &)));
61 connect(stationListView, SIGNAL(aboutTriggered()),
62 SLOT(showAboutDialog()));
63 connect(stationView, SIGNAL(aboutTriggered()),
64 SLOT(showAboutDialog()));
66 connect(stationListView, SIGNAL(settingsChangeRequested()),
67 SLOT(showSettingsDialog()));
68 connect(stationView, SIGNAL(settingsChangeRequested()),
69 SLOT(showSettingsDialog()));
71 connect(stationView, SIGNAL(stationListSelectTriggered()),
72 SLOT(showStationSelectView()));
76 qDebug() << "found" << stationListModel->rowCount() << "stations";
78 if (recentStations.isEmpty() || !stationViewPreferred) {
79 stationListView->show();
81 queryStation(recentStations.front());
84 // Testing only: start updates rigt away.
85 if (positionInfoSource) {
86 positionInfoSource->startUpdates();
96 void App::downloadFinished(void)
98 disconnect(stationQueryReply, SIGNAL(finished()),
99 this, SLOT(downloadFinished()));
100 stationView->updateView(stationQueryReply->readAll());
101 stationListView->hide();
102 stationQueryReply->deleteLater();
103 stationQueryReply = 0;
105 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
109 void App::queryStation(const QString &station)
111 QNetworkRequest request;
112 request.setUrl(queryBaseUrl);
113 const QString queryString = "stazione=" + station;
114 const QByteArray query(queryString.toLocal8Bit());
115 stationQueryReply = accessManager->post(request, query);
116 connect(stationQueryReply, SIGNAL(finished()),
117 this, SLOT(downloadFinished()));
118 recentStations.push_front(station);
119 recentStations.removeDuplicates();
120 if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
121 recentStations.pop_back();
124 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
128 void App::showSettingsDialog()
130 qDebug() << "Settings Dialog called";
132 SettingsDialog *dialog = new SettingsDialog(stationView);
133 if (dialog->exec() == QDialog::Accepted) {
139 void App::showAboutDialog()
141 qDebug() << "About Dialog called";
142 QString name = QApplication::instance()->applicationName();
143 QString version = QApplication::instance()->applicationVersion();
144 QString aboutText = QString(
145 tr("<p>%1 version %2</p>"
146 "<p>Copyright (c) 2010, 2011</p>"
147 "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
148 "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
149 QMessageBox::about(stationView, name, aboutText);
152 void App::showStationSelectView(void)
154 stationListView->show();
157 void App::readSettings(void)
160 queryBaseUrl = settings.value("QueryURL",
161 "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
162 stationView->setBaseUrl(queryBaseUrl);
164 recentStations = settings.value("RecentStations").toString().split(",");
165 checkingInterval = settings.value("CheckInterval", 2000).toInt();
166 stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
169 void App::saveSettings(void)
173 qDebug() << "Saving Settings to" << settings.fileName();
175 settings.setValue("QueryURL", queryBaseUrl);
176 settings.setValue("RecentStations", recentStations.join(","));
177 settings.setValue("CheckInterval", checkingInterval);
178 settings.setValue("StationViewPreferred", stationViewPreferred);
181 QString App::dataDir(void)
184 return QString("/opt/usr/share/apps/quandoparte/");
186 return QString("/usr/share/apps/quandoparte/");