Simplify traffic info class.
[yandex-traffic] / traffic.hpp
1 #ifndef __TRAFFIC_H__
2 #define __TRAFFIC_H__
3
4 #include <QtCore>
5 #include <QtNetwork>
6 #include <QtXml>
7
8
9 #include "http_fetcher.hpp"
10
11 // Base data of traffic information
12 class TrafficInfo
13 {
14 private:
15     bool _valid;
16
17 protected:
18     void setValid (bool new_val)
19     { _valid = new_val; };
20
21 public:
22     TrafficInfo ()
23         : _valid (false)
24     {};
25
26     bool valid () const
27     { return _valid; };
28 };
29
30
31 class CityTrafficInfo : public TrafficInfo
32 {
33 public:
34     enum light_color {
35         Unknown,
36         Red,
37         Yellow,
38         Green
39     };
40
41 private:
42     QDateTime _ts;
43     int _level, _tend;
44     light_color _color;
45     QString _hint;
46
47     float getFloatNode (const QDomElement& elem, const char* node, float def);
48     int getIntNode (const QDomElement& elem, const char* node, int def);
49     QString getStringNode (const QDomElement& elem, const char* node) throw (const QString&);
50     QDateTime getTSNode (const QDomElement& elem, const char* node) throw (const QString&);
51
52 public:
53     CityTrafficInfo ()
54         : TrafficInfo ()
55     {};
56
57     CityTrafficInfo (const QDomElement& elem) throw (const QString&);
58
59     QDateTime ts () const
60     { return _ts; };
61
62     int level () const
63     { return _level; };
64
65     int tend () const
66     { return _tend; };
67
68     QString hint () const
69     { return _hint; };
70
71     CityTrafficInfo::light_color color () const
72     { return _color; };
73
74     virtual void dump ();
75 };
76
77
78 class Traffic : public QObject
79 {
80     Q_OBJECT
81
82 private:
83     QDateTime _ts;
84
85     QMap<QString, CityTrafficInfo> _ext_info;
86
87     HttpFetcher _fetcher;
88
89     bool parse_traffic_data (const QString& xml);
90
91 private slots:
92     void fetchDone (const QByteArray& data);
93
94 signals:
95     void updated ();
96
97 public:
98     Traffic ();
99
100     void update ();
101
102     QDateTime ts () const
103     { return _ts; };
104
105     CityTrafficInfo lookup_ext (const QString &id) const;
106 };
107
108
109 #endif // __TRAFFIC_H__