9d05ea3c70fbcee78d08c0f00e398ed4ad0f0ce1
[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 #include <QWebElement>
32 #include <QWebFrame>
33 #include <QWebPage>
34
35 // Constants
36 static const int RECENT_STATIONS_MAX_COUNT = 10;
37
38 // Class methods
39
40 DataProvider *DataProvider::instance()
41 {
42     static DataProvider *dataProvider = 0;
43
44     if (!dataProvider)
45         dataProvider = new DataProvider(0);
46     return dataProvider;
47 }
48
49 DataProvider::DataProvider(QObject *parent) :
50     QObject(parent),
51     accessManager(new QNetworkAccessManager(this))
52 {
53 }
54
55 void DataProvider::fetchStationSchedule(const QString &station)
56 {
57     QNetworkRequest request;
58     Settings *settings = Settings::instance();
59     request.setUrl(settings->queryBaseUrl() + "stazione");
60
61     qDebug() << "fetching schedule for station" << station;
62     const QString queryString = "stazione=" + station;
63     const QByteArray query(queryString.toLocal8Bit());
64     stationQueryReply = accessManager->post(request, query);
65     connect(stationQueryReply, SIGNAL(finished()),
66             SLOT(onStationScheduleFetched()));
67     QStringList recentStations = settings->recentStations();
68     recentStations.push_front(station);
69     recentStations.removeDuplicates();
70     if (recentStations.count() > RECENT_STATIONS_MAX_COUNT) {
71         recentStations.pop_back();
72     }
73     settings->setRecentStations(recentStations);
74 }
75
76 void DataProvider::updateStation()
77 {
78     Settings *settings = Settings::instance();
79
80     qDebug() << "updating station data";
81     if (!settings->recentStations().isEmpty()) {
82         fetchStationSchedule(settings->recentStations().front());
83     }
84 }
85
86 void DataProvider::onStationScheduleFetched()
87 {
88     disconnect(stationQueryReply, SIGNAL(finished()),
89                this, SLOT(onStationScheduleFetched()));
90
91     QString name = Settings::instance()->recentStations().front();
92
93     emit stationScheduleReady(stationQueryReply->readAll(), stationQueryReply->url());
94     stationQueryReply->deleteLater();
95     stationQueryReply = 0;
96 }