From 81ce7e9126d61a657712ce01594a3280dcf6c0ee Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Mon, 8 Mar 2010 21:41:40 +0300 Subject: [PATCH] HttpFetcher helper class for simple data fetch. --- http_fetcher.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ http_fetcher.hpp | 30 ++++++++++++++++++++++++++++++ tests/traffic/mainwindow.hpp | 11 +++++++++-- traffic.cpp | 32 ++++++++++++++++++++++++++++++++ traffic.hpp | 17 ++++++++++++++++- yandex-traffic-core.pri | 6 ++++-- 6 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 http_fetcher.cpp create mode 100644 http_fetcher.hpp diff --git a/http_fetcher.cpp b/http_fetcher.cpp new file mode 100644 index 0000000..787b543 --- /dev/null +++ b/http_fetcher.cpp @@ -0,0 +1,42 @@ +#include +#include + +#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 index 0000000..73a5528 --- /dev/null +++ b/http_fetcher.hpp @@ -0,0 +1,30 @@ +#ifndef __HTTP_FETCHER_H__ +#define __HTTP_FETCHER_H__ + +#include +#include + + +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__ diff --git a/tests/traffic/mainwindow.hpp b/tests/traffic/mainwindow.hpp index 9568a02..4754d0c 100644 --- a/tests/traffic/mainwindow.hpp +++ b/tests/traffic/mainwindow.hpp @@ -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"); } }; diff --git a/traffic.cpp b/traffic.cpp index 2abfeed..6b29452 100644 --- a/traffic.cpp +++ b/traffic.cpp @@ -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 (); +} + diff --git a/traffic.hpp b/traffic.hpp index 5671b9e..dbbfc56 100644 --- a/traffic.hpp +++ b/traffic.hpp @@ -2,8 +2,11 @@ #define __TRAFFIC_H__ #include +#include +#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 _info; QMap _ext_info; + HttpFetcher _fetcher; + +private slots: + void fetchDone (const QByteArray& data); + +signals: + void updated (); + public: Traffic (); + + void update (); }; diff --git a/yandex-traffic-core.pri b/yandex-traffic-core.pri index 4e6ebec..08521a2 100644 --- a/yandex-traffic-core.pri +++ b/yandex-traffic-core.pri @@ -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 -- 1.7.9.5