REfactored DataProvider method names
[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
25 #include <QDebug>
26 #include <QNetworkAccessManager>
27 #include <QNetworkReply>
28 #include <QNetworkRequest>
29 #include <QWebElement>
30 #include <QWebFrame>
31 #include <QWebPage>
32
33 // Constants
34 static const int RECENT_STATIONS_MAX_COUNT = 10;
35
36 // Class methods
37
38 DataProvider::DataProvider(QObject *parent) :
39     QObject(parent),
40     accessManager(new QNetworkAccessManager(this))
41 {
42 }
43
44 void DataProvider::stationSchedule(const QString &station)
45 {
46     QNetworkRequest request;
47     Settings *settings = Settings::instance();
48     request.setUrl(settings->queryBaseUrl());
49     const QString queryString = "stazione=" + station;
50     const QByteArray query(queryString.toLocal8Bit());
51     stationQueryReply = accessManager->post(request, query);
52     connect(stationQueryReply, SIGNAL(finished()),
53             SLOT(onStationScheduleReady()));
54     settings->recentStations().push_front(station);
55     settings->recentStations().removeDuplicates();
56     if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
57         settings->recentStations().pop_back();
58     }
59     // TODO Implement busy indication...
60 }
61
62 void DataProvider::updateStation()
63 {
64     Settings *settings = Settings::instance();
65
66     qDebug() << "updating station data";
67     if (!settings->recentStations().isEmpty()) {
68         stationSchedule(settings->recentStations().front());
69     }
70 }
71
72 void DataProvider::onStationScheduleReady()
73 {
74     disconnect(stationQueryReply, SIGNAL(finished()),
75                this, SLOT(downloadFinished()));
76     // TODO implement parsing or data returning...
77     emit stationScheduleReady(stationQueryReply->readAll());
78     stationQueryReply->deleteLater();
79     stationQueryReply = 0;
80 }