downloadmanager.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "downloadmanager.h"
  2. #include <e32base.h>
  3. #include <QNetworkAccessManager>
  4. #include <QNetworkRequest>
  5. #include <QUrl>
  6. #include <QStringList>
  7. #include <QVariant>
  8. #include <QDebug>
  9. //TODO: replace KHostName value with your host URL
  10. #define KHostName "http://www.izinin.fn-sandbox.net"
  11. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. // PTV server API definition see http://projects.developer.nokia.com/iap_sample/wiki
  13. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. #define KCmdGetProductList "/mobile_getproductlist.php"
  15. #define KCmdGetProductURL "/mobile_getproducturl.php"
  16. #define KAccountFieldName "accountdigest"
  17. #define KProductIdFieldName "productid"
  18. #define KProductTicketFieldName "ticket"
  19. // predefined server response
  20. #define KHaveToBuyResponse "HAVE2BUY"
  21. // common request related literals definition
  22. #define KmanagerLineBreak "</br>"
  23. #define KFieldDelimiter "&"
  24. DownloadManager::DownloadManager(QObject* parent):
  25. QObject(parent),
  26. state(State_IDLE)
  27. {
  28. manager = new QNetworkAccessManager(this);
  29. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
  30. }
  31. int DownloadManager::getProductList(QStringList& products)
  32. {
  33. if(state != State_IDLE){
  34. qDebug() << "DownloadManager is busy, getProductList rejected";
  35. return KErrInUse;
  36. }
  37. state = State_GETPRODUCTLIST;
  38. productList = &products;
  39. productList->clear();
  40. QNetworkRequest request;
  41. request.setUrl(QUrl(KHostName KCmdGetProductList));
  42. QNetworkReply *reply = manager->get(request);
  43. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
  44. this, SLOT(replyError(QNetworkReply::NetworkError)));
  45. return KErrNone;
  46. }
  47. void DownloadManager::replyFinished(QNetworkReply* reply)
  48. {
  49. QByteArray buf;
  50. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
  51. if(reply->error() != QNetworkReply::NoError){
  52. qDebug() << "http reply error : " << reply->error();
  53. }else{
  54. buf = reply->readAll();
  55. }
  56. switch(state){
  57. case State_GETPRODUCTLIST:
  58. handleProductList(buf);
  59. break;
  60. case State_GETPRODUCT:
  61. handleProduct(buf);
  62. break;
  63. default:
  64. break;
  65. }
  66. state = State_IDLE;
  67. }
  68. void DownloadManager::handleProductList(QByteArray& buf)
  69. {
  70. QTextStream stream(&buf);
  71. QString line = stream.readLine();
  72. while (!line.isNull()){
  73. line.trimmed();
  74. if(!line.isEmpty()){
  75. QStringList elems = line.split(KmanagerLineBreak);
  76. foreach(QString product, elems){
  77. if(!product.isEmpty()){
  78. productList->append(product.trimmed());
  79. }
  80. }
  81. }
  82. line = stream.readLine();
  83. }
  84. emit productListComplete();
  85. }
  86. void DownloadManager::handleProduct(QByteArray& buf)
  87. {
  88. emit productComplete(&buf);
  89. }
  90. void DownloadManager::replyError(QNetworkReply::NetworkError error)
  91. {
  92. qDebug() << "http error : " << error;
  93. }
  94. int DownloadManager::getProduct(QString account,
  95. QString productId, QString* ticket)
  96. {
  97. if(state != State_IDLE){
  98. qDebug() << "DownloadManager is busy, getProduct rejected";
  99. return KErrInUse;
  100. }
  101. qDebug() << "PTV server getProduct : " << "account digest "
  102. << account << "product ID" << productId;
  103. state = State_GETPRODUCT;
  104. QNetworkRequest request;
  105. request.setUrl(QUrl(KHostName KCmdGetProductURL));
  106. QByteArray postData;
  107. postData.append(KAccountFieldName "=");
  108. //NOTE "+" character that may be in base64 string
  109. // stands for space symbol in the POST data encoding scheme
  110. postData.append(QUrl::toPercentEncoding(account, "", "+"));
  111. postData.append(KFieldDelimiter);
  112. postData.append(KProductIdFieldName "=");
  113. postData.append(productId);
  114. if(ticket){
  115. postData.append(KFieldDelimiter);
  116. postData.append(KProductTicketFieldName "=");
  117. postData.append(QUrl::toPercentEncoding(*ticket, "", "+"));
  118. }
  119. QNetworkReply *reply = manager->post(request, postData);
  120. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
  121. this, SLOT(replyError(QNetworkReply::NetworkError)));
  122. return KErrNone;
  123. }
  124. // NOTE: this function is only
  125. // for having PTV server API definition in one place, this module
  126. bool DownloadManager::isNeedToBuy(QByteArray& response)
  127. {
  128. return (response == KHaveToBuyResponse);
  129. }