8 // --------------------------------------------------
10 // --------------------------------------------------
11 TrafficInfo::TrafficInfo (const QDomElement& elem) throw (const QString&)
14 _len1 = getFloatNode (elem, "length_1", 0);
15 _len2 = getFloatNode (elem, "length_2", 0);
16 _len = getFloatNode (elem, "length", 0);
17 _ts = getTSNode (elem, "timestamp");
18 _isotime = getStringNode (elem, "isotime");
19 _localtime = getStringNode (elem, "localtime");
24 float TrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def)
30 e = elem.firstChildElement (node);
33 val = e.text ().toFloat (&ok);
40 int TrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def)
46 e = elem.firstChildElement (node);
49 val = e.text ().toInt (&ok);
56 QString TrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&)
61 e = elem.firstChildElement (node);
63 throw QString (QString::fromAscii (node) + " not found");
68 QDateTime TrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&)
75 e = elem.firstChildElement (node);
77 throw QString (QString::fromAscii (node) + " not found");
78 val = e.text ().toUInt (&ok);
80 throw QString (QString::fromAscii (node) + " is not a timestamp");
86 void TrafficInfo::dump ()
88 Log::instance ()->add (QString ("TrafficInfo (%1): time = %2").arg (_valid ? "valid" : "not valid").arg (_localtime));
92 // --------------------------------------------------
93 // ExtendedTrafficInfo
94 // --------------------------------------------------
95 ExtendedTrafficInfo::ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&)
101 _level_raw = getFloatNode (elem, "level_raw", 0);
102 _level = getIntNode (elem, "level", 1);
103 _tend = getIntNode (elem, "tend", 0);
104 _hint = getStringNode (elem, "hint");
106 color = getStringNode (elem, "icon");
107 if (color == "green")
109 else if (color == "yellow")
111 else if (color == "red")
114 throw "Color is unknown";
120 void ExtendedTrafficInfo::dump ()
122 TrafficInfo::dump ();
123 Log::instance ()->add (QString ("ExtTrafficInfo: level = %1, hint = %2").arg (_level).arg (_hint));
127 // --------------------------------------------------
129 // --------------------------------------------------
133 connect (&_fetcher, SIGNAL (done (const QByteArray&)),
134 SLOT (fetchDone (const QByteArray&)));
138 // Perform asyncronous refresh of traffic information. If another update
139 // request is in progress, new is discarded. If update request finished
140 // successfully, updated() signal called.
141 void Traffic::update ()
143 Log::instance ()->add ("Traffic::update: Request status download");
144 _fetcher.fetch ("http://trf.maps.yandex.net/trf/stat.xml");
148 void Traffic::fetchDone (const QByteArray& data)
151 if (parse_traffic_data (QString::fromUtf8 (data.data ()))) {
152 Log::instance ()->add ("Traffic::fetchDone: data parsed successfully");
156 Log::instance ()->add ("Traffic::fetchDone: data parse error");
160 bool Traffic::parse_traffic_data (const QString& xml)
168 QMap<QString, TrafficInfo> new_info;
169 QMap<QString, ExtendedTrafficInfo> new_ext_info;
171 if (!doc.setContent (xml))
175 e = doc.documentElement ();
176 if (e.isNull () || e.tagName () != "jams_stat")
179 s = e.attribute ("timestamp");
183 new_ts.setTime_t (s.toUInt (&ok));
189 while (!n.isNull ()) {
191 if (!e.isNull () && e.tagName () == "region") {
192 s = e.attribute ("id");
194 // Check that it is an extended traffic info
195 if (!e.firstChildElement ("level").isNull ()) {
196 ExtendedTrafficInfo info (e);
198 new_ext_info[s] = info;
201 TrafficInfo info (e);
206 catch (const QString& msg) {
209 n = n.nextSibling ();
214 _ext_info = new_ext_info;
220 TrafficInfo Traffic::lookup (const QString &id) const
222 QMap<QString, TrafficInfo>::const_iterator it = _info.find (id);
224 if (it == _info.end ())
225 return TrafficInfo ();
231 ExtendedTrafficInfo Traffic::lookup_ext (const QString &id) const
233 QMap<QString, ExtendedTrafficInfo>::const_iterator it = _ext_info.find (id);
235 if (it == _ext_info.end ())
236 return ExtendedTrafficInfo ();