Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. parser_abstract.h:
  2. /*******************************************************************************
  3.  
  4. This file is a part of Fahrplan for maemo 2009-2010
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19.  
  20. */
  21.  
  22. #ifndef PARSER_ABSTRACT_H
  23. #define PARSER_ABSTRACT_H
  24.  
  25. #include <QObject>
  26. #include <QtNetwork>
  27.  
  28. //#include <QNetworkAccessManager>
  29.  
  30. //#include "mainwindow.h"
  31.  
  32. struct DetailResultItem
  33. {
  34. QString fromStation;
  35. QString fromInfo;
  36. QDateTime fromTime;
  37. QString toStation;
  38. QString toInfo;
  39. QDateTime toTime;
  40. QString train;
  41. QString info;
  42. };
  43.  
  44. struct DetailResultInfo
  45. {
  46. QString duration;
  47. QString info;
  48. QList<DetailResultItem> items;
  49. };
  50.  
  51. struct ResultItem
  52. {
  53. QString id;
  54. QString fromTime;
  55. QString toTime;
  56. QString trainType;
  57. QString duration;
  58. QString changes;
  59. QString state;
  60. QString detailsUrl;
  61. QDate tripDate;
  62. DetailResultInfo detailsInfo;
  63. };
  64.  
  65. struct ResultInfo
  66. {
  67. QString errorMsg;
  68. QString fromStation;
  69. QString toStation;
  70. QString timeInfo;
  71. QString earlierUrl;
  72. QString laterUrl;
  73. QList<ResultItem> items;
  74. };
  75.  
  76. class parserAbstract : public QObject
  77. {
  78. Q_OBJECT
  79. public:
  80. virtual QStringList getStationsByName(QString stationName);
  81. virtual QStringList getTrainRestrictions();
  82. virtual QStringList getStationsByGPS(qreal latitude, qreal longitude);
  83. virtual ResultInfo getJourneyData(QString destinationStation, QString arrivalStation, QString viaStation, QDate date, QTime time, int mode, int trainrestrictions);
  84. virtual ResultInfo getJourneyData(QString queryUrl);
  85. virtual DetailResultInfo getJourneyDetailsData(QString queryUrl);
  86. virtual void httpRequestTimeout();
  87.  
  88. // void NetworkManagerInit(void);
  89.  
  90. virtual bool supportsGps();
  91. signals:
  92.  
  93. public slots:
  94.  
  95. protected:
  96. int currentRequestId;
  97. QEventLoop loop;
  98.  
  99. public:
  100. int progress;
  101. int progressTotal;
  102.  
  103.  
  104. private slots:
  105. void httpRequestFinished(int requestId, bool error);
  106. void httpDataReadProgress(int done, int total);
  107.  
  108. // void replyFinished(QNetworkReply*);
  109.  
  110. };
  111.  
  112.  
  113.  
  114.  
  115.  
  116. #endif // PARSER_ABSTRACT_H
  117.  
  118. parser_abstract.cpp:
  119. /*******************************************************************************
  120.  
  121. This file is a part of Fahrplan for maemo 2009-2010
  122.  
  123. This program is free software; you can redistribute it and/or modify
  124. it under the terms of the GNU General Public License as published by
  125. the Free Software Foundation; either version 2 of the License, or
  126. (at your option) any later version.
  127.  
  128. This program is distributed in the hope that it will be useful,
  129. but WITHOUT ANY WARRANTY; without even the implied warranty of
  130. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  131. GNU General Public License for more details.
  132.  
  133. You should have received a copy of the GNU General Public License
  134. along with this program; if not, write to the Free Software
  135. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  136.  
  137. */
  138.  
  139. #include "parser_abstract.h"
  140.  
  141.  
  142. void parserAbstract::httpDataReadProgress(int done, int total)
  143. {
  144. qDebug() << "httpDataReadProgress:" << done << "/" << total;
  145. progress = done;
  146. progressTotal = total;
  147. }
  148.  
  149.  
  150.  
  151. void parserAbstract::httpRequestFinished(int requestId, bool error)
  152. {
  153. Q_UNUSED(error);
  154. if(currentRequestId != requestId) return;
  155. loop.exit();
  156. }
  157.  
  158.  
  159. void parserAbstract::httpRequestTimeout()
  160. {
  161. loop.exit();
  162. }
  163.  
  164.  
  165.  
  166.  
  167. //void parserAbstract::NetworkManagerInit(void)
  168. //{
  169. // qDebug() << "NetworkManager init";
  170.  
  171.  
  172. //}
  173.  
  174.  
  175.  
  176.  
  177.  
  178. //void parserAbstract::replyFinished(QNetworkReply*)
  179. //{
  180. // qDebug() << "NetworkManager reply finished";
  181. // loop.exit();
  182. //}
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. bool parserAbstract::supportsGps()
  198. {
  199. return false;
  200. }
  201.  
  202. QStringList parserAbstract::getStationsByName(QString stationName)
  203. {
  204. Q_UNUSED(stationName);
  205. QStringList result;
  206. return result;
  207. }
  208.  
  209. QStringList parserAbstract::getTrainRestrictions()
  210. {
  211. QStringList result;
  212. return result;
  213. }
  214.  
  215. QStringList parserAbstract::getStationsByGPS(qreal latitude, qreal longitude)
  216. {
  217. Q_UNUSED(latitude);
  218. Q_UNUSED(longitude);
  219. QStringList result;
  220. return result;
  221. }
  222.  
  223. ResultInfo parserAbstract::getJourneyData(QString destinationStation, QString arrivalStation, QString viaStation, QDate date, QTime time, int mode, int trainrestrictions)
  224. {
  225. Q_UNUSED(destinationStation);
  226. Q_UNUSED(arrivalStation);
  227. Q_UNUSED(viaStation);
  228. Q_UNUSED(date);
  229. Q_UNUSED(time);
  230. Q_UNUSED(mode);
  231. Q_UNUSED(trainrestrictions);
  232. ResultInfo result;
  233. return result;
  234. }
  235.  
  236. ResultInfo parserAbstract::getJourneyData(QString queryUrl)
  237. {
  238. Q_UNUSED(queryUrl);
  239. ResultInfo result;
  240. return result;
  241. }
  242.  
  243. DetailResultInfo parserAbstract::getJourneyDetailsData(QString queryUrl)
  244. {
  245. Q_UNUSED(queryUrl);
  246. DetailResultInfo result;
  247. return result;
  248. }
  249.  
  250. parser_translink.h:
  251. /*******************************************************************************
  252.  
  253. This file is a part of Fahrplan for maemo 2009-2010
  254.  
  255. This program is free software; you can redistribute it and/or modify
  256. it under the terms of the GNU General Public License as published by
  257. the Free Software Foundation; either version 2 of the License, or
  258. (at your option) any later version.
  259.  
  260. This program is distributed in the hope that it will be useful,
  261. but WITHOUT ANY WARRANTY; without even the implied warranty of
  262. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  263. GNU General Public License for more details.
  264.  
  265. You should have received a copy of the GNU General Public License
  266. along with this program; if not, write to the Free Software
  267. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  268.  
  269. */
  270.  
  271. #ifndef PARSER_TRANSLINK_H
  272. #define PARSER_TRANSLINK_H
  273.  
  274. #include <QObject>
  275. #include <QtNetwork>
  276. #include <qjson/parser.h>
  277. #include "fahrplanutils.h"
  278.  
  279. #include "parser_abstract.h"
  280.  
  281. class parserTranslink : public parserAbstract
  282. {
  283. Q_OBJECT
  284. public:
  285. explicit parserTranslink(QObject *parent = 0);
  286. QStringList getStationsByName(QString stationName);
  287. QStringList getTrainRestrictions();
  288. QStringList getStationsByGPS(qreal latitude, qreal longitude);
  289. ResultInfo getJourneyData(QString destinationStation, QString arrivalStation, QString viaStation, QDate date, QTime time, int mode, int trainrestrictions);
  290. ResultInfo getJourneyData(QString queryUrl);
  291. DetailResultInfo getJourneyDetailsData(QString queryUrl);
  292. bool supportsGps();
  293.  
  294. signals:
  295.  
  296. public slots:
  297.  
  298. private:
  299. QHttp *http;
  300. QBuffer *filebuffer;
  301. ResultInfo parseJourneyData(QByteArray data);
  302. };
  303.  
  304. #endif // PARSER_TRANSLINK_H
  305.  
  306. parser_translink.cpp:
  307. /*******************************************************************************
  308.  
  309. This file is a part of Fahrplan for maemo 2009-2010
  310.  
  311. This program is free software; you can redistribute it and/or modify
  312. it under the terms of the GNU General Public License as published by
  313. the Free Software Foundation; either version 2 of the License, or
  314. (at your option) any later version.
  315.  
  316. This program is distributed in the hope that it will be useful,
  317. but WITHOUT ANY WARRANTY; without even the implied warranty of
  318. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  319. GNU General Public License for more details.
  320.  
  321. You should have received a copy of the GNU General Public License
  322. along with this program; if not, write to the Free Software
  323. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  324.  
  325. */
  326.  
  327. #include "parser_translink.h"
  328.  
  329. parserTranslink::parserTranslink(QObject *parent)
  330. {
  331. Q_UNUSED(parent);
  332. http = new QHttp(this);
  333.  
  334. connect(http, SIGNAL(requestFinished(int,bool)),
  335. this, SLOT(httpRequestFinished(int,bool)));
  336. }
  337.  
  338. bool parserTranslink::supportsGps()
  339. {
  340. return true;
  341. }
  342.  
  343. QStringList parserTranslink::getStationsByName(QString stationName)
  344. {
  345. QByteArray postData = "location=";
  346. postData.append(stationName);
  347.  
  348. qDebug() << "Translink: getStationsByName";
  349.  
  350. QUrl url("https://app.jp.translink.com.au/plan-your-journey/location/find-location");
  351.  
  352. http->setHost(url.host(), QHttp::ConnectionModeHttps, url.port() == -1 ? 0 : url.port());
  353.  
  354. filebuffer = new QBuffer();
  355.  
  356. if (!filebuffer->open(QIODevice::WriteOnly))
  357. {
  358. qDebug() << "Can't open Buffer";
  359. }
  360.  
  361. QHttpRequestHeader header;
  362. header.setRequest("POST", url.path());
  363. header.setValue("User-Agent", "Wget/1.10.2");
  364. header.setValue("Accept", "*/*");
  365. header.setValue("Host", url.host());
  366. header.setValue("Content-Type", "application/x-www-form-urlencoded");
  367. header.setValue("Content-Length", QString::number(postData.length()));
  368.  
  369. currentRequestId = http->request(header, postData, filebuffer);
  370.  
  371. loop.exec();
  372.  
  373. filebuffer->close();
  374.  
  375. QJson::Parser parser;
  376.  
  377. bool ok;
  378.  
  379. QStringList result;
  380. QVariantList list = parser.parse(filebuffer->buffer(),&ok).toList();
  381. foreach (QVariant station, list)
  382. {
  383. QVariantMap map = station.toMap();
  384. result.append(map["Description"].toString());
  385. }
  386.  
  387. return result;
  388. }
  389.  
  390. QStringList parserTranslink::getTrainRestrictions()
  391. {
  392. QStringList result;
  393. result.append(tr("Bus"));
  394. result.append(tr("Train"));
  395. result.append(tr("Ferry"));
  396. result.append(tr("Tram"));
  397. return result;
  398. }
  399.  
  400. QStringList parserTranslink::getStationsByGPS(qreal latitude, qreal longitude)
  401. {
  402. Q_UNUSED(latitude);
  403. Q_UNUSED(longitude);
  404. QStringList result;
  405. return result;
  406. }
  407.  
  408. ResultInfo parserTranslink::getJourneyData(QString destinationStation, QString arrivalStation, QString viaStation, QDate date, QTime time, int mode, int trainrestrictions)
  409. {
  410. Q_UNUSED(destinationStation);
  411. Q_UNUSED(arrivalStation);
  412. Q_UNUSED(viaStation);
  413. Q_UNUSED(date);
  414. Q_UNUSED(time);
  415. Q_UNUSED(mode);
  416. Q_UNUSED(trainrestrictions);
  417. ResultInfo result;
  418. return result;
  419. }
  420.  
  421. ResultInfo parserTranslink::getJourneyData(QString queryUrl)
  422. {
  423. Q_UNUSED(queryUrl);
  424. ResultInfo result;
  425. return result;
  426. }
  427.  
  428. DetailResultInfo parserTranslink::getJourneyDetailsData(QString queryUrl)
  429. {
  430. Q_UNUSED(queryUrl);
  431. DetailResultInfo result;
  432. return result;
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement