symbianclientapihelper.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. *
  5. * For the applicable distribution terms see the license.txt -file, included in
  6. * the distribution.
  7. */
  8. #include "symbianclientapihelper.h"
  9. #include <e32debug.h>
  10. #include <aknappui.h>
  11. //#include "drmfile.h"
  12. //#include <mw/vwsdef.h>
  13. #include <e32def.h>
  14. #include "../miniiapsymbianclient.h"
  15. SymbianClientApiHelper::SymbianClientApiHelper() :
  16. MIAPSymbianClientApiCallbackInterface(),
  17. m_IAPClient(NULL),
  18. m_requestId(-1),
  19. isPurchaseRunning(EFalse),
  20. m_miniIAPSymbianClient(NULL)
  21. {
  22. m_productIdAsText[0] = '\0';
  23. }
  24. SymbianClientApiHelper::~SymbianClientApiHelper()
  25. {
  26. if (m_IAPClient) delete m_IAPClient;
  27. }
  28. bool SymbianClientApiHelper::purchaseProduct(const char * productId)
  29. {
  30. RDebug::Printf("SymbianClientApiHelper::purchaseProduct");
  31. if (!m_miniIAPSymbianClient) return false;
  32. if (isPurchaseRunning) return false;
  33. if (!m_IAPClient) return false;
  34. isPurchaseRunning = ETrue;
  35. bool result = false;
  36. TText8 *productIdAsText = (TText8 *)productId;
  37. TPtr8 ptrTo(m_productIdAsText, KMaxProductIdLength);
  38. TPtrC8 ptrFrom(productIdAsText);
  39. if (ptrFrom.Size() >= KMaxProductIdLength) {
  40. RDebug::Printf("SymbianClientApiHelper::purchaseProduct: too long");
  41. return false;
  42. }
  43. ptrTo.Zero();
  44. TBuf<KMaxProductIdLength> productIdAsTBuf16;
  45. RDebug::Printf("pointerSize: %d %s",ptrTo.Size(), productId);
  46. RDebug::RawPrint(productIdAsTBuf16);
  47. productIdAsTBuf16.Copy(ptrFrom);
  48. ptrTo.Copy(ptrFrom);
  49. ptrTo.Append('\0');
  50. m_requestId = m_IAPClient->PurchaseProduct(productIdAsTBuf16, CIAPSymbianClientApi::EForcedAutomaticRestoration);
  51. // @TODO: fix this to "(m_requestId >= 0)" comparison when IAP is working ok!
  52. if (m_requestId >= 0) {
  53. RDebug::Printf("SymbianClientApiHelper::purchaseProduct: started OK, m_requestId == %d",m_requestId);
  54. result = true;
  55. }
  56. else {
  57. RDebug::Printf("SymbianClientApiHelper::purchaseProduct: not started OK, m_requestId == %d",m_requestId);
  58. clearPurchasing();
  59. result = false;
  60. }
  61. return result;
  62. }
  63. void SymbianClientApiHelper::PurchaseCompleted(TInt aRequestId, const TDesC& aStatus,
  64. const TDesC& aPurchaseTicket)
  65. {
  66. RDebug::Printf("SymbianClientApiHelper::PurchaseCompleted, m_miniIAPSymbianClient");
  67. if (!m_miniIAPSymbianClient) return;
  68. RDebug::Printf("SymbianClientApiHelper::PurchaseCompleted, %d =? %d ",m_requestId,aRequestId);
  69. // @TODO: fix this to "(m_requestId != aRequestId)" comparison when IAP is working ok!
  70. if (m_requestId != aRequestId && m_requestId != 0) return;
  71. TText8 statusAsText[KMaxStatusLength];
  72. TPtr8 statusPointer(statusAsText, KMaxStatusLength);
  73. statusPointer.Copy(aStatus);
  74. statusPointer.Append('\0'); // add \0 to end, is needed?
  75. (void)aPurchaseTicket;
  76. RDebug::Printf("SymbianClientApiHelper::PurchaseCompleted, %d", statusPointer.Length());
  77. RDebug::RawPrint(statusPointer);
  78. m_miniIAPSymbianClient->PurchaseCompleted(m_productIdAsText, statusAsText);
  79. }
  80. void SymbianClientApiHelper::RestorationCompleted(TInt aRequestId, const TDesC& aStatus,
  81. const TDesC& aPurchaseTicket)
  82. {
  83. RDebug::Printf("MiniIAPSymbianClient::RestorationCompleted");
  84. if (!m_miniIAPSymbianClient) return;
  85. // @TODO: fix this to "(m_requestId != aRequestId)" comparison when IAP is working ok!
  86. if (m_requestId != aRequestId && m_requestId != 0) return;
  87. // <copied>
  88. TText8 statusAsText[KMaxStatusLength];
  89. TPtr8 statusPointer(statusAsText, KMaxStatusLength);
  90. statusPointer.Copy(aStatus);
  91. statusPointer.Append('\0'); // add \0 to end, is needed?
  92. (void)aPurchaseTicket;
  93. // </copied>
  94. RDebug::Printf("SymbianClientApiHelper::RestorationCompleted, %d", statusPointer.Length());
  95. RDebug::RawPrint(statusPointer);
  96. m_miniIAPSymbianClient->PurchaseCompleted(m_productIdAsText, statusAsText);
  97. }
  98. void SymbianClientApiHelper::PurchaseFlowFinished(TInt aRequestId)
  99. {
  100. RDebug::Printf("SymbianClientApiHelper::PurchaseFlowFinished");
  101. if (!m_miniIAPSymbianClient) return;
  102. // @TODO: fix this to "(m_requestId != aRequestId)" comparison when IAP is working ok!
  103. if (m_requestId != aRequestId && m_requestId != 0) return;
  104. m_miniIAPSymbianClient->PurchaseFlowFinished(m_productIdAsText);
  105. clearPurchasing();
  106. }
  107. void SymbianClientApiHelper::prepare(MiniIAPSymbianClient *aMiniIAPSymbianClient)
  108. {
  109. RDebug::Printf("SymbianClientApiHelper::prepare");
  110. m_miniIAPSymbianClient = aMiniIAPSymbianClient;
  111. if (!m_IAPClient) {
  112. TVwsViewId viewId;
  113. ((CAknAppUi*)CEikonEnv::Static()->EikAppUi())->GetActiveViewId(viewId);
  114. m_IAPClient = new CIAPSymbianClientApi(this, viewId);
  115. }
  116. }
  117. void SymbianClientApiHelper::clearPurchasing()
  118. {
  119. m_requestId = -1;
  120. isPurchaseRunning = EFalse;
  121. }
  122. void SymbianClientApiHelper::ProductDataReceived(TInt aRequestId, const TDesC& aStatus,
  123. MIAPSymbianClientApiCallbackInterface::RProductDataHash aProductData) {
  124. RDebug::Printf("MiniIAPSymbianClient::PurchaseFlowFinished");
  125. }
  126. void SymbianClientApiHelper::RestorableProductsReceived(TInt aRequestId, const TDesC& aStatus,
  127. MIAPSymbianClientApiCallbackInterface::RProductDataList aItems) {
  128. RDebug::Printf("MiniIAPSymbianClient::PurchaseFlowFinished");
  129. }
  130. void SymbianClientApiHelper::UserAndDeviceDataReceived(TInt aRequestId, const TDesC& aStatus,
  131. MIAPSymbianClientApiCallbackInterface::RUserAndDeviceDataHash aData) {
  132. RDebug::Printf("MiniIAPSymbianClient::PurchaseFlowFinished");
  133. }