Add routine which returns light color of ExtendedTrafficInfo.
[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     float _len1, _len2, _len;
16     QDateTime _ts;
17     QString _isotime;
18     QString _localtime;
19
20     bool _valid;
21
22 protected:
23     float getFloatNode (const QDomElement& elem, const char* node, float def);
24     int getIntNode (const QDomElement& elem, const char* node, int def);
25     QString getStringNode (const QDomElement& elem, const char* node) throw (const QString&);
26     QDateTime getTSNode (const QDomElement& elem, const char* node) throw (const QString&);
27
28     void setValid (bool new_val)
29     { _valid = new_val; };
30
31 public:
32     TrafficInfo ()
33         : _valid (false)
34     {};
35
36     TrafficInfo (const QDomElement& elem) throw (const QString&);
37
38     bool valid () const
39     { return _valid; };
40 };
41
42
43 class ExtendedTrafficInfo : public TrafficInfo
44 {
45 public:
46     enum light_color {
47         Unknown,
48         Red,
49         Yellow,
50         Green
51     };
52
53 private:
54     float _level_raw;
55     int _level;
56     light_color _color;
57     int _tend;
58     QString _hint;
59
60 public:
61     ExtendedTrafficInfo ()
62         : TrafficInfo ()
63     {};
64
65     ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&);
66
67     int level () const
68     { return _level; };
69
70     int tend () const
71     { return _tend; };
72
73     QString hint () const
74     { return _hint; };
75
76     ExtendedTrafficInfo::light_color color () const
77     { return _color; };
78 };
79
80
81 class Traffic : public QObject
82 {
83     Q_OBJECT
84
85 private:
86     QDateTime _ts;
87
88     QMap<QString, TrafficInfo> _info;
89     QMap<QString, ExtendedTrafficInfo> _ext_info;
90
91     HttpFetcher _fetcher;
92
93     bool parse_traffic_data (const QString& xml);
94
95 private slots:
96     void fetchDone (const QByteArray& data);
97
98 signals:
99     void updated ();
100
101 public:
102     Traffic ();
103
104     void update ();
105
106     QDateTime ts () const
107     { return _ts; };
108
109     TrafficInfo lookup (const QString &id) const;
110     ExtendedTrafficInfo lookup_ext (const QString &id) const;
111 };
112
113
114 #endif // __TRAFFIC_H__