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

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0004  * Copyright (c) 2008 Casey Link <unnamedrambler@gmail.com>                             *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef SERVICECOLLECTION_H
0020 #define SERVICECOLLECTION_H
0021 
0022 #include "amarok_export.h"
0023 #include "core/collections/Collection.h"
0024 #include "core-impl/collections/support/MemoryCollection.h"
0025 #include "ServiceBase.h"
0026 
0027 #include <QIcon>
0028 
0029 #include <QtGlobal>
0030 #include <QSharedPointer>
0031 
0032 typedef QMap<int, Meta::TrackPtr> TrackIdMap;
0033 typedef QMap<int, Meta::ArtistPtr> ArtistIdMap;
0034 typedef QMap<int, Meta::AlbumPtr> AlbumIdMap;
0035 typedef QMap<int, Meta::GenrePtr> GenreIdMap;
0036 
0037 namespace Collections {
0038 
0039 /**
0040  *  This is a specialized collection that can be used by services who dynamically
0041  *  fetch their data from somewhere ( a web service, an external program, etc....)
0042  */
0043 
0044 class AMAROK_EXPORT ServiceCollection : public Collections::Collection
0045 {
0046     Q_OBJECT
0047     public:
0048         explicit ServiceCollection( ServiceBase * service = nullptr );
0049         ServiceCollection( ServiceBase * service, const QString &id, const QString &prettyName );
0050         ~ServiceCollection() override;
0051 
0052         Collections::QueryMaker* queryMaker() override;
0053 
0054         QString collectionId() const override;
0055         QString prettyName() const override;
0056         QIcon icon() const override { return QIcon::fromTheme(QStringLiteral("action-view-services-scripted-amarok")); }
0057 
0058         CollectionLocation* location() override;
0059 
0060         void emitUpdated();
0061 
0062         virtual QStringList query( const QString &query ) { Q_UNUSED( query ); return QStringList(); }
0063         virtual int insert( const QString &statement, const QString &table ) { Q_UNUSED( statement ); Q_UNUSED( table ); return 0; }
0064 
0065         virtual QString escape( const QString &text ) const { Q_UNUSED( text ); return QString(); }
0066 
0067 
0068         Meta::TrackPtr trackById( int id );
0069         Meta::AlbumPtr albumById( int id );
0070         Meta::ArtistPtr artistById( int id );
0071         Meta::GenrePtr genreById( int id );
0072 
0073         //Override some stuff to be able to handle id mappings
0074 
0075         void addTrack( const Meta::TrackPtr &trackPtr );
0076         void addArtist( const Meta::ArtistPtr &artistPtr );
0077         void addAlbum( const Meta::AlbumPtr &albumPtr );
0078         void addGenre( const Meta::GenrePtr &genrePtr );
0079 
0080         //TODO:
0081         //void setTrackMap( TrackMap map ) { m_trackMap = map; }
0082         //void setArtistMap( ArtistMap map ) { m_artistMap = map; }
0083         //void setAlbumMap( AlbumMap map ) { m_albumMap = map; }
0084         //void setGenreMap( GenreMap map ) { m_genreMap = map; }
0085 
0086         ServiceBase * service();
0087 
0088         //convenience functions for subclasses
0089         void acquireWriteLock() { m_mc->acquireWriteLock(); }
0090         void acquireReadLock() { m_mc->acquireReadLock(); }
0091         void releaseLock() { m_mc->releaseLock(); }
0092         GenreMap genreMap() const { return m_mc->genreMap(); }
0093         void setGenreMap( const GenreMap &map ) { m_mc->setGenreMap( map ); }
0094         ArtistMap artistMap() const { return m_mc->artistMap(); }
0095         void setArtistMap( const ArtistMap &map ) { m_mc->setArtistMap( map ); }
0096         TrackMap trackMap() const { return m_mc->trackMap(); }
0097         void setTrackMap( const TrackMap &map ) { m_mc->setTrackMap( map ); }
0098         AlbumMap albumMap() const { return m_mc->albumMap(); }
0099         void setAlbumMap( const AlbumMap &map ) { m_mc->setAlbumMap( map ); }
0100 
0101     private:
0102         ServiceBase * m_service;
0103         QSharedPointer<MemoryCollection> m_mc;
0104 
0105         QString m_collectionId;
0106         QString m_prettyName;
0107 
0108         TrackIdMap m_trackIdMap;
0109         ArtistIdMap m_artistIdMap;
0110         AlbumIdMap m_albumIdMap;
0111         GenreIdMap m_genreIdMap;
0112 };
0113 
0114 } //namespace Collections
0115 
0116 #endif