Added periodic check feature.
[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
28 #include <QDebug>
29 #include <QMessageBox>
30 #include <QNetworkAccessManager>
31 #include <QNetworkReply>
32 #include <QNetworkRequest>
33 #include <QObject>
34 #include <QSettings>
35 #include <QTimer>
36 #include <QUrl>
37
38 #include <QGeoPositionInfoSource>
39
40 // Constants
41 static const int RECENT_STATIONS_MAX_COUNT = 10;
42
43 QTM_USE_NAMESPACE
44
45 App::App(QObject *parent) :
46     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))
52 {
53     stationListModel->load("stations:stations.qpl");
54
55     connect(stationListView, SIGNAL(stationSelected(const QString &)),
56             SLOT(queryStation(const QString &)));
57
58     connect(stationListView, SIGNAL(aboutTriggered()),
59             SLOT(showAboutDialog()));
60     connect(stationView, SIGNAL(aboutTriggered()),
61             SLOT(showAboutDialog()));
62
63     connect(stationListView, SIGNAL(settingsChangeRequested()),
64             SLOT(showSettingsDialog()));
65     connect(stationView, SIGNAL(settingsChangeRequested()),
66             SLOT(showSettingsDialog()));
67
68     connect(stationView, SIGNAL(stationListSelectTriggered()),
69             SLOT(showStationSelectView()));
70
71     readSettings();
72
73     qDebug() << "found" << stationListModel->rowCount() << "stations";
74
75     connect(checkingTimer, SIGNAL(timeout()), SLOT(updateStation()));
76     stationView->show();
77     if (recentStations.isEmpty() || !stationViewPreferred) {
78         stationListView->show();
79     } else {
80         queryStation(recentStations.front());
81     }
82 }
83
84 App::~App()
85 {
86     disconnect();
87     delete stationView;
88     delete stationListView;
89     saveSettings();
90 }
91
92 void App::downloadFinished(void)
93 {
94     disconnect(stationQueryReply, SIGNAL(finished()),
95                this, SLOT(downloadFinished()));
96     stationView->updateView(stationQueryReply->readAll());
97     stationListView->hide();
98     stationQueryReply->deleteLater();
99     stationQueryReply = 0;
100 #ifdef Q_WS_MAEMO_5
101     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
102 #endif
103 }
104
105 void App::queryStation(const QString &station)
106 {
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();
118     }
119 #ifdef Q_WS_MAEMO_5
120     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
121 #endif
122 }
123
124 void App::updateStation()
125 {
126     qDebug() << "updating station data";
127     if (!recentStations.isEmpty() && !stationListView->isVisible()) {
128         queryStation(recentStations.front());
129     }
130 }
131
132 void App::showSettingsDialog()
133 {
134     qDebug() << "Settings Dialog called";
135
136     SettingsDialog *dialog = new SettingsDialog(stationView);
137     if (dialog->exec() == QDialog::Accepted) {
138         readSettings();
139     }
140     delete dialog;
141 }
142
143 void App::showAboutDialog()
144 {
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);
154 }
155
156 void App::showStationSelectView(void)
157 {
158     stationListView->show();
159 }
160
161 void App::readSettings(void)
162 {
163     QSettings settings;
164     queryBaseUrl = settings.value("QueryURL",
165                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
166     stationView->setBaseUrl(queryBaseUrl);
167
168     recentStations = settings.value("RecentStations").toString().split(",");
169     qDebug() << "RecentStations:" << recentStations;
170
171     stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
172     qDebug() << "StationsViewPreferred:" << stationViewPreferred;
173
174     checkingInterval = settings.value("CheckInterval", 0).toInt();
175     qDebug() << "CheckInterval:" << checkingInterval;
176
177     /*
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.
182     */
183     if (checkingInterval > 30000) {
184         checkingTimer->setInterval(checkingInterval);
185         checkingTimer->start();
186     } else {
187         checkingTimer->setInterval(-1);
188         checkingTimer->stop();
189     }
190 }
191
192 void App::saveSettings(void)
193 {
194     QSettings settings;
195
196     qDebug() << "Saving Settings to" << settings.fileName();
197
198     settings.setValue("QueryURL", queryBaseUrl);
199     settings.setValue("RecentStations", recentStations.join(","));
200     settings.setValue("CheckInterval", checkingInterval);
201     settings.setValue("StationViewPreferred", stationViewPreferred);
202 }
203
204 QString App::dataDir(void)
205 {
206     return QString(DATADIR);
207 }