diff --git a/src/albumcoverfetcher.cpp b/src/albumcoverfetcher.cpp new file mode 100644 index 000000000..75034236e --- /dev/null +++ b/src/albumcoverfetcher.cpp @@ -0,0 +1,48 @@ +#include "albumcoverfetcher.h" + +#include + +#include +#include +#include + +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(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(sender()); + reply->deleteLater(); + + QImage image; + image.loadFromData(reply->readAll()); + + lastfm::Album album = requests_.take(reply); + emit AlbumCoverFetched(album, image); +} diff --git a/src/albumcoverfetcher.h b/src/albumcoverfetcher.h new file mode 100644 index 000000000..a8477abb4 --- /dev/null +++ b/src/albumcoverfetcher.h @@ -0,0 +1,35 @@ +#ifndef ALBUMCOVERFETCHER_H +#define ALBUMCOVERFETCHER_H + +#include +#include +#include +#include + +#include + +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 requests_; +}; + +#endif // ALBUMCOVERFETCHER_H diff --git a/src/lastfmservice.cpp b/src/lastfmservice.cpp index 8971de013..6424db89d 100644 --- a/src/lastfmservice.cpp +++ b/src/lastfmservice.cpp @@ -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( diff --git a/src/main.cpp b/src/main.cpp index 6858b33fc..29a026201 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,9 @@ # include #endif // Q_OS_WIN32 -#include "mainwindow.h" #include "directory.h" +#include "lastfmservice.h" +#include "mainwindow.h" #include "song.h" #include @@ -22,6 +23,9 @@ int main(int argc, char *argv[]) { qRegisterMetaType("DirectoryList"); qRegisterMetaType("SongList"); + lastfm::ws::ApiKey = LastFMService::kApiKey; + lastfm::ws::SharedSecret = LastFMService::kSecret; + QtSingleApplication a(argc, argv); if (a.isRunning()) { diff --git a/src/src.pro b/src/src.pro index af60d94d2..d0d9b744a 100644 --- a/src/src.pro +++ b/src/src.pro @@ -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 \