Implement Position tracking
[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 QTM_USE_NAMESPACE
40
41 App::App(QObject *parent) :
42     QObject(parent),
43     accessManager(new QNetworkAccessManager(this)),
44     positionInfoSource(QGeoPositionInfoSource::createDefaultSource(this)),
45     stationView(new StationView()),
46     stationListModel(new StationListModel(this)),
47     stationListView(new StationListView(stationListModel, stationView))
48 {
49     stationListModel->load(dataDir() + "stations/stations.qpl");
50
51     connect(positionInfoSource, SIGNAL(positionUpdated(QGeoPositionInfo)),
52             stationListView, SLOT(updatePosition(QGeoPositionInfo)));
53     connect(stationListView, SIGNAL(stationSelected(const QString &)),
54             SLOT(queryStation(const QString &)));
55     connect(stationListView, SIGNAL(aboutTriggered()),
56             SLOT(showAboutDialog()));
57     connect(stationView, SIGNAL(aboutTriggered()),
58             SLOT(showAboutDialog()));
59     connect(stationView, SIGNAL(stationListSelectTriggered()),
60             SLOT(showStationSelectView()));
61
62     connect(stationView, SIGNAL(showingArrivalsChanged(bool)),
63             SLOT(setShowingArrivals(bool)));
64     readSettings();
65
66     qDebug() << "found" << stationListModel->rowCount() << "stations";
67 #if defined(Q_WS_S60)
68     stationView->showMaximized();
69 #else
70     stationView->show();
71 #endif
72
73     if (stationName.isEmpty()) {
74 #if defined(Q_WS_S60)
75         stationListView->showMaximized();
76 #else
77         stationListView->show();
78 #endif
79     }
80
81     // Testing only: start updates rigt away.
82     positionInfoSource->startUpdates();
83 }
84
85 App::~App()
86 {
87     delete stationView;
88     saveSettings();
89 }
90
91 void App::downloadFinished(void)
92 {
93     disconnect(stationQueryReply, SIGNAL(finished()),
94                this, SLOT(downloadFinished()));
95     stationView->updateView(stationQueryReply->readAll());
96     stationListView->hide();
97     stationQueryReply->deleteLater();
98     stationQueryReply = 0;
99 #ifdef Q_WS_MAEMO_5
100     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
101 #endif
102 }
103
104 void App::queryStation(const QString &station)
105 {
106     QNetworkRequest request;
107     request.setUrl(queryBaseUrl);
108     const QString queryString = "stazione=" + station;
109     const QByteArray query(queryString.toLocal8Bit());
110     stationQueryReply = accessManager->post(request, query);
111     connect(stationQueryReply, SIGNAL(finished()),
112             this, SLOT(downloadFinished()));
113 #ifdef Q_WS_MAEMO_5
114     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
115 #endif
116 }
117
118 void App::showSettingsDialog()
119 {
120 }
121
122 void App::showAboutDialog()
123 {
124     qDebug() << "About Dialog called";
125     QString name = QApplication::instance()->applicationName();
126     QString version = QApplication::instance()->applicationVersion();
127     QString aboutText = QString(
128                 tr("<p>%1 version %2</p>"
129                    "<p>Copyright (c) 2010, 2011</p>"
130                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
131                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
132     QMessageBox::about(stationView, name, aboutText);
133 }
134
135 void App::showStationSelectView(void)
136 {
137     stationListView->show();
138 }
139
140 void App::readSettings(void)
141 {
142     QSettings settings;
143     queryBaseUrl = settings.value("QueryURL",
144                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
145     stationView->setBaseUrl(queryBaseUrl);
146
147     stationName = settings.value("CurrentStation").toString();
148     showingArrivals = settings.value("ShowingArrivals", false).toBool();
149     checkingInterval = settings.value("CheckInterval", 2000).toInt();
150 }
151
152 void App::saveSettings(void)
153 {
154     QSettings settings;
155     settings.setValue("QueryURL", queryBaseUrl);
156     settings.value("CurrentStation", stationName);
157     settings.value("ShowingArrivals", showingArrivals);
158     settings.value("CheckInterval", checkingInterval);
159 }
160
161 void App::setShowingArrivals(bool showArrivals)
162 {
163     showingArrivals = showArrivals;
164 }
165
166 QString App::dataDir(void)
167 {
168 #ifdef Q_WS_MAEMO_5
169     return QString("/opt/usr/share/apps/quandoparte/");
170 #else
171     return QString("/usr/share/apps/quandoparte/");
172 #endif
173 }