Fixed Settings Dialog
[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     connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
55             stationListView, SLOT(updatePosition(QGeoPositionInfo)));
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     stationView->show();
76     if (recentStations.isEmpty() || !stationViewPreferred) {
77         stationListView->show();
78     } else {
79         queryStation(recentStations.front());
80     }
81
82     // Testing only: start updates rigt away.
83     positionInfoSource->startUpdates();
84 }
85
86 App::~App()
87 {
88     delete stationView;
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::showSettingsDialog()
125 {
126     qDebug() << "Settings Dialog called";
127
128     SettingsDialog *dialog = new SettingsDialog(stationView);
129     if (dialog->exec() == QDialog::Accepted) {
130         readSettings();
131     }
132     delete dialog;
133 }
134
135 void App::showAboutDialog()
136 {
137     qDebug() << "About Dialog called";
138     QString name = QApplication::instance()->applicationName();
139     QString version = QApplication::instance()->applicationVersion();
140     QString aboutText = QString(
141                 tr("<p>%1 version %2</p>"
142                    "<p>Copyright (c) 2010, 2011</p>"
143                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
144                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
145     QMessageBox::about(stationView, name, aboutText);
146 }
147
148 void App::showStationSelectView(void)
149 {
150     stationListView->show();
151 }
152
153 void App::readSettings(void)
154 {
155     QSettings settings;
156     queryBaseUrl = settings.value("QueryURL",
157                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
158     stationView->setBaseUrl(queryBaseUrl);
159
160     recentStations = settings.value("RecentStations").toString().split(",");
161     checkingInterval = settings.value("CheckInterval", 2000).toInt();
162     stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
163 }
164
165 void App::saveSettings(void)
166 {
167     QSettings settings;
168
169     qDebug() << "Saving Settings to" << settings.fileName();
170
171     settings.setValue("QueryURL", queryBaseUrl);
172     settings.setValue("RecentStations", recentStations.join(","));
173     settings.setValue("CheckInterval", checkingInterval);
174     settings.setValue("StationViewPreferred", stationViewPreferred);
175 }
176
177 QString App::dataDir(void)
178 {
179 #ifdef Q_WS_MAEMO_5
180     return QString("/opt/usr/share/apps/quandoparte/");
181 #else
182     return QString("/usr/share/apps/quandoparte/");
183 #endif
184 }