File indexing completed on 2024-05-12 16:18:06

0001 /***************************************************************************
0002  *   Copyright (C) 2010 Ralf Engels <ralf-engels@gmx.de>                  *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0018  ***************************************************************************/
0019 
0020 #include "Album.h"
0021 
0022 #include "Track.h"
0023 
0024 #include <QDebug>
0025 #include <QFileInfo>
0026 #include <QXmlStreamReader>
0027 #include <QXmlStreamWriter>
0028 
0029 // constructor is needed to put albums in a hash
0030 CollectionScanner::Album::Album()
0031 {}
0032 
0033 CollectionScanner::Album::Album( const QString &name, const QString &artist )
0034     : m_name( name )
0035     , m_artist( artist )
0036 {}
0037 
0038 void
0039 CollectionScanner::Album::addTrack( Track *track )
0040 {
0041     m_tracks.append( track );
0042 }
0043 
0044 QString
0045 CollectionScanner::Album::name() const
0046 {
0047     return m_name;
0048 }
0049 
0050 QString
0051 CollectionScanner::Album::artist() const
0052 {
0053     return m_artist;
0054 }
0055 
0056 void
0057 CollectionScanner::Album::setArtist( const QString &artist )
0058 {
0059     m_artist = artist;
0060 }
0061 
0062 
0063 QString
0064 CollectionScanner::Album::cover() const
0065 {
0066     // we prefer covers included in tracks.
0067     // At least we know exactly that they really belong to the album
0068     foreach( Track *track, m_tracks )
0069     {
0070         // IMPROVEMENT: skip covers that have a strange aspect ratio or are
0071         // unrealistically small, or do not resolve to a valid image
0072         if( track->hasCover() )
0073             return QStringLiteral("amarok-sqltrackuid://") + track->uniqueid();
0074     }
0075 
0076     // ok. Now we have to figure out which of the cover images is
0077     // the best.
0078     QString bestCover;
0079     int     bestRating = -1;
0080     qint64  bestSize = 0;
0081 
0082     foreach( const QString &cover, m_covers )
0083     {
0084         int rating = 0;
0085 
0086         if( cover.contains( QLatin1String("front"), Qt::CaseInsensitive ) ||
0087             cover.contains( QObject::tr( "front", "Front cover of an album" ), Qt::CaseInsensitive ) )
0088             rating += 2;
0089 
0090         if( cover.contains( QLatin1String("cover"), Qt::CaseInsensitive ) ||
0091             cover.contains( QObject::tr( "cover", "(Front) Cover of an album" ), Qt::CaseInsensitive ) )
0092             rating += 2;
0093 
0094         //next: try "folder" (some applications apparently use this)
0095         //using compare and not contains to not hit "Folder-Back" or something.
0096         if( cover.compare( QLatin1String("folder"), Qt::CaseInsensitive ) == 0)
0097             rating += 1;
0098 
0099         QFileInfo info( cover );
0100         if( (rating == bestRating && info.size() > bestSize) ||
0101             (rating > bestRating) )
0102         {
0103             bestCover = cover;
0104             bestRating = rating;
0105             bestSize = info.size();
0106         }
0107     }
0108 
0109     return bestCover;
0110 }
0111 
0112 QStringList
0113 CollectionScanner::Album::covers() const
0114 {
0115     return m_covers;
0116 }
0117 
0118 void
0119 CollectionScanner::Album::setCovers( const QStringList &covers )
0120 {
0121     m_covers = covers;
0122 }
0123 
0124 
0125 
0126 QList<CollectionScanner::Track*>
0127 CollectionScanner::Album::tracks() const
0128 {
0129     return m_tracks;
0130 }
0131 
0132 bool
0133 CollectionScanner::Album::isNoCompilation() const
0134 {
0135     foreach( CollectionScanner::Track *track, m_tracks )
0136     {
0137         if( track->isNoCompilation() )
0138             return true;
0139     }
0140 
0141     return false;
0142 }