Use the new Settings class
[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 "stationview.h"
24 #include "stationlistmodel.h"
25 #include "stationlistview.h"
26 #include "settingsdialog.h"
27 #include "settings.h"
28
29 #include <QDebug>
30 #include <QMessageBox>
31 #include <QNetworkAccessManager>
32 #include <QNetworkReply>
33 #include <QNetworkRequest>
34 #include <QObject>
35 #include <QSettings>
36 #include <QTimer>
37 #include <QUrl>
38
39 #include <QGeoPositionInfoSource>
40
41 // Constants
42 static const int RECENT_STATIONS_MAX_COUNT = 10;
43
44 QTM_USE_NAMESPACE
45
46 App::App(QObject *parent) :
47     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))
53 {
54     stationListModel->load("stations:stations.qpl");
55
56     connect(stationListView, SIGNAL(stationSelected(const QString &)),
57             SLOT(queryStation(const QString &)));
58
59     connect(stationListView, SIGNAL(aboutTriggered()),
60             SLOT(showAboutDialog()));
61     connect(stationView, SIGNAL(aboutTriggered()),
62             SLOT(showAboutDialog()));
63
64     connect(stationListView, SIGNAL(settingsChangeRequested()),
65             SLOT(showSettingsDialog()));
66     connect(stationView, SIGNAL(settingsChangeRequested()),
67             SLOT(showSettingsDialog()));
68
69     connect(stationView, SIGNAL(stationListSelectTriggered()),
70             SLOT(showStationSelectView()));
71
72     readSettings();
73
74     qDebug() << "found" << stationListModel->rowCount() << "stations";
75
76     connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
77     stationView->show();
78     Settings *settings = Settings::instance();
79     if (settings->recentStations().isEmpty() || !settings->stationViewPreferred()) {
80         stationListView->show();
81     } else {
82         queryStation(settings->recentStations().front());
83     }
84 }
85
86 App::~App()
87 {
88     saveSettings();
89     disconnect();
90     delete stationView;
91 }
92
93 void App::downloadFinished(void)
94 {
95     disconnect(stationQueryReply, SIGNAL(finished()),
96                this, SLOT(downloadFinished()));
97     stationView->updateView(stationQueryReply->readAll());
98     stationListView->hide();
99     stationQueryReply->deleteLater();
100     stationQueryReply = 0;
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     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();
120     }
121 #ifdef Q_WS_MAEMO_5
122     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
123 #endif
124 }
125
126 void App::updateStation()
127 {
128     Settings *settings = Settings::instance();
129
130     qDebug() << "updating station data";
131     if (!settings->recentStations().isEmpty() && !stationListView->isVisible()) {
132         queryStation(settings->recentStations().front());
133     }
134 }
135
136 void App::showSettingsDialog()
137 {
138     qDebug() << "Settings Dialog called";
139
140     SettingsDialog *dialog = new SettingsDialog(stationView);
141     if (dialog->exec() == QDialog::Accepted) {
142         readSettings();
143     }
144     delete dialog;
145 }
146
147 void App::showAboutDialog()
148 {
149     qDebug() << "About Dialog called";
150     QString name = QApplication::instance()->applicationName();
151     QString version = QApplication::instance()->applicationVersion();
152     QString aboutText = QString(
153                 tr("<h2>"
154                    "<a href='http://quandoparte.garage.maemo.org'>%1</a> version %2"
155                    "</h2>"
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>"
161                    "</p>"
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);
166 }
167
168 void App::showStationSelectView(void)
169 {
170     stationListView->show();
171 }
172
173 void App::readSettings(void)
174 {
175     Settings *settings = Settings::instance();
176     stationView->setBaseUrl(settings->queryBaseUrl());
177
178     /*
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.
183     */
184     if (settings->checkingInterval() > 30000) {
185         checkingTimer->setInterval(settings->checkingInterval());
186         checkingTimer->start();
187     } else {
188         checkingTimer->setInterval(-1);
189         checkingTimer->stop();
190     }
191 }
192
193 void App::saveSettings(void)
194 {
195     Settings::instance()->save();
196 }
197
198 QString App::dataDir(void)
199 {
200     return QString(DATADIR);
201 }