7 // --------------------------------------------------
9 // --------------------------------------------------
10 TrafficInfo::TrafficInfo (const QDomElement& elem) throw (const QString&)
13 _len1 = getFloatNode (elem, "length_1", 0);
14 _len2 = getFloatNode (elem, "length_2", 0);
15 _len = getFloatNode (elem, "length", 0);
16 _ts = getTSNode (elem, "timestamp");
17 _isotime = getStringNode (elem, "isotime");
18 _localtime = getStringNode (elem, "localtime");
23 float TrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def)
29 e = elem.firstChildElement (node);
32 val = e.text ().toFloat (&ok);
39 int TrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def)
45 e = elem.firstChildElement (node);
48 val = e.text ().toInt (&ok);
55 QString TrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&)
60 e = elem.firstChildElement (node);
62 throw QString (QString::fromAscii (node) + " not found");
67 QDateTime TrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&)
74 e = elem.firstChildElement (node);
76 throw QString (QString::fromAscii (node) + " not found");
77 val = e.text ().toUInt (&ok);
79 throw QString (QString::fromAscii (node) + " is not a timestamp");
85 // --------------------------------------------------
86 // ExtendedTrafficInfo
87 // --------------------------------------------------
88 ExtendedTrafficInfo::ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&)
94 _level_raw = getFloatNode (elem, "level_raw", 0);
95 _level = getIntNode (elem, "level", 1);
96 _tend = getIntNode (elem, "tend", 0);
97 _hint = getStringNode (elem, "hint");
99 color = getStringNode (elem, "icon");
100 if (color == "green")
102 else if (color == "yellow")
104 else if (color == "red")
107 throw "Color is unknown";
113 // --------------------------------------------------
115 // --------------------------------------------------
119 connect (&_fetcher, SIGNAL (done (const QByteArray&)),
120 SLOT (fetchDone (const QByteArray&)));
123 // Perform asyncronous refresh of traffic information. If another update
124 // request is in progress, new is discarded. If update request finished
125 // successfully, updated() signal called.
126 void Traffic::update ()
128 if (_fetcher.busy ())
131 _fetcher.fetch ("http://trf.maps.yandex.net/trf/stat.xml");
135 void Traffic::fetchDone (const QByteArray& data)
137 printf ("Got %d bytes of data\n", data.size ());
139 if (parse_traffic_data (QString::fromUtf8 (data.data ())))
142 printf ("Parse failed\n");
146 bool Traffic::parse_traffic_data (const QString& xml)
154 QMap<QString, TrafficInfo> new_info;
155 QMap<QString, ExtendedTrafficInfo> new_ext_info;
157 if (!doc.setContent (xml))
161 e = doc.documentElement ();
162 if (e.isNull () || e.tagName () != "jams_stat")
165 s = e.attribute ("timestamp");
169 new_ts.setTime_t (s.toUInt (&ok));
175 while (!n.isNull ()) {
177 if (!e.isNull () && e.tagName () == "region") {
178 s = e.attribute ("id");
179 printf ("Process region %s\n", s.toUtf8 ().data ());
181 // Check that it is an extended traffic info
182 if (!e.firstChildElement ("level").isNull ()) {
183 ExtendedTrafficInfo info (e);
185 new_ext_info[s] = info;
188 TrafficInfo info (e);
193 catch (const QString& msg) {
194 printf ("Region %s parse failed: %s\n", s.toUtf8 ().data (), msg.toUtf8 ().data ());
197 n = n.nextSibling ();
202 _ext_info = new_ext_info;
208 TrafficInfo Traffic::lookup (const QString &id) const
210 QMap<QString, TrafficInfo>::const_iterator it = _info.find (id);
212 if (it == _info.end ())
213 return TrafficInfo ();
219 ExtendedTrafficInfo Traffic::lookup_ext (const QString &id) const
221 QMap<QString, ExtendedTrafficInfo>::const_iterator it = _ext_info.find (id);
223 if (it == _ext_info.end ())
224 return ExtendedTrafficInfo ();