6 #include "connection.hpp"
9 // --------------------------------------------------
11 // --------------------------------------------------
12 TrafficInfo::TrafficInfo (const QDomElement& elem) throw (const QString&)
15 _len1 = getFloatNode (elem, "length_1", 0);
16 _len2 = getFloatNode (elem, "length_2", 0);
17 _len = getFloatNode (elem, "length", 0);
18 _ts = getTSNode (elem, "timestamp");
19 _isotime = getStringNode (elem, "isotime");
20 _localtime = getStringNode (elem, "localtime");
25 float TrafficInfo::getFloatNode (const QDomElement& elem, const char* node, float def)
31 e = elem.firstChildElement (node);
34 val = e.text ().toFloat (&ok);
41 int TrafficInfo::getIntNode (const QDomElement& elem, const char* node, int def)
47 e = elem.firstChildElement (node);
50 val = e.text ().toInt (&ok);
57 QString TrafficInfo::getStringNode (const QDomElement& elem, const char* node) throw (const QString&)
62 e = elem.firstChildElement (node);
64 throw QString (QString::fromAscii (node) + " not found");
69 QDateTime TrafficInfo::getTSNode (const QDomElement& elem, const char* node) throw (const QString&)
76 e = elem.firstChildElement (node);
78 throw QString (QString::fromAscii (node) + " not found");
79 val = e.text ().toUInt (&ok);
81 throw QString (QString::fromAscii (node) + " is not a timestamp");
87 void TrafficInfo::dump ()
89 Log::instance ()->add (QString ("TrafficInfo (%1): time = %2").arg (_valid ? "valid" : "not valid").arg (_localtime));
93 // --------------------------------------------------
94 // ExtendedTrafficInfo
95 // --------------------------------------------------
96 ExtendedTrafficInfo::ExtendedTrafficInfo (const QDomElement& elem) throw (const QString&)
102 _level_raw = getFloatNode (elem, "level_raw", 0);
103 _level = getIntNode (elem, "level", 1);
104 _tend = getIntNode (elem, "tend", 0);
105 _hint = getStringNode (elem, "hint");
107 color = getStringNode (elem, "icon");
108 if (color == "green")
110 else if (color == "yellow")
112 else if (color == "red")
115 throw "Color is unknown";
121 void ExtendedTrafficInfo::dump ()
123 TrafficInfo::dump ();
124 Log::instance ()->add (QString ("ExtTrafficInfo: level = %1, hint = %2").arg (_level).arg (_hint));
128 // --------------------------------------------------
130 // --------------------------------------------------
134 connect (&_fetcher, SIGNAL (done (const QByteArray&)),
135 SLOT (fetchDone (const QByteArray&)));
136 connect (ConnectionChecker::instance (), SIGNAL (connected (bool)), SLOT (connectionChanged (bool)));
140 // Perform asyncronous refresh of traffic information. If another update
141 // request is in progress, new is discarded. If update request finished
142 // successfully, updated() signal called.
143 void Traffic::update ()
145 if (_fetcher.busy ()) {
146 Log::instance ()->add ("Traffic::update: fetcher is busy");
150 Log::instance ()->add ("Traffic::update: Request status download");
151 _fetcher.fetch ("http://trf.maps.yandex.net/trf/stat.xml");
155 void Traffic::fetchDone (const QByteArray& data)
158 if (parse_traffic_data (QString::fromUtf8 (data.data ()))) {
159 Log::instance ()->add ("Traffic::fetchDone: data parsed successfully");
163 Log::instance ()->add ("Traffic::fetchDone: data parse error");
167 bool Traffic::parse_traffic_data (const QString& xml)
175 QMap<QString, TrafficInfo> new_info;
176 QMap<QString, ExtendedTrafficInfo> new_ext_info;
178 if (!doc.setContent (xml))
182 e = doc.documentElement ();
183 if (e.isNull () || e.tagName () != "jams_stat")
186 s = e.attribute ("timestamp");
190 new_ts.setTime_t (s.toUInt (&ok));
196 while (!n.isNull ()) {
198 if (!e.isNull () && e.tagName () == "region") {
199 s = e.attribute ("id");
201 // Check that it is an extended traffic info
202 if (!e.firstChildElement ("level").isNull ()) {
203 ExtendedTrafficInfo info (e);
205 new_ext_info[s] = info;
208 TrafficInfo info (e);
213 catch (const QString& msg) {
216 n = n.nextSibling ();
221 _ext_info = new_ext_info;
227 TrafficInfo Traffic::lookup (const QString &id) const
229 QMap<QString, TrafficInfo>::const_iterator it = _info.find (id);
231 if (it == _info.end ())
232 return TrafficInfo ();
238 ExtendedTrafficInfo Traffic::lookup_ext (const QString &id) const
240 QMap<QString, ExtendedTrafficInfo>::const_iterator it = _ext_info.find (id);
242 if (it == _ext_info.end ())
243 return ExtendedTrafficInfo ();
249 void Traffic::connectionChanged (bool active)