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 "ClementineTrack.h" 0018 0019 #include "importers/ImporterSqlConnection.h" 0020 0021 #include <QReadLocker> 0022 #include <QStringList> 0023 #include <QWriteLocker> 0024 0025 using namespace StatSyncing; 0026 0027 ClementineTrack::ClementineTrack( const QVariant &filename, 0028 const ImporterSqlConnectionPtr &connection, 0029 const Meta::FieldHash &metadata ) 0030 : SimpleWritableTrack( metadata ) 0031 , m_connection( connection ) 0032 , m_filename( filename ) 0033 { 0034 } 0035 0036 ClementineTrack::~ClementineTrack() 0037 { 0038 } 0039 0040 int 0041 ClementineTrack::year() const 0042 { 0043 const int yr = SimpleWritableTrack::year(); 0044 return yr == -1 ? 0 : yr; 0045 } 0046 0047 int 0048 ClementineTrack::trackNumber() const 0049 { 0050 const int tn = SimpleWritableTrack::trackNumber(); 0051 return tn == -1 ? 0 : tn; 0052 } 0053 0054 int 0055 ClementineTrack::discNumber() const 0056 { 0057 const int dn = SimpleWritableTrack::discNumber(); 0058 return dn == -1 ? 0 : dn; 0059 } 0060 0061 QDateTime 0062 ClementineTrack::lastPlayed() const 0063 { 0064 QReadLocker lock( &m_lock ); 0065 const int lp = m_statistics.value( Meta::valLastPlayed ).toInt(); 0066 return lp == -1 ? QDateTime() : getDateTime( lp ); 0067 } 0068 0069 void 0070 ClementineTrack::setLastPlayed( const QDateTime &lastPlayed ) 0071 { 0072 QWriteLocker lock( &m_lock ); 0073 m_statistics.insert( Meta::valLastPlayed, lastPlayed.isValid() 0074 ? lastPlayed.toSecsSinceEpoch() : -1 ); 0075 m_changes |= Meta::valLastPlayed; 0076 } 0077 0078 int 0079 ClementineTrack::playCount() const 0080 { 0081 const int pc = SimpleWritableTrack::playCount(); 0082 return pc == -1 ? 0 : pc; 0083 } 0084 0085 void 0086 ClementineTrack::setPlayCount( int playCount ) 0087 { 0088 SimpleWritableTrack::setPlayCount( playCount == 0 ? -1 : playCount ); 0089 } 0090 0091 int 0092 ClementineTrack::rating() const 0093 { 0094 QReadLocker lock( &m_lock ); 0095 const qreal rt = m_statistics.value( Meta::valRating ).toReal(); 0096 return rt < 0 ? 0 : qRound( rt * 10 ); 0097 } 0098 0099 void 0100 ClementineTrack::setRating( int rating ) 0101 { 0102 QWriteLocker lock( &m_lock ); 0103 m_statistics.insert( Meta::valRating, rating == 0 ? -1.0 : 0.1 * rating ); 0104 m_changes |= Meta::valRating; 0105 } 0106 0107 void 0108 ClementineTrack::doCommit( const qint64 fields ) 0109 { 0110 QStringList updates; 0111 QVariantMap bindValues; 0112 0113 if( fields & Meta::valLastPlayed ) 0114 { 0115 updates << "lastplayed = :lastplayed"; 0116 bindValues.insert( ":lastplayed", m_statistics.value( Meta::valLastPlayed ) ); 0117 } 0118 if( fields & Meta::valRating ) 0119 { 0120 updates << "rating = :rating"; 0121 bindValues.insert( ":rating", m_statistics.value( Meta::valRating ) ); 0122 } 0123 if( fields & Meta::valPlaycount ) 0124 { 0125 updates << "playcount = :playcount"; 0126 bindValues.insert( ":playcount", m_statistics.value( Meta::valPlaycount ) ); 0127 } 0128 0129 if( !updates.empty() ) 0130 { 0131 const QString query = "UPDATE songs SET " + updates.join(", ") + 0132 " WHERE filename = :name"; 0133 0134 bindValues.insert( ":name", m_filename ); 0135 m_connection->query( query, bindValues ); 0136 } 0137 }