~mixxxdevelopers/mixxx/trunk

« back to all changes in this revision

Viewing changes to mixxx/src/library/parserm3u.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: parserm3u
 
3
//
 
4
// Description: module to parse m3u(plaintext) 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 <QTextStream>
 
13
#include <QDebug>
 
14
#include "parserm3u.h"
 
15
#include <QUrl>
 
16
 
 
17
/**
 
18
   @author Ingo Kossyk (kossyki@cs.tu-berlin.de)
 
19
 **/
 
20
 
 
21
/**
 
22
   ToDo:
 
23
    - parse ALL informations from the pls file if available ,
 
24
          not only the filepath;
 
25
 
 
26
          Userinformation :
 
27
          The M3U format is just a headerless plaintext format
 
28
          where every line of text either represents
 
29
          a file location or a comment. comments are being
 
30
          preceeded by a '#'. This parser will try to parse all
 
31
          file information from the given file and add the filepaths
 
32
          to the locations ptrlist when the file is existing locally
 
33
          or on a mounted harddrive.
 
34
 **/
 
35
 
 
36
ParserM3u::ParserM3u() : Parser()
 
37
{
 
38
}
 
39
 
 
40
ParserM3u::~ParserM3u()
 
41
{
 
42
 
 
43
}
 
44
 
 
45
 
 
46
QList<QString> ParserM3u::parse(QString sFilename)
 
47
{
 
48
    QFile file(sFilename);
 
49
    QString basepath = sFilename.section('/', 0, -2);
 
50
 
 
51
    clearLocations();
 
52
    //qDebug() << "ParserM3u: Starting to parse.";
 
53
    if (file.open(QIODevice::ReadOnly) && !isBinary(sFilename)) {
 
54
        /* Unfortunately, QT 4.7 does not handle <CR> line breaks.
 
55
         * This is important on OS X where iTunes, e.g., exports M3U playlists using <CR>
 
56
         *
 
57
         * Using QFile::readAll() we obtain the complete content of the playlist as a ByteArray.
 
58
         * We replace any '\r' with '\n' if applicaple
 
59
         * This ensures that playlists from iTunes on OS X can be parsed
 
60
         */
 
61
        QByteArray ba = file.readAll();
 
62
        ba.replace('\r',"\n");
 
63
        QTextStream textstream(ba.data());
 
64
        
 
65
        while(!textstream.atEnd()) {
 
66
            QString sLine = getFilepath(&textstream, basepath);
 
67
            if(sLine.isEmpty())
 
68
                break;
 
69
 
 
70
            //qDebug) << ("ParserM3u: parsed: " << (sLine);
 
71
            m_sLocations.append(sLine);
 
72
        }
 
73
 
 
74
        file.close();
 
75
 
 
76
        if(m_sLocations.count() != 0)
 
77
            return m_sLocations;
 
78
        else
 
79
            return QList<QString>(); // NULL pointer returned when no locations were found
 
80
 
 
81
    }
 
82
 
 
83
    file.close();
 
84
    return QList<QString>(); //if we get here something went wrong
 
85
}
 
86
 
 
87
 
 
88
QString ParserM3u::getFilepath(QTextStream *stream, QString basepath)
 
89
{
 
90
    QString textline,filename = "";
 
91
 
 
92
    textline = stream->readLine();
 
93
    qDebug() << textline;
 
94
    while(!textline.isEmpty()){
 
95
        if(textline.isNull())
 
96
            break;
 
97
 
 
98
        if(!textline.contains("#") && !textline.isEmpty()){
 
99
            filename = textline;
 
100
            filename.remove("file://");
 
101
            QByteArray strlocbytes = filename.toUtf8();
 
102
            QUrl location = QUrl::fromEncoded(strlocbytes);
 
103
            QString trackLocation = location.toLocalFile();
 
104
            //qDebug() << trackLocation;
 
105
            if(isFilepath(trackLocation)) {
 
106
                return trackLocation;
 
107
            } else {
 
108
                // Try relative to m3u dir
 
109
                QString rel = basepath + "/" + trackLocation;
 
110
                if (isFilepath(rel)) {
 
111
                    return rel;
 
112
                }
 
113
                // We couldn't match this to a real file so ignore it
 
114
            }
 
115
        }
 
116
        textline = stream->readLine();
 
117
    }
 
118
 
 
119
    // Signal we reached the end
 
120
    return 0;
 
121
 
 
122
}