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

Allow dropping iTunes Library

This commit is contained in:
Hugo Lindström 2013-04-16 08:22:11 +02:00
parent b6449fcd63
commit ebe9f6fb34
4 changed files with 177 additions and 1 deletions

View File

@ -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

View File

@ -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 );

View File

@ -0,0 +1,103 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, 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 "ItunesLoader.h"
#include <QUrl>
#include <QSettings>
#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 ];
}
}
}
}

View File

@ -0,0 +1,54 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, 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 __ITUNESLOADER__
#define __ITUNESLOADER__
#include "DllMacro.h"
#include "Typedefs.h"
#include "Query.h"
#include <QMap>
#include <QStringList>
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<Tomahawk::query_ptr > > m_playlists;
QMap< int, Tomahawk::query_ptr > m_tracks;
};
}
#endif