-
Notifications
You must be signed in to change notification settings - Fork 342
/
pls.cpp
163 lines (128 loc) · 3.96 KB
/
pls.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
/*
playlistfile (.pls) parser
Eskil Heyn Olsen, 2005, distributed under the GPL as part of mythtv.
Update July 2010 updated for Qt4 (Paul Harrison)
Update December 2012 updated to use QSettings for the pls parser
Update September 2014 add simple asx parser
*/
// c++
#include <string>
// qt
#include <QPair>
#include <QList>
#include <QMap>
#include <QStringList>
#include <QFileInfo>
#include <QSettings>
#include <QDomDocument>
// mythtv
#include <mythlogging.h>
// mythmusic
#include "pls.h"
using namespace std;
PlayListFile::~PlayListFile(void)
{
clear();
}
int PlayListFile::parse(PlayListFile *pls, const QString &filename)
{
int result = 0;
QString extension = QFileInfo(filename).suffix().toLower();
if (extension == "pls")
result = PlayListFile::parsePLS(pls, filename);
else if (extension == "m3u")
result = PlayListFile::parseM3U(pls, filename);
else if (extension == "asx")
result = PlayListFile::parseASX(pls, filename);
return result;
}
int PlayListFile::parsePLS(PlayListFile *pls, const QString &filename)
{
QSettings settings(filename, QSettings::IniFormat);
settings.beginGroup("playlist");
int num_entries = settings.value("numberofentries", -1).toInt();
// Some pls files have "numberofentries", some has "NumberOfEntries".
if (num_entries == -1)
num_entries = settings.value("NumberOfEntries", -1).toInt();
for (int n = 1; n <= num_entries; n++)
{
PlayListFileEntry *e = new PlayListFileEntry();
QString t_key = QString("Title%1").arg(n);
QString f_key = QString("File%1").arg(n);
QString l_key = QString("Length%1").arg(n);
e->setFile(settings.value(f_key).toString());
e->setTitle(settings.value(t_key).toString());
e->setLength(settings.value(l_key).toInt());
pls->add(e);
}
return pls->size();
}
#define M3U_HEADER "#EXTM3U"
#define M3U_INFO "#EXTINF"
int PlayListFile::parseM3U(PlayListFile *pls, const QString &filename)
{
QFile f(filename);
if (!f.open(QIODevice::ReadOnly))
return 0;
QTextStream stream(&f);
QString data = stream.readAll();
QStringList lines = data.split(QRegExp("[\r\n]"));
QStringList::iterator it;
for (it = lines.begin(); it != lines.end(); ++it)
{
// ignore empty lines
if (it->isEmpty())
continue;
// ignore the M3U header
if (it->startsWith(M3U_HEADER))
continue;
// for now ignore M3U info lines
if (it->startsWith(M3U_INFO))
continue;
// add to the playlist
PlayListFileEntry *e = new PlayListFileEntry();
e->setFile(*it);
e->setTitle(*it);
e->setLength(-1);
pls->add(e);
}
return pls->size();
}
int PlayListFile::parseASX(PlayListFile *pls, const QString &filename)
{
QDomDocument doc("mydocument");
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
return 0;
if (!doc.setContent(&file))
{
file.close();
return 0;
}
file.close();
//QDomElement docElem = doc.documentElement();
QDomNodeList entryList = doc.elementsByTagName("Entry");
QString url;
for (int x = 0; x < entryList.count(); x++)
{
QDomNode n = entryList.item(x);
QDomElement elem = n.toElement();
QDomNodeList refList = elem.elementsByTagName("ref");
for (int y = 0; y < refList.count(); y++)
{
QDomNode n2 = refList.item(y);
QDomElement elem2 = n2.toElement();
if (!elem2.isNull())
{
url = elem2.attribute("href");
// add to the playlist
PlayListFileEntry *e = new PlayListFileEntry();
e->setFile(url.replace("mms://", "mmsh://"));
e->setTitle(url.replace("mms://", "mmsh://"));
e->setLength(-1);
pls->add(e);
}
}
}
return pls->size();
}