Rework the picture download behavior

Should fix #412
This commit is contained in:
Fabio Bas 2014-11-08 15:08:49 +01:00
parent 8d2e5d3c43
commit 8ca0b96d10
2 changed files with 60 additions and 53 deletions

View file

@ -101,6 +101,14 @@ QString PictureToLoad::getSetName() const
return QString(""); return QString("");
} }
CardSet *PictureToLoad::getCurrentSet() const
{
if (setIndex < sortedSets.size())
return sortedSets[setIndex];
else
return 0;
}
PictureLoader::PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent) PictureLoader::PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent)
: QObject(parent), : QObject(parent),
_picsPath(__picsPath), picDownload(_picDownload), picDownloadHq(_picDownloadHq), _picsPath(__picsPath), picDownload(_picDownload), picDownloadHq(_picDownloadHq),
@ -134,15 +142,17 @@ void PictureLoader::processLoadQueue()
PictureToLoad ptl = loadQueue.takeFirst(); PictureToLoad ptl = loadQueue.takeFirst();
mutex.unlock(); mutex.unlock();
//The list of paths to the folders in which to search for images
QList<QString> picsPaths = QList<QString>() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName();
QString setName=ptl.getSetName(); QString setName=ptl.getSetName();
QString correctedCardname=ptl.getCard()->getCorrectedName();
qDebug() << "Trying to load picture (set: " << setName << " card: " << correctedCardname << ")";
//The list of paths to the folders in which to search for images
QList<QString> picsPaths = QList<QString>() << _picsPath + "/CUSTOM/" + correctedCardname;
if(!setName.isEmpty()) if(!setName.isEmpty())
{ {
picsPaths << _picsPath + "/" + setName + "/" + ptl.getCard()->getCorrectedName() picsPaths << _picsPath + "/" + setName + "/" + correctedCardname
<< _picsPath + "/downloadedPics/" + setName + "/" + ptl.getCard()->getCorrectedName(); << _picsPath + "/downloadedPics/" + setName + "/" + correctedCardname;
} }
QImage image; QImage image;
@ -154,12 +164,14 @@ void PictureLoader::processLoadQueue()
for (int i = 0; i < picsPaths.length() && !found; i ++) { for (int i = 0; i < picsPaths.length() && !found; i ++) {
imgReader.setFileName(picsPaths.at(i)); imgReader.setFileName(picsPaths.at(i));
if (imgReader.read(&image)) { if (imgReader.read(&image)) {
qDebug() << "Picture found on disk (set: " << setName << " card: " << correctedCardname << ")";
emit imageLoaded(ptl.getCard(), image); emit imageLoaded(ptl.getCard(), image);
found = true; found = true;
break; break;
} }
imgReader.setFileName(picsPaths.at(i) + ".full"); imgReader.setFileName(picsPaths.at(i) + ".full");
if (imgReader.read(&image)) { if (imgReader.read(&image)) {
qDebug() << "Picture.full found on disk (set: " << setName << " card: " << correctedCardname << ")";
emit imageLoaded(ptl.getCard(), image); emit imageLoaded(ptl.getCard(), image);
found = true; found = true;
} }
@ -167,24 +179,32 @@ void PictureLoader::processLoadQueue()
if (!found) { if (!found) {
if (picDownload) { if (picDownload) {
qDebug() << "Picture NOT found, trying to download (set: " << setName << " card: " << correctedCardname << ")";
cardsToDownload.append(ptl); cardsToDownload.append(ptl);
if (!downloadRunning) if (!downloadRunning)
startNextPicDownload(); startNextPicDownload();
} else { } else {
if (ptl.nextSet()) if (ptl.nextSet())
{
qDebug() << "Picture NOT found and download disabled, moving to next set (newset: " << setName << " card: " << correctedCardname << ")";
mutex.lock();
loadQueue.prepend(ptl); loadQueue.prepend(ptl);
else mutex.unlock();
} else {
qDebug() << "Picture NOT found, download disabled, no more sets to try: BAILING OUT (oldset: " << setName << " card: " << correctedCardname << ")";
emit imageLoaded(ptl.getCard(), QImage()); emit imageLoaded(ptl.getCard(), QImage());
}
} }
} }
} }
} }
QString PictureLoader::getPicUrl(CardInfo *card) QString PictureLoader::getPicUrl()
{ {
if (!picDownload) return QString(""); if (!picDownload) return QString("");
CardSet *set = card->getPreferredSet(); CardInfo *card = cardBeingDownloaded.getCard();
CardSet *set=cardBeingDownloaded.getCurrentSet();
QString picUrl = QString(""); QString picUrl = QString("");
// if sets have been defined for the card, they can contain custom picUrls // if sets have been defined for the card, they can contain custom picUrls
@ -211,10 +231,10 @@ QString PictureLoader::getPicUrl(CardInfo *card)
if (set) { if (set) {
picUrl.replace("!setcode!", QUrl::toPercentEncoding(set->getShortName())); picUrl.replace("!setcode!", QUrl::toPercentEncoding(set->getShortName()));
picUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName())); picUrl.replace("!setname!", QUrl::toPercentEncoding(set->getLongName()));
int muid = card->getMuId(set->getShortName());
if (muid)
picUrl.replace("!cardid!", QUrl::toPercentEncoding(QString::number(muid)));
} }
int muid = card->getPreferredMuId();
if (muid)
picUrl.replace("!cardid!", QUrl::toPercentEncoding(QString::number(muid)));
if (picUrl.contains("!name!") || if (picUrl.contains("!name!") ||
picUrl.contains("!setcode!") || picUrl.contains("!setcode!") ||
@ -239,19 +259,33 @@ void PictureLoader::startNextPicDownload()
cardBeingDownloaded = cardsToDownload.takeFirst(); cardBeingDownloaded = cardsToDownload.takeFirst();
QString picUrl = getPicUrl(cardBeingDownloaded.getCard()); QString picUrl = getPicUrl();
if (picUrl.isEmpty()) { if (picUrl.isEmpty()) {
qDebug() << "No url for" << cardBeingDownloaded.getCard()->getName();
cardBeingDownloaded = 0;
downloadRunning = false; downloadRunning = false;
return; picDownloadFailed();
} else {
QUrl url(picUrl);
QNetworkRequest req(url);
qDebug() << "starting picture download:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url();
networkManager->get(req);
} }
}
QUrl url(picUrl); void PictureLoader::picDownloadFailed()
{
QNetworkRequest req(url); if (cardBeingDownloaded.nextSet())
qDebug() << "starting picture download:" << cardBeingDownloaded.getCard()->getName() << "Url:" << req.url(); {
networkManager->get(req); qDebug() << "Picture NOT found, download failed, moving to next set (newset: " << cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getCorrectedName() << ")";
mutex.lock();
loadQueue.prepend(cardBeingDownloaded);
mutex.unlock();
emit startLoadQueue();
} else {
qDebug() << "Picture NOT found, download failed, no more sets to try: BAILING OUT (oldset: " << cardBeingDownloaded.getSetName() << " card: " << cardBeingDownloaded.getCard()->getCorrectedName() << ")";
cardBeingDownloaded = 0;
emit imageLoaded(cardBeingDownloaded.getCard(), QImage());
}
} }
void PictureLoader::picDownloadFinished(QNetworkReply *reply) void PictureLoader::picDownloadFinished(QNetworkReply *reply)
@ -288,21 +322,9 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply)
} }
emit imageLoaded(cardBeingDownloaded.getCard(), testImage); emit imageLoaded(cardBeingDownloaded.getCard(), testImage);
} else if (cardBeingDownloaded.getHq()) {
qDebug() << "HQ: received invalid picture. URL:" << reply->request().url();
cardBeingDownloaded.setHq(false);
cardsToDownload.prepend(cardBeingDownloaded);
} else { } else {
qDebug() << "LQ: received invalid picture. URL:" << reply->request().url(); picDownloadFailed();
if (cardBeingDownloaded.nextSet()) { }
cardBeingDownloaded.setHq(true);
mutex.lock();
loadQueue.prepend(cardBeingDownloaded);
mutex.unlock();
emit startLoadQueue();
} else
emit imageLoaded(cardBeingDownloaded.getCard(), QImage());
}
reply->deleteLater(); reply->deleteLater();
startNextPicDownload(); startNextPicDownload();
@ -497,21 +519,6 @@ void CardInfo::updatePixmapCache()
emit pixmapUpdated(); emit pixmapUpdated();
} }
CardSet* CardInfo::getPreferredSet()
{
if(sets.isEmpty())
return 0;
SetList sortedSets = sets;
sortedSets.sortByKey();
return sortedSets.first();
}
int CardInfo::getPreferredMuId()
{
CardSet *set = getPreferredSet();
return set ? muIds[set->getShortName()] : 0;
}
QString CardInfo::simplifyName(const QString &name) { QString CardInfo::simplifyName(const QString &name) {
QString simpleName(name); QString simpleName(name);

View file

@ -53,6 +53,7 @@ private:
public: public:
PictureToLoad(CardInfo *_card = 0, bool _hq = true); PictureToLoad(CardInfo *_card = 0, bool _hq = true);
CardInfo *getCard() const { return card; } CardInfo *getCard() const { return card; }
CardSet *getCurrentSet() const;
QString getSetName() const; QString getSetName() const;
bool nextSet(); bool nextSet();
bool getHq() const { return hq; } bool getHq() const { return hq; }
@ -70,7 +71,7 @@ private:
PictureToLoad cardBeingDownloaded; PictureToLoad cardBeingDownloaded;
bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning; bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning;
void startNextPicDownload(); void startNextPicDownload();
QString getPicUrl(CardInfo* card); QString getPicUrl();
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();
@ -80,6 +81,7 @@ public:
void loadImage(CardInfo *card); void loadImage(CardInfo *card);
private slots: private slots:
void picDownloadFinished(QNetworkReply *reply); void picDownloadFinished(QNetworkReply *reply);
void picDownloadFailed();
public slots: public slots:
void processLoadQueue(); void processLoadQueue();
signals: signals:
@ -163,8 +165,6 @@ public:
void clearPixmapCache(); void clearPixmapCache();
void clearPixmapCacheMiss(); void clearPixmapCacheMiss();
void imageLoaded(const QImage &image); void imageLoaded(const QImage &image);
CardSet *getPreferredSet();
int getPreferredMuId();
/** /**
* Simplify a name to have no punctuation and lowercase all letters, for * Simplify a name to have no punctuation and lowercase all letters, for