Make BINDIR/DATADIR based on PREFIX
[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 #include <QGeoPositionInfoSource>
38
39 // Constants
40 static const int RECENT_STATIONS_MAX_COUNT = 10;
41
42 QTM_USE_NAMESPACE
43
44 App::App(QObject *parent) :
45     QObject(parent),
46     accessManager(new QNetworkAccessManager(this)),
47     stationView(new StationView()),
48     stationListModel(new StationListModel(this)),
49     stationListView(new StationListView(stationListModel, stationView))
50 {
51     stationListModel->load("stations:stations.qpl");
52
53     connect(stationListView, SIGNAL(stationSelected(const QString &)),
54             SLOT(queryStation(const QString &)));
55
56     connect(stationListView, SIGNAL(aboutTriggered()),
57             SLOT(showAboutDialog()));
58     connect(stationView, SIGNAL(aboutTriggered()),
59             SLOT(showAboutDialog()));
60
61     connect(stationListView, SIGNAL(settingsChangeRequested()),
62             SLOT(showSettingsDialog()));
63     connect(stationView, SIGNAL(settingsChangeRequested()),
64             SLOT(showSettingsDialog()));
65
66     connect(stationView, SIGNAL(stationListSelectTriggered()),
67             SLOT(showStationSelectView()));
68
69     readSettings();
70
71     qDebug() << "found" << stationListModel->rowCount() << "stations";
72     stationView->show();
73     if (recentStations.isEmpty() || !stationViewPreferred) {
74         stationListView->show();
75     } else {
76         queryStation(recentStations.front());
77     }
78 }
79
80 App::~App()
81 {
82     delete stationView;
83     saveSettings();
84 }
85
86 void App::downloadFinished(void)
87 {
88     disconnect(stationQueryReply, SIGNAL(finished()),
89                this, SLOT(downloadFinished()));
90     stationView->updateView(stationQueryReply->readAll());
91     stationListView->hide();
92     stationQueryReply->deleteLater();
93     stationQueryReply = 0;
94 #ifdef Q_WS_MAEMO_5
95     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
96 #endif
97 }
98
99 void App::queryStation(const QString &station)
100 {
101     QNetworkRequest request;
102     request.setUrl(queryBaseUrl);
103     const QString queryString = "stazione=" + station;
104     const QByteArray query(queryString.toLocal8Bit());
105     stationQueryReply = accessManager->post(request, query);
106     connect(stationQueryReply, SIGNAL(finished()),
107             this, SLOT(downloadFinished()));
108     recentStations.push_front(station);
109     recentStations.removeDuplicates();
110     if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
111         recentStations.pop_back();
112     }
113 #ifdef Q_WS_MAEMO_5
114     stationListView->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
115 #endif
116 }
117
118 void App::showSettingsDialog()
119 {
120     qDebug() << "Settings Dialog called";
121
122     SettingsDialog *dialog = new SettingsDialog(stationView);
123     if (dialog->exec() == QDialog::Accepted) {
124         readSettings();
125     }
126     delete dialog;
127 }
128
129 void App::showAboutDialog()
130 {
131     qDebug() << "About Dialog called";
132     QString name = QApplication::instance()->applicationName();
133     QString version = QApplication::instance()->applicationVersion();
134     QString aboutText = QString(
135                 tr("<p>%1 version %2</p>"
136                    "<p>Copyright (c) 2010, 2011</p>"
137                    "<p>Luciano Montanaro (mikelima@cirulla.net)</p>"
138                    "<p>Licensed under the GNU Public License v2 or above</p>")).arg(name).arg(version);
139     QMessageBox::about(stationView, name, aboutText);
140 }
141
142 void App::showStationSelectView(void)
143 {
144     stationListView->show();
145 }
146
147 void App::readSettings(void)
148 {
149     QSettings settings;
150     queryBaseUrl = settings.value("QueryURL",
151                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
152     stationView->setBaseUrl(queryBaseUrl);
153
154     recentStations = settings.value("RecentStations").toString().split(",");
155     checkingInterval = settings.value("CheckInterval", 2000).toInt();
156     stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
157 }
158
159 void App::saveSettings(void)
160 {
161     QSettings settings;
162
163     qDebug() << "Saving Settings to" << settings.fileName();
164
165     settings.setValue("QueryURL", queryBaseUrl);
166     settings.setValue("RecentStations", recentStations.join(","));
167     settings.setValue("CheckInterval", checkingInterval);
168     settings.setValue("StationViewPreferred", stationViewPreferred);
169 }
170
171 QString App::dataDir(void)
172 {
173     return QString(DATADIR);
174 }