From 12a151a0c326891d9690589afa7e2888daf431db Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 14 Aug 2009 01:21:45 +0200 Subject: [PATCH] added automatic download of card pictures --- cockatrice/src/carddatabase.cpp | 39 +++++++++++++++++++++++++++++++-- cockatrice/src/carddatabase.h | 12 +++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index e928c41d3..6114d89dd 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -6,6 +6,7 @@ #include #include #include +#include CardSet::CardSet(const QString &_shortName, const QString &_longName) : shortName(_shortName), longName(_longName) @@ -125,10 +126,42 @@ QPixmap *CardInfo::loadPixmap() return pixmap; if (pixmap->load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(sets[i]->getShortName()).arg(correctedName).arg(1))) return pixmap; + if(pixmap->load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("downloadedPics").arg(correctedName))) + return pixmap; + + startDownload(picsPath, correctedName); + + } return pixmap; } +void CardInfo::startDownload(QString picsPath, QString cardName) +{ if(!QDir(QString(picsPath+"/downloadedPics/")).exists()) + { + QDir dir(picsPath); + dir.mkdir("downloadedPics"); + } + newPic = new QFile(picsPath+"/downloadedPics/"+cardName+".full.jpg"); + newPic->open(QIODevice::WriteOnly); + connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(picDownloadFinished(int, bool))); + QUrl url(picURL); + http.setHost(url.host(), url.port(80)); + dlID = http.get(url.path(), newPic); +} + +void CardInfo::picDownloadFinished(int id, bool result) +{ + if(id == dlID){ + newPic->flush(); + newPic->close(); + http.close(); + updatePixmapCache(); + disconnect(&http, 0, this, 0); + delete newPic; + } +} + QPixmap *CardInfo::getPixmap(QSize size) { qDebug(QString("CardInfo::getPixmap(%1, %2) for %3").arg(size.width()).arg(size.height()).arg(getName()).toLatin1()); @@ -307,7 +340,7 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) if (xml.readNext() == QXmlStreamReader::EndElement) break; if (xml.name() == "card") { - QString name, manacost, type, pt, text; + QString name, manacost, type, pt, text, picURL; QStringList colors; SetList sets; int tableRow = 0; @@ -330,8 +363,10 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) colors << xml.readElementText(); else if (xml.name() == "tablerow") tableRow = xml.readElementText().toInt(); + else if (xml.name() == "picURL") + picURL = xml.readElementText(); } - cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, tableRow, sets)); + cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, colors, tableRow, sets, picURL)); } } } diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index 320440c25..56ccfce18 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include class CardDatabase; class CardInfo; @@ -31,7 +33,8 @@ public: void sortByKey(); }; -class CardInfo { +class CardInfo : QObject{ + Q_OBJECT private: CardDatabase *db; @@ -43,9 +46,14 @@ private: QString text; QStringList colors; QString picURL; + QHttp http; + QFile *newPic; + int dlID; int tableRow; QPixmap *pixmap; QMap scaledPixmapCache; + + void startDownload(QString, QString); public: CardInfo(CardDatabase *_db, const QString &_name = QString(), @@ -75,6 +83,8 @@ public: QPixmap *getPixmap(QSize size); void clearPixmapCache(); void updatePixmapCache(); +public slots: + void picDownloadFinished(int, bool); }; class CardDatabase : public QObject {