Bump version to 0.9.0
[quandoparte] / application / app.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
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.
9
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.
14
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.
19
20 */
21
22 #include "app.h"
23 #include "dataprovider.h"
24 #include "stationview.h"
25 #include "stationlistmodel.h"
26 #include "stationlistview.h"
27 #include "settingsdialog.h"
28 #include "settings.h"
29
30 #include <QDebug>
31 #include <QMessageBox>
32 #include <QNetworkAccessManager>
33 #include <QNetworkReply>
34 #include <QNetworkRequest>
35 #include <QObject>
36 #include <QSettings>
37 #include <QTimer>
38 #include <QUrl>
39
40 #include <QGeoPositionInfoSource>
41
42 // Constants
43 static const int RECENT_STATIONS_MAX_COUNT = 10;
44
45 QTM_USE_NAMESPACE
46
47 App::App(QObject *parent) :
48     QObject(parent),
49     dataProvider(new DataProvider(this)),
50     checkingTimer(new QTimer(this)),
51     stationView(new StationView()),
52     stationListModel(new StationListModel(this)),
53     stationListView(new StationListView(stationListModel, stationView))
54 {
55     stationListModel->load("stations:stations.qpl");
56
57     connect(dataProvider, SIGNAL(stationScheduleReady(QByteArray, QUrl)),
58             SLOT(downloadFinished(QByteArray)));
59
60     connect(stationListView, SIGNAL(stationSelected(QString)),
61             SLOT(queryStation(QString)));
62
63     connect(stationListView, SIGNAL(aboutTriggered()),
64             SLOT(showAboutDialog()));
65     connect(stationView, SIGNAL(aboutTriggered()),
66             SLOT(showAboutDialog()));
67
68     connect(stationListView, SIGNAL(settingsChangeRequested()),
69             SLOT(showSettingsDialog()));
70     connect(stationView, SIGNAL(settingsChangeRequested()),
71             SLOT(showSettingsDialog()));
72
73     connect(stationView, SIGNAL(stationListSelectTriggered()),
74             SLOT(showStationSelectView()));
75
76     readSettings();
77
78     qDebug() << "found" << stationListModel->rowCount() << "stations";
79
80     connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
81     stationView->show();
82     Settings *settings = Settings::instance();
83     if (settings->recentStations().isEmpty() || !settings->stationViewPreferred()) {
84         stationListView->show();
85     } else {
86         updateStation();
87     }
88 }
89
90 App::~App()
91 {
92     saveSettings();
93     disconnect();
94     delete stationView;
95 }
96
97 void App::downloadFinished(const QByteArray &data)
98 {
99     stationView->updateView(data);
100     stationListView->hide();
101 #ifdef Q_WS_MAEMO_5
102     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
103 #endif
104 }
105
106 void App::queryStation(const QString &station)
107 {
108     dataProvider->stationSchedule(station);
109 #ifdef Q_WS_MAEMO_5
110     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
111 #endif
112 }
113
114 void App::updateStation()
115 {
116     if (!stationListView->isVisible()) {
117         dataProvider->updateStation();
118     }
119 }
120
121 void App::showSettingsDialog()
122 {
123     qDebug() << "Settings Dialog called";
124
125     SettingsDialog *dialog = new SettingsDialog(stationView);
126     if (dialog->exec() == QDialog::Accepted) {
127         readSettings();
128     }
129     delete dialog;
130 }
131
132 void App::showAboutDialog()
133 {
134     qDebug() << "About Dialog called";
135     QString name = QApplication::instance()->applicationName();
136     QString version = QApplication::instance()->applicationVersion();
137     QString aboutText = QString(
138                 tr("<h2>"
139                    "<a href='http://quandoparte.garage.maemo.org'>%1</a> version %2"
140                    "</h2>"
141                    "<p>Copyright (c) 2010, 2011</p>"
142                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
143                    "<p>Licensed under the GNU Public License v2 or above</p>"
144                    "<p>Station geolocation data from "
145                    "<a href='http://www.openstreetmap.org'>OpenStreetMap</a>"
146                    "</p>"
147                    "<p>Realtime train data from "
148                    "<a href='http://mobile.viaggiatreno.it'>Viaggiatreno</a>"
149                    "</p>")).arg(name).arg(version);
150     QMessageBox::about(stationView, name, aboutText);
151 }
152
153 void App::showStationSelectView(void)
154 {
155     stationListView->show();
156 }
157
158 void App::readSettings(void)
159 {
160     Settings *settings = Settings::instance();
161     stationView->setBaseUrl(settings->queryBaseUrl() + "stazione");
162
163     /*
164        I would use > 0 here, but people may have an old settings file with a 2
165        seconds timeout which is way too short.
166        As a workaround I consider anything less than 30 seconds as too short
167        and disable the timer.
168     */
169     if (settings->checkingInterval() > 30000) {
170         checkingTimer->setInterval(settings->checkingInterval());
171         checkingTimer->start();
172     } else {
173         checkingTimer->setInterval(-1);
174         checkingTimer->stop();
175     }
176 }
177
178 void App::saveSettings(void)
179 {
180     Settings::instance()->save();
181 }
182
183 QString App::dataDir(void)
184 {
185     return QString(DATADIR);
186 }