Refactored the magic bytes into two QLists: one containing a list of QStrings representing the supported extensions, the other containing a list of QByteArrays representing the magic bytes in hex.

Refactored the image loading loop to be two nested for loops: the outer loop iterating through the QList of paths to folders in which to search for images and the inner loop iterating through the QList of supported extensions.
This commit is contained in:
Peng Liu 2014-08-05 03:48:30 -04:00
parent 75122c3c9d
commit 8587b8c349
2 changed files with 45 additions and 25 deletions

View file

@ -16,6 +16,10 @@
const int CardDatabase::versionNeeded = 3; const int CardDatabase::versionNeeded = 3;
//Specifies image formats and their associated signature bytes (expected first x bytes, in hex)
const QList<QString> PictureLoader::imgFormats = QList<QString>() << ".png";
const QList<QByteArray> PictureLoader::imgSignatures = QList<QByteArray>() << "89504E470D0A1A0A";
static QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set) static QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set)
{ {
xml.writeStartElement("set"); xml.writeStartElement("set");
@ -124,31 +128,42 @@ void PictureLoader::processLoadQueue()
} }
PictureToLoad ptl = loadQueue.takeFirst(); PictureToLoad ptl = loadQueue.takeFirst();
mutex.unlock(); mutex.unlock();
QString correctedName = ptl.getCard()->getCorrectedName();
QString picsPath = _picsPath; //The list of paths to the folders in which to search for images
QString setName = ptl.getSetName(); QList<QString> picsPaths = QList<QString>() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full"
<< _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"
<< _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full";
QImage image; QImage image;
//Supports loading JPG and PNG images; default is JPG QString extension;
if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) bool found;
if (!image.load(QString("%1/%2/%3.full.png").arg(picsPath).arg("CUSTOM").arg(correctedName))) int i, j;
if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName)))
if (!image.load(QString("%1/%2/%3.full.png").arg(picsPath).arg(setName).arg(correctedName))) //Iterates through the list of paths, iterating through imgFormats in each to find the image in any supported format; default is JPG
if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) for (found = false, i = 0; i < picsPaths.length() && !found; i ++)
if (!image.load(QString("%1/%2/%3/%4.full.png").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) { for (extension = ".jpg", j = 0; !found; j ++) {
if (picDownload) { //qDebug() << picsPaths.at(i) + extension;
cardsToDownload.append(ptl); if (image.load(picsPaths.at(i) + extension)) {
if (!downloadRunning) emit imageLoaded(ptl.getCard(), image);
startNextPicDownload(); found = true;
} else { }
if (ptl.nextSet()) if (j >= imgFormats.length())
loadQueue.prepend(ptl); break;
else extension = imgFormats.at(j);
emit imageLoaded(ptl.getCard(), QImage()); }
}
}
emit imageLoaded(ptl.getCard(), image); if (!found) {
if (picDownload) {
cardsToDownload.append(ptl);
if (!downloadRunning)
startNextPicDownload();
} else {
if (ptl.nextSet())
loadQueue.prepend(ptl);
else
emit imageLoaded(ptl.getCard(), QImage());
}
}
} }
} }
@ -228,10 +243,13 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply)
if (!cardBeingDownloaded.getStripped()) if (!cardBeingDownloaded.getStripped())
suffix = ".full"; suffix = ".full";
//Supports JPG and PNG images; default is JPG //Supports JPG and formats specified in imgFormats; default is JPG
QString extension = ".jpg"; QString extension = ".jpg";
if (picData.left(8) == QByteArray::fromHex("89504E470D0A1A0A")) for (int i = 0; i < imgFormats.length(); i ++)
extension = ".png"; if (picData.left(QByteArray::fromHex(imgSignatures.at(i)).length()) == QByteArray::fromHex(imgSignatures.at(i))) {
extension = imgFormats.at(i);
break;
}
QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension);
if (!newPic.open(QIODevice::WriteOnly)) if (!newPic.open(QIODevice::WriteOnly))

View file

@ -73,6 +73,8 @@ private:
bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning; bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning;
void startNextPicDownload(); void startNextPicDownload();
QString getPicUrl(CardInfo* card); QString getPicUrl(CardInfo* card);
static const QList<QString> imgFormats;
static const QList<QByteArray> imgSignatures;
public: public:
PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent = 0); PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent = 0);
~PictureLoader(); ~PictureLoader();