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_PROVIDER_H
0018 #define STATSYNCING_PROVIDER_H
0019 
0020 #include "amarok_export.h"
0021 #include "statsyncing/Track.h"
0022 
0023 #include <QIcon>
0024 #include <QMap>
0025 #include <QSet>
0026 #include <QString>
0027 #include <QVariantMap>
0028 #include <QWidget>
0029 
0030 namespace StatSyncing
0031 {
0032     /**
0033      * A widget class used for configuring providers.
0034      */
0035     class AMAROK_EXPORT ProviderConfigWidget : public QWidget
0036     {
0037         Q_OBJECT
0038 
0039         public:
0040             explicit ProviderConfigWidget( QWidget *parent, Qt::WindowFlags f = {} );
0041             ~ProviderConfigWidget() override;
0042 
0043             /**
0044              * Return a QVariantMap holding configuration for the provider. Types stored
0045              * in QVariantMap must be supported by @see KConfigGroup .
0046              */
0047             virtual QVariantMap config() const = 0;
0048     };
0049 
0050     /**
0051      * A class that can provide tracks for statistics synchronization. It can be backed
0052      * by local Amarok collections or by online services such as Last.fm.
0053      *
0054      * Instances of subclasses are guaranteed to be created in the main thread.
0055      * Providers are memory-managed as explicitly shared data, always use ProviderPtr
0056      * to store a reference to Provider.
0057      */
0058     class AMAROK_EXPORT Provider : public QObject
0059     {
0060         Q_OBJECT
0061 
0062         public:
0063             Provider();
0064             ~Provider() override;
0065 
0066             /**
0067              * Unique identifier for this collection; may be used as a key to store
0068              * configuration; must be thread-safe
0069              */
0070             virtual QString id() const = 0;
0071 
0072             /**
0073              * User-visible name of the provider; must be thread-safe
0074              */
0075             virtual QString prettyName() const = 0;
0076 
0077             /**
0078              * User-visible short localized description. Default implementation returns
0079              * an empty string.
0080              */
0081             virtual QString description() const;
0082 
0083             /**
0084              * Icon of this provider; must be thread-safe
0085              */
0086             virtual QIcon icon() const = 0;
0087 
0088             /**
0089              * Return true if this provider can be reconfigured after creation. Returns
0090              * false by default.
0091              */
0092             virtual bool isConfigurable() const;
0093 
0094             /**
0095              * Return a ProviderConfigWidget of configuration widget for this provider.
0096              * Returns a null pointer by default. Please note that Provider does *not*
0097              * retain ownership of this pointer, therefore should always return a new
0098              * instance.
0099              */
0100             virtual ProviderConfigWidget *configWidget();
0101 
0102             /**
0103              * Reconfigure the provider using configuration stored in @p config.
0104              * Does nothing by default.
0105              *
0106              * @param config the configuration
0107              */
0108             virtual void reconfigure( const QVariantMap &config );
0109 
0110             /**
0111              * Return binary OR of Meta::val* fields that this provider knows about its
0112              * tracks. Must include at least: Meta::valTitle, Meta::valArtist and
0113              * Meta::valAlbum. Optional fields: Meta::valComposer, Meta::valYear
0114              * Meta::valTrackNr and Meta::valDiscNr
0115              */
0116             virtual qint64 reliableTrackMetaData() const = 0;
0117 
0118             /**
0119              * Return binary OR of Meta::val* fields that this provider can write back
0120              * to tracks. Choose a combination of: Meta::valRating, valFirstPlayed,
0121              * valLastPlayed, valPlaycount, valLabel.
0122              */
0123             virtual qint64 writableTrackStatsData() const = 0;
0124 
0125             enum Preference {
0126                 Never, /// never synchronize automatically
0127                 NoByDefault, /// don't synchronize automatically by default
0128                 Ask, /// ask on first appearance whether to synchronize by default
0129                 YesByDefault /// enable auto syncing on first appearance without asking
0130                              /// intended only for Local Collection
0131             };
0132 
0133             /**
0134              * Return if this provider should participate in synchronization by
0135              * default even when the user does not actively add it. User can always
0136              * disable providers even if they are checked by default.
0137              */
0138             virtual Preference defaultPreference() = 0;
0139 
0140             /**
0141              * Return a set of track artist names that appear in this provider. Multiple
0142              * artists differing just in letter case are allowed, or rather mandated,
0143              * because @see artistTracks() method is case-sensitive.
0144              *
0145              * This method must be called in non-main thread and is allowed to block for
0146              * a longer time; it must be implemented in a reentrant manner.
0147              */
0148             virtual QSet<QString> artists() = 0;
0149 
0150             /**
0151              * Return a list of track delegates from (track) artist @param artistName that
0152              * appear in this provider; the matching should be performed CASE-SENSITIVELY
0153              * and should match the whole string, not just substring. If you have multiple
0154              * variants of the artist name differing just in letter case, you should
0155              * return all of the variants in @see artists().
0156              *
0157              * This method must be called in non-main thread and is allowed to block for
0158              * a longer time; it must be implemented in a reentrant manner.
0159              */
0160             virtual TrackList artistTracks( const QString &artistName ) = 0;
0161 
0162             /**
0163              * Write back statistics to the underlying storage for all updated tracks
0164              * managed by this provider that weren't yet saved. Default implementation
0165              * does nothing.
0166              *
0167              * Guaranteed to be (and must be) called from non-main thread. Can block for
0168              * a longer time.
0169              */
0170             virtual void commitTracks();
0171 
0172         Q_SIGNALS:
0173             /**
0174              * Emitted when some data such as prettyName() were updated.
0175              */
0176             void updated();
0177     };
0178 
0179     typedef QSharedPointer<Provider> ProviderPtr;
0180     typedef QList<ProviderPtr> ProviderPtrList;
0181     typedef QSet<ProviderPtr> ProviderPtrSet;
0182 
0183     /**
0184      * Container for a set of track frovider lists, one for each provider
0185      */
0186     typedef QMap<ProviderPtr, TrackList> PerProviderTrackList;
0187 
0188 } // namespace StatSyncing
0189 
0190 #endif // STATSYNCING_PROVIDER_H