~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/playlistparsers/xspfparser.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
*/
17
17
 
18
18
#include "xspfparser.h"
 
19
#include "core/timeconstants.h"
19
20
 
20
21
#include <QDomDocument>
21
22
#include <QFile>
29
30
{
30
31
}
31
32
 
32
 
SongList XSPFParser::Load(QIODevice *device, const QString& playlist_path, const QDir&) const {
 
33
SongList XSPFParser::Load(QIODevice *device, const QString& playlist_path,
 
34
                          const QDir& dir) const {
33
35
  SongList ret;
34
36
 
35
37
  QXmlStreamReader reader(device);
39
41
  }
40
42
 
41
43
  while (!reader.atEnd() && ParseUntilElement(&reader, "track")) {
42
 
    Song song = ParseTrack(&reader);
 
44
    Song song = ParseTrack(&reader, dir);
43
45
    if (song.is_valid()) {
44
46
      ret << song;
45
47
    }
47
49
  return ret;
48
50
}
49
51
 
50
 
Song XSPFParser::ParseTrack(QXmlStreamReader* reader) const {
51
 
  Song song;
52
 
  QString title, artist, album;
 
52
Song XSPFParser::ParseTrack(QXmlStreamReader* reader, const QDir& dir) const {
 
53
  QString title, artist, album, location;
53
54
  qint64 nanosec = -1;
 
55
 
54
56
  while (!reader->atEnd()) {
55
57
    QXmlStreamReader::TokenType type = reader->readNext();
56
58
    switch (type) {
57
59
      case QXmlStreamReader::StartElement: {
58
60
        QStringRef name = reader->name();
59
61
        if (name == "location") {
60
 
          QUrl url(reader->readElementText());
61
 
          if (url.scheme() == "file") {
62
 
            QString filename = url.toLocalFile();
63
 
            if (!QFile::exists(filename)) {
64
 
              return Song();
65
 
            }
66
 
 
67
 
            // Load the song from the library if it's there.
68
 
            Song library_song = LoadLibrarySong(filename);
69
 
            if (library_song.is_valid())
70
 
              return library_song;
71
 
 
72
 
            song.InitFromFile(filename, -1);
73
 
            return song;
74
 
          } else {
75
 
            song.set_filename(url.toString());
76
 
            song.set_filetype(Song::Type_Stream);
77
 
          }
 
62
          location = reader->readElementText();
78
63
        } else if (name == "title") {
79
64
          title = reader->readElementText();
80
65
        } else if (name == "creator") {
82
67
        } else if (name == "album") {
83
68
          album = reader->readElementText();
84
69
        } else if (name == "duration") {  // in milliseconds.
85
 
          const QString& duration = reader->readElementText();
 
70
          const QString duration = reader->readElementText();
86
71
          bool ok = false;
87
72
          nanosec = duration.toInt(&ok) * kNsecPerMsec;
88
73
          if (!ok) {
97
82
      }
98
83
      case QXmlStreamReader::EndElement: {
99
84
        if (reader->name() == "track") {
100
 
          song.Init(title, artist, album, nanosec);
101
 
          return song;
 
85
          goto return_song;
102
86
        }
103
87
      }
104
88
      default:
105
89
        break;
106
90
    }
107
91
  }
108
 
  // At least make an effort if we never find a </track>.
109
 
  song.Init(title, artist, album, nanosec);
 
92
 
 
93
return_song:
 
94
  Song song = LoadSong(location, 0, dir);
 
95
 
 
96
  // Override metadata with what was in the playlist
 
97
  song.set_title(title);
 
98
  song.set_artist(artist);
 
99
  song.set_album(album);
 
100
  song.set_length_nanosec(nanosec);
110
101
  return song;
111
102
}
112
103
 
120
111
  StreamElement tracklist("trackList", &writer);
121
112
  foreach (const Song& song, songs) {
122
113
    StreamElement track("track", &writer);
123
 
    writer.writeTextElement("location", MakeUrl(song.filename()));
 
114
    writer.writeTextElement("location", song.url().toString());
124
115
    writer.writeTextElement("title", song.title());
125
116
    if (!song.artist().isEmpty()) {
126
117
      writer.writeTextElement("creator", song.artist());
136
127
    // Ignore images that are in our resource bundle.
137
128
    if (!art.startsWith(":") && !art.isEmpty()) {
138
129
      // Convert local files to URLs.
139
 
      art = MakeUrl(art);
 
130
      if (!art.contains("://")) {
 
131
        art = QUrl::fromLocalFile(art).toString();
 
132
      }
140
133
      writer.writeTextElement("image", art);
141
134
    }
142
135
  }