1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-19 07:19:42 +01:00

Begun M3u parsing

This commit is contained in:
Hugo Lindström 2011-11-23 22:50:10 +01:00
parent 11a6f419aa
commit 31945b9017
5 changed files with 237 additions and 2 deletions

View File

@ -97,6 +97,7 @@ set( libGuiSources
utils/xspfgenerator.cpp
utils/jspfloader.cpp
utils/spotifyparser.cpp
utils/m3uloader.cpp
utils/itunesparser.cpp
utils/rdioparser.cpp
utils/shortenedlinkparser.cpp
@ -212,6 +213,7 @@ set( libGuiHeaders
utils/xspfgenerator.h
utils/jspfloader.h
utils/spotifyparser.h
utils/m3uloader.h
utils/itunesparser.h
utils/rdioparser.h
utils/shortenedlinkparser.h

View File

@ -26,6 +26,7 @@
#include "utils/spotifyparser.h"
#include "utils/itunesparser.h"
#include "utils/rdioparser.h"
#include "utils/m3uloader.h"
#include "utils/shortenedlinkparser.h"
#include "utils/logger.h"
#include "utils/tomahawkutils.h"
@ -108,9 +109,15 @@ DropJob::acceptsMimeData( const QMimeData* data, DropJob::DropTypes acceptedType
if( url.contains( "xspf" ) )
return true;
if( url.contains( "m3u" ) )
return true;
if( data->data( "text/uri-list" ).contains( "xspf" ) )
return true;
if( data->data( "text/uri-list" ).contains( "m3u" ) )
return true;
// Not the most elegant
if ( url.contains( "spotify" ) && url.contains( "playlist" ) && s_canParseSpotifyPlaylists )
return true;
@ -118,6 +125,14 @@ DropJob::acceptsMimeData( const QMimeData* data, DropJob::DropTypes acceptedType
if ( acceptedType.testFlag( Track ) )
{
if( url.contains( "m3u" ) )
return true;
if( data->data( "text/uri-list" ).contains( "m3u" ) )
return true;
if ( url.contains( "itunes" ) && url.contains( "album" ) ) // YES itunes is fucked up and song links have album/ in the url.
return true;
@ -167,6 +182,9 @@ DropJob::isDropType( DropJob::DropType desired, const QMimeData* data )
if( url.contains( "xspf" ) || data->data( "text/uri-list").contains( "xspf" ) )
return true;
if( url.contains( "m3u" ) || data->data( "text/uri-list").contains( "m3u" ) )
return true;
// Not the most elegant
if ( url.contains( "spotify" ) && url.contains( "playlist" ) && s_canParseSpotifyPlaylists )
return true;
@ -428,6 +446,29 @@ DropJob::tracksFromMixedData( const QMimeData *data )
return queries;
}
void
DropJob::handleM3u( const QString& fileUrls )
{
tDebug() << Q_FUNC_INFO << "Got M3u playlist!!" << fileUrls;
QStringList urls = fileUrls.split( QRegExp( "\n" ), QString::SkipEmptyParts );
if ( dropAction() == Default )
setDropAction( Create );
tDebug() << "Got a M3U playlist url to parse!" << urls;
M3uLoader* m = new M3uLoader( urls, dropAction() == Create, this );
if ( dropAction() == Append )
{
tDebug() << Q_FUNC_INFO << "Trying to append contents from" << urls;
connect( m, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( onTracksAdded( QList< Tomahawk::query_ptr > ) ) );
}
m_queryCount++;
}
void
DropJob::handleXspfs( const QString& fileUrls )
@ -466,8 +507,9 @@ DropJob::handleXspfs( const QString& fileUrls )
{
qDebug() << Q_FUNC_INFO << "Trying to append xspf";
connect( l, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( onTracksAdded( QList< Tomahawk::query_ptr > ) ) );
m_queryCount++;
}
m_queryCount++;
}
}
@ -522,6 +564,8 @@ DropJob::handleAllUrls( const QString& urls )
{
if ( urls.contains( "xspf" ) )
handleXspfs( urls );
else if ( urls.contains( "m3u" ) )
handleM3u( urls );
else if ( urls.contains( "spotify" ) /// Handle all the spotify uris on internal server, if not avail. fallback to spotify
&& ( urls.contains( "playlist" ) || urls.contains( "artist" ) || urls.contains( "album" ) || urls.contains( "track" ) )
&& s_canParseSpotifyPlaylists )
@ -588,6 +632,7 @@ DropJob::expandedUrls( QStringList urls )
void
DropJob::onTracksAdded( const QList<Tomahawk::query_ptr>& tracksList )
{
qDebug() << Q_FUNC_INFO;
if ( m_dropJob )
{
m_dropJob->setFinished();

View File

@ -102,7 +102,7 @@ public:
void setGetWholeAlbums( bool getWholeAlbums );
void tracksFromMimeData( const QMimeData* data, bool allowDuplicates = false, bool onlyLocal = false, bool top10 = false );
void handleXspfs( const QString& files );
void handleM3u( const QString& urls );
void handleSpotifyUrls( const QString& urls );
void handleRdioUrls( const QString& urls );

View File

@ -0,0 +1,129 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Hugo Lindström <hugolm84@gmail.com>
*
* 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 "m3uloader.h"
#include "utils/logger.h"
#include "utils/tomahawkutils.h"
#include "query.h"
#include "sourcelist.h"
#include "jobview/JobStatusView.h"
#include "jobview/JobStatusModel.h"
#include "viewmanager.h"
#include <QtCore/QRegExp>
#include <QtCore/QFileInfo>
#include <QtCore/QFile>
/* taglib */
#include <taglib/fileref.h>
#include <taglib/tag.h>
using namespace Tomahawk;
M3uLoader::M3uLoader( const QStringList& Urls, bool createNewPlaylist, QObject* parent )
: QObject ( parent )
, m_single( false )
, m_trackMode( true )
, m_createNewPlaylist( createNewPlaylist )
{
foreach ( const QString& url, Urls )
parseM3u( url );
}
M3uLoader::M3uLoader( const QString& Url, bool createNewPlaylist, QObject* parent )
: QObject ( parent )
, m_single( false )
, m_trackMode( true )
, m_createNewPlaylist( createNewPlaylist )
{
parseM3u( Url );
}
M3uLoader::~M3uLoader()
{
}
Tomahawk::query_ptr
M3uLoader::getTags( const QFileInfo& info )
{
QByteArray fileName = QFile::encodeName( info.canonicalFilePath() );
const char *encodedName = fileName.constData();
TagLib::FileRef f( encodedName );
TagLib::Tag *tag = f.tag();
QString artist = TStringToQString( tag->artist() ).trimmed();
QString album = TStringToQString( tag->album() ).trimmed();
QString track = TStringToQString( tag->title() ).trimmed();
if ( artist.isEmpty() || track.isEmpty() )
{
qDebug() << "Error parsing" << info.fileName();
return Tomahawk::query_ptr();
}
else
{
qDebug() << Q_FUNC_INFO << artist << track << album;
Tomahawk::query_ptr q = Tomahawk::Query::get( artist, track, album, uuid(), true );
return q;
}
}
void
M3uLoader::parseM3u( const QString& fileLink )
{
QFileInfo fileInfo( fileLink );
QFile file( QUrl::fromUserInput( fileLink ).toLocalFile() );
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "Error" << file.errorString();
return;
}
while (!file.atEnd()) {
QByteArray line = file.readLine();
/// If anyone wants to take on the regex for parsing EXT, go ahead
/// But the notion that users does not tag by a common rule. that seems hard
if( line.contains("EXT") )
continue;
qDebug() << line.simplified();
QFileInfo tmpFile( QUrl::fromUserInput( QString( line.simplified() ) ).toLocalFile() );
if( tmpFile.exists() )
m_tracks << getTags( tmpFile );
else
{
QFileInfo tmpFile( QUrl::fromUserInput( QString( fileInfo.canonicalPath() + "/" + line.simplified() ) ).toLocalFile() );
if( tmpFile.exists() )
m_tracks << getTags( tmpFile );
}
}
if( !m_tracks.isEmpty() ){
qDebug() << Q_FUNC_INFO << "Emitting tracks!";
emit tracks( m_tracks );
}
file.deleteLater();
}

View File

@ -0,0 +1,59 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2011-2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 M3U_LOADER_H
#define M3U_LOADER_H
#include "dllmacro.h"
#include "typedefs.h"
#include "query.h"
#include "dropjobnotifier.h"
#include <QtCore/QFileInfo>
#include <QObject>
#include <QSet>
#include <QtCore/QStringList>
class TrackModel;
namespace Tomahawk
{
class DLLEXPORT M3uLoader : public QObject
{
Q_OBJECT
public:
explicit M3uLoader( const QString& trackUrl, bool createNewPlaylist = false, QObject* parent = 0 );
explicit M3uLoader( const QStringList& trackUrls, bool createNewPlaylist = false, QObject* parent = 0 );
virtual ~M3uLoader();
signals:
void track( const Tomahawk::query_ptr& track );
void tracks( const QList< Tomahawk::query_ptr > tracks );
private:
void parseM3u( const QString& track );
Tomahawk::query_ptr getTags( const QFileInfo& info );
QList< query_ptr > m_tracks;
QString m_title, m_info, m_creator;
bool m_single;
bool m_trackMode;
bool m_createNewPlaylist;
};
}
#endif