From 31dd20682b751f3ed6822b368efef0a128fcc3d5 Mon Sep 17 00:00:00 2001 From: nhnielsen Date: Mon, 24 Sep 2007 06:43:14 +0000 Subject: [PATCH] Next up, .asx playlists. Again, untested as I do not have a playlist of this type git-svn-id: svn+ssh://svn.kde.org/home/kde/trunk/extragear/multimedia/amarok@716180 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- src/playlist/PlaylistLoader.cpp | 130 ++++++++++++++++++++++++++++++++++++++-- src/playlist/PlaylistLoader.h | 5 ++ 2 files changed, 131 insertions(+), 4 deletions(-) diff --git a/src/playlist/PlaylistLoader.cpp b/src/playlist/PlaylistLoader.cpp index fa5bf990e..9d3f6947b 100644 --- a/src/playlist/PlaylistLoader.cpp +++ b/src/playlist/PlaylistLoader.cpp @@ -108,13 +108,14 @@ void PlaylistLoader::handleByFormat( QTextStream &stream, Format format) case RAM: loadRealAudioRam( stream ); break; + case ASX: + loadASX( stream ); + break; case SMIL: loadSMIL( stream ); break; - /* case ASX: - loadASX( stream ); - break; - case XSPF: + + /*case XSPF: loadXSPF( stream ); break;*/ @@ -439,5 +440,126 @@ PlaylistLoader::loadSMIL( QTextStream &stream ) return true; } + +bool +PlaylistLoader::loadASX( QTextStream &stream ) +{ + //adapted from Kaffeine 0.7 + TrackList tracks; + QDomDocument doc; + QString errorMsg; + int errorLine, errorColumn; + stream.setCodec( "UTF8" ); + + QString content = stream.read(); + + //ASX looks a lot like xml, but doesn't require tags to be case sensitive, + //meaning we have to accept things like: ... + //We use a dirty way to achieve this: we make all tags lower case + QRegExp ex("(<[/]?[^>]*[A-Z]+[^>]*>)"); + ex.setCaseSensitive(true); + while ( (ex.search(content)) != -1 ) + content.replace(ex.cap( 1 ), ex.cap( 1 ).toLower()); + + + if (!doc.setContent(content, &errorMsg, &errorLine, &errorColumn)) + { + debug() << "Error loading xml file: " "(" << errorMsg << ")" + << " at line " << errorLine << ", column " << errorColumn << endl; + return false; + } + + QDomElement root = doc.documentElement(); + + QString url; + QString title; + QString author; + QTime length; + QString duration; + + if (root.nodeName().toLower() != "asx") return false; + + QDomNode node = root.firstChild(); + QDomNode subNode; + QDomElement element; + + while (!node.isNull()) + { + url.clear(); + title.clear(); + author.clear(); + length = QTime(); + if (node.nodeName().toLower() == "entry") + { + subNode = node.firstChild(); + while (!subNode.isNull()) + { + if ((subNode.nodeName().toLower() == "ref") && (subNode.isElement()) && (url.isNull())) + { + element = subNode.toElement(); + if (element.hasAttribute("href")) + url = element.attribute("href"); + if (element.hasAttribute("HREF")) + url = element.attribute("HREF"); + if (element.hasAttribute("Href")) + url = element.attribute("Href"); + if (element.hasAttribute("HRef")) + url = element.attribute("HRef"); + } + if ((subNode.nodeName().toLower() == "duration") && (subNode.isElement())) + { + duration.clear(); + element = subNode.toElement(); + if (element.hasAttribute("value")) + duration = element.attribute("value"); + if (element.hasAttribute("Value")) + duration = element.attribute("Value"); + if (element.hasAttribute("VALUE")) + duration = element.attribute("VALUE"); + + if (!duration.isNull()) + length = stringToTime(duration); + } + + if ((subNode.nodeName().toLower() == "title") && (subNode.isElement())) + { + title = subNode.toElement().text(); + } + if ((subNode.nodeName().toLower() == "author") && (subNode.isElement())) + { + author = subNode.toElement().text(); + } + subNode = subNode.nextSibling(); + } + if (!url.isNull()) + { + TrackPtr trackPtr = CollectionManager::instance()->trackForUrl( url ); + trackPtr->setTitle( title ); + tracks.append( trackPtr ); + } + } + node = node.nextSibling(); + } + + The::playlistModel()->insertOptioned( tracks, Playlist::Append ); + return true; +} + +QTime PlaylistLoader::stringToTime(const QString& timeString) +{ + int sec = 0; + bool ok = false; + QStringList tokens = QStringList::split(':',timeString); + + sec += tokens[0].toInt(&ok)*3600; //hours + sec += tokens[1].toInt(&ok)*60; //minutes + sec += tokens[2].toInt(&ok); //secs + + if (ok) + return QTime().addSecs(sec); + else + return QTime(); +} + #include "PlaylistLoader.moc" diff --git a/src/playlist/PlaylistLoader.h b/src/playlist/PlaylistLoader.h index d1c784f9c..ad8f3410a 100644 --- a/src/playlist/PlaylistLoader.h +++ b/src/playlist/PlaylistLoader.h @@ -27,6 +27,8 @@ #include #include +#include + class KJob; @@ -63,12 +65,15 @@ private: unsigned int loadPls_extractIndex( const QString &str ) const; bool loadM3u( QTextStream &stream ); bool loadRealAudioRam( QTextStream& ); + bool loadASX( QTextStream& ); bool loadSMIL( QTextStream& ); Format getFormat( const KUrl &path ); void handleByFormat( QTextStream &stream, Format format); Format getType( QString &contents ); + static QTime stringToTime(const QString&); + private slots: void downloadComplete( KJob *job ); -- 2.11.4.GIT