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)
138 if (parse_traffic_data (QString::fromUtf8 (data.data ())))
143 bool Traffic::parse_traffic_data (const QString& xml)
151 QMap<QString, TrafficInfo> new_info;
152 QMap<QString, ExtendedTrafficInfo> new_ext_info;
154 if (!doc.setContent (xml))
158 e = doc.documentElement ();
159 if (e.isNull () || e.tagName () != "jams_stat")
162 s = e.attribute ("timestamp");
166 new_ts.setTime_t (s.toUInt (&ok));
172 while (!n.isNull ()) {
174 if (!e.isNull () && e.tagName () == "region") {
175 s = e.attribute ("id");
177 // Check that it is an extended traffic info
178 if (!e.firstChildElement ("level").isNull ()) {
179 ExtendedTrafficInfo info (e);
181 new_ext_info[s] = info;
184 TrafficInfo info (e);
189 catch (const QString& msg) {
192 n = n.nextSibling ();
197 _ext_info = new_ext_info;
203 TrafficInfo Traffic::lookup (const QString &id) const
205 QMap<QString, TrafficInfo>::const_iterator it = _info.find (id);
207 if (it == _info.end ())
208 return TrafficInfo ();
214 ExtendedTrafficInfo Traffic::lookup_ext (const QString &id) const
216 QMap<QString, ExtendedTrafficInfo>::const_iterator it = _ext_info.find (id);
218 if (it == _ext_info.end ())
219 return ExtendedTrafficInfo ();