123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #include "mainwindow.h"
- #include "ui_TryAndBuyExample.h"
- #include "buycatalog.h"
- #include "DRMFile.h"
- #include <QtGui/QPushButton>
- #include <QtGui/QLabel>
- #include <QtGui/QMessageBox>
- #include <QtCore/QDirIterator>
- #include <QtCore/QDebug>
- #include <QtGui/QApplication>
- #include <QTimer>
- #include <QProgressDialog>
- #define TICKETFOLDERNAME "tickets"
- /*
- *
- */
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- gameLevels(NULL),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- catalog = new BuyCatalog(this);
- connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(mainWindowClose()));
- connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(mainWindowResetPurchases()));
- }
- /*
- *
- */
- MainWindow::~MainWindow()
- {
- delete catalog;
- delete gameLevels;
- delete ui;
- }
- /*
- *
- */
- void MainWindow::playLevel()
- {
- // which button triggered this action and what is the content of the corresponding label (file name)
- QObject* object ;
- QString levelName;
- foreach(object, sender()->parent()->children())
- {
- if(QString::compare(object->metaObject()->className(),"QLabel",Qt::CaseSensitive) == 0)
- {
- levelName = ((QLabel*)object)->text();
- break;
- }
- }
- // find the full path for that file name
- QStringList list = gameLevels->filter(levelName, Qt::CaseSensitive);
- if(list.count()>0)
- {
- // try to access it
- DRMFile file;
- int error = file.open(list[0]);
- if(isPurchased(error, list[0]))
- {
- // if you can, "play" the level
- uchar* buffer = NULL;
- int len = file.read(buffer);
- if(len>0)
- {
- QMessageBox msgBox;
- msgBox.setText(QString().fromAscii((const char*)buffer, len - 2)); //cut off CRLF
- msgBox.exec();
- }
- delete buffer;
- file.close();
- }
- else
- {
- file.close();
- // opening failed due to DRM protection?
- // start In-Application Purchase experience
- QString product = getProductId(list[0]);
- catalog->setDefaultProduct(product);
- catalog->showFullScreen();
- }
- }
- }
- /*
- *
- */
- void MainWindow::listLevels(const QString& path)
- {
- qDebug() << "Processing: " << path;
- ui->listWidget->clear();
- if(gameLevels==NULL)
- {
- gameLevels = new QStringList();
- QStringList filters;
- filters << "level*.dat";
- QDirIterator dirit(path, filters, QDir::NoFilter, QDirIterator::Subdirectories);
- while(dirit.hasNext())
- gameLevels->append(dirit.next());
- //gameLevels->sort();
- }
- if(gameLevels->count()==0)
- {
- QMessageBox mb;
- mb.setText(tr("No files found."));
- mb.exec();
- }
- for (int i = 0; i<gameLevels->count(); i++)
- {
- QString fileName = gameLevels->at(i);
- QStringList pathItems = fileName.split("/",QString::SkipEmptyParts,Qt::CaseInsensitive);
- QListWidgetItem *item = new QListWidgetItem();
- ui->listWidget->addItem(item);
- QPushButton *button = new QPushButton();
- button->setWhatsThis(fileName);
- DRMFile file;
- int error = file.open(fileName);
- if(isPurchased(error, fileName))
- button->setText(tr("Play"));
- else
- button->setText(tr("Unlock"));
- file.close();
- 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;}"));
- qDebug() << "File " << fileName << "is set as" << button->text() << ".";
- QLabel *label = new QLabel(pathItems.last());
- QHBoxLayout *layout= new QHBoxLayout();
- layout->addWidget(label);
- layout->addWidget(button);
- QWidget *widget = new QWidget();
- widget->setLayout(layout);
- item->setSizeHint(widget->sizeHint());
- ui->listWidget->setItemWidget(item, widget);
- connect(button, SIGNAL(clicked()), this, SLOT(playLevel()));
- }
- }
- void MainWindow::updateLevels()
- {
- for (int i = 0; i<gameLevels->count(); i++)
- {
- QString fileName = gameLevels->at(i);
- QPushButton *button = getButton(fileName);
- if(!button)
- continue;
- DRMFile file;
- int error = file.open(fileName);
- if(isPurchased(error, fileName))
- button->setText(tr("Play"));
- else
- button->setText(tr("Unlock"));
- file.close();
- }
- }
- QPushButton *MainWindow::getButton(QString& fname)
- {
- for(int i = 0; i < ui->listWidget->count(); i++)
- {
- QListWidgetItem* item = ui->listWidget->item(i);
- QWidget* widget = ui->listWidget->itemWidget(item);
- foreach(QObject* object, widget->children())
- {
- QWidget *child = qobject_cast<QWidget*>(object);
- if(!child)
- continue;
- if(QString::compare(child->whatsThis(), fname) == 0)
- {
- return qobject_cast<QPushButton*>(object);
- }
- }
- }
- return NULL;
- }
- /*
- *
- */
- void MainWindow::catalogClosed()
- {
- updateLevels();
- }
- void MainWindow::mainWindowClose()
- {
- close();
- }
- void MainWindow::mainWindowResetPurchases()
- {
- QString dirName = getTicketDir();
- QDir dir(dirName);
- if(!dir.exists(dirName))
- return;
- bool result;
- foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot |
- QDir::System |
- QDir::Hidden |
- QDir::AllDirs |
- QDir::Files,
- QDir::DirsFirst))
- {
- result = QFile::remove(info.absoluteFilePath());
- if (!result)
- {
- break;
- }
- }
- dir.rmdir(dirName);
- updateLevels();
- }
- bool MainWindow::isPurchased(int drmErrCode, QString& fileName)
- {
- QString drmPath(QApplication::applicationDirPath());
- drmPath.append("/drm");
- if(fileName.startsWith(drmPath))
- {
- QString productID = getProductId(fileName);
- return (drmErrCode == KErrNone && readTicket(productID));
- }
- else
- {
- return true;
- }
- }
- QString MainWindow::getProductId(const QString& path)
- {
- // breakdown the !:/private/<SID>/drm/data/resourceid_<productID>/file.ext
- // extract the product to which this file belongs
- QStringList elem = path.split("_", QString::SkipEmptyParts);
- elem=elem[1].split("/", QString::SkipEmptyParts);
- return (elem[0]);
- }
- void MainWindow::saveTicket(const QString& purchaseTicket, QString& productID)
- {
- if (purchaseTicket.isEmpty() || productID.isEmpty())
- return;
- QString privatedir(getTicketDir());
- if (!QDir(privatedir).exists())
- QDir().mkdir(privatedir);
- QFile file(getTicketUri(productID));
- if (file.open(QIODevice::WriteOnly)){
- QDataStream out(&file);
- out << purchaseTicket;
- file.close();
- }
- else
- {
- qDebug() << "MainWindow::testModeSaveTicket >>> Cannot open file for writing: ";
- }
- productID = "";
- }
- bool MainWindow::readTicket(const QString& productID)
- {
- return (QFile(getTicketUri(productID)).exists());
- }
- QString MainWindow::getTicketUri(const QString& productID)
- {
- QString fname(QApplication::applicationDirPath());
- fname.append("/");
- fname.append(TICKETFOLDERNAME);
- fname.append("/");
- fname.append(productID);
- fname.append(".ticket");
- return fname;
- }
- QString MainWindow::getTicketDir()
- {
- QString fname(QApplication::applicationDirPath());
- fname.append("/");
- fname.append(TICKETFOLDERNAME);
- return fname;
- }
|