mainwindow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "mainwindow.h"
  2. #include "ui_BuyAndDownloadExample.h"
  3. #include "buycatalog.h"
  4. #include <QtGui/QPushButton>
  5. #include <QtGui/QLabel>
  6. #include <QtGui/QMessageBox>
  7. #include <QtCore/QDirIterator>
  8. #include <QtCore/QDebug>
  9. #include <QtGui/QApplication>
  10. #include <QTimer>
  11. #include <QProgressDialog>
  12. #define KUnlockedContentPath "purchases"
  13. /*
  14. *
  15. */
  16. MainWindow::MainWindow(QWidget *parent) :
  17. QMainWindow(parent),
  18. ui(new Ui::MainWindow)
  19. {
  20. ui->setupUi(this);
  21. catalog = new BuyCatalog(this);
  22. connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(mainWindowClose()));
  23. connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(mainWindowResetPurchases()));
  24. connect(ui->goStoreButton, SIGNAL(clicked()), this, SLOT(mainWindowGotoStore()));
  25. }
  26. MainWindow::~MainWindow()
  27. {
  28. delete catalog;
  29. delete ui;
  30. }
  31. void MainWindow::playLevel()
  32. {
  33. // which button triggered this action and what is the content of the corresponding label (file name)
  34. QObject* object ;
  35. QString levelName;
  36. foreach(object, sender()->parent()->children())
  37. {
  38. if(QString::compare(object->metaObject()->className(),"QLabel",Qt::CaseSensitive) == 0)
  39. {
  40. levelName = ((QLabel*)object)->text();
  41. break;
  42. }
  43. }
  44. // find the full path for that file name
  45. QStringList list = purchasedContent.filter(levelName, Qt::CaseSensitive);
  46. if(list.count()>0)
  47. {
  48. QFile file(list[0]);
  49. if(file.open(QIODevice::ReadOnly)) {
  50. QTextStream in(&file);
  51. QString buffer;
  52. while(!in.atEnd()) {
  53. buffer.append(in.readLine());
  54. }
  55. //TODO : dear developer, implement text viewer here :)
  56. QMessageBox msgBox;
  57. msgBox.setText(buffer);
  58. msgBox.exec();
  59. }else{
  60. QMessageBox::information(0, "error", file.errorString());
  61. }
  62. file.close();
  63. }
  64. }
  65. void MainWindow::listPurchases()
  66. {
  67. ui->listWidget->clear();
  68. purchasedContent.clear();
  69. QStringList filters;
  70. filters << "*";
  71. QDirIterator dirit(getPurchaseDir(), filters, QDir::NoFilter, QDirIterator::Subdirectories);
  72. while(dirit.hasNext())
  73. purchasedContent.append(dirit.next());
  74. for (int i = 0; i<purchasedContent.count(); i++)
  75. {
  76. QString fileName = purchasedContent.at(i);
  77. QStringList pathItems = fileName.split("/",QString::SkipEmptyParts,Qt::CaseInsensitive);
  78. QListWidgetItem *item = new QListWidgetItem();
  79. ui->listWidget->addItem(item);
  80. QPushButton *button = new QPushButton();
  81. button->setText("Read");
  82. 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;}"));
  83. qDebug() << "File " << fileName << "is set as" << button->text() << ".";
  84. QLabel *label = new QLabel(pathItems.last());
  85. QHBoxLayout *layout= new QHBoxLayout();
  86. layout->addWidget(label);
  87. layout->addWidget(button);
  88. QWidget *widget = new QWidget();
  89. widget->setLayout(layout);
  90. item->setSizeHint(widget->sizeHint());
  91. ui->listWidget->setItemWidget(item, widget);
  92. connect(button, SIGNAL(clicked()), this, SLOT(playLevel()));
  93. }
  94. }
  95. QPushButton *MainWindow::getButton(QString& fname)
  96. {
  97. for(int i = 0; i < ui->listWidget->count(); i++)
  98. {
  99. QListWidgetItem* item = ui->listWidget->item(i);
  100. QWidget* widget = ui->listWidget->itemWidget(item);
  101. foreach(QObject* object, widget->children())
  102. {
  103. QWidget *child = qobject_cast<QWidget*>(object);
  104. if(!child)
  105. continue;
  106. if(QString::compare(child->whatsThis(), fname) == 0)
  107. {
  108. return qobject_cast<QPushButton*>(object);
  109. }
  110. }
  111. }
  112. return NULL;
  113. }
  114. /*
  115. *
  116. */
  117. void MainWindow::catalogClosed()
  118. {
  119. listPurchases();
  120. }
  121. void MainWindow::mainWindowClose()
  122. {
  123. close();
  124. }
  125. void MainWindow::mainWindowResetPurchases()
  126. {
  127. QString dirName = getPurchaseDir();
  128. QDir dir(dirName);
  129. if(!dir.exists(dirName))
  130. return;
  131. bool result;
  132. foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot |
  133. QDir::System |
  134. QDir::Hidden |
  135. QDir::AllDirs |
  136. QDir::Files,
  137. QDir::DirsFirst))
  138. {
  139. result = QFile::remove(info.absoluteFilePath());
  140. if (!result)
  141. {
  142. break;
  143. }
  144. }
  145. dir.rmdir(dirName);
  146. listPurchases();
  147. }
  148. void MainWindow::mainWindowGotoStore()
  149. {
  150. catalog->showFullScreen();
  151. }
  152. QString MainWindow::getPurchaseUri(const QString& productID)
  153. {
  154. QString fname(getPurchaseDir());
  155. fname.append("/");
  156. fname.append(productID);
  157. fname.append(".book");
  158. return fname;
  159. }
  160. QString MainWindow::getPurchaseDir()
  161. {
  162. QString fname(QApplication::applicationDirPath());
  163. fname.append("/");
  164. fname.append(KUnlockedContentPath);
  165. return fname;
  166. }