mainwindow.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "mainwindow.h"
  2. #include "ui_TryAndBuyExample.h"
  3. #include "buycatalog.h"
  4. #include "DRMFile.h"
  5. #include <QtGui/QPushButton>
  6. #include <QtGui/QLabel>
  7. #include <QtGui/QMessageBox>
  8. #include <QtCore/QDirIterator>
  9. #include <QtCore/QDebug>
  10. #include <QtGui/QApplication>
  11. #include <QTimer>
  12. #include <QProgressDialog>
  13. #define TICKETFOLDERNAME "tickets"
  14. /*
  15. *
  16. */
  17. MainWindow::MainWindow(QWidget *parent) :
  18. QMainWindow(parent),
  19. gameLevels(NULL),
  20. ui(new Ui::MainWindow)
  21. {
  22. ui->setupUi(this);
  23. catalog = new BuyCatalog(this);
  24. connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(mainWindowClose()));
  25. connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(mainWindowResetPurchases()));
  26. }
  27. /*
  28. *
  29. */
  30. MainWindow::~MainWindow()
  31. {
  32. delete catalog;
  33. delete gameLevels;
  34. delete ui;
  35. }
  36. /*
  37. *
  38. */
  39. void MainWindow::playLevel()
  40. {
  41. // which button triggered this action and what is the content of the corresponding label (file name)
  42. QObject* object ;
  43. QString levelName;
  44. foreach(object, sender()->parent()->children())
  45. {
  46. if(QString::compare(object->metaObject()->className(),"QLabel",Qt::CaseSensitive) == 0)
  47. {
  48. levelName = ((QLabel*)object)->text();
  49. break;
  50. }
  51. }
  52. // find the full path for that file name
  53. QStringList list = gameLevels->filter(levelName, Qt::CaseSensitive);
  54. if(list.count()>0)
  55. {
  56. // try to access it
  57. DRMFile file;
  58. int error = file.open(list[0]);
  59. if(isPurchased(error, list[0]))
  60. {
  61. // if you can, "play" the level
  62. uchar* buffer = NULL;
  63. int len = file.read(buffer);
  64. if(len>0)
  65. {
  66. QMessageBox msgBox;
  67. msgBox.setText(QString().fromAscii((const char*)buffer, len - 2)); //cut off CRLF
  68. msgBox.exec();
  69. }
  70. delete buffer;
  71. file.close();
  72. }
  73. else
  74. {
  75. file.close();
  76. // opening failed due to DRM protection?
  77. // start In-Application Purchase experience
  78. QString product = getProductId(list[0]);
  79. catalog->setDefaultProduct(product);
  80. catalog->showFullScreen();
  81. }
  82. }
  83. }
  84. /*
  85. *
  86. */
  87. void MainWindow::listLevels(const QString& path)
  88. {
  89. qDebug() << "Processing: " << path;
  90. ui->listWidget->clear();
  91. if(gameLevels==NULL)
  92. {
  93. gameLevels = new QStringList();
  94. QStringList filters;
  95. filters << "level*.dat";
  96. QDirIterator dirit(path, filters, QDir::NoFilter, QDirIterator::Subdirectories);
  97. while(dirit.hasNext())
  98. gameLevels->append(dirit.next());
  99. //gameLevels->sort();
  100. }
  101. if(gameLevels->count()==0)
  102. {
  103. QMessageBox mb;
  104. mb.setText(tr("No files found."));
  105. mb.exec();
  106. }
  107. for (int i = 0; i<gameLevels->count(); i++)
  108. {
  109. QString fileName = gameLevels->at(i);
  110. QStringList pathItems = fileName.split("/",QString::SkipEmptyParts,Qt::CaseInsensitive);
  111. QListWidgetItem *item = new QListWidgetItem();
  112. ui->listWidget->addItem(item);
  113. QPushButton *button = new QPushButton();
  114. button->setWhatsThis(fileName);
  115. DRMFile file;
  116. int error = file.open(fileName);
  117. if(isPurchased(error, fileName))
  118. button->setText(tr("Play"));
  119. else
  120. button->setText(tr("Unlock"));
  121. file.close();
  122. button->setStyleSheet(QString::fromUtf8("QPushButton{ background-color: rgb(0, 0, 255); border-style: outset; border-width: 2px; border-radius: 10px; border-color: beige; font: bold 14px; min-width: 4em; max-width: 5em; padding: 6px; } QPushButton:pressed { background-color: rgb(100, 100, 250); border-style: inset;}"));
  123. qDebug() << "File " << fileName << "is set as" << button->text() << ".";
  124. QLabel *label = new QLabel(pathItems.last());
  125. QHBoxLayout *layout= new QHBoxLayout();
  126. layout->addWidget(label);
  127. layout->addWidget(button);
  128. QWidget *widget = new QWidget();
  129. widget->setLayout(layout);
  130. item->setSizeHint(widget->sizeHint());
  131. ui->listWidget->setItemWidget(item, widget);
  132. connect(button, SIGNAL(clicked()), this, SLOT(playLevel()));
  133. }
  134. }
  135. void MainWindow::updateLevels()
  136. {
  137. for (int i = 0; i<gameLevels->count(); i++)
  138. {
  139. QString fileName = gameLevels->at(i);
  140. QPushButton *button = getButton(fileName);
  141. if(!button)
  142. continue;
  143. DRMFile file;
  144. int error = file.open(fileName);
  145. if(isPurchased(error, fileName))
  146. button->setText(tr("Play"));
  147. else
  148. button->setText(tr("Unlock"));
  149. file.close();
  150. }
  151. }
  152. QPushButton *MainWindow::getButton(QString& fname)
  153. {
  154. for(int i = 0; i < ui->listWidget->count(); i++)
  155. {
  156. QListWidgetItem* item = ui->listWidget->item(i);
  157. QWidget* widget = ui->listWidget->itemWidget(item);
  158. foreach(QObject* object, widget->children())
  159. {
  160. QWidget *child = qobject_cast<QWidget*>(object);
  161. if(!child)
  162. continue;
  163. if(QString::compare(child->whatsThis(), fname) == 0)
  164. {
  165. return qobject_cast<QPushButton*>(object);
  166. }
  167. }
  168. }
  169. return NULL;
  170. }
  171. /*
  172. *
  173. */
  174. void MainWindow::catalogClosed()
  175. {
  176. updateLevels();
  177. }
  178. void MainWindow::mainWindowClose()
  179. {
  180. close();
  181. }
  182. void MainWindow::mainWindowResetPurchases()
  183. {
  184. QString dirName = getTicketDir();
  185. QDir dir(dirName);
  186. if(!dir.exists(dirName))
  187. return;
  188. bool result;
  189. foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot |
  190. QDir::System |
  191. QDir::Hidden |
  192. QDir::AllDirs |
  193. QDir::Files,
  194. QDir::DirsFirst))
  195. {
  196. result = QFile::remove(info.absoluteFilePath());
  197. if (!result)
  198. {
  199. break;
  200. }
  201. }
  202. dir.rmdir(dirName);
  203. updateLevels();
  204. }
  205. bool MainWindow::isPurchased(int drmErrCode, QString& fileName)
  206. {
  207. QString drmPath(QApplication::applicationDirPath());
  208. drmPath.append("/drm");
  209. if(fileName.startsWith(drmPath))
  210. {
  211. QString productID = getProductId(fileName);
  212. return (drmErrCode == KErrNone && readTicket(productID));
  213. }
  214. else
  215. {
  216. return true;
  217. }
  218. }
  219. QString MainWindow::getProductId(const QString& path)
  220. {
  221. // breakdown the !:/private/<SID>/drm/data/resourceid_<productID>/file.ext
  222. // extract the product to which this file belongs
  223. QStringList elem = path.split("_", QString::SkipEmptyParts);
  224. elem=elem[1].split("/", QString::SkipEmptyParts);
  225. return (elem[0]);
  226. }
  227. void MainWindow::saveTicket(const QString& purchaseTicket, QString& productID)
  228. {
  229. if (purchaseTicket.isEmpty() || productID.isEmpty())
  230. return;
  231. QString privatedir(getTicketDir());
  232. if (!QDir(privatedir).exists())
  233. QDir().mkdir(privatedir);
  234. QFile file(getTicketUri(productID));
  235. if (file.open(QIODevice::WriteOnly)){
  236. QDataStream out(&file);
  237. out << purchaseTicket;
  238. file.close();
  239. }
  240. else
  241. {
  242. qDebug() << "MainWindow::testModeSaveTicket >>> Cannot open file for writing: ";
  243. }
  244. productID = "";
  245. }
  246. bool MainWindow::readTicket(const QString& productID)
  247. {
  248. return (QFile(getTicketUri(productID)).exists());
  249. }
  250. QString MainWindow::getTicketUri(const QString& productID)
  251. {
  252. QString fname(QApplication::applicationDirPath());
  253. fname.append("/");
  254. fname.append(TICKETFOLDERNAME);
  255. fname.append("/");
  256. fname.append(productID);
  257. fname.append(".ticket");
  258. return fname;
  259. }
  260. QString MainWindow::getTicketDir()
  261. {
  262. QString fname(QApplication::applicationDirPath());
  263. fname.append("/");
  264. fname.append(TICKETFOLDERNAME);
  265. return fname;
  266. }