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

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 #ifndef STATSYNCING_TRACK_H
0018 #define STATSYNCING_TRACK_H
0019 
0020 #include "amarok_export.h"
0021 #include "core/meta/Meta.h"
0022 
0023 #include "AmarokSharedPointer.h"
0024 
0025 #include <QDateTime>
0026 #include <QSet>
0027 #include <QSharedData>
0028 #include <QStringList>
0029 
0030 namespace Meta {
0031     typedef AmarokSharedPointer<Track> TrackPtr;
0032 }
0033 
0034 namespace StatSyncing
0035 {
0036     /**
0037      * Abstract representation of a track (either from Amarok or from scrobbling services).
0038      *
0039      * This class is used to perform track matching and synchronization. It must be
0040      * implemented in a thread-safe way. For optimal track matching, all string fields
0041      * should be trimmed of whitespace.
0042      *
0043      * Note: This will be probably morphed into Meta::Track someday, keep the interface
0044      * compatible as much as possible.
0045      */
0046     class AMAROK_EXPORT Track : public QSharedData
0047     {
0048         public:
0049             Track();
0050             virtual ~Track();
0051 
0052             /**
0053              * Get track title
0054              */
0055             virtual QString name() const = 0;
0056 
0057             /**
0058              * Get title of the album of this track
0059              */
0060             virtual QString album() const = 0;
0061 
0062             /**
0063              * Get track artist name
0064              */
0065             virtual QString artist() const = 0;
0066 
0067             /**
0068              * Get composer name; default implementation returns empty string that also
0069              * serves as a value for 'unknown'
0070              */
0071             virtual QString composer() const;
0072 
0073             /**
0074              * Get track release year, default implementation returns value of 0 that also
0075              * serves as a value for 'unknown'
0076              */
0077             virtual int year() const;
0078 
0079             /**
0080              * Get track number within its disc, default implementation returns value of 0
0081              * that also serves as a value for 'unknown'
0082              */
0083             virtual int trackNumber() const;
0084 
0085             /**
0086              * Get disc number, default implementation returns value of 0 that also serves
0087              * as a value for 'unknown'
0088              */
0089             virtual int discNumber() const;
0090 
0091             /**
0092              * Return true if 2 tracks delegated by this and @param other are equal based
0093              * on field mask @param fieldMask (binary OR of MetaValue.h values)
0094              */
0095             bool equals( const Track &other, qint64 fieldMask ) const;
0096 
0097             /**
0098              * Return true if this track delegate is considered smaller than @param other
0099              * based on field mask @param fieldMask (binary OR of MetaValue.h values)
0100              */
0101             bool lessThan( const Track &other, qint64 fieldMask ) const;
0102 
0103             /**
0104              * Get user-assigned rating on scale from 0 to 10; 0 means the track is not
0105              * rated - return this value if you don't know the rating
0106              */
0107             virtual int rating() const = 0;
0108             /**
0109              * Set user-assigned rating on scale from 0 to 10. Default implementation
0110              * does nothing.
0111              * @param rating the user-assigned rating
0112              */
0113             virtual void setRating( int rating );
0114 
0115             /**
0116              * Get date when the track was first played or invalid date if this is not
0117              * known or the track was not yet played
0118              */
0119             virtual QDateTime firstPlayed() const = 0;
0120             /**
0121              * Set date when the track was first played. Default implementation does
0122              * nothing.
0123              * @param firstPlayed the date the track was first played
0124              */
0125             virtual void setFirstPlayed( const QDateTime &firstPlayed );
0126 
0127             /**
0128              * Get date when the track was last played or invalid date if this is not
0129              * known or the track was not yet played
0130              */
0131             virtual QDateTime lastPlayed() const = 0;
0132             /**
0133              * Set date when the track was last played. Default implementation does
0134              * nothing.
0135              * @param lastPlayed the date the track was last played
0136              */
0137             virtual void setLastPlayed( const QDateTime &lastPlayed );
0138 
0139             /**
0140              * Get count of the track plays; return 0 in doubt
0141              */
0142             virtual int playCount() const = 0;
0143             /**
0144              * Return play count on device since it has been last connected to a computer.
0145              * This number is _already_ _included_ in playcount()!
0146              */
0147             virtual int recentPlayCount() const;
0148             /**
0149              * Set count of the track plays. Setting playcount must reset recent
0150              * playcount to 0. Default implementation does nothing.
0151              * @param playCount the count of the track plays
0152              */
0153             virtual void setPlayCount( int playCount );
0154 
0155             /**
0156              * Get user-assigned track labels or empty set if there are none
0157              */
0158             virtual QSet<QString> labels() const = 0;
0159             /**
0160              * Set user-assigned track labels. Default implementation does nothing.
0161              * @param labels the track labels
0162              */
0163             virtual void setLabels( const QSet<QString> &labels );
0164 
0165             /**
0166              * If this StatSyncing::Track represents a Meta::Track, this method returns a
0167              * pointer to it, otherwise it should return return a null pointer.
0168              *
0169              * This is used to enable drag in views and to enable scrobbling.
0170              *
0171              * Default implementation returns null pointer.
0172              */
0173             virtual Meta::TrackPtr metaTrack() const;
0174 
0175             /**
0176              * Write back statistics to the underlying storage. You must call this function
0177              * after calling any of the set* methods *and* you must you must call
0178              * Provider::commitTracks() at some later point. The implementation may decide
0179              * whether the actual writeback happens in set*, in commit() or in
0180              * Provider::commitTracks() . Default implementation does nothing.
0181              *
0182              * Guaranteed to be (and must be) called from non-main thread. Can block for
0183              * a longer time.
0184              */
0185             virtual void commit();
0186 
0187         private:
0188             Q_DISABLE_COPY(Track)
0189     };
0190 
0191     typedef AmarokSharedPointer<Track> TrackPtr;
0192     typedef QList<TrackPtr> TrackList;
0193 
0194     /**
0195      * Comparison function that compares track delegate pointer by pointed value.
0196      * Useful if you want to semantically sort TrackDelegateList using qSort()
0197      *
0198      * @p ControllingClass class name that implements static
0199      *      \::comparisonFields() method that returns binary OR of Meta::val* fields
0200      *      (as qint64) that should be used when comparing tracks.
0201      */
0202     template <class ControllingClass>
0203     bool trackDelegatePtrLessThan( const TrackPtr &first, const TrackPtr &second )
0204     {
0205         return first->lessThan( *second, ControllingClass::comparisonFields() );
0206     }
0207 
0208 } // namespace StatSyncing
0209 
0210 #endif // STATSYNCING_TRACK_H