miniiapqtclient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <QApplication>
  9. #include "miniiapqtclient.h"
  10. #include "drmfile.h"
  11. #include <QDir>
  12. #include <QDebug>
  13. Q_DECLARE_METATYPE(IAPClient::ProductDataList) //to be able to transfer data this type with Qt::QueuedConnection
  14. MiniIAPQtClient::MiniIAPQtClient(QObject *parent) :
  15. QObject(parent),
  16. MiniIAPClientApi(),
  17. m_requestId(-1),
  18. m_productId(),
  19. m_IAPClient(0),
  20. isPurchaseRunning(false)
  21. {
  22. }
  23. MiniIAPQtClient::~MiniIAPQtClient()
  24. {
  25. if (m_IAPClient) {
  26. delete m_IAPClient;
  27. m_IAPClient = 0;
  28. }
  29. }
  30. bool MiniIAPQtClient::isProductPurchased(const char *productId, const char *fileName)
  31. {
  32. QString path = QApplication::applicationDirPath();
  33. path.append(QString("/drm/data/resourceid_%1/%2").arg(productId)
  34. .arg(fileName));
  35. return canReadDRM(path);
  36. }
  37. bool MiniIAPQtClient::canReadDRM(const QString& fileName) const
  38. {
  39. #ifdef IAP_DEBUG
  40. qDebug() << "IAPImpl::canReadDRM" << fileName;
  41. #endif
  42. bool canRead = false;
  43. #ifdef Q_OS_SYMBIAN
  44. // try to access it
  45. DRMFile file;
  46. int error = file.open(fileName);
  47. if(!error) {
  48. // if you can, read the file
  49. #ifdef IAP_DEBUG
  50. qDebug() << "IAPImpl::canReadDRM: We can read the DRM file";
  51. #endif
  52. canRead = true;
  53. file.close();
  54. }
  55. else {
  56. file.close();
  57. // opening failed due to DRM protection?
  58. if(file.isDRMError(error)) {
  59. #ifdef IAP_DEBUG
  60. qDebug() << "IAPImpl::canReadDRM: DRM error occured" << error;
  61. #endif
  62. }
  63. else {
  64. #ifdef IAP_DEBUG
  65. qDebug() << "IAPImpl::canReadDRM: Unknown error occured" << error;
  66. #endif
  67. }
  68. }
  69. #endif // Q_OS_SYMBIAN
  70. return canRead;
  71. }
  72. bool MiniIAPQtClient::purchaseProduct(const char *productId)
  73. {
  74. qDebug() << "MiniIAPQtClient::purchaseProduct >>>";
  75. prepare();
  76. if (!m_miniIAPReceiver) return false;
  77. if (isPurchaseRunning) {
  78. qDebug() << "already running";
  79. return false;
  80. }
  81. isPurchaseRunning = true;
  82. m_productId = productId;
  83. m_requestId = m_IAPClient->purchaseProduct(m_productId, IAPClient::ForcedAutomaticRestoration);
  84. bool result = false;
  85. if (m_requestId > 0) {
  86. qDebug() << "MiniIAPQtClient::purchaseProduct INITIATED" << m_requestId << m_productId;
  87. result = true;
  88. }
  89. else {
  90. clearPurchasing();
  91. qDebug() << "MiniIAPQtClient::purchaseProduct FAILED" << m_requestId;
  92. }
  93. return result;
  94. }
  95. void MiniIAPQtClient::purchaseCompleted( int requestId, QString status, QString purchaseTicket )
  96. {
  97. qDebug() << "purchaseCompleted";
  98. if (!m_miniIAPReceiver) return;
  99. if (m_requestId != requestId) return;
  100. m_miniIAPReceiver->purchaseCompleted(m_productId.toLatin1().constData(), status.toLatin1().constData());
  101. Q_UNUSED(purchaseTicket)
  102. }
  103. void MiniIAPQtClient::restorationCompleted( int requestId, QString status, QString purchaseTicket )
  104. {
  105. qDebug() << "restorationCompleted";
  106. if (!m_miniIAPReceiver) return;
  107. if (m_requestId != requestId) return;
  108. m_miniIAPReceiver->purchaseCompleted(m_productId.toLatin1().constData(), status.toLatin1().constData());
  109. Q_UNUSED(purchaseTicket)
  110. }
  111. void MiniIAPQtClient::purchaseFlowFinished( int requestId )
  112. {
  113. qDebug() << "purchaseFlowFinished";
  114. if (!m_miniIAPReceiver) return;
  115. if (m_requestId != requestId) return;
  116. m_miniIAPReceiver->purchaseFlowFinished(m_productId.toLatin1().constData());
  117. clearPurchasing();
  118. }
  119. void MiniIAPQtClient::clearPurchasing()
  120. {
  121. m_requestId = -1;
  122. m_productId.clear();
  123. isPurchaseRunning = false;
  124. }
  125. void MiniIAPQtClient::prepare()
  126. {
  127. if (!m_IAPClient) {
  128. // required so that IAPClient::ProductData can be queued in the signal
  129. //qRegisterMetaType<IAPClient::ProductDataHash>("IAPClient::ProductDataHash");
  130. m_IAPClient = new IAPClient(this);
  131. connect(m_IAPClient, SIGNAL(purchaseCompleted(int,QString,QString)),
  132. SLOT(purchaseCompleted(int,QString,QString)));
  133. connect(m_IAPClient, SIGNAL(purchaseFlowFinished(int)),
  134. SLOT(purchaseFlowFinished(int)));
  135. connect(m_IAPClient, SIGNAL(restorationCompleted(int, QString, QString)), this,
  136. SLOT(restorationCompleted(int,QString,QString)));
  137. qDebug() << "MiniIAPQtClient::purchaseProduct >>> prepared";
  138. }
  139. }