void PictureLoader::loadImage(CardInfo *card) { QMutexLocker locker(&mutex); loadQueue.append(PictureToLoad(card)); emit startLoadQueue(); }
PictureLoader::PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent) : QObject(parent), _picsPath(__picsPath), picDownload(_picDownload), picDownloadHq(_picDownloadHq), downloadRunning(false), loadQueueRunning(false) { connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection); networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *))); }
void PictureLoader::picDownloadFinished(QNetworkReply *reply) { QString picsPath = _picsPath; if (reply->error()) { 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 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)) { QString setName = cardBeingDownloaded.getSetName(); if(!setName.isEmpty()) { if (!QDir().mkpath(picsPath + "/downloadedPics/" + setName)) { qDebug() << picsPath + "/downloadedPics/" + setName + " could not be created."; return; } QFile newPic(picsPath + "/downloadedPics/" + setName + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + 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(); cardBeingDownloaded.setHq(false); cardsToDownload.prepend(cardBeingDownloaded); } else { qDebug() << "LQ: received invalid picture. URL:" << reply->request().url(); if (cardBeingDownloaded.nextSet()) { cardBeingDownloaded.setHq(true); mutex.lock(); loadQueue.prepend(cardBeingDownloaded); mutex.unlock(); emit startLoadQueue(); } else emit imageLoaded(cardBeingDownloaded.getCard(), QImage()); } reply->deleteLater(); startNextPicDownload(); }
void PictureLoader::picDownloadFailed() { if (cardBeingDownloaded.nextSet()) { 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::loadImage(CardInfo *card) { QMutexLocker locker(&mutex); // avoid queueing the same card more than once if(card == cardBeingLoaded.getCard() || card == cardBeingDownloaded.getCard()) return; foreach(PictureToLoad pic, loadQueue) { if(pic.getCard() == card) return; } loadQueue.append(PictureToLoad(card)); emit startLoadQueue(); }
PictureLoader::PictureLoader() : QObject(0), downloadRunning(false), loadQueueRunning(false) { picsPath = settingsCache->getPicsPath(); picDownload = settingsCache->getPicDownload(); connect(this, SIGNAL(startLoadQueue()), this, SLOT(processLoadQueue()), Qt::QueuedConnection); connect(settingsCache, SIGNAL(picsPathChanged()), this, SLOT(picsPathChanged())); connect(settingsCache, SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged())); networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(picDownloadFinished(QNetworkReply *))); pictureLoaderThread = new QThread; pictureLoaderThread->start(QThread::LowPriority); moveToThread(pictureLoaderThread); }