2010-12-10 15:26:29 +01:00
|
|
|
#include "kittenloader.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
2012-10-12 12:38:12 +02:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
|
2010-12-10 15:26:29 +01:00
|
|
|
const char* KittenLoader::kFlickrKittenUrl =
|
|
|
|
"http://api.flickr.com/services/rest/"
|
|
|
|
"?method=flickr.photos.search"
|
2010-12-11 15:31:03 +01:00
|
|
|
"&api_key=b60e59d549d314c98ddb1dc06008a2c4"
|
2011-01-27 17:54:03 +01:00
|
|
|
"&tags=kitten"
|
2010-12-10 15:26:29 +01:00
|
|
|
"&sort=random"
|
|
|
|
"&safe_search=1"
|
2010-12-11 15:31:03 +01:00
|
|
|
"&content_type=1";
|
2010-12-10 15:26:29 +01:00
|
|
|
|
|
|
|
const char* KittenLoader::kFlickrPhotoUrl =
|
|
|
|
"http://farm%1.static.flickr.com/%2/%3_%4_m.jpg";
|
|
|
|
|
|
|
|
KittenLoader::KittenLoader(QObject* parent)
|
|
|
|
: AlbumCoverLoader(parent) {
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
quint64 KittenLoader::LoadKitten(const AlbumCoverLoaderOptions& options) {
|
2010-12-10 15:26:29 +01:00
|
|
|
if (!kitten_urls_.isEmpty()) {
|
|
|
|
QUrl url = kitten_urls_.dequeue();
|
|
|
|
return AlbumCoverLoader::LoadImageAsync(
|
2012-02-13 21:44:04 +01:00
|
|
|
options,
|
2010-12-10 15:26:29 +01:00
|
|
|
QString::null,
|
|
|
|
url.toString(),
|
|
|
|
QString::null,
|
|
|
|
QImage());
|
|
|
|
}
|
|
|
|
|
|
|
|
Task task;
|
2012-02-13 21:44:04 +01:00
|
|
|
task.options = options;
|
2010-12-10 15:26:29 +01:00
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
task.id = next_id_++;
|
|
|
|
pending_kittens_.enqueue(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
metaObject()->invokeMethod(this, "FetchMoreKittens", Qt::QueuedConnection);
|
|
|
|
|
|
|
|
return task.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KittenLoader::FetchMoreKittens() {
|
|
|
|
QNetworkRequest req = QNetworkRequest(QUrl(kFlickrKittenUrl));
|
|
|
|
QNetworkReply* reply = network_->get(req);
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(KittensRetrieved()));
|
2012-10-12 12:38:12 +02:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(KittensRetrieved(QNetworkReply*)), reply);
|
2010-12-10 15:26:29 +01:00
|
|
|
}
|
|
|
|
|
2012-10-12 12:38:12 +02:00
|
|
|
void KittenLoader::KittensRetrieved(QNetworkReply* reply) {
|
2010-12-10 15:26:29 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
reader.readNext();
|
|
|
|
if (reader.tokenType() == QXmlStreamReader::StartElement) {
|
|
|
|
if (reader.name() == "photo") {
|
|
|
|
QXmlStreamAttributes attrs = reader.attributes();
|
|
|
|
QStringRef farm_id = attrs.value("farm");
|
|
|
|
QStringRef photo_id = attrs.value("id");
|
|
|
|
QStringRef secret = attrs.value("secret");
|
|
|
|
QStringRef server = attrs.value("server");
|
|
|
|
QString photo_url = QString(kFlickrPhotoUrl)
|
|
|
|
.arg(farm_id.toString())
|
|
|
|
.arg(server.toString())
|
|
|
|
.arg(photo_id.toString())
|
|
|
|
.arg(secret.toString());
|
|
|
|
kitten_urls_ << QUrl(photo_url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
while (!kitten_urls_.isEmpty() && !pending_kittens_.isEmpty()) {
|
|
|
|
Task task = pending_kittens_.dequeue();
|
|
|
|
QUrl kitten_url = kitten_urls_.dequeue();
|
|
|
|
task.art_manual = kitten_url.toString();
|
|
|
|
task.state = State_TryingManual;
|
|
|
|
tasks_.enqueue(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kitten_urls_.isEmpty()) {
|
|
|
|
FetchMoreKittens();
|
|
|
|
}
|
|
|
|
|
|
|
|
metaObject()->invokeMethod(this, "ProcessTasks", Qt::QueuedConnection);
|
|
|
|
}
|