AdItem.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. *
  3. * AdItem.qml
  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. import QtQuick 1.0
  22. import "adFunctions.js" as AdF
  23. /*!
  24. \qmlclass AdItem
  25. \ingroup com.inneractive
  26. \brief Item to show Ad banner.
  27. AdItem shows image and optionaly text (see \l {showText}).
  28. The AdItem component is part of the \l {inneractive QML Components} module.
  29. */
  30. Item {
  31. id: root
  32. Component.onCompleted: {
  33. requestAd()
  34. }
  35. width: scaleAd ? width : adImage.width
  36. height: scaleAd ? height : (showText ? adImage.height + adText.paintedHeight : adImage.height)
  37. /*!
  38. \qmlproperty string AdItem::adTextString
  39. Text string of ad.
  40. Read-only
  41. */
  42. property alias adTextString: adText.text
  43. /*!
  44. \qmlproperty string AdItem::adImageUrl
  45. Url of image shown in ad.
  46. Read-only
  47. */
  48. property alias adImageUrl: adImage.source
  49. /*!
  50. Url which is opened when user clicks ad.
  51. Read-only
  52. */
  53. property url adClickUrl
  54. /*!
  55. \qmlproperty boolean AdItem::textClip
  56. Clip text of ad. See Text::clip.
  57. */
  58. property alias textClip: adText.clip
  59. /*!
  60. Show text in AdItem.
  61. The default value is true
  62. */
  63. property bool showText: true
  64. /*!
  65. Hide ad after user has clicked it open.
  66. The default value is false
  67. */
  68. property bool hideOnClick: false
  69. /*!
  70. Scale ad image to fit width and height.
  71. If false, AdItem will be scaled to contain image and text.
  72. The default value is false
  73. */
  74. property bool scaleAd: false
  75. /*!
  76. Ad request is automatically retried if previous request fails.
  77. The default value is false
  78. */
  79. property bool retryOnError: false
  80. /*!
  81. Interval at which AdItem requests new ad, in seconds.
  82. Value 0 disables automatic requests and requestAd() needs to be called to get new ad.
  83. The default value is 60
  84. */
  85. property int reloadInterval: 60
  86. /*!
  87. Status of the ad request.
  88. \list
  89. \o "Null" request not done yet
  90. \o "Loading" request is not yet ready
  91. \o "Error" request failed, error message set to \l {errorString}
  92. \o "Done" ad request loaded successfully
  93. \endlist
  94. Read-only
  95. */
  96. property string status: "Null"
  97. /*!
  98. Readable cause of last occured error.
  99. Read-only
  100. */
  101. property string errorString
  102. /*!
  103. Parameters used in ad request.
  104. This is mandatory and AdParameters have to contain \l {AdParameters::applicationId}{applicationId} and \l {AdParameters::distributionId}{distributionId}.
  105. */
  106. property AdParameters parameters
  107. /*!
  108. This signal is emited when ad request has completed and no error has occured.
  109. */
  110. signal adLoaded()
  111. /*!
  112. This signal is emited when ad request has failed.
  113. errorString contains message why request failed.
  114. */
  115. signal adError(string errorString)
  116. /*!
  117. This signal is emited when ad is clicked.
  118. */
  119. signal adClicked()
  120. /*!
  121. \brief Requests new Ad
  122. Status will update to "Loading".
  123. */
  124. function requestAd()
  125. {
  126. reloadTimer.running = false;
  127. retryTimer.running = false;
  128. status = "Loading";
  129. adInterface.requestAd(root);
  130. }
  131. /*!
  132. \internal
  133. Property alias for RequestQueue to set reply xml
  134. */
  135. property alias __xml: adModel.xml
  136. /*!
  137. \internal
  138. */
  139. property url __query
  140. /*!
  141. \internal
  142. Used by RequestQueue to create __query url
  143. */
  144. function __createQuery()
  145. {
  146. AdF.createQuery(root);
  147. }
  148. /*!
  149. \internal
  150. Used by RequestQueue to update AdParameters::__clientId
  151. */
  152. function __idUpdated(id)
  153. {
  154. parameters.__clientId = id;
  155. }
  156. // Handle networkaccessibility change
  157. Connections {
  158. target: adInterface
  159. onNetworkAccessibilityChanged: {
  160. if (!adInterface.networkAccessible)
  161. return;
  162. if ((status == "Error" && retryOnError) || status == "Null")
  163. requestAd();
  164. }
  165. }
  166. onAdError: {
  167. status = "Error";
  168. root.errorString = errorString;
  169. if (retryOnError && adInterface.networkAccessible)
  170. retryTimer.start();
  171. console.debug("Ad Error: " + errorString);
  172. }
  173. onAdLoaded: {
  174. status = "Done";
  175. console.debug("Ad Loaded");
  176. }
  177. Timer {
  178. id: reloadTimer
  179. interval: reloadInterval * 1000
  180. onTriggered: requestAd()
  181. }
  182. Timer {
  183. id: retryTimer
  184. interval: 5000
  185. onTriggered: requestAd()
  186. }
  187. XmlListModel {
  188. id: adModel
  189. namespaceDeclarations: "declare default element namespace 'http://www.inner-active.com/SimpleM2M/M2MResponse';"
  190. query: "/Response/Ad"
  191. XmlRole { name: "text"; query: "Text/string()" }
  192. XmlRole { name: "url"; query: "URL/string()" }
  193. XmlRole { name: "image"; query: "Image/string()" }
  194. onStatusChanged: {
  195. if (status == XmlListModel.Ready && adModel.count > 0) {
  196. adTextString = adModel.get(0).text;
  197. adClickUrl = adModel.get(0).url;
  198. adImageUrl = adModel.get(0).image;
  199. root.visible = true;
  200. adLoaded();
  201. if (reloadInterval > 0) {
  202. reloadTimer.start();
  203. }
  204. } else if (status == XmlListModel.Error) {
  205. root.visible = false;
  206. adError(errorString());
  207. }
  208. }
  209. }
  210. Image {
  211. id: adImage
  212. width: scaleAd ? root.width : sourceSize.width
  213. height: scaleAd
  214. ? (showText ? root.height - adText.paintedHeight : root.height)
  215. : sourceSize.height
  216. anchors.horizontalCenter: root.horizontalCenter
  217. anchors.top: root.top
  218. fillMode: Image.PreserveAspectFit
  219. }
  220. Text {
  221. id: adText
  222. width: scaleAd ? root.width : adImage.sourceSize.width
  223. clip: true
  224. wrapMode: Text.WordWrap
  225. visible: showText
  226. anchors.horizontalCenter: root.horizontalCenter
  227. anchors.top: adImage.bottom
  228. }
  229. MouseArea {
  230. id: clickArea
  231. anchors.fill: root
  232. onClicked: {
  233. adInterface.openAd(root.adClickUrl);
  234. adClicked();
  235. if (root.hideOnClick) {
  236. root.visible = false;
  237. }
  238. }
  239. }
  240. }