mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-01 20:00:13 +02:00
Add support for JSPF playlists, remote and local
This commit is contained in:
@@ -166,6 +166,7 @@ set( libSources
|
||||
utils/animatedsplitter.cpp
|
||||
utils/xspfloader.cpp
|
||||
utils/xspfgenerator.cpp
|
||||
utils/jspfloader.cpp
|
||||
|
||||
widgets/newplaylistwidget.cpp
|
||||
widgets/searchwidget.cpp
|
||||
@@ -336,6 +337,7 @@ set( libHeaders
|
||||
utils/animatedsplitter.h
|
||||
utils/xspfloader.h
|
||||
utils/xspfgenerator.h
|
||||
utils/jspfloader.h
|
||||
|
||||
widgets/newplaylistwidget.h
|
||||
widgets/searchwidget.h
|
||||
@@ -419,7 +421,7 @@ IF( APPLE )
|
||||
FIND_LIBRARY( SCRIPTINGBRIDGE_LIBRARY ScriptingBridge )
|
||||
MARK_AS_ADVANCED( COREAUDIO_LIBRARY COREFOUNDATION_LIBRARY FOUNDATION_LIBRARY SCRIPTINGBRIDGE_LIBRARY )
|
||||
|
||||
SET( libSources ${libSources}
|
||||
SET( libSources ${libSources}
|
||||
infosystem/infoplugins/mac/adium.mm
|
||||
infosystem/infoplugins/mac/adiumplugin.cpp
|
||||
widgets/maclineedit.mm
|
||||
|
@@ -41,6 +41,7 @@
|
||||
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
#include "utils/jspfloader.h"
|
||||
|
||||
GlobalActionManager* GlobalActionManager::s_instance = 0;
|
||||
|
||||
@@ -195,6 +196,14 @@ GlobalActionManager::parseTomahawkLink( const QString& url )
|
||||
l->load( xspf );
|
||||
connect( l, SIGNAL( ok( Tomahawk::playlist_ptr ) ), ViewManager::instance(), SLOT( show( Tomahawk::playlist_ptr ) ) );
|
||||
|
||||
return true;
|
||||
} else if( u.hasQueryItem( "jspf" ) ) {
|
||||
QUrl jspf = QUrl::fromUserInput( u.queryItemValue( "jspf" ) );
|
||||
Tomahawk::JSPFLoader* l = new Tomahawk::JSPFLoader( true, this );
|
||||
tDebug() << "Loading jspiff:" << jspf.toString();
|
||||
l->load( jspf );
|
||||
connect( l, SIGNAL( ok( Tomahawk::playlist_ptr ) ), ViewManager::instance(), SLOT( show( Tomahawk::playlist_ptr ) ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
191
src/libtomahawk/utils/jspfloader.cpp
Normal file
191
src/libtomahawk/utils/jspfloader.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "jspfloader.h"
|
||||
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDomDocument>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include <qjson/parser.h>
|
||||
|
||||
#include "utils/tomahawkutils.h"
|
||||
#include "utils/logger.h"
|
||||
|
||||
#include "sourcelist.h"
|
||||
#include "playlist.h"
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
void
|
||||
JSPFLoader::load( const QUrl& url )
|
||||
{
|
||||
QNetworkRequest request( url );
|
||||
Q_ASSERT( TomahawkUtils::nam() != 0 );
|
||||
QNetworkReply* reply = TomahawkUtils::nam()->get( request );
|
||||
|
||||
// isn't there a race condition here? something could happen before we connect()
|
||||
// no---the event loop is needed to make the request, i think (leo)
|
||||
connect( reply, SIGNAL( finished() ),
|
||||
SLOT( networkLoadFinished() ) );
|
||||
|
||||
connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ),
|
||||
SLOT( networkError( QNetworkReply::NetworkError ) ) );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPFLoader::load( QFile& file )
|
||||
{
|
||||
if( file.open( QFile::ReadOnly ) )
|
||||
{
|
||||
m_body = file.readAll();
|
||||
gotBody();
|
||||
}
|
||||
else
|
||||
{
|
||||
tLog() << "Failed to open jspf file";
|
||||
reportError();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPFLoader::reportError()
|
||||
{
|
||||
emit failed();
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPFLoader::networkLoadFinished()
|
||||
{
|
||||
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
||||
m_body = reply->readAll();
|
||||
gotBody();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPFLoader::networkError( QNetworkReply::NetworkError e )
|
||||
{
|
||||
tLog() << Q_FUNC_INFO << "Network error loading jspf" << e;
|
||||
reportError();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
JSPFLoader::gotBody()
|
||||
{
|
||||
QJson::Parser p;
|
||||
bool retOk;
|
||||
QVariantMap wrapper = p.parse( m_body, &retOk ).toMap();
|
||||
|
||||
if ( !retOk )
|
||||
{
|
||||
tLog() << "Failed to parse jspf json:" << p.errorString() << "on line" << p.errorLine();
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !wrapper.contains( "playlist" ) )
|
||||
{
|
||||
tLog() << "No playlist element in JSPF!";
|
||||
return;
|
||||
}
|
||||
|
||||
QVariantMap pl = wrapper.value( "playlist" ).toMap();
|
||||
QString origTitle = pl.value( "title" ).toString();
|
||||
m_info = pl.value( "info" ).toString();
|
||||
m_creator = pl.value( "creator" ).toString();
|
||||
|
||||
m_title = origTitle;
|
||||
if ( m_title.isEmpty() )
|
||||
m_title = tr( "New Playlist" );
|
||||
if ( !m_overrideTitle.isEmpty() )
|
||||
m_title = m_overrideTitle;
|
||||
|
||||
if ( pl.contains( "track" ) )
|
||||
{
|
||||
QVariantList tracks = pl.value( "track" ).toList();
|
||||
|
||||
bool shownError = false;
|
||||
foreach ( const QVariant& track, tracks )
|
||||
{
|
||||
QVariantMap tM = track.toMap();
|
||||
QString artist, album, track, duration, annotation, url;
|
||||
|
||||
artist = tM.value( "creator" ).toString();
|
||||
album = tM.value( "album" ).toString();
|
||||
track = tM.value( "title" ).toString();
|
||||
duration = tM.value( "duration" ).toString();
|
||||
annotation = tM.value( "annotation" ).toString();
|
||||
if ( tM.value( "location" ).toList().size() > 0 )
|
||||
url = tM.value( "location" ).toList().first().toString();
|
||||
|
||||
if( artist.isEmpty() || track.isEmpty() )
|
||||
{
|
||||
if( !shownError )
|
||||
{
|
||||
QMessageBox::warning( 0, tr( "Failed to save tracks" ), tr( "Some tracks in the playlist do not contain an artist and a title. They will be ignored." ), QMessageBox::Ok );
|
||||
shownError = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
query_ptr q = Tomahawk::Query::get( artist, track, album, uuid() );
|
||||
q->setDuration( duration.toInt() / 1000 );
|
||||
if( !url.isEmpty() )
|
||||
q->setResultHint( url );
|
||||
|
||||
m_entries << q;
|
||||
}
|
||||
}
|
||||
|
||||
if ( origTitle.isEmpty() && m_entries.isEmpty() )
|
||||
{
|
||||
if ( m_autoCreate )
|
||||
{
|
||||
QMessageBox::critical( 0, tr( "XSPF Error" ), tr( "This is not a valid XSPF playlist." ) );
|
||||
deleteLater();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
emit failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ( m_autoCreate )
|
||||
{
|
||||
m_playlist = Playlist::create( SourceList::instance()->getLocal(),
|
||||
uuid(),
|
||||
m_title,
|
||||
m_info,
|
||||
m_creator,
|
||||
false,
|
||||
m_entries );
|
||||
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
emit ok( m_playlist );
|
||||
}
|
77
src/libtomahawk/utils/jspfloader.h
Normal file
77
src/libtomahawk/utils/jspfloader.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef JSPFLOADER_H
|
||||
#define JSPFLOADER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QFile>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
#include "playlist.h"
|
||||
#include "typedefs.h"
|
||||
|
||||
#include "dllmacro.h"
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
class DLLEXPORT JSPFLoader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit JSPFLoader( bool autoCreate = true, QObject* parent = 0 )
|
||||
: QObject( parent )
|
||||
, m_autoCreate( autoCreate )
|
||||
{}
|
||||
|
||||
virtual ~JSPFLoader() {}
|
||||
|
||||
QList< Tomahawk::query_ptr > entries() const { return m_entries; }
|
||||
void setOverrideTitle( const QString& newTitle ) { m_overrideTitle = newTitle; }
|
||||
|
||||
signals:
|
||||
void failed();
|
||||
void ok( const Tomahawk::playlist_ptr& );
|
||||
|
||||
public slots:
|
||||
void load( const QUrl& url );
|
||||
void load( QFile& file );
|
||||
|
||||
private slots:
|
||||
void networkLoadFinished();
|
||||
void networkError( QNetworkReply::NetworkError e );
|
||||
|
||||
private:
|
||||
void reportError();
|
||||
void gotBody();
|
||||
|
||||
bool m_autoCreate;
|
||||
QList< Tomahawk::query_ptr > m_entries;
|
||||
QString m_title, m_info, m_creator, m_overrideTitle;
|
||||
|
||||
QByteArray m_body;
|
||||
Tomahawk::playlist_ptr m_playlist;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // JSPFLOADER_H
|
@@ -54,6 +54,7 @@
|
||||
|
||||
#include "audio/audioengine.h"
|
||||
#include "utils/xspfloader.h"
|
||||
#include "utils/jspfloader.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/tomahawkutils.h"
|
||||
|
||||
@@ -520,11 +521,19 @@ TomahawkApp::loadUrl( const QString& url )
|
||||
{
|
||||
QFile f( url );
|
||||
QFileInfo info( f );
|
||||
if ( f.exists() && info.suffix() == "xspf" ) {
|
||||
if ( info.suffix() == "xspf" )
|
||||
{
|
||||
XSPFLoader* l = new XSPFLoader( true, this );
|
||||
tDebug( LOGINFO ) << "Loading spiff:" << url;
|
||||
l->load( QUrl::fromUserInput( url ) );
|
||||
|
||||
return true;
|
||||
} else if ( info.suffix() == "jspf" )
|
||||
{
|
||||
JSPFLoader* l = new JSPFLoader( true, this );
|
||||
tDebug( LOGINFO ) << "Loading j-spiff:" << url;
|
||||
l->load( QUrl::fromUserInput( url ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user