Advertisement
Guest User

Untitled

a guest
Dec 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "serverinfo.h"
  2.  
  3. QJsonObject ServerInfo::objectFromString(const QString& in) {
  4.     QJsonObject obj;
  5.     QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());
  6.  
  7.     if (!doc.isNull()) {
  8.         if (doc.isObject()) {
  9.             obj = doc.object();
  10.         } else {
  11.             qDebug() << "Document is not an object" << endl;
  12.         }
  13.     } else {
  14.         qDebug() << "Invalid JSON...\n" << in << endl;
  15.     }
  16.     return obj;
  17. }
  18.  
  19. ServerInfo::ServerInfo(QObject *parent) : QObject(parent){
  20.     manager = new QNetworkAccessManager(this);
  21.     connect(manager, SIGNAL(finished(QNetworkReply*)),
  22.             this, SLOT(recive(QNetworkReply*)));
  23.     manager->get(QNetworkRequest(QUrl("http://ip-api.com/json")));
  24. }
  25.  
  26. void ServerInfo::recive(QNetworkReply *reply){
  27.     QString rep = reply->readAll();
  28.     delete reply;
  29.     QJsonObject obj = objectFromString(rep);
  30.     setIpAdress(obj.value("query").toString());
  31.     setIsp(obj.value("isp").toString());
  32.     setCity(obj.value("city").toString());
  33.     setCountry(obj.value("country").toString());
  34.     emit ready();
  35. }
  36.  
  37.  
  38. QString ServerInfo::getCity() const { return m_city; }
  39. QString ServerInfo::getIsp() const { return m_isp; }
  40. QString ServerInfo::getCountry() const { return m_country; }
  41. QString ServerInfo::getIpAdress() const { return m_ipAdress; }
  42. void ServerInfo::setCountry(const QString &country) { m_country = country; }
  43. void ServerInfo::setIpAdress(const QString &ipAdress) { m_ipAdress = ipAdress; }
  44. void ServerInfo::setIsp(const QString &isp) { m_isp = isp; }
  45. void ServerInfo::setCity(const QString &city) { m_city = city; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement