From 3bce98e3a94f51b3d00718a9d0c8c831b704ea2f Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 7 Aug 2014 22:28:08 +0200 Subject: [PATCH] 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; }