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 #ifndef STATSYNCING_SIMPLE_WRITABLE_TRACK_H
0018 #define STATSYNCING_SIMPLE_WRITABLE_TRACK_H
0019 
0020 #include "SimpleTrack.h"
0021 
0022 #include <QReadWriteLock>
0023 
0024 namespace StatSyncing {
0025 
0026 /**
0027  * A simple implementation of @see StatSyncing::Track which wraps data contained
0028  * in Meta::FieldHash @param metadata container, and for every function returns value
0029  * corresponding to an adequate Meta::val* key. Labels are passed through @param labels .
0030  *
0031  * This class is suitable for read-write operations. If you only need read capabilities,
0032  * consider using @see StatSyncing::SimpleTrack .
0033  */
0034 class AMAROK_EXPORT SimpleWritableTrack : public SimpleTrack
0035 {
0036 public:
0037     explicit SimpleWritableTrack( const Meta::FieldHash &metadata,
0038                                   const QSet<QString> &labels = QSet<QString>() );
0039     ~SimpleWritableTrack() override;
0040 
0041     QDateTime firstPlayed() const override;
0042 
0043     /**
0044      * Sets the First Played statistic. This method saves @param firstPlayed in the form
0045      * of unix timestamp.
0046      */
0047     void setFirstPlayed( const QDateTime &firstPlayed ) override;
0048 
0049     QDateTime lastPlayed() const override;
0050 
0051     /**
0052      * Sets the Last Played statistic. This method saves @param lastPlayed in the form
0053      * of unix timestamp.
0054      */
0055     void setLastPlayed( const QDateTime &lastPlayed ) override;
0056 
0057     int playCount() const override;
0058     void setPlayCount( int playCount ) override;
0059 
0060     int rating() const override;
0061     void setRating( int rating ) override;
0062 
0063     QSet<QString> labels() const override;
0064     void setLabels( const QSet<QString> &labels ) override;
0065 
0066     void commit() override;
0067 
0068 protected:
0069     /**
0070      * You must reimplement this method to save statistics to the database.
0071      * @param changes holds a bitmask of fields changed since last doCommit() call.
0072      * Note that the changes already are visible in the track through getter methods.
0073      * Label changes will be indicated by Meta::valLabel field.
0074      *
0075      * Also note that m_changeLock will already be write-locked when this method
0076      * is called.
0077      */
0078     virtual void doCommit( const qint64 changes ) = 0;
0079 
0080     Meta::FieldHash m_statistics;
0081 
0082     /**
0083      * You must read-lock lock before reading, and write-lock before
0084      * writing, m_labels, m_metadata and m_statistics members.
0085      */
0086     mutable QReadWriteLock m_lock;
0087 
0088     /**
0089      * A bitmask containing changed fields. Only modify this in set* methods, and only
0090      * using bitwise-OR.
0091      */
0092     qint64 m_changes;
0093 };
0094 
0095 } // namespace StatSyncing
0096 
0097 #endif // STATSYNCING_SIMPLE_WRITABLE_TRACK_H