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