-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataprovider.cpp
169 lines (146 loc) · 5.83 KB
/
dataprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <QDebug>
#include <QStandardPaths>
#include <QXmlStreamReader>
#include <QXmlStreamAttributes>
#include "dataprovider.h"
DataProvider::DataProvider(QObject *parent) :
QObject(parent)
{
qDebug() << "DataProvider::DataProvider";
m_networkManager = new QNetworkAccessManager(this);
m_diskCache = new QNetworkDiskCache(this);
m_channelsUrl = QUrl("http://somafm.com/channels.xml");
m_cacheDir =
QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
m_diskCache->setCacheDirectory(m_cacheDir);
m_networkManager->setCache(m_diskCache);
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(handleNetworkReply(QNetworkReply*)));
}
void DataProvider::sendRequest(QUrl url) {
qDebug() << "DataProvider::sendRequest(" << url.toString() << ")";
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
QNetworkRequest::PreferCache);
req.setAttribute(QNetworkRequest::CacheSaveControlAttribute, true);
m_networkManager->get(req);
}
void DataProvider::provideChannelList() {
qDebug() << "DataProvider::provideChannelList";
// first, look for the file in the cache manually
// and use it to provide fast setup at start
// since the networkmanager will always fetch the
// remote file due to the received Last-Modified header
QIODevice *cacheDevice = m_diskCache->data(m_channelsUrl);
if(cacheDevice != 0) { // file was found in cache
parseChannelsXml(cacheDevice->readAll());
cacheDevice->deleteLater();
}
sendRequest(m_channelsUrl);
}
void DataProvider::provideChannelIcon(QString id) {
qDebug() << "DataProvider::provideChannelIcon";
if(m_channelData.contains(id)) {
QUrl imageUrl(m_channelData.value(id).value("image"));
m_iconRequests.insert(imageUrl, id);
sendRequest(imageUrl);
}
}
void DataProvider::provideChannelImage(QString id) {
qDebug() << "DataProvider::provideChannelImage";
if(m_channelData.contains(id)) {
ChannelInfo chan = m_channelData.value(id);
QStringList keys = QStringList() << "xlimage"
<< "largeimage"
<< "image";
for(int i=0; i<keys.size(); ++i) {
QString key = keys.at(i);
if(chan.contains(key)) {
QUrl url(chan.value(key));
m_imageRequests.insert(url, id);
sendRequest(url);
return;
}
}
}
}
QUrl DataProvider::getPlaylistUrl(QString id) {
qDebug() << "DataProvider::getPlaylistUrl";
if(m_channelData.contains(id)) {
ChannelInfo chan = m_channelData.value(id);
QStringList keys = QStringList() << "highestpls_aac"
<< "fastpls_aacp"
<< "fastpls_mp3"
<< "slowpls_aacp"
<< "slowpls__mp3";
QStringList::const_iterator it;
for(it=keys.constBegin(); it!=keys.constEnd(); ++it) {
if(chan.contains((*it))) {
return QUrl(chan.value(*it));
}
}
}
return QUrl();
}
void DataProvider::handleNetworkReply(QNetworkReply *reply) {
qDebug() << "DataProvider::handleNetworkReply("
<< reply->url().toString() << ")";
QByteArray data = reply->readAll();
QUrl url = reply->url();
if(url == m_channelsUrl) {
parseChannelsXml(data);
} else if(m_iconRequests.contains(url)) {
QString id = m_iconRequests.value(url);
QImage icon = QImage::fromData(data);
emit channelIconProvided(id, icon);
m_iconRequests.remove(url);
} else if(m_imageRequests.contains(url)) {
QString id = m_imageRequests.value(url);
QImage image = QImage::fromData(data);
emit channelImageProvided(id, image);
m_imageRequests.remove(url);
}
reply->deleteLater();
}
void DataProvider::parseChannelsXml(QByteArray data) {
qDebug() << "DataProvider::parseChannelsXml()";
QXmlStreamReader xml;
xml.addData(data);
QStringList channelListElements = QStringList() << "title" << "description"
<< "genre" << "dj"
<< "listeners"
<< "lastplaying";
ChannelData channelData;
ChannelList channelList;
while (!xml.atEnd()) {
xml.readNext();
if(xml.isStartElement()) {
if(xml.name() == "channel") {
ChannelInfo dataInfo, listInfo;
QString id = xml.attributes().value("id").toString();
listInfo.insert("id", id);
while(!xml.atEnd() &&
!(xml.isEndElement() && xml.name() == "channel")) {
xml.readNext();
if(xml.isStartElement()) {
QString key = xml.name().toString().toLower();
if(xml.attributes().hasAttribute("format"))
key.append("_").append(xml.attributes().value("format").toString());
xml.readNext();
if(xml.isCharacters()) {
QString value = xml.text().toString();
if(channelListElements.contains(key)) {
listInfo.insert(key, value);
}
dataInfo.insert(key, value);
}
}
}
channelData.insert(id, dataInfo);
channelList.append(listInfo);
}
}
}
m_channelData = channelData;
emit channelListProvided(channelList);
}