d248f10a21b4b0fab17884bf82cd1f06abbb66bd
[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(stationListView, SIGNAL(aboutTriggered()),
45             SLOT(showAboutDialog()));
46     connect(stationView, SIGNAL(aboutTriggered()),
47             SLOT(showAboutDialog()));
48     connect(stationView, SIGNAL(stationListSelectTriggered()),
49             SLOT(showStationSelectView()));
50
51     connect(stationView, SIGNAL(showingArrivalsChanged(bool)),
52             SLOT(setShowingArrivals(bool)));
53     readSettings();
54
55 #if defined(Q_WS_S60)
56     stationView->showMaximized();
57 #else
58     stationView->show();
59 #endif
60
61     if (stationName.isEmpty()) {
62 #if defined(Q_WS_S60)
63         stationListView->showMaximized();
64 #else
65         stationListView->show();
66 #endif
67     }
68 }
69
70 App::~App()
71 {
72     saveSettings();
73 }
74
75 void App::downloadFinished(void)
76 {
77     disconnect(stationQueryReply, SIGNAL(finished()),
78                this, SLOT(downloadFinished()));
79     stationView->updateView(stationQueryReply->readAll());
80     stationListView->hide();
81     stationQueryReply->deleteLater();
82     stationQueryReply = 0;
83 #ifdef Q_WS_MAEMO_5
84     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
85 #endif
86 }
87
88 void App::queryStation(const QString &station)
89 {
90     QNetworkRequest request;
91     request.setUrl(queryBaseUrl);
92     const QString queryString = "stazione=" + station;
93     const QByteArray query(queryString.toLocal8Bit());
94     stationQueryReply = accessManager->post(request, query);
95     connect(stationQueryReply, SIGNAL(finished()),
96             this, SLOT(downloadFinished()));
97 #ifdef Q_WS_MAEMO_5
98     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
99 #endif
100 }
101
102 void App::showSettingsDialog()
103 {
104 }
105
106 void App::showAboutDialog()
107 {
108     qDebug() << "About Dialog called";
109     QString name = QApplication::instance()->applicationName();
110     QString version = QApplication::instance()->applicationVersion();
111     QString aboutText = QString(
112                 tr("<p>%1 version %2</p>"
113                    "<p>Copyright (c) 2010</p>"
114                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
115                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
116     QMessageBox::about(stationView, name, aboutText);
117 }
118
119 void App::showStationSelectView(void)
120 {
121     stationListView->show();
122 }
123
124 void App::readSettings(void)
125 {
126     QSettings settings;
127     queryBaseUrl = settings.value("QueryURL",
128                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
129     stationView->setBaseUrl(queryBaseUrl);
130
131     stationName = settings.value("CurrentStation").toString();
132     showingArrivals = settings.value("ShowingArrivals", false).toBool();
133     checkingInterval = settings.value("CheckInterval", 2000).toInt();
134 }
135
136 void App::saveSettings(void)
137 {
138     QSettings settings;
139     settings.setValue("QueryURL", queryBaseUrl);
140     settings.value("CurrentStation", stationName);
141     settings.value("ShowingArrivals", showingArrivals);
142     settings.value("CheckInterval", checkingInterval);
143 }
144
145 void App::setShowingArrivals(bool showArrivals)
146 {
147     showingArrivals = showArrivals;
148 }