requestqueue.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. *
  3. * requestqueue.cpp
  4. * © Copyrights 2012 inneractive LTD, Nokia. All rights reserved
  5. *
  6. * This file is part of inneractiveAdQML.
  7. *
  8. * inneractiveAdQML is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * inneractiveAdQML is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with inneractiveAdQML. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "requestqueue.h"
  22. #include "adinterface.h"
  23. #include <qplatformdefs.h>
  24. #include <QNetworkAccessManager>
  25. #include <QNetworkRequest>
  26. #include <QNetworkReply>
  27. #include <QUrl>
  28. #include <QDateTime>
  29. #include <QTimer>
  30. #include <QDebug>
  31. #include <QNetworkConfigurationManager>
  32. #include <QNetworkSession>
  33. RequestQueue::RequestQueue(AdInterface *parent) :
  34. QObject(parent)
  35. , m_nam(new QNetworkAccessManager(this))
  36. , m_requestRunning(false)
  37. , m_confman(new QNetworkConfigurationManager(this))
  38. , m_nsession(0)
  39. , m_onlineCheck(false)
  40. , m_networkError(false)
  41. {
  42. connect(m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(adRequestFinished(QNetworkReply*)));
  43. m_nsession = new QNetworkSession(m_confman->defaultConfiguration(), this);
  44. connect(m_nsession, SIGNAL(stateChanged(QNetworkSession::State)),
  45. this, SLOT(netSessionStateChanged(QNetworkSession::State)));
  46. connect(m_nsession, SIGNAL(opened()), this, SLOT(netSessionStateChanged()));
  47. }
  48. RequestQueue::~RequestQueue()
  49. {
  50. }
  51. void RequestQueue::netSessionStateChanged(QNetworkSession::State state)
  52. {
  53. AdInterface *adInterface = qobject_cast<AdInterface*>(parent());
  54. if (state == QNetworkSession::Connected) {
  55. emit adInterface->networkAccessibilityChanged(true);
  56. } else {
  57. emit adInterface->networkAccessibilityChanged(m_nsession->isOpen());
  58. }
  59. }
  60. bool RequestQueue::isOnline() const
  61. {
  62. return m_nsession->isOpen();
  63. }
  64. // Adds AdItem to request queue and calls handleRequests
  65. void RequestQueue::addToQueue(QObject *object)
  66. {
  67. // no need to add same object multipletimes
  68. if (!m_adItemQueue.contains(object)) {
  69. m_adItemQueue.enqueue(object);
  70. QTimer::singleShot(0, this, SLOT(handleRequests()));
  71. }
  72. }
  73. // Takes AdItem from queue and creates request for ad
  74. void RequestQueue::handleRequests()
  75. {
  76. // return if request queue is empty or other ad request is running
  77. if (m_adItemQueue.isEmpty() || m_requestRunning)
  78. return;
  79. m_requestRunning = true;
  80. QObject *adItem = m_adItemQueue.dequeue();
  81. QMetaObject::invokeMethod(adItem, "__createQuery", Qt::DirectConnection);
  82. QUrl requestUrl = adItem->property("__query").toUrl();
  83. if (!requestUrl.isValid()) {
  84. QMetaObject::invokeMethod(adItem, "adError",
  85. Q_ARG(QString, tr("Not valid query url")));
  86. m_requestRunning = false;
  87. return;
  88. }
  89. #if defined(Q_OS_SYMBIAN) || defined(MEEGO_EDITION_HARMATTAN) || defined(Q_WS_MAEMO_5)
  90. // online checking only on mobile
  91. if (!m_nsession->isOpen()) {
  92. if (!m_onlineCheck) {
  93. m_onlineCheck = true;
  94. m_nsession->open();
  95. if (!m_nsession->waitForOpened()) {
  96. AdInterface *adI = qobject_cast<AdInterface*>(parent());
  97. emit adI->networkNotAccessible();
  98. m_networkError = true;
  99. }
  100. } else {
  101. if (!m_networkError) {
  102. m_networkError = true;
  103. AdInterface *adI = qobject_cast<AdInterface*>(parent());
  104. emit adI->networkNotAccessible();
  105. }
  106. QMetaObject::invokeMethod(adItem, "adError",
  107. Q_ARG(QString, tr("Network not accessible")));
  108. m_requestRunning = false;
  109. return;
  110. }
  111. }
  112. #endif
  113. // Add timestamp to request
  114. requestUrl.addQueryItem(QLatin1String("t"), QString::number(QDateTime::currentDateTime().toTime_t()));
  115. // Set User-Agent header
  116. QNetworkRequest req(requestUrl);
  117. req.setRawHeader("User-Agent", m_userAgent);
  118. qDebug() << "AdRequest:" << req.url();
  119. qDebug() << "UA:" << m_userAgent;
  120. QNetworkReply *rep = m_nam->get(req);
  121. rep->setProperty("AdItem", QVariant::fromValue(adItem));
  122. }
  123. void RequestQueue::adRequestFinished(QNetworkReply *req)
  124. {
  125. if (!req)
  126. return;
  127. QObject *adItem = req->property("AdItem").value<QObject*>();
  128. if (!adItem) {
  129. req->deleteLater();
  130. return;
  131. }
  132. QByteArray data = req->readAll();
  133. m_requestRunning = false;
  134. if (req->attribute(QNetworkRequest::HttpStatusCodeAttribute) != 200) {
  135. QMetaObject::invokeMethod(adItem, "adError",
  136. Q_ARG(QString,req->errorString()));
  137. // When no connectivity -> UnknownNetworkError
  138. if (req->error() == QNetworkReply::UnknownNetworkError) {
  139. AdInterface *adI = qobject_cast<AdInterface*>(parent());
  140. emit adI->networkNotAccessible();
  141. }
  142. req->deleteLater();
  143. QTimer::singleShot(0, this, SLOT(handleRequests()));
  144. return;
  145. }
  146. req->deleteLater();
  147. // Update client ID
  148. int idStart = data.indexOf("Client Id=\"")+11;
  149. int idSize = data.indexOf("\"/>",data.indexOf("Id=\""))-idStart;
  150. QMetaObject::invokeMethod(adItem, "__idUpdated", Qt::DirectConnection,
  151. Q_ARG(QVariant, data.mid(idStart,idSize)));
  152. adItem->setProperty("__xml", QString::fromUtf8(data));
  153. QTimer::singleShot(0, this, SLOT(handleRequests()));
  154. }