Debian Codegraph @

src/carrierinfo.h

raw symbols SCIP
1 // Copyright (C) 2014 Sacha Refshauge
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 3.0.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 3.0 for more details.
11
12 // A copy of the GPL 3.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official GIT repository and contact information can be found at
16 // http://github.com/xsacha/Sachesi
17
18 #pragma once
19 #include <QNetworkAccessManager>
20 #include <QNetworkReply>
21 #include <QTimer>
22 #include <QXmlStreamReader>
23
24 class CarrierInfo : public QObject {
25 Q_OBJECT
26 Q_PROPERTY(QString country MEMBER _country NOTIFY resultChanged)
27 Q_PROPERTY(QString carrier MEMBER _carrier NOTIFY resultChanged)
28 Q_PROPERTY(QString mcc MEMBER _mcc NOTIFY resultChanged)
29 Q_PROPERTY(QString mnc MEMBER _mnc NOTIFY resultChanged)
30 Q_PROPERTY(int image MEMBER _image NOTIFY resultChanged)
31
32 public:
33 CarrierInfo(QObject* parent = 0)
34 : QObject(parent)
35 {
36 _manager = new QNetworkAccessManager();
37 _timer = new QTimer();
38 _timer->setSingleShot(true);
39 connect(_timer, SIGNAL(timeout()), this, SLOT(update()));
40 _mcc = "";
41 _mnc = "";
42 _image = 0;
43 }
44
45 Q_INVOKABLE void mccChange(QString mcc) {
46 if (mcc != _mcc) {
47 _mcc = mcc;
48 update();
49 }
50 }
51
52 Q_INVOKABLE void mncChange(QString mnc){
53 if (mnc != _mnc) {
54 _mnc = mnc;
55 update();
56 }
57 }
58
59 public slots:
60 void update() {
61 // Make sure the search is still relevant when the results come in
62 QString mcc = _mcc;
63 QString mnc = _mnc;
64 if (mcc == "" || mnc == "")
65 return;
66
67 QNetworkRequest request(QString("http://appworld.blackberry.com/ClientAPI/checkcarrier?homemcc=%1&homemnc=%2&devicevendorid=-1&pin=0")
68 .arg(_mcc)
69 .arg(_mnc));
70 request.setHeader(QNetworkRequest::KnownHeaders::UserAgentHeader, "AppWorld/5.1.0.60");
71 QNetworkReply* reply = _manager->get(request);
72 connect(reply, &QNetworkReply::finished, [=] {
73 int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
74 if (reply->size() > 0 && (status == 200 || (status > 300 && status <= 308))) {
75 if (mcc != _mcc || mnc != _mnc)
76 return;
77 QXmlStreamReader xml(reply->readAll());
78 while(!xml.atEnd() && !xml.hasError()) {
79 if(xml.tokenType() == QXmlStreamReader::StartElement) {
80 if (xml.name() == "country") {
81 _country = xml.attributes().value("name").toString();
82 } else if (xml.name() == "carrier") {
83 _carrier = xml.attributes().value("name").toString().split(" ").first();
84 _image = xml.attributes().value("icon").toInt();
85
86 // Don't show generic
87 if (_carrier == "default") {
88 _carrier = "";
89 _image = 0;
90 }
91 }
92 }
93 xml.readNext();
94 }
95 }
96 emit resultChanged();
97 reply->deleteLater();
98 });
99 connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), [=]() {
100 _country = "";
101 _carrier = "";
102 _image = 0;
103 reply->deleteLater();
104 });
105 }
106
107 signals:
108 void resultChanged();
109 private:
110 QString _country, _carrier;
111 QString _mcc, _mnc;
112 int _image;
113 QNetworkAccessManager* _manager;
114 QTimer* _timer;
115 };