File indexing completed on 2024-05-19 04:49:44

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "BansheeProvider.h"
0018 
0019 #include "BansheeTrack.h"
0020 #include "importers/ImporterSqlConnection.h"
0021 
0022 using namespace StatSyncing;
0023 
0024 BansheeProvider::BansheeProvider( const QVariantMap &config, ImporterManager *importer )
0025     : ImporterProvider( config, importer )
0026     , m_connection( new ImporterSqlConnection( m_config.value( "dbPath" ).toString() ) )
0027 {
0028 }
0029 
0030 BansheeProvider::~BansheeProvider()
0031 {
0032 }
0033 
0034 qint64
0035 BansheeProvider::reliableTrackMetaData() const
0036 {
0037     return Meta::valTitle | Meta::valArtist | Meta::valAlbum | Meta::valComposer
0038             | Meta::valYear | Meta::valTrackNr | Meta::valDiscNr;
0039 }
0040 
0041 qint64
0042 BansheeProvider::writableTrackStatsData() const
0043 {
0044     return Meta::valRating | Meta::valLastPlayed | Meta::valPlaycount;
0045 }
0046 
0047 QSet<QString>
0048 BansheeProvider::artists()
0049 {
0050     QSet<QString> result;
0051     foreach( const QVariantList &row,
0052              m_connection->query( "SELECT Name FROM coreartists" ) )
0053         result.insert( row[0].toString() );
0054 
0055     return result;
0056 }
0057 
0058 TrackList
0059 BansheeProvider::artistTracks( const QString &artistName )
0060 {
0061     // Due to Banshee's peculiar track info storage, to avoid massive amount of confusion
0062     // we only take tracks from PrimarySource: MusicLibrarySource-Library (always ID 1)
0063     const QString query = "SELECT TrackID, TRIM(t.Title), ar.Name, al.Title, "
0064             "TRIM(t.Composer), t.Year, t.TrackNumber, t.Disc, t.Rating, "
0065             "t.LastPlayedStamp, t.PlayCount "
0066             "FROM coretracks t "
0067             "INNER JOIN coreartists ar USING(ArtistID) "
0068             "LEFT JOIN corealbums al USING(AlbumID) "
0069             "WHERE ar.Name = :artist AND t.PrimarySourceID = 1";
0070 
0071     QVariantMap bindValues;
0072     bindValues.insert( ":artist", artistName );
0073 
0074     const QList<qint64> fields = QList<qint64>() << Meta::valTitle << Meta::valArtist
0075            << Meta::valAlbum << Meta::valComposer << Meta::valYear << Meta::valTrackNr
0076            << Meta::valDiscNr << Meta::valRating << Meta::valLastPlayed
0077            << Meta::valPlaycount;
0078 
0079     TrackList result;
0080     foreach( const QVariantList &row, m_connection->query( query, bindValues ) )
0081     {
0082         const qint64 trackId = row[0].toLongLong();
0083 
0084         Meta::FieldHash metadata;
0085         for( int i = 0; i < fields.size(); ++i )
0086             metadata.insert( fields[i], row[i + 1] );
0087 
0088         result << TrackPtr( new BansheeTrack( trackId, m_connection, metadata ) );
0089     }
0090 
0091     return result;
0092 }