InnerActiveAd.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import QtQuick 1.0
  2. Item {
  3. id: adContainer
  4. property string appid: ""
  5. //these probably don't need to be specified
  6. property string channelid: "551"
  7. property int screenWidth: 0
  8. property int screenHeight: 0
  9. //defaults - hopefully overridden
  10. //width: 100
  11. //height: 100
  12. //parent needs to set width and height so that way we can use it to request the correct sized ad
  13. //private properties
  14. //property string __adUrl: ""
  15. property string __clientId: ""
  16. property string __clickUrl: ""
  17. property string __imageUrl: ""
  18. signal finishedLoading()
  19. Image {
  20. id: ad
  21. //width: parent.width
  22. //height: parent.height
  23. anchors.horizontalCenter: parent.horizontalCenter
  24. anchors.verticalCenter: parent.verticalCenter
  25. width: parent.width <= sourceSize.width ? parent.width : sourceSize.width
  26. height: parent.height <= sourceSize.height ? parent.height : sourceSize.height
  27. fillMode: Image.PreserveAspectFit
  28. MouseArea {
  29. anchors.fill: parent
  30. id: adMouse
  31. onClicked: { console.log("you clicked the ad"); adClicked(); }
  32. }
  33. }
  34. Component.onCompleted: { init(); }
  35. //setup
  36. function init() {
  37. if(appid == "")
  38. console.log("Warning: no app id specified in inner active ad module");
  39. }
  40. //loads a new ad
  41. function loadAd() {
  42. var baseUrl = "http://m2m1.inner-active.com/simpleM2M/clientRequestAd?";
  43. var requestUrl = baseUrl + "aid=" + appid + "&po=" + channelid;
  44. if(__clientId != "")
  45. requestUrl = requestUrl + "&cid=" + __clientId;
  46. if(screenWidth > 0 && screenHeight > 0)
  47. requestUrl = requestUrl + "&w=" + screenWidth + "&h=" + screenHeight;
  48. //request ad
  49. var adResponse = new XMLHttpRequest();
  50. adResponse.onreadystatechange = function() {
  51. if(adResponse.readyState == XMLHttpRequest.DONE) {
  52. var doc = adResponse.responseXML.documentElement;
  53. var haveImage = false;
  54. var haveUrl = false
  55. //traverse(adResponse.responseXML);
  56. //console.log(adResponse.responseText)
  57. //process xml
  58. for(var i =0; i< doc.childNodes.length; i++) {
  59. console.log(doc.childNodes[i].nodeName + "=" + doc.childNodes[i].nodeValue);
  60. if(doc.childNodes[i].nodeName == 'Client')
  61. //assume only attribute is the client id
  62. __clientId = doc.childNodes[i].attributes[0].value;
  63. if(doc.childNodes[i].nodeName == "Ad") {
  64. var adNode = doc.childNodes[i];
  65. console.log("number of ad children " + doc.childNodes[i].length);
  66. for(var x =0; x < adNode.childNodes.length; x++) {
  67. console.log(adNode.childNodes[x].nodeName + "=" + adNode.childNodes[x].nodeValue);
  68. if(adNode.childNodes[x].nodeName == 'URL') {
  69. //Click URL
  70. __clickUrl = adNode.childNodes[x].firstChild.nodeValue;
  71. haveUrl = true;
  72. }
  73. if(adNode.childNodes[x].nodeName == 'Image') {
  74. //Image URL
  75. //console.log("image url " + adNode.childNodes[x].firstChild.nodeValue);
  76. __imageUrl = adNode.childNodes[x].firstChild.nodeValue
  77. haveImage = true;
  78. }
  79. if(adNode.childNodes[x].nodeName == 'Text') {
  80. //Banner text
  81. //not doing text ads right now
  82. }
  83. }
  84. }
  85. }
  86. //assign image url and click url
  87. if(haveUrl && haveImage) {
  88. ad.source = __imageUrl;
  89. adContainer.finishedLoading();
  90. }
  91. }
  92. }
  93. console.log("requesting: " + requestUrl);
  94. //pretend to be a Nokia N8
  95. adResponse.open("GET", requestUrl);
  96. adResponse.setRequestHeader("User-Agent","Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/014.002; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 BrowserNG/7.2.6.4 3gpp-gba");
  97. adResponse.send();
  98. }
  99. //ad clicked launch browser
  100. function adClicked() {
  101. //will probably need custom browser launcher code because of qt bugs with launching browser
  102. //http://www.developer.nokia.com/Community/Wiki/How_to_launch_other_applications_in_Qt
  103. if(__clickUrl != "") {
  104. console.log("Ad clicked, launching: " + __clickUrl);
  105. Qt.openUrlExternally(__clickUrl);
  106. }
  107. else
  108. console.log("Something strange here you clicked an ad but we don't have a URL registered");
  109. }
  110. }