2
// C++ Implementation: parserpls
4
// Description: module to parse pls formated playlists
7
// Author: Ingo Kossyk <kossyki@cs.tu-berlin.de>, (C) 2004
9
// Copyright: See COPYING file that comes with this distribution
13
#include "parserpls.h"
15
#include <QTextStream>
20
@author Ingo Kossyk (kossyki@cs.tu-berlin.de)
25
- parse ALL informations from the pls file if available ,
26
not only the filepath;
29
ParserPls::ParserPls() : Parser()
33
ParserPls::~ParserPls()
37
QList<QString> ParserPls::parse(QString sFilename)
40
QFile file(sFilename);
41
QString basepath = sFilename.section('/', 0, -2);
45
if (file.open(QIODevice::ReadOnly) && !isBinary(sFilename) ) {
47
/* Unfortunately, QT 4.7 does not handle <CR> line breaks.
48
* This is important on OS X where iTunes, e.g., exports M3U playlists using <CR>
50
* Using QFile::readAll() we obtain the complete content of the playlist as a ByteArray.
51
* We replace any '\r' with '\n' if applicaple
52
* This ensures that playlists from iTunes on OS X can be parsed
54
QByteArray ba = file.readAll();
55
ba.replace('\r',"\n");
56
QTextStream textstream(ba.data());
58
while(!textstream.atEnd()) {
59
QString psLine = getFilepath(&textstream, basepath);
63
m_sLocations.append(psLine);
71
if(m_sLocations.count() != 0)
74
return QList<QString>(); // NULL pointer returned when no locations were found
78
return QList<QString>(); //if we get here something went wrong :D
81
long ParserPls::getNumEntries(QTextStream *stream)
85
textline = stream->readLine();
87
if(textline.contains("[playlist]")){
89
while(!textline.contains("NumberOfEntries"))
90
textline = stream->readLine();
92
QString temp = textline.section("=",-1,-1);
97
qDebug() << "ParserPls: pls file is not a playlist! \n";
104
QString ParserPls::getFilepath(QTextStream *stream, QString basepath)
106
QString textline,filename = "";
107
textline = stream->readLine();
108
while(!textline.isEmpty()){
109
if(textline.isNull())
112
if(textline.contains("File")) {
113
int iPos = textline.indexOf("=",0);
116
filename = textline.right(textline.length()-iPos);
118
//Rythmbox playlists starts with file://<path>
119
//We remove the file protocol if found.
120
filename.remove("file://");
121
QByteArray strlocbytes = filename.toUtf8();
122
QUrl location = QUrl::fromEncoded(strlocbytes);
123
QString trackLocation = location.toLocalFile();
124
//qDebug() << trackLocation;
126
if(isFilepath(trackLocation)) {
127
return trackLocation;
129
// Try relative to m3u dir
130
QString rel = basepath + "/" + trackLocation;
131
if (isFilepath(rel)) {
134
// We couldn't match this to a real file so ignore it
137
textline = stream->readLine();
140
// Signal we reached the end