From 3bce98e3a94f51b3d00718a9d0c8c831b704ea2f Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 7 Aug 2014 22:28:08 +0200 Subject: [PATCH 1/3] Fix crashes trying to load the picture of a card with no --- cockatrice/src/carddatabase.cpp | 46 ++++++++++++++++++++++++--------- cockatrice/src/carddatabase.h | 2 +- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 84b387a6..778709d1 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -93,6 +93,14 @@ bool PictureToLoad::nextSet() return true; } +QString PictureToLoad::getSetName() const +{ + if (setIndex < sortedSets.size() - 1) + return sortedSets[setIndex]->getCorrectedShortName(); + else + return QString(""); +} + PictureLoader::PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent) : QObject(parent), _picsPath(__picsPath), picDownload(_picDownload), picDownloadHq(_picDownloadHq), @@ -127,9 +135,15 @@ void PictureLoader::processLoadQueue() 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"; + QList picsPaths = QList() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full"; + + + QString setName=ptl.getSetName(); + if(!setName.isEmpty()) + { + picsPaths << _picsPath + "/" + setName + "/" + ptl.getCard()->getCorrectedName() + ".full" + << _picsPath + "/downloadedPics/" + setName + "/" + ptl.getCard()->getCorrectedName() + ".full"; + } QImage image; QImageReader imgReader; @@ -166,19 +180,24 @@ QString PictureLoader::getPicUrl(CardInfo *card) CardSet *set = card->getPreferredSet(); QString picUrl = QString(""); - // first check if Hq is enabled and a custom Hq card url exists in cards.xml - if(picDownloadHq) + + // if sets have been defined for the card, they can contain custom picUrls + if(set) { - picUrl = card->getCustomPicURLHq(set->getShortName()); + // first check if Hq is enabled and a custom Hq card url exists in cards.xml + if(picDownloadHq) + { + picUrl = card->getCustomPicURLHq(set->getShortName()); + if (!picUrl.isEmpty()) + return picUrl; + } + + // then, test for a custom, non-Hq card url in cards.xml + picUrl = card->getCustomPicURL(set->getShortName()); if (!picUrl.isEmpty()) return picUrl; } - // then, test for a custom, non-Hq card url in cards.xml - picUrl = card->getCustomPicURL(set->getShortName()); - if (!picUrl.isEmpty()) - return picUrl; - // otherwise, fallback to the default url picUrl = picDownloadHq ? settingsCache->getPicUrlHq() : settingsCache->getPicUrl(); picUrl.replace("!name!", QUrl::toPercentEncoding(card->getCorrectedName())); @@ -456,6 +475,8 @@ void CardInfo::updatePixmapCache() CardSet* CardInfo::getPreferredSet() { + if(sets.isEmpty()) + return 0; SetList sortedSets = sets; sortedSets.sortByKey(); return sortedSets.first(); @@ -463,7 +484,8 @@ CardSet* CardInfo::getPreferredSet() int CardInfo::getPreferredMuId() { - return muIds[getPreferredSet()->getShortName()]; + CardSet *set = getPreferredSet(); + return set ? muIds[set->getShortName()] : 0; } QString CardInfo::simplifyName(const QString &name) { diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index bb151e7d..91abac59 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -55,7 +55,7 @@ public: PictureToLoad(CardInfo *_card = 0, bool _stripped = false, bool _hq = true); CardInfo *getCard() const { return card; } bool getStripped() const { return stripped; } - QString getSetName() const { return sortedSets[setIndex]->getCorrectedShortName(); } + QString getSetName() const; bool nextSet(); bool getHq() const { return hq; } void setHq(bool _hq) { hq = _hq; } From 88dfea8ece73c56be2a5307c949d60ceecde7b79 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 7 Aug 2014 22:45:43 +0200 Subject: [PATCH 2/3] Don't save card pictures if we don't have a proper set name for it --- cockatrice/src/carddatabase.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 778709d1..8e45dca0 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -248,21 +248,25 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) extension = ".jpg"; if (imgReader.read(&testImage)) { - if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) { - qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created."; - return; + QString setName = cardBeingDownloaded.getSetName(); + if(!setName.isEmpty()) + { + if (!QDir().mkpath(picsPath + "/downloadedPics/" + setName)) { + qDebug() << picsPath + "/downloadedPics/" + setName + " could not be created."; + return; + } + + QString suffix; + if (!cardBeingDownloaded.getStripped()) + suffix = ".full"; + + QFile newPic(picsPath + "/downloadedPics/" + setName + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); + if (!newPic.open(QIODevice::WriteOnly)) + return; + newPic.write(picData); + newPic.close(); } - QString suffix; - if (!cardBeingDownloaded.getStripped()) - suffix = ".full"; - - QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); - if (!newPic.open(QIODevice::WriteOnly)) - return; - newPic.write(picData); - newPic.close(); - emit imageLoaded(cardBeingDownloaded.getCard(), testImage); } else if (cardBeingDownloaded.getHq()) { qDebug() << "HQ: received invalid picture. URL:" << reply->request().url(); From bba8184d3715646489c2640a22c300710772c6c8 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 7 Aug 2014 22:45:59 +0200 Subject: [PATCH 3/3] Fix off by one --- cockatrice/src/carddatabase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 8e45dca0..9e7b38dc 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -95,7 +95,7 @@ bool PictureToLoad::nextSet() QString PictureToLoad::getSetName() const { - if (setIndex < sortedSets.size() - 1) + if (setIndex < sortedSets.size()) return sortedSets[setIndex]->getCorrectedShortName(); else return QString("");