diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index b3d4bca98..c1a4ab22e 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -13,7 +13,6 @@ #include #include #include -#include const int CardDatabase::versionNeeded = 3; @@ -125,38 +124,29 @@ void PictureLoader::processLoadQueue() } PictureToLoad ptl = loadQueue.takeFirst(); mutex.unlock(); - - //The list of paths to the folders in which to search for images - QList picsPaths = QList() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full" - << _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full" - << _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"; + QString correctedName = ptl.getCard()->getCorrectedName(); + QString picsPath = _picsPath; + QString setName = ptl.getSetName(); QImage image; - QImageReader imgReader; - imgReader.setDecideFormatFromContent(true); - bool found = false; - - //Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension - for (int i = 0; i < picsPaths.length() && !found; i ++) { - imgReader.setFileName(picsPaths.at(i)); - if (imgReader.read(&image)) { - emit imageLoaded(ptl.getCard(), image); - found = true; - } + if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) { + if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName))) + //if (!image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(setName).arg(correctedName).arg(1))) + if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) { + if (picDownload) { + cardsToDownload.append(ptl); + if (!downloadRunning) + startNextPicDownload(); + } else { + if (ptl.nextSet()) + loadQueue.prepend(ptl); + else + emit imageLoaded(ptl.getCard(), QImage()); + } + } } - if (!found) { - if (picDownload) { - cardsToDownload.append(ptl); - if (!downloadRunning) - startNextPicDownload(); - } else { - if (ptl.nextSet()) - loadQueue.prepend(ptl); - else - emit imageLoaded(ptl.getCard(), QImage()); - } - } + emit imageLoaded(ptl.getCard(), image); } } @@ -218,27 +208,25 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) qDebug() << "Download failed:" << reply->errorString(); } - const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader + const QByteArray &picData = reply->readAll(); QImage testImage; - - QImageReader imgReader; - imgReader.setDecideFormatFromContent(true); - imgReader.setDevice(reply); - QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer - if (extension == ".jpeg") - extension = ".jpg"; - - if (imgReader.read(&testImage)) { - if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) { - qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created."; - return; + if (testImage.loadFromData(picData)) { + if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) { + QDir dir(picsPath); + if (!dir.exists()) + return; + dir.mkdir("downloadedPics"); + } + if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) { + QDir dir(QString(picsPath + "/downloadedPics")); + dir.mkdir(cardBeingDownloaded.getSetName()); } QString suffix; if (!cardBeingDownloaded.getStripped()) suffix = ".full"; - QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); + QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg"); if (!newPic.open(QIODevice::WriteOnly)) return; newPic.write(picData); @@ -450,7 +438,7 @@ void CardInfo::updatePixmapCache() qDebug() << "Updating pixmap cache for" << name; clearPixmapCache(); loadPixmap(); - + emit pixmapUpdated(); } @@ -555,7 +543,7 @@ CardDatabase::~CardDatabase() { clear(); delete noCard; - + pictureLoader->deleteLater(); pictureLoaderThread->wait(); delete pictureLoaderThread; @@ -569,7 +557,7 @@ void CardDatabase::clear() delete setIt.value(); } sets.clear(); - + QHashIterator i(cards); while (i.hasNext()) { i.next(); @@ -717,11 +705,22 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) } } +CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { + for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { + auto std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()) + if (auto.first == prefix.end()) + return it.value(); + } + return NULL; +} + CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound) { + CardInfo *foundCard; + if (cardName.isEmpty()) return noCard; - else if (cardMap.contains(cardName)) - return cardMap.value(cardName); + else if ((foundCard = cardMap.findByPrefix(cardName.toStdString()))) + return foundCard; else if (createIfNotFound) { CardInfo *newCard = new CardInfo(this, cardName, true); newCard->addToSet(getSet("TK")); diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index bb151e7d0..dc961d96f 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -182,7 +182,13 @@ signals: enum LoadStatus { Ok, VersionTooOld, Invalid, NotLoaded, FileError, NoCards }; -typedef QHash CardNameMap; + +class CardNameMap: public QHash +{ + public: + CardInfo *findByPrefix(const std::string &prefix); +}; + typedef QHash SetNameMap; class CardDatabase : public QObject {