mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-23 07:28:24 -08:00
oracle importer changes
This commit is contained in:
@@ -7,7 +7,15 @@
|
||||
OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
|
||||
: CardDatabase(parent), dataDir(_dataDir), setIndex(-1)
|
||||
{
|
||||
QString fileName = dataDir + "/sets.xml";
|
||||
buffer = new QBuffer(this);
|
||||
http = new QHttp(this);
|
||||
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
|
||||
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
||||
connect(http, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(dataReadProgress(int, int)));
|
||||
}
|
||||
|
||||
void OracleImporter::readSetsFromFile(const QString &fileName)
|
||||
{
|
||||
QFile setsFile(fileName);
|
||||
if (!setsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QMessageBox::critical(0, tr("Error"), tr("Cannot open file '%1'.").arg(fileName));
|
||||
@@ -15,6 +23,19 @@ OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
|
||||
}
|
||||
|
||||
QXmlStreamReader xml(&setsFile);
|
||||
readSetsFromXml(xml);
|
||||
}
|
||||
|
||||
void OracleImporter::readSetsFromByteArray(const QByteArray &data)
|
||||
{
|
||||
QXmlStreamReader xml(data);
|
||||
readSetsFromXml(xml);
|
||||
}
|
||||
|
||||
void OracleImporter::readSetsFromXml(QXmlStreamReader &xml)
|
||||
{
|
||||
allSets.clear();
|
||||
|
||||
QString edition;
|
||||
QString editionLong;
|
||||
QString editionURL;
|
||||
@@ -34,19 +55,13 @@ OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
|
||||
else if (xml.name() == "url")
|
||||
editionURL = xml.readElementText();
|
||||
}
|
||||
setsToDownload << SetToDownload(edition, editionLong, editionURL, import);
|
||||
allSets << SetToDownload(edition, editionLong, editionURL, import);
|
||||
edition = editionLong = editionURL = QString();
|
||||
} else if (xml.name() == "picture_url")
|
||||
pictureUrl = xml.readElementText();
|
||||
else if (xml.name() == "set_url")
|
||||
setUrl = xml.readElementText();
|
||||
}
|
||||
|
||||
buffer = new QBuffer(this);
|
||||
http = new QHttp(this);
|
||||
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
|
||||
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
||||
connect(http, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(dataReadProgress(int, int)));
|
||||
}
|
||||
|
||||
CardInfo *OracleImporter::addCard(QString cardName, const QString &cardCost, const QString &cardType, const QString &cardPT, const QStringList &cardText)
|
||||
@@ -188,19 +203,24 @@ QString OracleImporter::getURLFromName(QString name) const
|
||||
);
|
||||
}
|
||||
|
||||
int OracleImporter::startDownload()
|
||||
{
|
||||
setsToDownload.clear();
|
||||
for (int i = 0; i < allSets.size(); ++i)
|
||||
if (allSets[i].getImport())
|
||||
setsToDownload.append(allSets[i]);
|
||||
|
||||
if (setsToDownload.isEmpty())
|
||||
return 0;
|
||||
setIndex = 0;
|
||||
emit setIndexChanged(0, 0, setsToDownload[0].getLongName());
|
||||
|
||||
downloadNextFile();
|
||||
return setsToDownload.size();
|
||||
}
|
||||
|
||||
void OracleImporter::downloadNextFile()
|
||||
{
|
||||
if (setIndex == -1) {
|
||||
for (int i = 0; i < setsToDownload.size(); ++i)
|
||||
if (!setsToDownload[i].getImport()) {
|
||||
setsToDownload.removeAt(i);
|
||||
--i;
|
||||
}
|
||||
if (setsToDownload.isEmpty())
|
||||
return;
|
||||
setIndex = 0;
|
||||
emit setIndexChanged(0, 0, setsToDownload[0].getLongName());
|
||||
}
|
||||
QString urlString = setsToDownload[setIndex].getUrl();
|
||||
if (urlString.isEmpty())
|
||||
urlString = setUrl;
|
||||
@@ -245,9 +265,9 @@ void OracleImporter::httpRequestFinished(int requestId, bool error)
|
||||
++setIndex;
|
||||
|
||||
if (setIndex == setsToDownload.size()) {
|
||||
setIndex = -1;
|
||||
saveToFile(dataDir + "/cards.xml");
|
||||
emit setIndexChanged(cards, setIndex, QString());
|
||||
setIndex = -1;
|
||||
} else {
|
||||
downloadNextFile();
|
||||
emit setIndexChanged(cards, setIndex, setsToDownload[setIndex].getLongName());
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QHttp>
|
||||
|
||||
class QBuffer;
|
||||
class QXmlStreamReader;
|
||||
|
||||
class SetToDownload {
|
||||
private:
|
||||
@@ -15,7 +16,7 @@ public:
|
||||
const QString &getLongName() const { return longName; }
|
||||
const QString &getUrl() const { return url; }
|
||||
bool getImport() const { return import; }
|
||||
void setImport(bool _import) { qDebug(QString("%1: setting import to %2").arg(getShortName()).arg(_import).toUtf8()); import = _import; }
|
||||
void setImport(bool _import) { import = _import; }
|
||||
SetToDownload(const QString &_shortName, const QString &_longName, const QString &_url, bool _import)
|
||||
: shortName(_shortName), longName(_longName), url(_url), import(_import) { }
|
||||
};
|
||||
@@ -23,7 +24,7 @@ public:
|
||||
class OracleImporter : public CardDatabase {
|
||||
Q_OBJECT
|
||||
private:
|
||||
QList<SetToDownload> setsToDownload;
|
||||
QList<SetToDownload> allSets, setsToDownload;
|
||||
QString pictureUrl, setUrl;
|
||||
QString dataDir;
|
||||
int setIndex;
|
||||
@@ -32,6 +33,8 @@ private:
|
||||
QHttp *http;
|
||||
QString getURLFromName(QString name) const;
|
||||
|
||||
void downloadNextFile();
|
||||
void readSetsFromXml(QXmlStreamReader &xml);
|
||||
CardInfo *addCard(QString cardName, const QString &cardCost, const QString &cardType, const QString &cardPT, const QStringList &cardText);
|
||||
private slots:
|
||||
void httpRequestFinished(int requestId, bool error);
|
||||
@@ -41,10 +44,11 @@ signals:
|
||||
void dataReadProgress(int bytesRead, int totalBytes);
|
||||
public:
|
||||
OracleImporter(const QString &_dataDir, QObject *parent = 0);
|
||||
void readSetsFromByteArray(const QByteArray &data);
|
||||
void readSetsFromFile(const QString &fileName);
|
||||
int startDownload();
|
||||
int importTextSpoiler(CardSet *set, const QByteArray &data);
|
||||
void downloadNextFile();
|
||||
int getSetsCount() const { return setsToDownload.size(); }
|
||||
QList<SetToDownload> &getSets() { return setsToDownload; }
|
||||
QList<SetToDownload> &getSets() { return allSets; }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
#include <QtGui>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include "window_main.h"
|
||||
#include "oracleimporter.h"
|
||||
|
||||
const QString WindowMain::defaultSetsUrl = QString("http://www.cockatrice.de/files/sets.xml");
|
||||
|
||||
WindowMain::WindowMain(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
importer = new OracleImporter(qApp->applicationDirPath() + "/../oracle", this);
|
||||
importer = new OracleImporter(qApp->applicationDirPath(), this);
|
||||
nam = new QNetworkAccessManager(this);
|
||||
|
||||
QVBoxLayout *checkboxLayout = new QVBoxLayout;
|
||||
QList<SetToDownload> &sets = importer->getSets();
|
||||
for (int i = 0; i < sets.size(); ++i) {
|
||||
QCheckBox *checkBox = new QCheckBox(sets[i].getLongName());
|
||||
checkBox->setChecked(sets[i].getImport());
|
||||
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int)));
|
||||
checkboxLayout->addWidget(checkBox);
|
||||
checkBoxList << checkBox;
|
||||
}
|
||||
checkBoxLayout = new QVBoxLayout;
|
||||
|
||||
QWidget *checkboxFrame = new QWidget;
|
||||
checkboxFrame->setLayout(checkboxLayout);
|
||||
checkboxFrame->setLayout(checkBoxLayout);
|
||||
|
||||
QScrollArea *checkboxArea = new QScrollArea;
|
||||
checkboxArea->setWidget(checkboxFrame);
|
||||
@@ -61,10 +58,66 @@ WindowMain::WindowMain(QWidget *parent)
|
||||
|
||||
connect(importer, SIGNAL(setIndexChanged(int, int, const QString &)), this, SLOT(updateTotalProgress(int, int, const QString &)));
|
||||
connect(importer, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateFileProgress(int, int)));
|
||||
totalProgressBar->setMaximum(importer->getSetsCount());
|
||||
|
||||
aLoadSetsFile = new QAction(tr("Load sets information from &file..."), this);
|
||||
connect(aLoadSetsFile, SIGNAL(triggered()), this, SLOT(actLoadSetsFile()));
|
||||
aDownloadSetsFile = new QAction(tr("&Download sets information..."), this);
|
||||
connect(aDownloadSetsFile, SIGNAL(triggered()), this, SLOT(actDownloadSetsFile()));
|
||||
aExit = new QAction(tr("E&xit"), this);
|
||||
connect(aExit, SIGNAL(triggered()), this, SLOT(close()));
|
||||
|
||||
fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
fileMenu->addAction(aLoadSetsFile);
|
||||
fileMenu->addAction(aDownloadSetsFile);
|
||||
fileMenu->addSeparator();
|
||||
fileMenu->addAction(aExit);
|
||||
|
||||
setWindowTitle(tr("Oracle importer"));
|
||||
setFixedSize(500, 300);
|
||||
setFixedSize(600, 500);
|
||||
}
|
||||
|
||||
void WindowMain::updateSetList()
|
||||
{
|
||||
for (int i = 0; i < checkBoxList.size(); ++i)
|
||||
delete checkBoxList[i];
|
||||
checkBoxList.clear();
|
||||
|
||||
QList<SetToDownload> &sets = importer->getSets();
|
||||
for (int i = 0; i < sets.size(); ++i) {
|
||||
QCheckBox *checkBox = new QCheckBox(sets[i].getLongName());
|
||||
checkBox->setChecked(sets[i].getImport());
|
||||
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int)));
|
||||
checkBoxLayout->addWidget(checkBox);
|
||||
checkBoxList << checkBox;
|
||||
}
|
||||
}
|
||||
|
||||
void WindowMain::actLoadSetsFile()
|
||||
{
|
||||
QFileDialog dialog(this, tr("Load sets file"));
|
||||
dialog.setFileMode(QFileDialog::ExistingFile);
|
||||
dialog.setNameFilter("Sets XML file (*.xml)");
|
||||
if (!dialog.exec())
|
||||
return;
|
||||
|
||||
QString fileName = dialog.selectedFiles().at(0);
|
||||
importer->readSetsFromFile(fileName);
|
||||
updateSetList();
|
||||
}
|
||||
|
||||
void WindowMain::actDownloadSetsFile()
|
||||
{
|
||||
QString url = QInputDialog::getText(this, tr("Load sets from URL"), tr("Please enter the URL of the sets file:"), QLineEdit::Normal, defaultSetsUrl);
|
||||
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(setsDownloadFinished()));
|
||||
}
|
||||
|
||||
void WindowMain::setsDownloadFinished()
|
||||
{
|
||||
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
|
||||
importer->readSetsFromByteArray(reply->readAll());
|
||||
reply->deleteLater();
|
||||
updateSetList();
|
||||
}
|
||||
|
||||
void WindowMain::updateTotalProgress(int cardsImported, int setIndex, const QString &nextSetName)
|
||||
@@ -91,7 +144,8 @@ void WindowMain::actStart()
|
||||
startButton->setEnabled(false);
|
||||
for (int i = 0; i < checkBoxList.size(); ++i)
|
||||
checkBoxList[i]->setEnabled(false);
|
||||
importer->downloadNextFile();
|
||||
int setsCount = importer->startDownload();
|
||||
totalProgressBar->setMaximum(setsCount);
|
||||
}
|
||||
|
||||
void WindowMain::checkBoxChanged(int state)
|
||||
|
||||
@@ -10,21 +10,35 @@ class QProgressBar;
|
||||
class QTextEdit;
|
||||
class QPushButton;
|
||||
class QCheckBox;
|
||||
class QVBoxLayout;
|
||||
class QMenu;
|
||||
class QAction;
|
||||
class QNetworkAccessManager;
|
||||
|
||||
class WindowMain : public QMainWindow {
|
||||
Q_OBJECT
|
||||
private:
|
||||
OracleImporter *importer;
|
||||
static const QString defaultSetsUrl;
|
||||
|
||||
OracleImporter *importer;
|
||||
QNetworkAccessManager *nam;
|
||||
|
||||
QMenu *fileMenu;
|
||||
QAction *aLoadSetsFile, *aDownloadSetsFile, *aExit;
|
||||
QPushButton *startButton;
|
||||
QLabel *totalLabel, *fileLabel, *nextSetLabel1, *nextSetLabel2;
|
||||
QProgressBar *totalProgressBar, *fileProgressBar;
|
||||
QTextEdit *messageLog;
|
||||
QVBoxLayout *checkBoxLayout;
|
||||
QList<QCheckBox *> checkBoxList;
|
||||
private slots:
|
||||
void updateTotalProgress(int cardsImported, int setIndex, const QString &nextSetName);
|
||||
void updateFileProgress(int bytesRead, int totalBytes);
|
||||
void updateSetList();
|
||||
void actStart();
|
||||
void actLoadSetsFile();
|
||||
void actDownloadSetsFile();
|
||||
void setsDownloadFinished();
|
||||
void checkBoxChanged(int state);
|
||||
public:
|
||||
WindowMain(QWidget *parent = 0);
|
||||
|
||||
Reference in New Issue
Block a user