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