8 // --------------------------------------------------
10 // --------------------------------------------------
11 CityTrafficInfo::CityTrafficInfo (const QDomElement& elem) throw (const QString&)
17 _ts = getTSNode (elem, "timestamp");
18 _level = getIntNode (elem, "level", 1);
19 _tend = getIntNode (elem, "tend", 0);
20 _hint = getStringNode (elem, "hint");
22 color = getStringNode (elem, "icon");
25 else if (color == "yellow")
27 else if (color == "red")
30 throw "Color is unknown";
36 void CityTrafficInfo::dump ()
38 Log::instance ()->add (QString ("CityTrafficInfo: level = %1, hint = %2").arg (_level).arg (_hint));
42 float CityTrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def)
48 e = elem.firstChildElement (node);
51 val = e.text ().toFloat (&ok);
58 int CityTrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def)
64 e = elem.firstChildElement (node);
67 val = e.text ().toInt (&ok);
74 QString CityTrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&)
79 e = elem.firstChildElement (node);
81 throw QString (QString::fromAscii (node) + " not found");
86 QDateTime CityTrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&)
93 e = elem.firstChildElement (node);
95 throw QString (QString::fromAscii (node) + " not found");
96 val = e.text ().toUInt (&ok);
98 throw QString (QString::fromAscii (node) + " is not a timestamp");
104 // --------------------------------------------------
106 // --------------------------------------------------
110 connect (&_fetcher, SIGNAL (done (const QByteArray&)),
111 SLOT (fetchDone (const QByteArray&)));
115 // Perform asyncronous refresh of traffic information. If another update
116 // request is in progress, new is discarded. If update request finished
117 // successfully, updated() signal called.
118 void Traffic::update ()
120 Log::instance ()->add ("Traffic::update: Request status download");
121 _fetcher.fetch ("http://trf.maps.yandex.net/trf/stat.xml");
125 void Traffic::fetchDone (const QByteArray& data)
128 if (parse_traffic_data (QString::fromUtf8 (data.data ()))) {
129 Log::instance ()->add ("Traffic::fetchDone: data parsed successfully");
133 Log::instance ()->add ("Traffic::fetchDone: data parse error");
137 bool Traffic::parse_traffic_data (const QString& xml)
145 QMap<QString, CityTrafficInfo> new_ext_info;
147 if (!doc.setContent (xml))
151 e = doc.documentElement ();
152 if (e.isNull () || e.tagName () != "jams_stat")
155 s = e.attribute ("timestamp");
159 new_ts.setTime_t (s.toUInt (&ok));
165 while (!n.isNull ()) {
167 if (!e.isNull () && e.tagName () == "region") {
168 s = e.attribute ("id");
170 // Check that it is an extended traffic info
171 if (!e.firstChildElement ("level").isNull ()) {
172 CityTrafficInfo info (e);
174 new_ext_info[s] = info;
177 catch (const QString& msg) {
180 n = n.nextSibling ();
184 _ext_info = new_ext_info;
190 CityTrafficInfo Traffic::lookup_ext (const QString &id) const
192 QMap<QString, CityTrafficInfo>::const_iterator it = _ext_info.find (id);
194 if (it == _ext_info.end ())
195 return CityTrafficInfo ();