Added DataProvider class
authorLuciano Montanaro <mikelima@cirulla.net>
Wed, 27 Jul 2011 21:51:49 +0000 (23:51 +0200)
committerLuciano Montanaro <mikelima@cirulla.net>
Tue, 27 Dec 2011 22:16:47 +0000 (23:16 +0100)
Move network request and queries to their own class, so that QML can
eventually interact with it.

application/application.pro
application/dataprovider.cpp [new file with mode: 0644]
application/dataprovider.h [new file with mode: 0644]

index 5ad72e8..595e31d 100644 (file)
@@ -73,13 +73,15 @@ SOURCES += \
     main.cpp \
     stationlistmodel.cpp \
     stationlistproxymodel.cpp \
-    settings.cpp
+    settings.cpp \
+    dataprovider.cpp
 
 HEADERS += \
     $$PLATFORM_HEADERS \
     stationlistmodel.h \
     stationlistproxymodel.h \
-    settings.h
+    settings.h \
+    dataprovider.h
 
 FORMS += \
     settingsdialog.ui \
diff --git a/application/dataprovider.cpp b/application/dataprovider.cpp
new file mode 100644 (file)
index 0000000..1395d1a
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+
+Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.
+
+*/
+
+#include "dataprovider.h"
+#include "settings.h"
+
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QNetworkRequest>
+
+// Constants
+static const int RECENT_STATIONS_MAX_COUNT = 10;
+
+DataProvider::DataProvider(QDeclarativeItem *parent) :
+    QDeclarativeItem(parent),
+    accessManager(new QNetworkAccessManager(this))
+{
+}
+
+void DataProvider::queryStation(const QString &station)
+{
+    QNetworkRequest request;
+    Settings *settings = Settings::instance();
+    request.setUrl(settings->queryBaseUrl());
+    const QString queryString = "stazione=" + station;
+    const QByteArray query(queryString.toLocal8Bit());
+    stationQueryReply = accessManager->post(request, query);
+    connect(stationQueryReply, SIGNAL(finished()),
+            this, SLOT(queryStationCompleted()));
+    settings->recentStations().push_front(station);
+    settings->recentStations().removeDuplicates();
+    if (settings->recentStations().count() > RECENT_STATIONS_MAX_COUNT) {
+        settings->recentStations().pop_back();
+    }
+    // TODO Implement busy indication...
+}
+
+void DataProvider::updateStation()
+{
+    Settings *settings = Settings::instance();
+
+    qDebug() << "updating station data";
+    if (!settings->recentStations().isEmpty()) {
+        queryStation(settings->recentStations().front());
+    }
+}
+
+void DataProvider::queryStationCompleted()
+{
+    disconnect(stationQueryReply, SIGNAL(finished()),
+               this, SLOT(downloadFinished()));
+    // TODO implement parsing or data returning...
+    stationQueryReply->deleteLater();
+    stationQueryReply = 0;
+}
diff --git a/application/dataprovider.h b/application/dataprovider.h
new file mode 100644 (file)
index 0000000..d6c6eaf
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+
+Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.
+
+*/
+
+#ifndef DATAPROVIDER_H
+#define DATAPROVIDER_H
+
+#include <QDeclarativeItem>
+
+class QNetworkAccessManager;
+class QNetworkReply;
+
+class DataProvider : public QDeclarativeItem
+{
+    Q_OBJECT
+public:
+    explicit DataProvider(QDeclarativeItem *parent = 0);
+
+signals:
+
+public slots:
+    void queryStation(const QString &station);
+    void updateStation();
+
+private slots:
+    void queryStationCompleted(void);
+
+private:
+    QNetworkAccessManager *accessManager;
+    QNetworkReply *stationQueryReply;
+};
+
+#endif // DATAPROVIDER_H