From f6abacbce82dd978ccec367ff3a8173e568a27c7 Mon Sep 17 00:00:00 2001 From: Nikolay Tischenko Date: Sat, 16 Oct 2010 20:10:34 +0700 Subject: [PATCH] Add convertion from old playlist format to new --- src/filestorage.cpp | 106 +++++++++++++++++++++++++++++++++++++-------------- src/filestorage.h | 3 ++ 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/filestorage.cpp b/src/filestorage.cpp index 3d46f0a..dea1e68 100644 --- a/src/filestorage.cpp +++ b/src/filestorage.cpp @@ -23,6 +23,9 @@ #include #include #include +// legacy _start_ +#include +// legacy _end_ using namespace SomePlayer::Storage; using namespace SomePlayer::DataObjects; @@ -45,45 +48,84 @@ QList FileStorage::getPlaylists() { Playlist FileStorage::getPlaylist(QString name) { if (name == _CURRENT_PLAYLIST_SUBST_) name = _CURRENT_PLAYLIST_NAME_; - QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_); + QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_OLD_); // remove OLD_ in next version Playlist playlist; playlist.setName(PLAYLIST_BAD_NAME); + // legacy _start_ if (playlistFile.exists()) { - playlist.setName(name); - QDomDocument doc; + qWarning() << "Old playlist file format deteced"; + QRegExp meta_regexp; + meta_regexp.setPattern("#META\\ +\\[(\\d+)\\]\\[(\\d+)\\].*::(.+)::,::(.+)::,::(.+)::"); + QRegExp path_regexp; + path_regexp.setPattern("#PATH (.+)"); playlistFile.open(QFile::ReadOnly); - doc.setContent(&playlistFile); + QTextStream stream(&playlistFile); + QString buffer = stream.readLine(); + if (buffer.startsWith("#SOMEPLAYLIST")) { + while (!stream.atEnd()) { + buffer = stream.readLine(); + if (meta_regexp.indexIn(buffer) != -1) { + int seconds = meta_regexp.cap(2).toInt(); + QString artist = meta_regexp.cap(3); + QString album = meta_regexp.cap(4); + QString title = meta_regexp.cap(5); + buffer = stream.readLine(); + if (path_regexp.indexIn(buffer) != -1) { + QString source = path_regexp.cap(1); + TrackMetadata meta(title, artist, album, seconds); + Track track(meta, source); + playlist.addTrack(track); + } + } + } + } + playlist.setName(name); + qWarning() << "Saving playlist in new format"; + savePlaylist(playlist); playlistFile.close(); - QDomElement eplaylist = doc.documentElement(); - if (eplaylist.tagName() == "playlist") { - QDomElement etracklist = eplaylist.firstChildElement("trackList"); - if (!etracklist.isNull()) { - QDomElement etrack = etracklist.firstChildElement("track"); - while (!etrack.isNull()) { - QDomElement elocation = etrack.firstChildElement("location"); - QDomElement eextension = etrack.firstChildElement("extension"); - if (!eextension.isNull()) { - QDomElement ecl_clip = eextension.firstChildElement("cl:clip"); - if (!ecl_clip.isNull()) { - QString artist = ecl_clip.attribute("artist"); - QString album = ecl_clip.attribute("album"); - QString title = ecl_clip.attribute("title"); - QDomElement eduration = etrack.firstChildElement("duration"); - if (!eduration.isNull()) { - QVariant duration = eduration.text(); - QByteArray basource; - basource.append(elocation.text()); - QString source = QUrl::fromEncoded(basource).toLocalFile(); - TrackMetadata meta(title, artist, album, duration.toInt()/1000); - Track track(meta, source); - playlist.addTrack(track); + playlistFile.remove(); + } else { + playlistFile.setFileName(_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_); + // legacy _end_ + if (playlistFile.exists()) { + playlist.setName(name); + QDomDocument doc; + playlistFile.open(QFile::ReadOnly); + doc.setContent(&playlistFile); + playlistFile.close(); + QDomElement eplaylist = doc.documentElement(); + if (eplaylist.tagName() == "playlist") { + QDomElement etracklist = eplaylist.firstChildElement("trackList"); + if (!etracklist.isNull()) { + QDomElement etrack = etracklist.firstChildElement("track"); + while (!etrack.isNull()) { + QDomElement elocation = etrack.firstChildElement("location"); + QDomElement eextension = etrack.firstChildElement("extension"); + if (!eextension.isNull()) { + QDomElement ecl_clip = eextension.firstChildElement("cl:clip"); + if (!ecl_clip.isNull()) { + QString artist = ecl_clip.attribute("artist"); + QString album = ecl_clip.attribute("album"); + QString title = ecl_clip.attribute("title"); + QDomElement eduration = etrack.firstChildElement("duration"); + if (!eduration.isNull()) { + QVariant duration = eduration.text(); + QByteArray basource; + basource.append(elocation.text()); + QString source = QUrl::fromEncoded(basource).toLocalFile(); + TrackMetadata meta(title, artist, album, duration.toInt()/1000); + Track track(meta, source); + playlist.addTrack(track); + } } } + etrack = etrack.nextSiblingElement("track"); } - etrack = etrack.nextSiblingElement("track"); } } + // legacy _start_ } + // legacy _end_ } return playlist; } @@ -102,6 +144,14 @@ QStringList FileStorage::getPlaylistsNames() { name = _CURRENT_PLAYLIST_SUBST_; playlistNames.append(name); } + // legacy _start_ + else if (suffix == _PLAYLIST_FILE_EXTENSION_OLD_){ + QString name = info.fileName().replace(QString(".%1").arg(_PLAYLIST_FILE_EXTENSION_OLD_), "", Qt::CaseInsensitive); + if (name == _CURRENT_PLAYLIST_NAME_) + name = _CURRENT_PLAYLIST_SUBST_; + playlistNames.append(name); + } + // legacy _end_ } return playlistNames; } diff --git a/src/filestorage.h b/src/filestorage.h index 9d6482d..79311d6 100644 --- a/src/filestorage.h +++ b/src/filestorage.h @@ -27,6 +27,9 @@ #define _CURRENT_PLAYLIST_NAME_ "___current" #define _CURRENT_PLAYLIST_SUBST_ "Now playing" #define _PLAYLIST_FILE_EXTENSION_ "xspf" +// legacy _start_ +#define _PLAYLIST_FILE_EXTENSION_OLD_ "spls" +// legacy _end_ // format: XSPF (http://xspf.org) // required elements: -- 1.7.9.5