--- /dev/null
+#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);
+}
--- /dev/null
+#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__
protected slots:
void fetchTraffic ()
{
- printf ("Clicked\n");
+ printf ("Traffic update requested\n");
+ _traffic.update ();
+ }
+
+ void trafficUpdated ()
+ {
+ printf ("Traffic data updated\n");
}
public:
: QPushButton ()
{
connect (this, SIGNAL (clicked ()), this, SLOT (fetchTraffic ()));
- setText ("Push me");
+ connect (&_traffic, SIGNAL (updated ()), this, SLOT (trafficUpdated ()));
+ setText ("Fetch traffic information");
}
};
#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 ();
+}
+
#define __TRAFFIC_H__
#include <QtCore>
+#include <QtNetwork>
+#include "http_fetcher.hpp"
+
// Base data of traffic information
class TrafficInfo
{
};
-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 ();
};
-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