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