Implemented Arrival/Departures selection
[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 "stationlistview.h"
25 #include "settingsdialog.h"
26
27 #include <QDebug>
28 #include <QMessageBox>
29 #include <QNetworkAccessManager>
30 #include <QNetworkReply>
31 #include <QNetworkRequest>
32 #include <QObject>
33 #include <QSettings>
34 #include <QUrl>
35
36 App::App(QObject *parent) :
37     QObject(parent),
38     accessManager(new QNetworkAccessManager(this)),
39     stationView(new StationView(NULL)),
40     stationListView(new StationListView(stationView))
41 {
42     connect(stationListView, SIGNAL(stationSelected(const QString &)),
43             SLOT(queryStation(const QString &)));
44     connect(stationView, SIGNAL(aboutTriggered()),
45             SLOT(showAboutDialog()));
46     connect(stationView, SIGNAL(stationListSelectTriggered()),
47             SLOT(showStationSelectView()));
48
49     connect(stationView, SIGNAL(showingArrivalsChanged(bool)),
50             SLOT(setShowingArrivals(bool)));
51     readSettings();
52
53 #if defined(Q_WS_S60)
54     stationView->showMaximized();
55 #else
56     stationView->show();
57 #endif
58
59     if (stationName.isEmpty()) {
60 #if defined(Q_WS_S60)
61         stationListView->showMaximized();
62 #else
63         stationListView->show();
64 #endif
65     }
66 }
67
68 App::~App()
69 {
70     saveSettings();
71 }
72
73 void App::downloadFinished(void)
74 {
75     disconnect(stationQueryReply, SIGNAL(finished()),
76                this, SLOT(downloadFinished()));
77     stationView->updateView(stationQueryReply->readAll());
78     stationListView->hide();
79     stationQueryReply->deleteLater();
80     stationQueryReply = 0;
81 }
82
83 void App::queryStation(const QString &station)
84 {
85     QNetworkRequest request;
86     request.setUrl(queryBaseUrl);
87     const QString queryString = "stazione=" + station;
88     const QByteArray query(queryString.toLocal8Bit());
89     stationQueryReply = accessManager->post(request, query);
90     connect(stationQueryReply, SIGNAL(finished()),
91             this, SLOT(downloadFinished()));
92 }
93
94 void App::showSettingsDialog()
95 {
96 }
97
98 void App::showAboutDialog()
99 {
100     qDebug() << "About Dialog called";
101     QString name = QApplication::instance()->applicationName();
102     QString aboutText = QString(
103                 tr("<p>%1</p>"
104                    "<p>Copyright (c) 2010</p>"
105                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
106                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name);
107     QMessageBox::about(stationView, name, aboutText);
108 }
109
110 void App::showStationSelectView(void)
111 {
112     stationListView->show();
113 }
114
115 void App::readSettings(void)
116 {
117     QSettings settings;
118     queryBaseUrl = settings.value("QueryURL",
119                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
120     stationView->setBaseUrl(queryBaseUrl);
121
122     stationName = settings.value("CurrentStation").toString();
123     showingArrivals = settings.value("ShowingArrivals", false).toBool();
124     checkingInterval = settings.value("CheckInterval", 2000).toInt();
125 }
126
127 void App::saveSettings(void)
128 {
129     QSettings settings;
130     settings.setValue("QueryURL", queryBaseUrl);
131     settings.value("CurrentStation", stationName);
132     settings.value("ShowingArrivals", showingArrivals);
133     settings.value("CheckInterval", checkingInterval);
134 }
135
136 void App::setShowingArrivals(bool showArrivals)
137 {
138     showingArrivals = showArrivals;
139 }