~mixxxdevelopers/mixxx/trunk

« back to all changes in this revision

Viewing changes to mixxx/src/library/parserpls.cpp

  • Committer: Raffitea
  • Date: 2011-01-07 00:44:11 UTC
  • mfrom: (2616.1.25 traktor_library)
  • Revision ID: raffitea-20110107004411-lz9m8mnny3sf86rq
Merging from lp:~mixxxdevelopers/mixxx/traktor_library
* support for n-level childmodels in sidebar
* M3U and PLS playlist import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: parserpls
 
3
//
 
4
// Description: module to parse pls formated playlists
 
5
//
 
6
//
 
7
// Author: Ingo Kossyk <kossyki@cs.tu-berlin.de>, (C) 2004
 
8
//
 
9
// Copyright: See COPYING file that comes with this distribution
 
10
//
 
11
//
 
12
#include "parser.h"
 
13
#include "parserpls.h"
 
14
#include <QDebug>
 
15
#include <QTextStream>
 
16
#include <QFile>
 
17
#include <QUrl>
 
18
 
 
19
/**
 
20
   @author Ingo Kossyk (kossyki@cs.tu-berlin.de)
 
21
 **/
 
22
 
 
23
/**
 
24
   ToDo:
 
25
    - parse ALL informations from the pls file if available ,
 
26
          not only the filepath;
 
27
 **/
 
28
 
 
29
ParserPls::ParserPls() : Parser()
 
30
{
 
31
}
 
32
 
 
33
ParserPls::~ParserPls()
 
34
{
 
35
}
 
36
 
 
37
QList<QString> ParserPls::parse(QString sFilename)
 
38
{
 
39
    //long numEntries =0;
 
40
    QFile file(sFilename);
 
41
    QString basepath = sFilename.section('/', 0, -2);
 
42
 
 
43
    clearLocations();
 
44
 
 
45
    if (file.open(QIODevice::ReadOnly) && !isBinary(sFilename) ) {
 
46
 
 
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>
 
49
         *
 
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
 
53
         */
 
54
        QByteArray ba = file.readAll();
 
55
        ba.replace('\r',"\n");
 
56
        QTextStream textstream(ba.data());
 
57
 
 
58
        while(!textstream.atEnd()) {
 
59
            QString psLine = getFilepath(&textstream, basepath);
 
60
            if(psLine.isNull()) {
 
61
                break;
 
62
            } else {
 
63
                m_sLocations.append(psLine);
 
64
            }
 
65
 
 
66
            //--numEntries;
 
67
        }
 
68
 
 
69
        file.close();
 
70
 
 
71
        if(m_sLocations.count() != 0)
 
72
            return m_sLocations;
 
73
        else
 
74
            return QList<QString>(); // NULL pointer returned when no locations were found
 
75
    }
 
76
 
 
77
    file.close();
 
78
    return QList<QString>(); //if we get here something went wrong :D
 
79
}
 
80
 
 
81
long ParserPls::getNumEntries(QTextStream *stream)
 
82
{
 
83
 
 
84
    QString textline;
 
85
    textline = stream->readLine();
 
86
 
 
87
    if(textline.contains("[playlist]")){
 
88
 
 
89
        while(!textline.contains("NumberOfEntries"))
 
90
            textline = stream->readLine();
 
91
 
 
92
        QString temp = textline.section("=",-1,-1);
 
93
 
 
94
        return temp.toLong();
 
95
 
 
96
    } else{
 
97
        qDebug() << "ParserPls: pls file is not a playlist! \n";
 
98
        return 0;
 
99
    }
 
100
 
 
101
}
 
102
 
 
103
 
 
104
QString ParserPls::getFilepath(QTextStream *stream, QString basepath)
 
105
{
 
106
    QString textline,filename = "";
 
107
    textline = stream->readLine();
 
108
    while(!textline.isEmpty()){
 
109
        if(textline.isNull())
 
110
            break;
 
111
 
 
112
        if(textline.contains("File")) {
 
113
            int iPos = textline.indexOf("=",0);
 
114
            ++iPos;
 
115
 
 
116
            filename = textline.right(textline.length()-iPos);
 
117
           
 
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;
 
125
 
 
126
            if(isFilepath(trackLocation)) {
 
127
                return trackLocation;
 
128
            } else {
 
129
                // Try relative to m3u dir
 
130
                QString rel = basepath + "/" + trackLocation;
 
131
                if (isFilepath(rel)) {
 
132
                    return rel;
 
133
                }
 
134
                // We couldn't match this to a real file so ignore it
 
135
            }
 
136
        }
 
137
        textline = stream->readLine();
 
138
    }
 
139
 
 
140
    // Signal we reached the end
 
141
    return 0;
 
142
 
 
143
}