Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Donald Haase
Cockatrice
Commits
91a74abc
Commit
91a74abc
authored
Apr 08, 2010
by
Max-Wilhelm Bruker
Browse files
rewrote picture downloader
parent
7afb8669
Changes
2
Hide whitespace changes
Inline
Side-by-side
cockatrice/src/carddatabase.cpp
View file @
91a74abc
...
...
@@ -9,6 +9,9 @@
#include
<QPainter>
#include
<QUrl>
#include
<QSet>
#include
<QNetworkAccessManager>
#include
<QNetworkReply>
#include
<QNetworkRequest>
CardSet
::
CardSet
(
const
QString
&
_shortName
,
const
QString
&
_longName
)
:
shortName
(
_shortName
),
longName
(
_longName
)
...
...
@@ -235,8 +238,8 @@ CardDatabase::CardDatabase(QObject *parent)
connect
(
settingsCache
,
SIGNAL
(
cardDatabasePathChanged
()),
this
,
SLOT
(
loadCardDatabase
()));
connect
(
settingsCache
,
SIGNAL
(
picDownloadChanged
()),
this
,
SLOT
(
picDownloadChanged
()));
http
=
new
QHttp
(
this
);
connect
(
http
,
SIGNAL
(
requestFinished
(
int
,
bool
)),
this
,
SLOT
(
picDownloadFinished
(
int
,
bool
)));
networkManager
=
new
QNetworkAccessManager
(
this
);
connect
(
networkManager
,
SIGNAL
(
finished
(
QNetworkReply
*
)),
this
,
SLOT
(
picDownloadFinished
(
QNetworkReply
*
)));
loadCardDatabase
();
...
...
@@ -317,46 +320,54 @@ void CardDatabase::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
)
);
i
nt
dlID
=
http
->
get
(
url
.
path
(),
buffer
);
downloadBuffers
.
insert
(
dlID
,
QPair
<
CardInfo
*
,
QBuffer
*>
(
card
,
buffer
)
);
if
(
card
->
getPicURL
().
isEmpty
())
return
;
cardsToDownload
.
append
(
card
);
i
f
(
!
downloadRunning
)
startNextPicDownload
(
);
}
void
CardDatabase
::
picDownloadFinished
(
int
id
,
bool
error
)
void
CardDatabase
::
startNextPicDownload
(
)
{
if
(
!
downloadBuffers
.
contains
(
id
))
if
(
cardsToDownload
.
isEmpty
())
{
downloadRunning
=
false
;
return
;
}
const
QPair
<
CardInfo
*
,
QBuffer
*>
&
temp
=
downloadBuffers
.
value
(
id
);
CardInfo
*
card
=
temp
.
first
;
QBuffer
*
buffer
=
temp
.
second
;
downloadBuffers
.
remove
(
id
);
buffer
->
close
();
downloadRunning
=
true
;
if
(
!
error
)
{
QString
picsPath
=
settingsCache
->
getPicsPath
();
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
))
CardInfo
*
card
=
cardsToDownload
.
takeFirst
();
QNetworkRequest
req
(
QUrl
(
card
->
getPicURL
()));
req
.
setOriginatingObject
(
card
);
networkManager
->
get
(
req
);
}
void
CardDatabase
::
picDownloadFinished
(
QNetworkReply
*
reply
)
{
CardInfo
*
card
=
static_cast
<
CardInfo
*>
(
reply
->
request
().
originatingObject
());
QString
picsPath
=
settingsCache
->
getPicsPath
();
const
QByteArray
&
picData
=
reply
->
readAll
();
QPixmap
testPixmap
;
if
(
testPixmap
.
loadFromData
(
picData
))
{
if
(
!
QDir
(
QString
(
picsPath
+
"/downloadedPics/"
)).
exists
())
{
QDir
dir
(
picsPath
);
if
(
!
dir
.
exists
())
return
;
newPic
.
write
(
picData
);
newPic
.
close
();
card
->
updatePixmapCache
();
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
;
reply
->
deleteLater
();
startNextPicDownload
();
}
void
CardDatabase
::
loadSetsFromXml
(
QXmlStreamReader
&
xml
)
...
...
cockatrice/src/carddatabase.h
View file @
91a74abc
...
...
@@ -7,11 +7,12 @@
#include
<QDataStream>
#include
<QList>
#include
<QXmlStreamReader>
#include
<QHttp>
#include
<QBuffer>
class
CardDatabase
;
class
CardInfo
;
class
QNetworkAccessManager
;
class
QNetworkReply
;
class
QNetworkRequest
;
class
CardSet
:
public
QList
<
CardInfo
*>
{
private:
...
...
@@ -93,13 +94,14 @@ class CardDatabase : public QObject {
protected:
QHash
<
QString
,
CardInfo
*>
cardHash
;
QHash
<
QString
,
CardSet
*>
setHash
;
QMap
<
int
,
QPair
<
CardInfo
*
,
QBuffer
*>
>
downloadBuffers
;
QNetworkAccessManager
*
networkManager
;
QList
<
CardInfo
*>
cardsToDownload
;
bool
downloadRunning
;
CardInfo
*
noCard
;
private:
void
loadCardsFromXml
(
QXmlStreamReader
&
xml
);
void
loadSetsFromXml
(
QXmlStreamReader
&
xml
);
bool
picDownload
;
QHttp
*
http
;
void
startNextPicDownload
();
public:
CardDatabase
(
QObject
*
parent
=
0
);
~
CardDatabase
();
...
...
@@ -116,7 +118,7 @@ public:
public
slots
:
void
clearPixmapCache
();
private
slots
:
void
picDownloadFinished
(
int
id
,
bool
error
);
void
picDownloadFinished
(
QNetworkReply
*
reply
);
void
loadCardDatabase
();
void
picDownloadChanged
();
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment