Add a missing station code
[quandoparte] / application / dataprovider.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 "dataprovider.h"
23 #include "settings.h"
24 #include "stationschedulemodel.h"
25
26 #include <QDebug>
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QNetworkRequest>
30 #include <QSharedPointer>
31 #if 0
32 #include <QWebElement>
33 #include <QWebFrame>
34 #include <QWebPage>
35 #endif
36
37 // Constants
38 static const int RECENT_STATIONS_MAX_COUNT = 10;
39
40 // Class methods
41
42 DataProvider *DataProvider::instance()
43 {
44     static DataProvider *dataProvider = 0;
45
46     if (!dataProvider)
47         dataProvider = new DataProvider(0);
48     return dataProvider;
49 }
50
51 DataProvider::DataProvider(QObject *parent) :
52     QObject(parent),
53     accessManager(new QNetworkAccessManager(this)),
54     stationQueryReply(0)
55 {
56 }
57
58 void DataProvider::fetchStationSchedule(const QString &station,
59                                         const QString &stationCode)
60 {
61     QNetworkRequest request;
62     Settings *settings = Settings::instance();
63     request.setUrl(settings->queryBaseUrl() + "stazione");
64     qDebug() << "fetching schedule for station:" << station << "code:" << stationCode;
65     const QString queryString =
66             stationCode.isEmpty() ? "stazione=" + station :
67                                     "codiceStazione=" + stationCode;
68     const QByteArray query(queryString.toLocal8Bit());
69     stationQueryReply = accessManager->post(request, query);
70     connect(stationQueryReply, SIGNAL(metaDataChanged()),
71             SLOT(onStationQueryMetadataChanged()));
72     connect(stationQueryReply, SIGNAL(finished()),
73             SLOT(onStationScheduleFetched()));
74     connect(stationQueryReply, SIGNAL(error(QNetworkReply::NetworkError)),
75             SLOT(onNetworkError(QNetworkReply::NetworkError)));
76     QStringList recentStations = settings->recentStations();
77     recentStations.push_front(station);
78     recentStations.removeDuplicates();
79     if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
80         recentStations.pop_back();
81     }
82     settings->setRecentStations(recentStations);
83 }
84
85 void DataProvider::updateStation()
86 {
87     Settings *settings = Settings::instance();
88
89     qDebug() << "updating station data";
90     if (!settings->recentStations().isEmpty()) {
91         fetchStationSchedule(settings->recentStations().front());
92     }
93 }
94
95 void DataProvider::onStationScheduleFetched()
96 {
97     disconnect(stationQueryReply);
98
99     QVariant httpStatus = stationQueryReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
100     if (httpStatus.isValid()) {
101         qDebug() << "Metadata changed, Http status is:" << httpStatus.toInt();
102     }
103     emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
104     stationQueryReply->deleteLater();
105     stationQueryReply = 0;
106 }
107
108 void DataProvider::onStationQueryMetadataChanged(void)
109 {
110 }
111
112 void DataProvider::onNetworkError(QNetworkReply::NetworkError errorCode)
113 {
114     switch (errorCode) {
115     case QNetworkReply::NoError:
116         qDebug() << "No Network error" << errorCode;
117         break;
118     default:
119         qDebug() << "Network error" << errorCode;
120         emit error();
121         break;
122     }
123 }