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