fixed card picture download
This commit is contained in:
parent
912c0bcdbf
commit
df8c01992b
2 changed files with 52 additions and 48 deletions
|
@ -138,50 +138,10 @@ QPixmap *CardInfo::loadPixmap()
|
|||
return pixmap;
|
||||
}
|
||||
if (db->getPicDownload())
|
||||
startDownload();
|
||||
db->startPicDownload(this);
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
void CardInfo::startDownload()
|
||||
{
|
||||
downloadBuffer = new QBuffer(this);
|
||||
downloadBuffer->open(QIODevice::ReadWrite);
|
||||
http = new QHttp(this);
|
||||
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(), downloadBuffer);
|
||||
}
|
||||
|
||||
void CardInfo::picDownloadFinished(int id, bool error)
|
||||
{
|
||||
if (id != dlID)
|
||||
return;
|
||||
http->close();
|
||||
disconnect(http, 0, this, 0);
|
||||
http->deleteLater();
|
||||
|
||||
downloadBuffer->close();
|
||||
if (!error) {
|
||||
QString picsPath = db->getPicsPath();
|
||||
const QByteArray &picData = downloadBuffer->data();
|
||||
QPixmap testPixmap;
|
||||
if (testPixmap.loadFromData(picData)) {
|
||||
if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) {
|
||||
QDir dir(picsPath);
|
||||
dir.mkdir("downloadedPics");
|
||||
}
|
||||
QFile newPic(picsPath + "/downloadedPics/" + getCorrectedName() + ".full.jpg");
|
||||
newPic.open(QIODevice::WriteOnly);
|
||||
newPic.write(picData);
|
||||
newPic.close();
|
||||
|
||||
updatePixmapCache();
|
||||
}
|
||||
}
|
||||
delete downloadBuffer;
|
||||
}
|
||||
|
||||
QPixmap *CardInfo::getPixmap(QSize size)
|
||||
{
|
||||
qDebug(QString("CardInfo::getPixmap(%1, %2) for %3").arg(size.width()).arg(size.height()).arg(getName()).toLatin1());
|
||||
|
@ -266,6 +226,9 @@ QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)
|
|||
CardDatabase::CardDatabase(QObject *parent)
|
||||
: QObject(parent), noCard(0)
|
||||
{
|
||||
http = new QHttp(this);
|
||||
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(picDownloadFinished(int, bool)));
|
||||
|
||||
updateDatabasePath();
|
||||
updatePicDownload();
|
||||
updatePicsPath();
|
||||
|
@ -345,6 +308,49 @@ void CardDatabase::clearPixmapCache()
|
|||
noCard->clearPixmapCache();
|
||||
}
|
||||
|
||||
void CardDatabase::startPicDownload(CardInfo *card)
|
||||
{
|
||||
QBuffer *buffer = new QBuffer(this);
|
||||
buffer->open(QIODevice::ReadWrite);
|
||||
QUrl url(card->getPicURL());
|
||||
http->setHost(url.host(), url.port(80));
|
||||
int dlID = http->get(url.path(), buffer);
|
||||
downloadBuffers.insert(dlID, QPair<CardInfo *, QBuffer *>(card, buffer));
|
||||
}
|
||||
|
||||
void CardDatabase::picDownloadFinished(int id, bool error)
|
||||
{
|
||||
if (!downloadBuffers.contains(id))
|
||||
return;
|
||||
|
||||
const QPair<CardInfo *, QBuffer *> &temp = downloadBuffers.value(id);
|
||||
CardInfo *card = temp.first;
|
||||
QBuffer *buffer = temp.second;
|
||||
downloadBuffers.remove(id);
|
||||
buffer->close();
|
||||
|
||||
if (!error) {
|
||||
const QByteArray &picData = buffer->data();
|
||||
QPixmap testPixmap;
|
||||
if (testPixmap.loadFromData(picData)) {
|
||||
if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) {
|
||||
QDir dir(picsPath);
|
||||
if (!dir.exists())
|
||||
return;
|
||||
dir.mkdir("downloadedPics");
|
||||
}
|
||||
QFile newPic(picsPath + "/downloadedPics/" + card->getCorrectedName() + ".full.jpg");
|
||||
if (!newPic.open(QIODevice::WriteOnly))
|
||||
return;
|
||||
newPic.write(picData);
|
||||
newPic.close();
|
||||
|
||||
card->updatePixmapCache();
|
||||
}
|
||||
}
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
void CardDatabase::loadSetsFromXml(QXmlStreamReader &xml)
|
||||
{
|
||||
while (!xml.atEnd()) {
|
||||
|
|
|
@ -46,14 +46,9 @@ private:
|
|||
QString text;
|
||||
QStringList colors;
|
||||
QString picURL;
|
||||
QHttp *http;
|
||||
QBuffer *downloadBuffer;
|
||||
int dlID;
|
||||
int tableRow;
|
||||
QPixmap *pixmap;
|
||||
QMap<int, QPixmap *> scaledPixmapCache;
|
||||
|
||||
void startDownload();
|
||||
public:
|
||||
CardInfo(CardDatabase *_db,
|
||||
const QString &_name = QString(),
|
||||
|
@ -86,8 +81,6 @@ public:
|
|||
void clearPixmapCache();
|
||||
void clearPixmapCacheMiss();
|
||||
void updatePixmapCache();
|
||||
private slots:
|
||||
void picDownloadFinished(int id, bool error);
|
||||
signals:
|
||||
void pixmapUpdated();
|
||||
};
|
||||
|
@ -97,12 +90,14 @@ class CardDatabase : public QObject {
|
|||
protected:
|
||||
QHash<QString, CardInfo *> cardHash;
|
||||
QHash<QString, CardSet *> setHash;
|
||||
QMap<int, QPair<CardInfo *, QBuffer *> > downloadBuffers;
|
||||
CardInfo *noCard;
|
||||
QString picsPath, cardDatabasePath;
|
||||
private:
|
||||
void loadCardsFromXml(QXmlStreamReader &xml);
|
||||
void loadSetsFromXml(QXmlStreamReader &xml);
|
||||
bool picDownload;
|
||||
QHttp *http;
|
||||
public:
|
||||
CardDatabase(QObject *parent = 0);
|
||||
~CardDatabase();
|
||||
|
@ -116,6 +111,9 @@ public:
|
|||
int loadFromFile(const QString &fileName);
|
||||
bool saveToFile(const QString &fileName);
|
||||
const QString &getPicsPath() const { return picsPath; }
|
||||
void startPicDownload(CardInfo *card);
|
||||
private slots:
|
||||
void picDownloadFinished(int id, bool error);
|
||||
public slots:
|
||||
void updatePicsPath(const QString &path = QString());
|
||||
void updateDatabasePath(const QString &path = QString());
|
||||
|
|
Loading…
Reference in a new issue