2010-01-18 03:23:55 +01:00
|
|
|
#include "somafmservice.h"
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QTemporaryFile>
|
2010-01-18 03:49:07 +01:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QDesktopServices>
|
2010-01-18 03:23:55 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
const char* SomaFMService::kServiceName = "SomaFM";
|
|
|
|
const char* SomaFMService::kLoadingChannelsText = "Getting channels";
|
|
|
|
const char* SomaFMService::kLoadingStreamText = "Loading stream";
|
|
|
|
const char* SomaFMService::kChannelListUrl = "http://somafm.com/channels.xml";
|
2010-01-18 03:49:07 +01:00
|
|
|
const char* SomaFMService::kHomepage = "http://somafm.com";
|
2010-01-18 03:23:55 +01:00
|
|
|
|
|
|
|
SomaFMService::SomaFMService(QObject* parent)
|
|
|
|
: RadioService(kServiceName, parent),
|
|
|
|
root_(NULL),
|
2010-01-18 03:49:07 +01:00
|
|
|
context_menu_(new QMenu),
|
2010-01-18 03:23:55 +01:00
|
|
|
network_(new QNetworkAccessManager(this))
|
|
|
|
{
|
2010-01-18 03:49:07 +01:00
|
|
|
context_menu_->addAction(QIcon(":media-playback-start.png"), "Add to playlist", this, SLOT(AddToPlaylist()));
|
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addAction(QIcon(":web.png"), "Open somafm.com in browser", this, SLOT(Homepage()));
|
|
|
|
context_menu_->addAction(QIcon(":refresh.png"), "Refresh channels", this, SLOT(RefreshChannels()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SomaFMService::~SomaFMService() {
|
|
|
|
delete context_menu_;
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RadioItem* SomaFMService::CreateRootItem(RadioItem* parent) {
|
|
|
|
root_ = new RadioItem(this, RadioItem::Type_Service, kServiceName, parent);
|
2010-01-18 03:49:07 +01:00
|
|
|
root_->icon = QIcon(":somafm.png");
|
2010-01-18 03:23:55 +01:00
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::LazyPopulate(RadioItem* item) {
|
|
|
|
switch (item->type) {
|
|
|
|
case RadioItem::Type_Service:
|
|
|
|
RefreshChannels();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::ShowContextMenu(RadioItem* item, const QPoint& global_pos) {
|
2010-01-18 03:49:07 +01:00
|
|
|
context_item_ = item;
|
|
|
|
context_menu_->popup(global_pos);
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::StartLoading(const QUrl& url) {
|
|
|
|
// Load the playlist
|
|
|
|
QNetworkRequest request = QNetworkRequest(url);
|
|
|
|
request.setRawHeader("User-Agent", QString("%1 %2").arg(
|
|
|
|
QCoreApplication::applicationName(), QCoreApplication::applicationVersion()).toUtf8());
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(request);
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(LoadPlaylistFinished()));
|
|
|
|
|
|
|
|
emit TaskStarted(kLoadingStreamText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::LoadPlaylistFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
emit TaskFinished(kLoadingStreamText);
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// TODO: Error handling
|
|
|
|
qDebug() << reply->errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Replace with some more robust .pls parsing :(
|
|
|
|
QTemporaryFile temp_file;
|
|
|
|
temp_file.open();
|
|
|
|
temp_file.write(reply->readAll());
|
|
|
|
temp_file.flush();
|
|
|
|
|
|
|
|
QSettings s(temp_file.fileName(), QSettings::IniFormat);
|
|
|
|
s.beginGroup("playlist");
|
|
|
|
|
|
|
|
emit StreamReady(reply->url().toString(), s.value("File1").toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::RefreshChannels() {
|
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl(kChannelListUrl));
|
|
|
|
request.setRawHeader("User-Agent", QString("%1 %2").arg(
|
|
|
|
QCoreApplication::applicationName(), QCoreApplication::applicationVersion()).toUtf8());
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(request);
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(RefreshChannelsFinished()));
|
|
|
|
|
|
|
|
emit TaskStarted(kLoadingChannelsText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::RefreshChannelsFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
emit TaskFinished(kLoadingChannelsText);
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// TODO: Error handling
|
|
|
|
qDebug() << reply->errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-18 03:49:07 +01:00
|
|
|
root_->ClearNotify();
|
|
|
|
|
2010-01-18 03:23:55 +01:00
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
reader.readNext();
|
|
|
|
|
|
|
|
if (reader.tokenType() == QXmlStreamReader::StartElement &&
|
|
|
|
reader.name() == "channel") {
|
|
|
|
ReadChannel(reader);
|
|
|
|
}
|
|
|
|
}
|
2010-01-18 03:49:07 +01:00
|
|
|
|
|
|
|
root_->lazy_loaded = true;
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::ReadChannel(QXmlStreamReader& reader) {
|
|
|
|
RadioItem* item = new RadioItem(this, Type_Stream, QString::null);
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
item->playable = true;
|
2010-01-18 03:49:07 +01:00
|
|
|
item->icon = QIcon(":last.fm/icon_radio.png");
|
2010-01-18 03:23:55 +01:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
switch (reader.readNext()) {
|
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
if (item->key.isNull()) {
|
|
|
|
// Didn't find a URL
|
|
|
|
delete item;
|
|
|
|
} else {
|
|
|
|
item->InsertNotify(root_);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (reader.name() == "title") {
|
|
|
|
item->display_text = reader.readElementText();
|
|
|
|
} else if (reader.name() == "dj") {
|
|
|
|
item->artist = reader.readElementText();
|
|
|
|
} else if (reader.name() == "fastpls" && reader.attributes().value("format") == "mp3") {
|
|
|
|
item->key = reader.readElementText();
|
|
|
|
} else {
|
|
|
|
ConsumeElement(reader);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::ConsumeElement(QXmlStreamReader& reader) {
|
|
|
|
int level = 1;
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
switch (reader.readNext()) {
|
|
|
|
case QXmlStreamReader::StartElement: level++; break;
|
|
|
|
case QXmlStreamReader::EndElement: level--; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SomaFMService::TitleForItem(const RadioItem* item) const {
|
|
|
|
return "SomaFM " + item->display_text;
|
|
|
|
}
|
2010-01-18 03:49:07 +01:00
|
|
|
|
|
|
|
void SomaFMService::Homepage() {
|
|
|
|
QDesktopServices::openUrl(QUrl(kHomepage));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::AddToPlaylist() {
|
|
|
|
emit AddItemToPlaylist(context_item_);
|
|
|
|
}
|