Fix crash when running on the desktop
[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 <QUrl>
36
37 #include <QGeoPositionInfoSource>
38
39 // Constants
40 static const int RECENT_STATIONS_MAX_COUNT = 10;
41
42 QTM_USE_NAMESPACE
43
44 App::App(QObject *parent) :
45     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))
51 {
52     stationListModel->load(dataDir() + "stations/stations.qpl");
53
54     if (positionInfoSource) {
55         connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
56                 stationListView, SLOT(updatePosition(QGeoPositionInfo)));
57     }
58     connect(stationListView, SIGNAL(stationSelected(const QString &)),
59             SLOT(queryStation(const QString &)));
60
61     connect(stationListView, SIGNAL(aboutTriggered()),
62             SLOT(showAboutDialog()));
63     connect(stationView, SIGNAL(aboutTriggered()),
64             SLOT(showAboutDialog()));
65
66     connect(stationListView, SIGNAL(settingsChangeRequested()),
67             SLOT(showSettingsDialog()));
68     connect(stationView, SIGNAL(settingsChangeRequested()),
69             SLOT(showSettingsDialog()));
70
71     connect(stationView, SIGNAL(stationListSelectTriggered()),
72             SLOT(showStationSelectView()));
73
74     readSettings();
75
76     qDebug() << "found" << stationListModel->rowCount() << "stations";
77     stationView->show();
78     if (recentStations.isEmpty() || !stationViewPreferred) {
79         stationListView->show();
80     } else {
81         queryStation(recentStations.front());
82     }
83
84     // Testing only: start updates rigt away.
85     if (positionInfoSource) {
86         positionInfoSource->startUpdates();
87     }
88 }
89
90 App::~App()
91 {
92     delete stationView;
93     saveSettings();
94 }
95
96 void App::downloadFinished(void)
97 {
98     disconnect(stationQueryReply, SIGNAL(finished()),
99                this, SLOT(downloadFinished()));
100     stationView->updateView(stationQueryReply->readAll());
101     stationListView->hide();
102     stationQueryReply->deleteLater();
103     stationQueryReply = 0;
104 #ifdef Q_WS_MAEMO_5
105     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
106 #endif
107 }
108
109 void App::queryStation(const QString &station)
110 {
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();
122     }
123 #ifdef Q_WS_MAEMO_5
124     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
125 #endif
126 }
127
128 void App::showSettingsDialog()
129 {
130     qDebug() << "Settings Dialog called";
131
132     SettingsDialog *dialog = new SettingsDialog(stationView);
133     if (dialog->exec() == QDialog::Accepted) {
134         readSettings();
135     }
136     delete dialog;
137 }
138
139 void App::showAboutDialog()
140 {
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);
150 }
151
152 void App::showStationSelectView(void)
153 {
154     stationListView->show();
155 }
156
157 void App::readSettings(void)
158 {
159     QSettings settings;
160     queryBaseUrl = settings.value("QueryURL",
161                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
162     stationView->setBaseUrl(queryBaseUrl);
163
164     recentStations = settings.value("RecentStations").toString().split(",");
165     checkingInterval = settings.value("CheckInterval", 2000).toInt();
166     stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
167 }
168
169 void App::saveSettings(void)
170 {
171     QSettings settings;
172
173     qDebug() << "Saving Settings to" << settings.fileName();
174
175     settings.setValue("QueryURL", queryBaseUrl);
176     settings.setValue("RecentStations", recentStations.join(","));
177     settings.setValue("CheckInterval", checkingInterval);
178     settings.setValue("StationViewPreferred", stationViewPreferred);
179 }
180
181 QString App::dataDir(void)
182 {
183 #ifdef Q_WS_MAEMO_5
184     return QString("/opt/usr/share/apps/quandoparte/");
185 #else
186     return QString("/usr/share/apps/quandoparte/");
187 #endif
188 }