2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-01-18 03:23:55 +01:00
|
|
|
#include "somafmservice.h"
|
2010-05-10 13:12:44 +02:00
|
|
|
#include "radiomodel.h"
|
2010-10-16 19:20:54 +02:00
|
|
|
#include "core/network.h"
|
2010-06-23 15:21:30 +02:00
|
|
|
#include "core/taskmanager.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-01-18 03:23:55 +01:00
|
|
|
|
|
|
|
#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::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
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
SomaFMService::SomaFMService(RadioModel* parent)
|
2010-01-18 03:23:55 +01:00
|
|
|
: RadioService(kServiceName, parent),
|
|
|
|
root_(NULL),
|
2010-08-27 17:20:52 +02:00
|
|
|
context_menu_(NULL),
|
2010-06-23 15:21:30 +02:00
|
|
|
get_channels_task_id_(0),
|
|
|
|
get_stream_task_id_(0),
|
2010-10-16 19:20:54 +02:00
|
|
|
network_(new NetworkAccessManager(this))
|
2010-01-18 03:23:55 +01:00
|
|
|
{
|
2010-01-18 03:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SomaFMService::~SomaFMService() {
|
|
|
|
delete context_menu_;
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
QStandardItem* SomaFMService::CreateRootItem() {
|
|
|
|
root_ = new QStandardItem(QIcon(":/providers/somafm.png"), kServiceName);
|
|
|
|
root_->setData(true, RadioModel::Role_CanLazyLoad);
|
2010-01-18 03:23:55 +01:00
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
void SomaFMService::LazyPopulate(QStandardItem* item) {
|
|
|
|
switch (item->data(RadioModel::Role_Type).toInt()) {
|
|
|
|
case RadioModel::Type_Service:
|
2010-01-18 03:23:55 +01:00
|
|
|
RefreshChannels();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
void SomaFMService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) {
|
2010-08-27 17:20:52 +02:00
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu;
|
|
|
|
context_menu_->addAction(IconLoader::Load("media-playback-start"), tr("Add to playlist"), this, SLOT(AddToPlaylist()));
|
2010-12-04 16:49:43 +01:00
|
|
|
context_menu_->addAction(IconLoader::Load("media-playback-start"), tr("Load"), this, SLOT(LoadToPlaylist()));
|
2010-08-27 17:20:52 +02:00
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addAction(IconLoader::Load("download"), tr("Open somafm.com in browser"), this, SLOT(Homepage()));
|
|
|
|
context_menu_->addAction(IconLoader::Load("view-refresh"), tr("Refresh channels"), this, SLOT(RefreshChannels()));
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
context_item_ = model()->itemFromIndex(index);
|
2010-01-18 03:49:07 +01:00
|
|
|
context_menu_->popup(global_pos);
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
2010-05-18 22:43:10 +02:00
|
|
|
PlaylistItem::SpecialLoadResult SomaFMService::StartLoading(const QUrl& url) {
|
2010-01-18 03:23:55 +01:00
|
|
|
// 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()));
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
if (!get_stream_task_id_)
|
|
|
|
get_stream_task_id_ = model()->task_manager()->StartTask(tr("Loading stream"));
|
2010-05-18 22:43:10 +02:00
|
|
|
|
|
|
|
return PlaylistItem::SpecialLoadResult(
|
|
|
|
PlaylistItem::SpecialLoadResult::WillLoadAsynchronously, url);
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::LoadPlaylistFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2010-06-23 15:21:30 +02:00
|
|
|
model()->task_manager()->SetTaskFinished(get_stream_task_id_);
|
|
|
|
get_stream_task_id_ = 0;
|
2010-01-18 03:23:55 +01:00
|
|
|
|
2010-05-18 22:43:10 +02:00
|
|
|
QUrl original_url(reply->url());
|
|
|
|
|
2010-01-18 03:23:55 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// TODO: Error handling
|
|
|
|
qDebug() << reply->errorString();
|
2010-05-18 22:43:10 +02:00
|
|
|
emit AsyncLoadFinished(PlaylistItem::SpecialLoadResult(
|
|
|
|
PlaylistItem::SpecialLoadResult::NoMoreTracks, original_url));
|
2010-01-18 03:23:55 +01:00
|
|
|
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");
|
|
|
|
|
2010-05-18 22:43:10 +02:00
|
|
|
emit AsyncLoadFinished(PlaylistItem::SpecialLoadResult(
|
|
|
|
PlaylistItem::SpecialLoadResult::TrackAvailable,
|
|
|
|
original_url, s.value("File1").toString()));
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
2010-06-23 15:21:30 +02:00
|
|
|
if (!get_channels_task_id_)
|
|
|
|
get_channels_task_id_ = model()->task_manager()->StartTask(tr("Getting channels"));
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::RefreshChannelsFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2010-06-23 15:21:30 +02:00
|
|
|
model()->task_manager()->SetTaskFinished(get_channels_task_id_);
|
|
|
|
get_channels_task_id_ = 0;
|
2010-01-18 03:23:55 +01:00
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// TODO: Error handling
|
|
|
|
qDebug() << reply->errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
if (root_->hasChildren())
|
|
|
|
root_->removeRows(0, root_->rowCount());
|
2010-01-18 03:49:07 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::ReadChannel(QXmlStreamReader& reader) {
|
2011-01-09 19:27:41 +01:00
|
|
|
QStandardItem* item = new QStandardItem(QIcon(":last.fm/icon_radio.png"), QString());
|
|
|
|
item->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
|
2010-01-18 03:23:55 +01:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
switch (reader.readNext()) {
|
|
|
|
case QXmlStreamReader::EndElement:
|
2011-01-09 19:27:41 +01:00
|
|
|
if (item->data(RadioModel::Role_Url).toString().isNull()) {
|
2010-01-18 03:23:55 +01:00
|
|
|
// Didn't find a URL
|
|
|
|
delete item;
|
|
|
|
} else {
|
2011-01-09 19:27:41 +01:00
|
|
|
root_->appendRow(item);
|
2010-01-18 03:23:55 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case QXmlStreamReader::StartElement:
|
|
|
|
if (reader.name() == "title") {
|
2011-01-09 19:27:41 +01:00
|
|
|
item->setText(reader.readElementText());
|
|
|
|
item->setData("SomaFM " + item->text(), RadioModel::Role_Title);
|
2010-01-18 03:23:55 +01:00
|
|
|
} else if (reader.name() == "dj") {
|
2011-01-09 19:27:41 +01:00
|
|
|
item->setData(reader.readElementText(), RadioModel::Role_Artist);
|
2010-01-18 03:23:55 +01:00
|
|
|
} else if (reader.name() == "fastpls" && reader.attributes().value("format") == "mp3") {
|
2011-01-09 19:27:41 +01:00
|
|
|
item->setData(reader.readElementText(), RadioModel::Role_Url);
|
2010-01-18 03:23:55 +01:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-18 03:49:07 +01:00
|
|
|
void SomaFMService::Homepage() {
|
|
|
|
QDesktopServices::openUrl(QUrl(kHomepage));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::AddToPlaylist() {
|
2011-01-10 23:26:13 +01:00
|
|
|
AddItemToPlaylist(context_item_->index(), false);
|
2010-12-04 16:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SomaFMService::LoadToPlaylist() {
|
2011-01-10 23:26:13 +01:00
|
|
|
AddItemToPlaylist(context_item_->index(), true);
|
2010-01-18 03:49:07 +01:00
|
|
|
}
|
2010-05-18 22:43:10 +02:00
|
|
|
|
|
|
|
PlaylistItem::Options SomaFMService::playlistitem_options() const {
|
|
|
|
return PlaylistItem::SpecialPlayBehaviour |
|
|
|
|
PlaylistItem::PauseDisabled;
|
|
|
|
}
|