diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 3f046cc36..ace74383f 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -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 diff --git a/src/libtomahawk/dropjob.cpp b/src/libtomahawk/dropjob.cpp index 1a409af5a..4b3aedea8 100644 --- a/src/libtomahawk/dropjob.cpp +++ b/src/libtomahawk/dropjob.cpp @@ -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 ) ), 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 ) ), 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& tracksList ) { + qDebug() << Q_FUNC_INFO; if ( m_dropJob ) { m_dropJob->setFinished(); diff --git a/src/libtomahawk/dropjob.h b/src/libtomahawk/dropjob.h index 492267740..2bee71afd 100644 --- a/src/libtomahawk/dropjob.h +++ b/src/libtomahawk/dropjob.h @@ -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 ); diff --git a/src/libtomahawk/utils/m3uloader.cpp b/src/libtomahawk/utils/m3uloader.cpp new file mode 100644 index 000000000..01d3b5d66 --- /dev/null +++ b/src/libtomahawk/utils/m3uloader.cpp @@ -0,0 +1,129 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Hugo Lindström + * + * 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 . + */ + +#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 +#include +#include +/* taglib */ +#include +#include + +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(); +} + diff --git a/src/libtomahawk/utils/m3uloader.h b/src/libtomahawk/utils/m3uloader.h new file mode 100644 index 000000000..bae91ba5b --- /dev/null +++ b/src/libtomahawk/utils/m3uloader.h @@ -0,0 +1,59 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2011-2012, Hugo Lindström + * + * 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 . + */ + +#ifndef M3U_LOADER_H +#define M3U_LOADER_H + +#include "dllmacro.h" +#include "typedefs.h" +#include "query.h" +#include "dropjobnotifier.h" +#include +#include +#include +#include + +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