#include "lastfmservice.h" #include "lastfmconfig.h" #include "radioitem.h" #include #include #include #include const char* LastFMService::kSettingsGroup = "Last.fm"; LastFMService::LastFMService(QObject* parent) : RadioService("Last.fm", parent) { lastfm::ws::ApiKey = "75d20fb472be99275392aefa2760ea09"; lastfm::ws::SharedSecret = "d3072b60ae626be12be69448f5c46e70"; QSettings settings; settings.beginGroup(kSettingsGroup); lastfm::ws::Username = settings.value("user").toString(); lastfm::ws::SessionKey = settings.value("session").toString(); config_ = new LastFMConfig(this); } LastFMService::~LastFMService() { delete config_; } RadioItem* LastFMService::CreateRootItem(RadioItem* parent) { RadioItem* item = new RadioItem(this, RadioItem::Type_Service, "Last.fm", parent); item->icon = QIcon(":last.fm/as.png"); return item; } void LastFMService::LazyPopulate(RadioItem *item) { switch (item->type) { case RadioItem::Type_Service: // Create child items CreateStationItem(Type_MyRecommendations, "My Recommendations", ":last.fm/recommended_radio.png", item); CreateStationItem(Type_MyRadio, "My Radio Station", ":last.fm/personal_radio.png", item); CreateStationItem(Type_MyLoved, "My Loved Tracks", ":last.fm/loved_radio.png", item); CreateStationItem(Type_MyNeighbourhood, "My Neighbourhood", ":last.fm/neighbour_radio.png", item); if (lastfm::ws::SessionKey.isEmpty()) config_->show(); break; default: break; } item->lazy_loaded = true; } RadioItem* LastFMService::CreateStationItem(ItemType type, const QString& name, const QString& icon, RadioItem* parent) { RadioItem* ret = new RadioItem(this, type, name, parent); ret->lazy_loaded = true; ret->icon = QIcon(icon); return ret; } void LastFMService::Authenticate(const QString& username, const QString& password) { QMap params; params["method"] = "auth.getMobileSession"; params["username"] = username; params["authToken"] = lastfm::md5((username + lastfm::md5(password.toUtf8())).toUtf8()); QNetworkReply* reply = lastfm::ws::post(params); connect(reply, SIGNAL(finished()), SLOT(AuthenticateReplyFinished())); } void LastFMService::AuthenticateReplyFinished() { QNetworkReply* reply = qobject_cast(sender()); if (!reply) { emit AuthenticationComplete(false); return; } // Parse the reply try { lastfm::XmlQuery const lfm = lastfm::ws::parse(reply); lastfm::ws::Username = lfm["session"]["name"].text(); lastfm::ws::SessionKey = lfm["session"]["key"].text(); } catch (std::runtime_error& e) { qDebug() << e.what(); emit AuthenticationComplete(false); return; } // Save the session key QSettings settings; settings.beginGroup(kSettingsGroup); settings.setValue("username", lastfm::ws::Username); settings.setValue("session", lastfm::ws::SessionKey); emit AuthenticationComplete(true); }