MythTV  master
pls.cpp
Go to the documentation of this file.
1 /*
2  playlistfile (.pls) parser
3  Eskil Heyn Olsen, 2005, distributed under the GPL as part of mythtv.
4 
5  Update July 2010 updated for Qt4 (Paul Harrison)
6  Update December 2012 updated to use QSettings for the pls parser
7  Update September 2014 add simple asx parser
8 */
9 
10 // c++
11 #include <string>
12 
13 // qt
14 #include <QPair>
15 #include <QList>
16 #include <QMap>
17 #include <QStringList>
18 #include <QFileInfo>
19 #include <QSettings>
20 #include <QDomDocument>
21 
22 // mythtv
23 #include <mythlogging.h>
24 
25 // mythmusic
26 #include "pls.h"
27 
28 
29 using namespace std;
30 
31 
33 {
34  clear();
35 }
36 
37 int PlayListFile::parse(PlayListFile *pls, const QString &filename)
38 {
39  int result = 0;
40  QString extension = QFileInfo(filename).suffix().toLower();
41 
42  if (extension == "pls")
43  result = PlayListFile::parsePLS(pls, filename);
44  else if (extension == "m3u")
45  result = PlayListFile::parseM3U(pls, filename);
46  else if (extension == "asx")
47  result = PlayListFile::parseASX(pls, filename);
48 
49  return result;
50 }
51 
52 int PlayListFile::parsePLS(PlayListFile *pls, const QString &filename)
53 {
54  QSettings settings(filename, QSettings::IniFormat);
55  settings.beginGroup("playlist");
56 
57  int num_entries = settings.value("numberofentries", -1).toInt();
58 
59  // Some pls files have "numberofentries", some has "NumberOfEntries".
60  if (num_entries == -1)
61  num_entries = settings.value("NumberOfEntries", -1).toInt();
62 
63  for (int n = 1; n <= num_entries; n++)
64  {
66  QString t_key = QString("Title%1").arg(n);
67  QString f_key = QString("File%1").arg(n);
68  QString l_key = QString("Length%1").arg(n);
69 
70  e->setFile(settings.value(f_key).toString());
71  e->setTitle(settings.value(t_key).toString());
72  e->setLength(settings.value(l_key).toInt());
73 
74  pls->add(e);
75  }
76 
77  return pls->size();
78 }
79 
80 #define M3U_HEADER "#EXTM3U"
81 #define M3U_INFO "#EXTINF"
82 
83 int PlayListFile::parseM3U(PlayListFile *pls, const QString &filename)
84 {
85  QFile f(filename);
86  if (!f.open(QIODevice::ReadOnly))
87  return 0;
88 
89  QTextStream stream(&f);
90  QString data = stream.readAll();
91  QStringList lines = data.split(QRegExp("[\r\n]"));
92 
93  QStringList::iterator it;
94  for (it = lines.begin(); it != lines.end(); ++it)
95  {
96  // ignore empty lines
97  if (it->isEmpty())
98  continue;
99 
100  // ignore the M3U header
101  if (it->startsWith(M3U_HEADER))
102  continue;
103 
104  // for now ignore M3U info lines
105  if (it->startsWith(M3U_INFO))
106  continue;
107 
108  // add to the playlist
110  e->setFile(*it);
111  e->setTitle(*it);
112  e->setLength(-1);
113 
114  pls->add(e);
115  }
116 
117  return pls->size();
118 }
119 
120 int PlayListFile::parseASX(PlayListFile *pls, const QString &filename)
121 {
122  QDomDocument doc("mydocument");
123  QFile file(filename);
124  if (!file.open(QIODevice::ReadOnly))
125  return 0;
126 
127  if (!doc.setContent(&file))
128  {
129  file.close();
130  return 0;
131  }
132  file.close();
133 
134  //QDomElement docElem = doc.documentElement();
135  QDomNodeList entryList = doc.elementsByTagName("Entry");
136  QString url;
137 
138  for (int x = 0; x < entryList.count(); x++)
139  {
140  QDomNode n = entryList.item(x);
141  QDomElement elem = n.toElement();
142  QDomNodeList refList = elem.elementsByTagName("ref");
143  for (int y = 0; y < refList.count(); y++)
144  {
145  QDomNode n2 = refList.item(y);
146  QDomElement elem2 = n2.toElement();
147  if (!elem2.isNull())
148  {
149  url = elem2.attribute("href");
150 
151  // add to the playlist
153  e->setFile(url.replace("mms://", "mmsh://"));
154  e->setTitle(url.replace("mms://", "mmsh://"));
155  e->setLength(-1);
156 
157  pls->add(e);
158  }
159  }
160  }
161 
162  return pls->size();
163 }
~PlayListFile(void)
Definition: pls.cpp:32
void add(PlayListFileEntry *e)
Add a entry to the playlist.
Definition: pls.h:80
static int parsePLS(PlayListFile *pls, const QString &filename)
Parse a pls file.
Definition: pls.cpp:52
void setTitle(const QString &t)
Definition: pls.h:30
static int parseASX(PlayListFile *pls, const QString &filename)
Parse a asx file.
Definition: pls.cpp:120
static int parseM3U(PlayListFile *pls, const QString &filename)
Parse a m3u file.
Definition: pls.cpp:83
#define M3U_INFO
Definition: pls.cpp:81
static void clear(SettingsMap &cache, SettingsMap &overrides, const QString &myKey)
Definition: mythdb.cpp:830
int size(void) const
Get the number of entries in the pls file.
Definition: pls.h:54
void setLength(int l)
Definition: pls.h:31
Class for containing the info of a pls or m3u file.
Definition: pls.h:41
void setFile(const QString &f)
Definition: pls.h:29
#define M3U_HEADER
Definition: pls.cpp:80
Class for representing entries in a pls file.
Definition: pls.h:19
static int parse(PlayListFile *pls, const QString &filename)
Parse a pls, m3u or asx playlist file.
Definition: pls.cpp:37