1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "somafmservice.h"
19
#include "radiomodel.h"
20
#include "core/network.h"
21
#include "core/taskmanager.h"
22
#include "ui/iconloader.h"
24
#include <QNetworkRequest>
25
#include <QNetworkReply>
26
#include <QXmlStreamReader>
28
#include <QTemporaryFile>
30
#include <QDesktopServices>
31
#include <QCoreApplication>
34
const char* SomaFMService::kServiceName = "SomaFM";
35
const char* SomaFMService::kChannelListUrl = "http://somafm.com/channels.xml";
36
const char* SomaFMService::kHomepage = "http://somafm.com";
38
SomaFMService::SomaFMService(RadioModel* parent)
39
: RadioService(kServiceName, parent),
42
get_channels_task_id_(0),
43
get_stream_task_id_(0),
44
network_(new NetworkAccessManager(this))
48
SomaFMService::~SomaFMService() {
52
QStandardItem* SomaFMService::CreateRootItem() {
53
root_ = new QStandardItem(QIcon(":/providers/somafm.png"), kServiceName);
54
root_->setData(true, RadioModel::Role_CanLazyLoad);
58
void SomaFMService::LazyPopulate(QStandardItem* item) {
59
switch (item->data(RadioModel::Role_Type).toInt()) {
60
case RadioModel::Type_Service:
69
void SomaFMService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) {
71
context_menu_ = new QMenu;
72
context_menu_->addActions(GetPlaylistActions());
73
context_menu_->addAction(IconLoader::Load("download"), tr("Open somafm.com in browser"), this, SLOT(Homepage()));
74
context_menu_->addAction(IconLoader::Load("view-refresh"), tr("Refresh channels"), this, SLOT(RefreshChannels()));
77
context_item_ = model()->itemFromIndex(index);
78
context_menu_->popup(global_pos);
81
PlaylistItem::SpecialLoadResult SomaFMService::StartLoading(const QUrl& url) {
83
QNetworkRequest request = QNetworkRequest(url);
84
request.setRawHeader("User-Agent", QString("%1 %2").arg(
85
QCoreApplication::applicationName(), QCoreApplication::applicationVersion()).toUtf8());
87
QNetworkReply* reply = network_->get(request);
88
connect(reply, SIGNAL(finished()), SLOT(LoadPlaylistFinished()));
90
if (!get_stream_task_id_)
91
get_stream_task_id_ = model()->task_manager()->StartTask(tr("Loading stream"));
93
return PlaylistItem::SpecialLoadResult(
94
PlaylistItem::SpecialLoadResult::WillLoadAsynchronously, url);
97
void SomaFMService::LoadPlaylistFinished() {
98
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
99
model()->task_manager()->SetTaskFinished(get_stream_task_id_);
100
get_stream_task_id_ = 0;
102
QUrl original_url(reply->url());
104
if (reply->error() != QNetworkReply::NoError) {
105
// TODO: Error handling
106
qDebug() << reply->errorString();
107
emit AsyncLoadFinished(PlaylistItem::SpecialLoadResult(
108
PlaylistItem::SpecialLoadResult::NoMoreTracks, original_url));
112
// TODO: Replace with some more robust .pls parsing :(
113
QTemporaryFile temp_file;
115
temp_file.write(reply->readAll());
118
QSettings s(temp_file.fileName(), QSettings::IniFormat);
119
s.beginGroup("playlist");
121
emit AsyncLoadFinished(PlaylistItem::SpecialLoadResult(
122
PlaylistItem::SpecialLoadResult::TrackAvailable,
123
original_url, s.value("File1").toString()));
126
void SomaFMService::RefreshChannels() {
127
QNetworkReply* reply = network_->get(QNetworkRequest(QUrl(kChannelListUrl)));
128
connect(reply, SIGNAL(finished()), SLOT(RefreshChannelsFinished()));
130
if (!get_channels_task_id_)
131
get_channels_task_id_ = model()->task_manager()->StartTask(tr("Getting channels"));
134
void SomaFMService::RefreshChannelsFinished() {
135
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
136
model()->task_manager()->SetTaskFinished(get_channels_task_id_);
137
get_channels_task_id_ = 0;
139
if (reply->error() != QNetworkReply::NoError) {
140
// TODO: Error handling
141
qDebug() << reply->errorString();
145
if (root_->hasChildren())
146
root_->removeRows(0, root_->rowCount());
148
QXmlStreamReader reader(reply);
149
while (!reader.atEnd()) {
152
if (reader.tokenType() == QXmlStreamReader::StartElement &&
153
reader.name() == "channel") {
159
void SomaFMService::ReadChannel(QXmlStreamReader& reader) {
160
QStandardItem* item = new QStandardItem(QIcon(":last.fm/icon_radio.png"), QString());
161
item->setData(RadioModel::PlayBehaviour_SingleItem, RadioModel::Role_PlayBehaviour);
163
while (!reader.atEnd()) {
164
switch (reader.readNext()) {
165
case QXmlStreamReader::EndElement:
166
if (item->data(RadioModel::Role_Url).toString().isNull()) {
170
root_->appendRow(item);
174
case QXmlStreamReader::StartElement:
175
if (reader.name() == "title") {
176
item->setText(reader.readElementText());
177
item->setData("SomaFM " + item->text(), RadioModel::Role_Title);
178
} else if (reader.name() == "dj") {
179
item->setData(reader.readElementText(), RadioModel::Role_Artist);
180
} else if (reader.name() == "fastpls" && reader.attributes().value("format") == "mp3") {
181
item->setData(reader.readElementText(), RadioModel::Role_Url);
183
ConsumeElement(reader);
195
void SomaFMService::ConsumeElement(QXmlStreamReader& reader) {
197
while (!reader.atEnd()) {
198
switch (reader.readNext()) {
199
case QXmlStreamReader::StartElement: level++; break;
200
case QXmlStreamReader::EndElement: level--; break;
209
void SomaFMService::Homepage() {
210
QDesktopServices::openUrl(QUrl(kHomepage));
213
QModelIndex SomaFMService::GetCurrentIndex() {
214
return context_item_->index();
217
PlaylistItem::Options SomaFMService::playlistitem_options() const {
218
return PlaylistItem::SpecialPlayBehaviour |
219
PlaylistItem::PauseDisabled;