File indexing completed on 2024-05-05 04:49:21

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 "SimpleWritableTrack.h"
0018 
0019 #include <QReadLocker>
0020 #include <QWriteLocker>
0021 
0022 using namespace StatSyncing;
0023 
0024 SimpleWritableTrack::SimpleWritableTrack( const Meta::FieldHash &metadata,
0025                                           const QSet<QString> &labels )
0026     : SimpleTrack( metadata, labels )
0027 {
0028     // Move statistics to separate container, so we don't have to lock for other metadata
0029     foreach( const qint64 metaValue, metadata.keys() )
0030     {
0031         switch( metaValue )
0032         {
0033             case Meta::valFirstPlayed:
0034             case Meta::valLastPlayed:
0035             case Meta::valRating:
0036             case Meta::valPlaycount:
0037                 m_metadata.remove( metaValue );
0038                 m_statistics.insert( metaValue, metadata[metaValue] );
0039                 break;
0040 
0041             default:
0042                 break;
0043         }
0044     }
0045 }
0046 
0047 SimpleWritableTrack::~SimpleWritableTrack()
0048 {
0049 }
0050 
0051 QDateTime
0052 SimpleWritableTrack::firstPlayed() const
0053 {
0054     QReadLocker lock( &m_lock );
0055     return getDateTime( m_statistics.value( Meta::valFirstPlayed ) );
0056 }
0057 
0058 void
0059 SimpleWritableTrack::setFirstPlayed( const QDateTime &firstPlayed )
0060 {
0061     QWriteLocker lock( &m_lock );
0062     m_statistics.insert( Meta::valFirstPlayed, firstPlayed.isValid()
0063                                                ? firstPlayed.toSecsSinceEpoch() : 0u );
0064     m_changes |= Meta::valFirstPlayed;
0065 }
0066 
0067 QDateTime
0068 SimpleWritableTrack::lastPlayed() const
0069 {
0070     QReadLocker lock( &m_lock );
0071     return getDateTime( m_statistics.value( Meta::valLastPlayed ) );
0072 }
0073 
0074 void
0075 SimpleWritableTrack::setLastPlayed( const QDateTime &lastPlayed )
0076 {
0077     QWriteLocker lock( &m_lock );
0078     m_statistics.insert( Meta::valLastPlayed, lastPlayed.isValid()
0079                                               ? lastPlayed.toSecsSinceEpoch() : 0u );
0080     m_changes |= Meta::valLastPlayed;
0081 }
0082 
0083 int
0084 SimpleWritableTrack::rating() const
0085 {
0086     QReadLocker lock( &m_lock );
0087     return m_statistics.value( Meta::valRating ).toInt();
0088 }
0089 
0090 void
0091 SimpleWritableTrack::setRating( int rating )
0092 {
0093     QWriteLocker lock( &m_lock );
0094     m_statistics.insert( Meta::valRating, rating );
0095     m_changes |= Meta::valRating;
0096 }
0097 
0098 int
0099 SimpleWritableTrack::playCount() const
0100 {
0101     QReadLocker lock( &m_lock );
0102     return m_statistics.value( Meta::valPlaycount ).toInt();
0103 }
0104 
0105 void
0106 SimpleWritableTrack::setPlayCount( int playCount )
0107 {
0108     QWriteLocker lock( &m_lock );
0109     m_statistics.insert( Meta::valPlaycount, playCount );
0110     m_changes |= Meta::valPlaycount;
0111 }
0112 
0113 QSet<QString>
0114 SimpleWritableTrack::labels() const
0115 {
0116     QReadLocker lock( &m_lock );
0117     return m_labels;
0118 }
0119 
0120 void
0121 SimpleWritableTrack::setLabels( const QSet<QString> &labels )
0122 {
0123     QWriteLocker lock( &m_lock );
0124     m_labels = labels;
0125     m_changes |= Meta::valLabel;
0126 }
0127 
0128 void
0129 SimpleWritableTrack::commit()
0130 {
0131     QWriteLocker lock( &m_lock );
0132     doCommit( m_changes );
0133     m_changes = 0;
0134 }