Add version information to the about dialog
[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     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
84 }
85
86 void App::queryStation(const QString &station)
87 {
88     QNetworkRequest request;
89     request.setUrl(queryBaseUrl);
90     const QString queryString = "stazione=" + station;
91     const QByteArray query(queryString.toLocal8Bit());
92     stationQueryReply = accessManager->post(request, query);
93     connect(stationQueryReply, SIGNAL(finished()),
94             this, SLOT(downloadFinished()));
95     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
96 }
97
98 void App::showSettingsDialog()
99 {
100 }
101
102 void App::showAboutDialog()
103 {
104     qDebug() << "About Dialog called";
105     QString name = QApplication::instance()->applicationName();
106     QString version = QApplication::instance()->applicationVersion();
107     QString aboutText = QString(
108                 tr("<p>%1 version %2</p>"
109                    "<p>Copyright (c) 2010</p>"
110                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
111                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
112     QMessageBox::about(stationView, name, aboutText);
113 }
114
115 void App::showStationSelectView(void)
116 {
117     stationListView->show();
118 }
119
120 void App::readSettings(void)
121 {
122     QSettings settings;
123     queryBaseUrl = settings.value("QueryURL",
124                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
125     stationView->setBaseUrl(queryBaseUrl);
126
127     stationName = settings.value("CurrentStation").toString();
128     showingArrivals = settings.value("ShowingArrivals", false).toBool();
129     checkingInterval = settings.value("CheckInterval", 2000).toInt();
130 }
131
132 void App::saveSettings(void)
133 {
134     QSettings settings;
135     settings.setValue("QueryURL", queryBaseUrl);
136     settings.value("CurrentStation", stationName);
137     settings.value("ShowingArrivals", showingArrivals);
138     settings.value("CheckInterval", checkingInterval);
139 }
140
141 void App::setShowingArrivals(bool showArrivals)
142 {
143     showingArrivals = showArrivals;
144 }