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 stationView(new StationView()),
48 stationListModel(new StationListModel(this)),
49 stationListView(new StationListView(stationListModel, stationView))
51 stationListModel->load("stations:stations.qpl");
53 connect(stationListView, SIGNAL(stationSelected(const QString &)),
54 SLOT(queryStation(const QString &)));
56 connect(stationListView, SIGNAL(aboutTriggered()),
57 SLOT(showAboutDialog()));
58 connect(stationView, SIGNAL(aboutTriggered()),
59 SLOT(showAboutDialog()));
61 connect(stationListView, SIGNAL(settingsChangeRequested()),
62 SLOT(showSettingsDialog()));
63 connect(stationView, SIGNAL(settingsChangeRequested()),
64 SLOT(showSettingsDialog()));
66 connect(stationView, SIGNAL(stationListSelectTriggered()),
67 SLOT(showStationSelectView()));
71 qDebug() << "found" << stationListModel->rowCount() << "stations";
73 if (recentStations.isEmpty() || !stationViewPreferred) {
74 stationListView->show();
76 queryStation(recentStations.front());
86 void App::downloadFinished(void)
88 disconnect(stationQueryReply, SIGNAL(finished()),
89 this, SLOT(downloadFinished()));
90 stationView->updateView(stationQueryReply->readAll());
91 stationListView->hide();
92 stationQueryReply->deleteLater();
93 stationQueryReply = 0;
95 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
99 void App::queryStation(const QString &station)
101 QNetworkRequest request;
102 request.setUrl(queryBaseUrl);
103 const QString queryString = "stazione=" + station;
104 const QByteArray query(queryString.toLocal8Bit());
105 stationQueryReply = accessManager->post(request, query);
106 connect(stationQueryReply, SIGNAL(finished()),
107 this, SLOT(downloadFinished()));
108 recentStations.push_front(station);
109 recentStations.removeDuplicates();
110 if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
111 recentStations.pop_back();
114 stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
118 void App::showSettingsDialog()
120 qDebug() << "Settings Dialog called";
122 SettingsDialog *dialog = new SettingsDialog(stationView);
123 if (dialog->exec() == QDialog::Accepted) {
129 void App::showAboutDialog()
131 qDebug() << "About Dialog called";
132 QString name = QApplication::instance()->applicationName();
133 QString version = QApplication::instance()->applicationVersion();
134 QString aboutText = QString(
135 tr("<p>%1 version %2</p>"
136 "<p>Copyright (c) 2010, 2011</p>"
137 "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
138 "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
139 QMessageBox::about(stationView, name, aboutText);
142 void App::showStationSelectView(void)
144 stationListView->show();
147 void App::readSettings(void)
150 queryBaseUrl = settings.value("QueryURL",
151 "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
152 stationView->setBaseUrl(queryBaseUrl);
154 recentStations = settings.value("RecentStations").toString().split(",");
155 checkingInterval = settings.value("CheckInterval", 2000).toInt();
156 stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
159 void App::saveSettings(void)
163 qDebug() << "Saving Settings to" << settings.fileName();
165 settings.setValue("QueryURL", queryBaseUrl);
166 settings.setValue("RecentStations", recentStations.join(","));
167 settings.setValue("CheckInterval", checkingInterval);
168 settings.setValue("StationViewPreferred", stationViewPreferred);
171 QString App::dataDir(void)
173 return QString(DATADIR);