adinterface.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. *
  3. * adinterface.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 "adinterface.h"
  22. #include "requestqueue.h"
  23. #include <qplatformdefs.h>
  24. #include <QEventLoop>
  25. #include <QVariant>
  26. #include <QTimer>
  27. #include <QDebug>
  28. #include <QProcess>
  29. #include <QUrl>
  30. #include <QDesktopServices>
  31. #ifdef Q_OS_SYMBIAN
  32. #include <APGTASK.H>
  33. #include <APGCLI.H>
  34. #include <EIKENV.H>
  35. #include <cuseragent/cuseragent.h>
  36. #endif
  37. /*!
  38. \qmlclass AdInterface
  39. \ingroup com.inneractive
  40. \inherits QtObject
  41. The adInterface object is part of the \l {inneractive QML Components} module.
  42. AdInterface is exposed as a context property called "adInterface". It is not possible to
  43. instantiate a AdInterface object in QML.
  44. */
  45. /*!
  46. \qmlproperty bool AdInterface::networkAccessible
  47. This property contains information of network accessibility.
  48. */
  49. /*!
  50. \qmlsignal AdInterface::networkNotAccessible()
  51. This signal is emited after network connection fails or user doesn't allow
  52. connection to internet.
  53. For example:
  54. \code
  55. Connections {
  56. target: adInterface
  57. onNetworkNotAccessible: {
  58. //... show dialog to user about networkaccessibility and quit application...
  59. }
  60. }
  61. \endcode
  62. */
  63. AdInterface::AdInterface(QObject *parent) :
  64. QObject(parent)
  65. , m_requestQueue(new RequestQueue(this))
  66. {
  67. checkUserAgent();
  68. }
  69. bool AdInterface::networkAccessible() const
  70. {
  71. return m_requestQueue->isOnline();
  72. }
  73. void AdInterface::requestAd(const QVariant &adItem)
  74. {
  75. QObject *object = adItem.value<QObject*>();
  76. m_requestQueue->addToQueue(object);
  77. }
  78. // Opens url in native browser in Harmattan and Symbian
  79. void AdInterface::openAd(const QUrl &adUrl)
  80. {
  81. #if defined(Q_OS_SYMBIAN)
  82. QString adUrlString(adUrl.toString());
  83. TUid browserUid = TUid::Uid(0x10008D39);
  84. adUrlString.prepend("4 ");
  85. TUid id( browserUid ); // S3
  86. TApaTaskList taskList( CEikonEnv::Static()->WsSession() );
  87. TApaTask task = taskList.FindApp( id );
  88. if ( task.Exists() ) {
  89. task.SendMessage( TUid::Uid( 0 ), TPtrC8((TUint8*)adUrlString.toUtf8().data()));
  90. } else {
  91. RApaLsSession appArcSession;
  92. TThreadId id;
  93. User::LeaveIfError( appArcSession.Connect() );
  94. appArcSession.StartDocument( TPtrC16(static_cast<const TUint16*>(adUrlString.utf16()), adUrlString.length()), browserUid, id);
  95. appArcSession.Close();
  96. }
  97. #elif defined(MEEGO_EDITION_HARMATTAN)
  98. QProcess::startDetached(QString("/usr/bin/invoker --type=m /usr/bin/grob %1").arg(adUrl.toString()));
  99. #else
  100. QDesktopServices::openUrl(adUrl);
  101. #endif
  102. }
  103. void AdInterface::checkUserAgent()
  104. {
  105. #ifdef Q_OS_SYMBIAN
  106. CUserAgent *ua = 0;
  107. HBufC8 *uabuf = 0;
  108. QT_TRAP_THROWING(ua = CUserAgent::NewL());
  109. QT_TRAP_THROWING(uabuf = ua->UserAgentL());
  110. m_userAgent = QByteArray((char*)uabuf->Des().Ptr(),uabuf->Length());
  111. m_userAgent.append(" 3gpp-gba");
  112. delete ua;
  113. delete uabuf;
  114. #elif defined(MEEGO_EDITION_HARMATTAN)
  115. m_userAgent = "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13";
  116. #elif defined(Q_WS_MAEMO_5)
  117. m_userAgent = "Mozilla/5.0 (X11; U; Linux armv7l; en-GB; rv:1.9.2.3pre) Gecko/20100723 Firefox/3.5 Maemo Browser 1.7.4.8 RX-51 N900";
  118. #else
  119. m_userAgent = "Mozilla/5.0 Safari";
  120. #endif
  121. m_requestQueue->setUserAgent(m_userAgent);
  122. }