diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index ba70a6c8a..32167c209 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -100,6 +100,7 @@ set( libGuiSources utils/SpotifyParser.cpp utils/M3uLoader.cpp utils/ItunesParser.cpp + utils/ItunesLoader.cpp utils/RdioParser.cpp utils/ShortenedLinkParser.cpp utils/SoundcloudParser.cpp diff --git a/src/libtomahawk/DropJob.cpp b/src/libtomahawk/DropJob.cpp index a1ccbfc9c..b9e02d958 100644 --- a/src/libtomahawk/DropJob.cpp +++ b/src/libtomahawk/DropJob.cpp @@ -27,6 +27,7 @@ #include "utils/SpotifyParser.h" #include "utils/ItunesParser.h" +#include "utils/ItunesLoader.h" #include "utils/RdioParser.h" #include "utils/M3uLoader.h" #include "utils/ShortenedLinkParser.h" @@ -114,6 +115,12 @@ DropJob::acceptsMimeData( const QMimeData* data, DropJob::DropTypes acceptedType const QString url = data->data( "text/plain" ); if ( acceptedType.testFlag( Playlist ) ) { + if ( url.contains( "xml" ) && url.contains( "iTunes") ) + return true; + + if ( data->data( "text/uri-list" ).contains( "xml" ) && data->data( "text/uri-list" ).contains( "iTunes") ) + return true; + if ( url.contains( "xspf" ) ) return true; @@ -204,6 +211,12 @@ DropJob::isDropType( DropJob::DropType desired, const QMimeData* data ) const QString url = data->data( "text/plain" ); if ( desired == Playlist ) { + if ( url.contains( "xml" ) && url.contains( "iTunes") ) + return true; + + if ( data->data( "text/uri-list" ).contains( "xml" ) && data->data( "text/uri-list" ).contains( "iTunes") ) + return true; + if( url.contains( "xspf" ) || data->data( "text/uri-list").contains( "xspf" ) ) return true; @@ -682,7 +695,12 @@ DropJob::handleAllUrls( const QString& urls ) void DropJob::handleTrackUrls( const QString& urls ) { - if ( urls.contains( "itunes.apple.com") ) + + if ( urls.contains( "xml" ) && urls.contains( "iTunes" ) ) + { + new iTunesLoader( urls, this ); + } + else if ( urls.contains( "itunes.apple.com") ) { QStringList tracks = urls.split( QRegExp( "\\s+" ), QString::SkipEmptyParts ); diff --git a/src/libtomahawk/utils/ItunesLoader.cpp b/src/libtomahawk/utils/ItunesLoader.cpp new file mode 100644 index 000000000..d4ccfb11f --- /dev/null +++ b/src/libtomahawk/utils/ItunesLoader.cpp @@ -0,0 +1,103 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, 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 "ItunesLoader.h" +#include +#include +#include "Playlist.h" +#include "SourceList.h" + +using namespace Tomahawk; + +ItunesLoader::ItunesLoader( const QString& input, QObject *parent) + : QObject(parent) + , m_itunesLibFile( input ) +{ + /** + * Paths to ItunesLibFile + * /Users/username/Music/iTunes/iTunes Library.xml + * \Users\username\Music\iTunes\iTunes Library.xml + * \Documents and Settings\username\My Documents\My Music\iTunes\iTunes Library.xml + * http://support.apple.com/kb/HT1660 + */ + + QSettings plist( QUrl::fromUserInput( QString( input.simplified() ) ).toLocalFile(), QSettings::NativeFormat); + + m_ignoreFields << "Library" << "Movies" << "TV Shows" << "Music Videos" << "Genius"; + + parseTracks( plist.value( "Tracks" ).toMap() ); + parsePlaylists( plist.value( "Playlists" ).toList() ); + + foreach ( const QString& name, m_playlists.keys()) + { + Playlist::create( SourceList::instance()->getLocal(), + uuid(), + name, + "iTunes imported playlist", + "", + false, + m_playlists[ name ] ); + } + + m_tracks.clear(); + m_playlists.clear(); +} + + +void +ItunesLoader::parseTracks( const QVariantMap& tracks ) +{ + foreach ( const QVariant& track, tracks ) + { + QVariantMap trackMap = track.toMap(); + if ( !trackMap.value( "Track ID" ).isValid() ) + continue; + + const QString artist = trackMap.value( "Artist", "" ).toString(); + const QString title = trackMap.value( "Name", "" ).toString(); + const QString album = trackMap.value( "Album", "" ).toString(); + + if ( artist.isEmpty() || title.isEmpty() ) + continue; + + m_tracks.insert( trackMap.value( "Track ID" ).toInt(), Tomahawk::Query::get( artist, title, album ) ); + } +} + + +void +ItunesLoader::parsePlaylists( const QVariantList& playlists ) +{ + foreach ( const QVariant& playlist, playlists ) + { + const QString title = playlist.toMap().value( "Name" ).toString(); + + if ( m_ignoreFields.contains( title ) ) + continue; + + foreach ( const QVariant& items, playlist.toMap() ) + { + if ( items.toMap().value( "Track ID" ).isValid() ) + { + int trackId = items.toMap().value( "Track ID" ).toInt(); + if ( m_tracks.contains( trackId ) ) + m_playlists[title] << m_tracks[ trackId ]; + } + } + } +} diff --git a/src/libtomahawk/utils/ItunesLoader.h b/src/libtomahawk/utils/ItunesLoader.h new file mode 100644 index 000000000..195023cf4 --- /dev/null +++ b/src/libtomahawk/utils/ItunesLoader.h @@ -0,0 +1,54 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, 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 __ITUNESLOADER__ +#define __ITUNESLOADER__ + + +#include "DllMacro.h" +#include "Typedefs.h" +#include "Query.h" +#include +#include + +namespace Tomahawk +{ + +class DLLEXPORT ItunesLoader : public QObject +{ + Q_OBJECT +public: + explicit ItunesLoader( const QString& input, QObject* parent = 0 ); + ~iTunesLoader(){} + +private: + void parseTracks( const QVariantMap& tracks ); + void parsePlaylists( const QVariantList& playlists ); + + QString m_itunesLibFile; + QStringList m_ignoreFields; + + QMap< QString, QList > m_playlists; + QMap< int, Tomahawk::query_ptr > m_tracks; +}; + +} +#endif + +