HttpFetcher helper class for simple data fetch.
authorMax Lapan <max.lapan@gmail.com>
Mon, 8 Mar 2010 18:41:40 +0000 (21:41 +0300)
committerMax Lapan <max.lapan@gmail.com>
Mon, 8 Mar 2010 18:41:40 +0000 (21:41 +0300)
http_fetcher.cpp [new file with mode: 0644]
http_fetcher.hpp [new file with mode: 0644]
tests/traffic/mainwindow.hpp
traffic.cpp
traffic.hpp
yandex-traffic-core.pri

diff --git a/http_fetcher.cpp b/http_fetcher.cpp
new file mode 100644 (file)
index 0000000..787b543
--- /dev/null
@@ -0,0 +1,42 @@
+#include <QtCore>
+#include <QtNetwork>
+
+#include "http_fetcher.hpp"
+
+
+// --------------------------------------------------
+// HttpFetcher
+// --------------------------------------------------
+HttpFetcher::HttpFetcher ()
+    : QObject ()
+{
+    connect (&_http, SIGNAL (done (bool)), SLOT (requestDone (bool)));
+}
+
+
+bool HttpFetcher::busy () const
+{
+    return _http.currentId () != 0;
+}
+
+
+void HttpFetcher::fetch (const QString& url)
+{
+    QUrl u (url);
+
+    if (u.isValid ()) {
+        _http.setHost (u.host ());
+        _http.get (u.encodedPath (), &_buffer);
+    }
+}
+
+
+void HttpFetcher::requestDone (bool err)
+{
+    if (err)
+        error (_http.error ());
+    else
+        done (_buffer.buffer ());
+    _buffer.close ();
+    _buffer.setBuffer (NULL);
+}
diff --git a/http_fetcher.hpp b/http_fetcher.hpp
new file mode 100644 (file)
index 0000000..73a5528
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef __HTTP_FETCHER_H__
+#define __HTTP_FETCHER_H__
+
+#include <QtCore>
+#include <QtNetwork>
+
+
+class HttpFetcher : public QObject
+{
+    Q_OBJECT
+private:
+    QHttp _http;
+    QBuffer _buffer;
+
+private slots:
+    void requestDone (bool err);
+
+signals:
+    void error (QHttp::Error error);
+    void done (const QByteArray& data);
+
+public:
+    HttpFetcher ();
+
+    bool busy () const;
+    void fetch (const QString& url);
+};
+
+
+#endif // __HTTP_FETCHER_H__
index 9568a02..4754d0c 100644 (file)
@@ -15,7 +15,13 @@ private:
 protected slots:
     void fetchTraffic ()
     {
-        printf ("Clicked\n");
+        printf ("Traffic update requested\n");
+        _traffic.update ();
+    }
+
+    void trafficUpdated ()
+    {
+        printf ("Traffic data updated\n");
     }
 
 public:
@@ -23,7 +29,8 @@ public:
         : QPushButton ()
     {
         connect (this, SIGNAL (clicked ()), this, SLOT (fetchTraffic ()));
-        setText ("Push me");
+        connect (&_traffic, SIGNAL (updated ()), this, SLOT (trafficUpdated ()));
+        setText ("Fetch traffic information");
     }
 };
 
index 2abfeed..6b29452 100644 (file)
@@ -2,16 +2,48 @@
 #include "traffic.hpp"
 
 
+// --------------------------------------------------
+// TrafficInfo
+// --------------------------------------------------
 TrafficInfo::TrafficInfo ()
 {
 }
 
 
+// --------------------------------------------------
+// ExtendedTrafficInfo
+// --------------------------------------------------
 ExtendedTrafficInfo::ExtendedTrafficInfo ()
 {
 }
 
 
+// --------------------------------------------------
+// Traffic
+// --------------------------------------------------
 Traffic::Traffic ()
+    : QObject ()
 {
+    connect (&_fetcher, SIGNAL (done (const QByteArray&)),
+             SLOT (fetchDone (const QByteArray&)));
 }
+
+// Perform asyncronous refresh of traffic information. If another update
+// request is in progress, new is discarded. If update request finished
+// successfully, updated() signal called.
+void Traffic::update ()
+{
+    if (_fetcher.busy ())
+        return;
+
+    _fetcher.fetch ("http://trf.maps.yandex.net/trf/stat.xml");
+}
+
+
+void Traffic::fetchDone (const QByteArray& data)
+{
+    printf ("Got %d bytes of data\n", data.size ());
+//    printf ("Data:\n%s\n", data.data ());
+    updated ();
+}
+
index 5671b9e..dbbfc56 100644 (file)
@@ -2,8 +2,11 @@
 #define __TRAFFIC_H__
 
 #include <QtCore>
+#include <QtNetwork>
 
 
+#include "http_fetcher.hpp"
+
 // Base data of traffic information
 class TrafficInfo
 {
@@ -39,16 +42,28 @@ public:
 };
 
 
-class Traffic
+class Traffic : public QObject
 {
+    Q_OBJECT
+
 private:
     QDateTime _ts;
 
     QMap<QString, TrafficInfo> _info;
     QMap<QString, ExtendedTrafficInfo> _ext_info;
 
+    HttpFetcher _fetcher;
+
+private slots:
+    void fetchDone (const QByteArray& data);
+
+signals:
+    void updated ();
+
 public:
     Traffic ();
+
+    void update ();
 };
 
 
index 4e6ebec..08521a2 100644 (file)
@@ -1,4 +1,6 @@
-HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp
-SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp
+HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp
+SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp
+
+QT += network
 
 INCLUDEPATH += $$PWD
\ No newline at end of file