purchasableitemBase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "purchasableitembase.h"
  9. #include <iapclient.h>
  10. #include <QDebug>
  11. #include <QApplication>
  12. #include <QFile>
  13. #include <QDir>
  14. #include "productlistmodel.h"
  15. #define TICKETFOLDERNAME "tickets"
  16. // For possible status values please see
  17. // https://projects.developer.nokia.com/dav/iap/doc/GUID-532F510B-9EE3-4069-AD8D-3E225C47A446/class_i_a_p_client.html
  18. // The purchase was successful
  19. #define REQUEST_STATUS_OK "OK"
  20. // The purchase failed but the product is restorable
  21. // It is recognized by OVI Store that current user has been already purchased the product
  22. // on other device or in the previous application installation
  23. #define REQUEST_STATUS_RESTORABLE "RestorableProduct"
  24. // local function declaration
  25. bool isTheSameProduct(IAPClient::ProductDataHash& left, IAPClient::ProductData& right);
  26. PurchasableItemBase::PurchasableItemBase(ProductListModel &model,
  27. const QString &productID,
  28. const QString &productUrl) :
  29. QObject(&model), //parent
  30. m_model(&model),
  31. m_isBusy(false),
  32. m_isKnown(false),
  33. m_unlocked_icon(NULL),
  34. m_buy_icon(NULL),
  35. m_notready_icon(NULL),
  36. m_productUrl(productUrl)
  37. {
  38. m_productMetadata.productId = productID;
  39. qDebug() << "PurchasableItemBase::construct " << m_productMetadata.productId;
  40. }
  41. void PurchasableItemBase::initProductData()
  42. {
  43. qDebug() << "initProductData " << m_productMetadata.productId;
  44. // connect IAP API's signals to app's slots
  45. IAPClient *client = &m_model->client();
  46. qRegisterMetaType<IAPClient::ProductDataHash>("IAPClient::ProductDataHash");
  47. connect(client, SIGNAL(productDataReceived( int, QString, IAPClient::ProductDataHash)),
  48. this, SLOT(productDataReceived(int, QString, IAPClient::ProductDataHash)), Qt::QueuedConnection);
  49. connect(client, SIGNAL(purchaseCompleted( int , QString, QString)),
  50. this, SLOT(purchaseCompleted( int , QString, QString)),Qt::QueuedConnection);
  51. connect(client, SIGNAL(purchaseFlowFinished(int)),
  52. this, SLOT(purchaseFlowFinished(int)), Qt::QueuedConnection);
  53. connect(client, SIGNAL(restorationCompleted (int , QString , QString)),
  54. this, SLOT(restorationCompleted (int , QString , QString)), Qt::QueuedConnection);
  55. //initialize data
  56. m_productMetadata.dataRequestResult = "Pending";
  57. m_productMetadata.drmProtection = IAPClient::NokiaDrm;
  58. fetchMetadata();
  59. }
  60. PurchasableItemBase::~PurchasableItemBase()
  61. {
  62. delete m_unlocked_icon;
  63. m_unlocked_icon = NULL;
  64. delete m_buy_icon;
  65. m_buy_icon = NULL;
  66. delete m_notready_icon;
  67. m_notready_icon = NULL;
  68. }
  69. QVariant PurchasableItemBase::metadata(int role) const
  70. {
  71. switch(role)
  72. {
  73. case ProductIdRole:
  74. return m_productMetadata.productId;
  75. case DataReqResultRole:
  76. return m_productMetadata.dataRequestResult;
  77. case TitleRole:
  78. return m_productMetadata.title;
  79. case ShortDescriptionRole:
  80. return m_productMetadata.shortDescription;
  81. case LongDescriptionRole:
  82. return m_productMetadata.longDescription;
  83. case PriceRole:
  84. return m_productMetadata.price;
  85. case DrmProtectionRole:
  86. return ((m_productMetadata.drmProtection == IAPClient::NokiaDrm) ? true : false);
  87. case IsMetadataKnown:
  88. return m_isKnown;
  89. case IsBusy:
  90. return m_isBusy;
  91. case IsActivated:
  92. return (isActivated());
  93. default:
  94. break;
  95. }
  96. return QVariant();
  97. }
  98. bool PurchasableItemBase::setMetadata(const QVariant & value, int role)
  99. {
  100. bool result = true;
  101. switch(role)
  102. {
  103. case ProductIdRole:
  104. m_productMetadata.productId = value.toString();
  105. break;
  106. case DataReqResultRole:
  107. m_productMetadata.dataRequestResult = value.toString();
  108. break;
  109. case TitleRole:
  110. m_productMetadata.title = value.toString();
  111. break;
  112. case ShortDescriptionRole:
  113. m_productMetadata.shortDescription = value.toString();
  114. break;
  115. case LongDescriptionRole:
  116. m_productMetadata.longDescription = value.toString();
  117. break;
  118. case PriceRole:
  119. m_productMetadata.price = value.toString();
  120. break;
  121. case DrmProtectionRole:
  122. m_productMetadata.drmProtection = (value.toBool()) ? IAPClient::NokiaDrm : IAPClient::OtherDrm;
  123. break;
  124. case IsMetadataKnown: //read-only properties
  125. case IsBusy:
  126. m_isBusy = value.toBool();
  127. break;
  128. case IsActivated:
  129. // cannot be unlocked by this call but can be locked
  130. if(value.toBool())
  131. result = false;
  132. else
  133. deactivate();
  134. break;
  135. default:
  136. result = false;
  137. break;
  138. }
  139. return result;
  140. }
  141. void PurchasableItemBase::deactivate()
  142. {
  143. delete m_unlocked_icon;
  144. m_unlocked_icon = NULL;
  145. }
  146. bool PurchasableItemBase::isMetaDataKnown() const
  147. {
  148. return m_isKnown;
  149. }
  150. bool PurchasableItemBase::isBusy()
  151. {
  152. return m_isBusy;
  153. }
  154. bool PurchasableItemBase::isActivated() const
  155. {
  156. return (m_unlocked_icon != NULL);
  157. }
  158. void PurchasableItemBase::fetchMetadata()
  159. {
  160. qDebug(__FUNCTION__);
  161. if(setBusy())
  162. {
  163. m_requestId = m_model->client().getProductData(m_productMetadata.productId);
  164. }
  165. }
  166. void PurchasableItemBase::purchase()
  167. {
  168. qDebug(__FUNCTION__);
  169. if(setBusy())
  170. {
  171. m_requestId = m_model->client().purchaseProduct(m_productMetadata.productId, IAPClient::NoForcedRestoration);
  172. }
  173. }
  174. void PurchasableItemBase::restore()
  175. {
  176. qDebug(__FUNCTION__);
  177. if(setBusy())
  178. {
  179. qDebug() << "restoreProduct : " << m_productMetadata.productId;
  180. m_requestId = m_model->client().restoreProduct(m_productMetadata.productId);
  181. }
  182. }
  183. void PurchasableItemBase::productDataReceived(int requestId, QString status,
  184. IAPClient::ProductDataHash productData)
  185. {
  186. if (requestId != m_requestId)
  187. return;
  188. qDebug(__FUNCTION__);
  189. m_model->beginModelUpdate();
  190. m_isBusy = false;
  191. m_isKnown = true;
  192. IAPClient::DrmType type = m_productMetadata.drmProtection;
  193. QString productId = m_productMetadata.productId;
  194. m_productMetadata.title = productData.value("info").toString();
  195. m_productMetadata.shortDescription = productData.value("shortdescription").toString();
  196. m_productMetadata.longDescription = productData.value("description").toString();
  197. m_productMetadata.price = productData.value("price").toString();
  198. m_productMetadata.dataRequestResult = productData.value("result").toString();
  199. m_productMetadata.drmProtection = type; //preserve DRM type
  200. m_productMetadata.productId = productId; //preserve Product ID
  201. if(isPurchased())
  202. setUnlocked(NULL);
  203. m_model->commitModelUpdate();
  204. }
  205. void PurchasableItemBase::purchaseCompleted(int requestId, QString status, QString purchaseTicket)
  206. {
  207. if(requestId != m_requestId)
  208. return;
  209. qDebug(__FUNCTION__);
  210. m_model->beginModelUpdate();
  211. m_isBusy = false;
  212. if ((status.compare(REQUEST_STATUS_OK, Qt::CaseSensitive) == 0) ||
  213. (status.compare(REQUEST_STATUS_RESTORABLE, Qt::CaseSensitive) == 0)){
  214. setUnlocked(&purchaseTicket);
  215. }
  216. m_model->commitModelUpdate();
  217. }
  218. void PurchasableItemBase::purchaseFlowFinished(int requestId)
  219. {
  220. if(requestId != m_requestId)
  221. return;
  222. qDebug(__FUNCTION__);
  223. m_isBusy = false;
  224. m_model->purchaseFlowFinished(isActivated());
  225. }
  226. void PurchasableItemBase::restorationCompleted(int requestId, QString status, QString purchaseTicket)
  227. {
  228. if(requestId != m_requestId)
  229. return;
  230. qDebug(__FUNCTION__);
  231. m_model->beginModelUpdate();
  232. m_isBusy = false;
  233. if(status.compare("OK", Qt::CaseSensitive) == 0)
  234. {
  235. setUnlocked(&purchaseTicket);
  236. }
  237. m_model->commitModelUpdate();
  238. }
  239. void PurchasableItemBase::restorationFlowFinished(int requestId)
  240. {
  241. if(requestId != m_requestId)
  242. return;
  243. qDebug(__FUNCTION__);
  244. m_isBusy = false;
  245. }
  246. QImage& PurchasableItemBase::stateIcon() const
  247. {
  248. if(m_unlocked_icon)
  249. return *m_unlocked_icon;
  250. else if(m_isKnown)
  251. return *m_buy_icon;
  252. else
  253. return *m_notready_icon;
  254. }
  255. bool PurchasableItemBase::setBusy()
  256. {
  257. if(isActivated())
  258. return false; // already unlocked, no remote operation possible
  259. if(m_isBusy)
  260. {
  261. qDebug() << "item is busy. you can set only one request at time on the item";
  262. return false;
  263. }
  264. m_model->beginModelUpdate();
  265. m_isBusy = true;
  266. m_model->commitModelUpdate();
  267. return true;
  268. }
  269. void PurchasableItemBase::saveTicket(QString* purchaseTicket)
  270. {
  271. if(!purchaseTicket)
  272. return;
  273. QString privatedir(getTicketDir());
  274. if(!QDir(privatedir).exists())
  275. QDir().mkdir(privatedir);
  276. QFile file(getTicketUri());
  277. if(file.open(QIODevice::WriteOnly))
  278. {
  279. QDataStream out(&file);
  280. out << *purchaseTicket;
  281. file.close();
  282. }
  283. else
  284. {
  285. qDebug() << "PurchasableItemBase::testModeSaveTicket >>> Cannot open file for writing: ";
  286. }
  287. }
  288. bool PurchasableItemBase::readTicket()
  289. {
  290. return (QFile(getTicketUri()).exists());
  291. }
  292. QString PurchasableItemBase::getTicketUri()
  293. {
  294. QString fname(QApplication::applicationDirPath());
  295. fname.append("/");
  296. fname.append(TICKETFOLDERNAME);
  297. fname.append("/");
  298. fname.append(m_productMetadata.productId);
  299. fname.append(".ticket");
  300. return fname;
  301. }
  302. QString PurchasableItemBase::getTicketDir()
  303. {
  304. QString fname(QApplication::applicationDirPath());
  305. fname.append("/");
  306. fname.append(TICKETFOLDERNAME);
  307. return fname;
  308. }
  309. bool PurchasableItemBase::isIncluded(IAPClient::ProductDataList& list)
  310. {
  311. foreach(IAPClient::ProductDataHash item, list)
  312. {
  313. if(isTheSameProduct(item, m_productMetadata))
  314. return true;
  315. }
  316. return false;
  317. }
  318. bool isTheSameProduct(IAPClient::ProductDataHash& left, IAPClient::ProductData& right)
  319. {
  320. return (left.value("id").toString().compare(right.productId) == 0);
  321. }