~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/radio/somafmservice.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Clementine.
2
 
   Copyright 2010, David Sansome <me@davidsansome.com>
3
 
 
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.
8
 
 
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.
13
 
 
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/>.
16
 
*/
17
 
 
18
 
#include "somafmservice.h"
19
 
#include "radiomodel.h"
20
 
#include "core/network.h"
21
 
#include "core/taskmanager.h"
22
 
#include "ui/iconloader.h"
23
 
 
24
 
#include <QNetworkRequest>
25
 
#include <QNetworkReply>
26
 
#include <QXmlStreamReader>
27
 
#include <QSettings>
28
 
#include <QTemporaryFile>
29
 
#include <QMenu>
30
 
#include <QDesktopServices>
31
 
#include <QCoreApplication>
32
 
#include <QtDebug>
33
 
 
34
 
const char* SomaFMService::kServiceName = "SomaFM";
35
 
const char* SomaFMService::kChannelListUrl = "http://somafm.com/channels.xml";
36
 
const char* SomaFMService::kHomepage = "http://somafm.com";
37
 
 
38
 
SomaFMService::SomaFMService(RadioModel* parent)
39
 
  : RadioService(kServiceName, parent),
40
 
    root_(NULL),
41
 
    context_menu_(NULL),
42
 
    get_channels_task_id_(0),
43
 
    get_stream_task_id_(0),
44
 
    network_(new NetworkAccessManager(this))
45
 
{
46
 
}
47
 
 
48
 
SomaFMService::~SomaFMService() {
49
 
  delete context_menu_;
50
 
}
51
 
 
52
 
QStandardItem* SomaFMService::CreateRootItem() {
53
 
  root_ = new QStandardItem(QIcon(":/providers/somafm.png"), kServiceName);
54
 
  root_->setData(true, RadioModel::Role_CanLazyLoad);
55
 
  return root_;
56
 
}
57
 
 
58
 
void SomaFMService::LazyPopulate(QStandardItem* item) {
59
 
  switch (item->data(RadioModel::Role_Type).toInt()) {
60
 
    case RadioModel::Type_Service:
61
 
      RefreshChannels();
62
 
      break;
63
 
 
64
 
    default:
65
 
      break;
66
 
  }
67
 
}
68
 
 
69
 
void SomaFMService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) {
70
 
  if (!context_menu_) {
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()));
75
 
  }
76
 
 
77
 
  context_item_ = model()->itemFromIndex(index);
78
 
  context_menu_->popup(global_pos);
79
 
}
80
 
 
81
 
PlaylistItem::SpecialLoadResult SomaFMService::StartLoading(const QUrl& url) {
82
 
  // Load the playlist
83
 
  QNetworkRequest request = QNetworkRequest(url);
84
 
  request.setRawHeader("User-Agent", QString("%1 %2").arg(
85
 
      QCoreApplication::applicationName(), QCoreApplication::applicationVersion()).toUtf8());
86
 
 
87
 
  QNetworkReply* reply = network_->get(request);
88
 
  connect(reply, SIGNAL(finished()), SLOT(LoadPlaylistFinished()));
89
 
 
90
 
  if (!get_stream_task_id_)
91
 
    get_stream_task_id_ = model()->task_manager()->StartTask(tr("Loading stream"));
92
 
 
93
 
  return PlaylistItem::SpecialLoadResult(
94
 
      PlaylistItem::SpecialLoadResult::WillLoadAsynchronously, url);
95
 
}
96
 
 
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;
101
 
 
102
 
  QUrl original_url(reply->url());
103
 
 
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));
109
 
    return;
110
 
  }
111
 
 
112
 
  // TODO: Replace with some more robust .pls parsing :(
113
 
  QTemporaryFile temp_file;
114
 
  temp_file.open();
115
 
  temp_file.write(reply->readAll());
116
 
  temp_file.flush();
117
 
 
118
 
  QSettings s(temp_file.fileName(), QSettings::IniFormat);
119
 
  s.beginGroup("playlist");
120
 
 
121
 
  emit AsyncLoadFinished(PlaylistItem::SpecialLoadResult(
122
 
      PlaylistItem::SpecialLoadResult::TrackAvailable,
123
 
      original_url, s.value("File1").toString()));
124
 
}
125
 
 
126
 
void SomaFMService::RefreshChannels() {
127
 
  QNetworkReply* reply = network_->get(QNetworkRequest(QUrl(kChannelListUrl)));
128
 
  connect(reply, SIGNAL(finished()), SLOT(RefreshChannelsFinished()));
129
 
 
130
 
  if (!get_channels_task_id_)
131
 
    get_channels_task_id_ = model()->task_manager()->StartTask(tr("Getting channels"));
132
 
}
133
 
 
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;
138
 
 
139
 
  if (reply->error() != QNetworkReply::NoError) {
140
 
    // TODO: Error handling
141
 
    qDebug() << reply->errorString();
142
 
    return;
143
 
  }
144
 
 
145
 
  if (root_->hasChildren())
146
 
    root_->removeRows(0, root_->rowCount());
147
 
 
148
 
  QXmlStreamReader reader(reply);
149
 
  while (!reader.atEnd()) {
150
 
    reader.readNext();
151
 
 
152
 
    if (reader.tokenType() == QXmlStreamReader::StartElement &&
153
 
        reader.name() == "channel") {
154
 
      ReadChannel(reader);
155
 
    }
156
 
  }
157
 
}
158
 
 
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);
162
 
 
163
 
  while (!reader.atEnd()) {
164
 
    switch (reader.readNext()) {
165
 
      case QXmlStreamReader::EndElement:
166
 
        if (item->data(RadioModel::Role_Url).toString().isNull()) {
167
 
          // Didn't find a URL
168
 
          delete item;
169
 
        } else {
170
 
          root_->appendRow(item);
171
 
        }
172
 
        return;
173
 
 
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);
182
 
        } else {
183
 
          ConsumeElement(reader);
184
 
        }
185
 
        break;
186
 
 
187
 
      default:
188
 
        break;
189
 
    }
190
 
  }
191
 
 
192
 
  delete item;
193
 
}
194
 
 
195
 
void SomaFMService::ConsumeElement(QXmlStreamReader& reader) {
196
 
  int level = 1;
197
 
  while (!reader.atEnd()) {
198
 
    switch (reader.readNext()) {
199
 
      case QXmlStreamReader::StartElement: level++; break;
200
 
      case QXmlStreamReader::EndElement:   level--; break;
201
 
      default: break;
202
 
    }
203
 
 
204
 
    if (level == 0)
205
 
      return;
206
 
  }
207
 
}
208
 
 
209
 
void SomaFMService::Homepage() {
210
 
  QDesktopServices::openUrl(QUrl(kHomepage));
211
 
}
212
 
 
213
 
QModelIndex SomaFMService::GetCurrentIndex() {
214
 
  return context_item_->index();
215
 
}
216
 
 
217
 
PlaylistItem::Options SomaFMService::playlistitem_options() const {
218
 
  return PlaylistItem::SpecialPlayBehaviour |
219
 
         PlaylistItem::PauseDisabled;
220
 
}