File indexing completed on 2024-05-19 04:50:28

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
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 "CollectionTrack.h"
0018 
0019 #include "core/meta/Meta.h"
0020 #include "core/meta/Statistics.h"
0021 #include "core/support/Debug.h"
0022 
0023 using namespace StatSyncing;
0024 
0025 CollectionTrack::CollectionTrack( Meta::TrackPtr track )
0026     : m_track( track )
0027     , m_trackStats( track->statistics() )
0028     , m_beginUpdateAlreadyCalled( false )
0029 {
0030     Q_ASSERT( m_track );
0031     Q_ASSERT( m_trackStats );
0032 }
0033 
0034 CollectionTrack::~CollectionTrack()
0035 {
0036 }
0037 
0038 QString
0039 CollectionTrack::name() const
0040 {
0041     return m_track->name();
0042 }
0043 
0044 QString
0045 CollectionTrack::album() const
0046 {
0047     Meta::AlbumPtr album = m_track->album();
0048     return album ? album->name() : QString();
0049 }
0050 
0051 QString
0052 CollectionTrack::artist() const
0053 {
0054     Meta::ArtistPtr artist = m_track->artist();
0055     return artist ? artist->name() : QString();
0056 }
0057 
0058 QString
0059 CollectionTrack::composer() const
0060 {
0061     Meta::ComposerPtr composer = m_track->composer();
0062     return composer ? composer->name() : QString();
0063 }
0064 
0065 int
0066 CollectionTrack::year() const
0067 {
0068     Meta::YearPtr year = m_track->year();
0069     return year ? year->year() : 0;
0070 }
0071 
0072 int
0073 CollectionTrack::trackNumber() const
0074 {
0075     return m_track->trackNumber();
0076 }
0077 
0078 int
0079 CollectionTrack::discNumber() const
0080 {
0081     return m_track->discNumber();
0082 }
0083 
0084 int
0085 CollectionTrack::rating() const
0086 {
0087     return qBound<int>( 0, m_trackStats->rating(), 10 );
0088 }
0089 
0090 void
0091 CollectionTrack::setRating( int rating )
0092 {
0093     beginUpdate();
0094     m_trackStats->setRating( rating );
0095 }
0096 
0097 QDateTime
0098 CollectionTrack::firstPlayed() const
0099 {
0100     return m_trackStats->firstPlayed();
0101 }
0102 
0103 void
0104 CollectionTrack::setFirstPlayed( const QDateTime &firstPlayed )
0105 {
0106     beginUpdate();
0107     m_trackStats->setFirstPlayed( firstPlayed );
0108 }
0109 
0110 QDateTime
0111 CollectionTrack::lastPlayed() const
0112 {
0113     return m_trackStats->lastPlayed();
0114 }
0115 
0116 void
0117 CollectionTrack::setLastPlayed( const QDateTime &lastPlayed )
0118 {
0119     beginUpdate();
0120     m_trackStats->setLastPlayed( lastPlayed );
0121 }
0122 
0123 int
0124 CollectionTrack::playCount() const
0125 {
0126     return m_trackStats->playCount();
0127 }
0128 
0129 int
0130 CollectionTrack::recentPlayCount() const
0131 {
0132     return m_trackStats->recentPlayCount();
0133 }
0134 
0135 void
0136 CollectionTrack::setPlayCount( int playCount )
0137 {
0138     beginUpdate();
0139     m_trackStats->setPlayCount( playCount );
0140 }
0141 
0142 QSet<QString>
0143 CollectionTrack::labels() const
0144 {
0145     Meta::LabelList labels = m_track->labels();
0146     QSet<QString> labelNames;
0147     foreach( Meta::LabelPtr label, labels )
0148         labelNames.insert( label->name() );
0149     return labelNames;
0150 }
0151 
0152 void
0153 CollectionTrack::setLabels( const QSet<QString> &labels )
0154 {
0155     QSet<QString> existingLabels;
0156     QMap<QString, Meta::LabelPtr> existingLabelsMap;
0157     foreach( const Meta::LabelPtr &label, m_track->labels() )
0158     {
0159         existingLabels.insert( label->name() );
0160         existingLabelsMap.insert( label->name(), label );
0161     }
0162 
0163     QSet<QString> toRemove = existingLabels - labels;
0164     foreach( const QString &labelName, toRemove )
0165     {
0166         Q_ASSERT( existingLabelsMap.contains( labelName ) );
0167         m_track->removeLabel( existingLabelsMap.value( labelName ) );
0168     }
0169 
0170     QSet<QString> toAdd = labels - existingLabels;
0171     foreach( const QString &labelName, toAdd )
0172     {
0173         m_track->addLabel( labelName );
0174     }
0175 }
0176 
0177 Meta::TrackPtr
0178 CollectionTrack::metaTrack() const
0179 {
0180     return m_track;
0181 }
0182 
0183 void
0184 CollectionTrack::commit()
0185 {
0186     if( !m_beginUpdateAlreadyCalled )
0187         return;
0188 
0189     m_trackStats->endUpdate();
0190     m_beginUpdateAlreadyCalled = false;
0191 }
0192 
0193 void
0194 CollectionTrack::beginUpdate()
0195 {
0196     if( m_beginUpdateAlreadyCalled )
0197         return;
0198 
0199     m_trackStats->beginUpdate();
0200     m_beginUpdateAlreadyCalled = true;
0201 }