Add album cover fetcher (not used yet).

This commit is contained in:
John Maguire 2010-02-23 19:26:21 +00:00
parent 441895a331
commit 992ce7ed0d
5 changed files with 92 additions and 6 deletions

48
src/albumcoverfetcher.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "albumcoverfetcher.h"
#include <QNetworkReply>
#include <lastfm/Artist>
#include <lastfm/XmlQuery>
#include <lastfm/ws.h>
AlbumCoverFetcher::AlbumCoverFetcher(QObject* parent)
: QObject(parent) {
}
lastfm::Album AlbumCoverFetcher::FetchAlbumCover(
const QString& artist_name, const QString& album_name) {
lastfm::Artist artist(artist_name);
lastfm::Album album(artist, album_name);
QNetworkReply* reply = album.getInfo();
connect(reply, SIGNAL(finished()), SLOT(AlbumGetInfoFinished()));
requests_.insert(reply, album);
return album;
}
void AlbumCoverFetcher::AlbumGetInfoFinished() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();
lastfm::XmlQuery query(lastfm::ws::parse(reply));
qDebug() << query["album"]["image size=large"].text();
QUrl image_url(query["album"]["image size=large"].text());
QNetworkReply* image_reply = network_.get(QNetworkRequest(image_url));
connect(image_reply, SIGNAL(finished()), SLOT(AlbumCoverFetchFinished()));
lastfm::Album album = requests_[reply];
requests_[image_reply] = album;
}
void AlbumCoverFetcher::AlbumCoverFetchFinished() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();
QImage image;
image.loadFromData(reply->readAll());
lastfm::Album album = requests_.take(reply);
emit AlbumCoverFetched(album, image);
}

35
src/albumcoverfetcher.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef ALBUMCOVERFETCHER_H
#define ALBUMCOVERFETCHER_H
#include <QImage>
#include <QMap>
#include <QNetworkAccessManager>
#include <QObject>
#include <lastfm/Album>
class QNetworkReply;
class QString;
class AlbumCoverFetcher : public QObject {
Q_OBJECT
public:
AlbumCoverFetcher(QObject* parent = 0);
virtual ~AlbumCoverFetcher() {}
lastfm::Album FetchAlbumCover(const QString& artist, const QString& album);
signals:
void AlbumCoverFetched(lastfm::Album, QImage cover);
private slots:
void AlbumGetInfoFinished();
void AlbumCoverFetchFinished();
private:
QNetworkAccessManager network_;
QMap<QNetworkReply*, lastfm::Album> requests_;
};
#endif // ALBUMCOVERFETCHER_H

View File

@ -32,9 +32,6 @@ LastFMService::LastFMService(QObject* parent)
friends_list_(NULL),
neighbours_list_(NULL)
{
lastfm::ws::ApiKey = kApiKey;
lastfm::ws::SharedSecret = kSecret;
ReloadSettings();
play_action_ = context_menu_->addAction(

View File

@ -4,8 +4,9 @@
# include <iostream>
#endif // Q_OS_WIN32
#include "mainwindow.h"
#include "directory.h"
#include "lastfmservice.h"
#include "mainwindow.h"
#include "song.h"
#include <QtSingleApplication>
@ -22,6 +23,9 @@ int main(int argc, char *argv[]) {
qRegisterMetaType<DirectoryList>("DirectoryList");
qRegisterMetaType<SongList>("SongList");
lastfm::ws::ApiKey = LastFMService::kApiKey;
lastfm::ws::SharedSecret = LastFMService::kSecret;
QtSingleApplication a(argc, argv);
if (a.isRunning()) {

View File

@ -51,7 +51,8 @@ SOURCES += main.cpp \
librarydirectorymodel.cpp \
libraryconfigdialog.cpp \
lastfmconfigdialog.cpp \
about.cpp
about.cpp \
albumcoverfetcher.cpp
HEADERS += mainwindow.h \
player.h \
library.h \
@ -103,7 +104,8 @@ HEADERS += mainwindow.h \
librarydirectorymodel.h \
libraryconfigdialog.h \
lastfmconfigdialog.h \
about.h
about.h \
albumcoverfetcher.h
FORMS += mainwindow.ui \
libraryconfig.ui \
fileview.ui \