File indexing completed on 2024-05-05 04:48:49
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Bart Cerneels <bart.cerneels@kde.org> * 0003 * Copyright (c) 2011 Lucas Lira Gomes <x8lucas8x@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #ifndef AMAROK_PLAYLISTMANAGER_H 0019 #define AMAROK_PLAYLISTMANAGER_H 0020 0021 #include "core/support/Amarok.h" 0022 #include "amarok_export.h" 0023 0024 #include "core/playlists/Playlist.h" 0025 #include "core/playlists/PlaylistProvider.h" 0026 0027 #include "SyncedPlaylist.h" 0028 #include "SyncRelationStorage.h" 0029 0030 #include <QMultiMap> 0031 #include <QList> 0032 0033 class KJob; 0034 class PlaylistManager; 0035 0036 namespace Playlists { 0037 class PlaylistFile; 0038 class PlaylistFileProvider; 0039 class UserPlaylistProvider; 0040 typedef AmarokSharedPointer<PlaylistFile> PlaylistFilePtr; 0041 } 0042 0043 namespace Podcasts { 0044 class PodcastProvider; 0045 } 0046 0047 namespace The { 0048 AMAROK_EXPORT PlaylistManager* playlistManager(); 0049 } 0050 0051 typedef QList<Playlists::PlaylistProvider *> PlaylistProviderList; 0052 0053 /** 0054 * Facility for managing PlaylistProviders registered by other 0055 * parts of the application, plugins and scripts. 0056 */ 0057 class AMAROK_EXPORT PlaylistManager : public QObject 0058 { 0059 Q_OBJECT 0060 0061 public: 0062 enum PlaylistCategory 0063 { 0064 UserPlaylist = 1, 0065 PodcastChannel 0066 }; 0067 Q_ENUM( PlaylistCategory ) 0068 0069 static PlaylistManager *instance(); 0070 static void destroy(); 0071 0072 /** 0073 * @returns all available categories registered at that moment 0074 */ 0075 QList<int> availableCategories() { return m_providerMap.uniqueKeys(); } 0076 0077 /** 0078 * returns playlists of a certain category from all registered PlaylistProviders 0079 */ 0080 Playlists::PlaylistList playlistsOfCategory( int playlistCategory ); 0081 0082 /** 0083 * returns all PlaylistProviders that provider a certain playlist category. 0084 **/ 0085 PlaylistProviderList providersForCategory( int playlistCategory ); 0086 0087 /** 0088 * Add a PlaylistProvider that contains Playlists of a category defined 0089 * in the PlaylistCategory enum. 0090 * @arg provider a PlaylistProvider 0091 * @arg category a default Category from the PlaylistManager::PlaylistCategory enum or a 0092 * custom one registered before with registerCustomCategory. 0093 */ 0094 void addProvider( Playlists::PlaylistProvider *provider, int category ); 0095 0096 /** 0097 * Do all the work necessary to sync playlists, including the 0098 * SyncedPlaylist creation and more. This sync is persistent. 0099 * @arg playlist of the master playlist 0100 * @arg playlist of the slave playlist 0101 */ 0102 void setupSync( const Playlists::PlaylistPtr master, const Playlists::PlaylistPtr slave ); 0103 0104 /** 0105 * Remove a PlaylistProvider. 0106 * @arg provider a PlaylistProvider 0107 */ 0108 void removeProvider( Playlists::PlaylistProvider *provider ); 0109 0110 Playlists::PlaylistProvider *playlistProvider( int category, QString name ); 0111 0112 /** 0113 * Saves a list of tracks to a new playlist. Used in the Playlist save button. 0114 * @arg tracks list of tracks to save 0115 * @arg name name of playlist to save 0116 * @arg toProvider If 0 (default) will save to the default UserPlaylistProvider 0117 * @arg editName whether to edit new playlist name as soon as it is created 0118 * @see defaultUserPlaylists 0119 */ 0120 bool save( Meta::TrackList tracks, const QString &name = QString(), 0121 Playlists::PlaylistProvider *toProvider = nullptr, bool editName = true ); 0122 0123 /** 0124 * Saves a playlist from a file to the database. 0125 * @param fromLocation Saved playlist file to load 0126 */ 0127 bool import( const QUrl &fromLocation ); 0128 0129 /** 0130 * Initiates renaming of playlists. Can Focus Saved Playlists and 0131 * initiate inline rename. 0132 * 0133 * @param playlist the playlists. 0134 */ 0135 void rename( Playlists::PlaylistPtr playlist ); 0136 0137 /** 0138 * Rename @p playlist to @p newName, return true if renaming was successful, 0139 * false otherwise. 0140 * 0141 * @param playlist the playlist. 0142 * @param newName the new name. 0143 */ 0144 bool rename( Playlists::PlaylistPtr playlist, const QString &newName ); 0145 0146 bool deletePlaylists( Playlists::PlaylistList playlistlist ); 0147 0148 Podcasts::PodcastProvider *defaultPodcasts() { return m_defaultPodcastProvider; } 0149 Playlists::UserPlaylistProvider *defaultUserPlaylists() 0150 { return m_defaultUserPlaylistProvider; } 0151 0152 /** 0153 * Retrieves the provider owning the given playlist. 0154 * Will only return multiple providers if this is a synced playlist 0155 * @arg playlist the playlist whose provider we want 0156 */ 0157 QList<Playlists::PlaylistProvider*> 0158 getProvidersForPlaylist( const Playlists::PlaylistPtr playlist ); 0159 0160 /** 0161 * Checks if the provider to whom this playlist belongs supports writing 0162 * @arg playlist the playlist we are testing for writability 0163 * @return whether or not the playlist is writable 0164 */ 0165 0166 bool isWritable( const Playlists::PlaylistPtr &playlist ); 0167 0168 void completePodcastDownloads(); 0169 0170 Q_SIGNALS: 0171 void updated( int category ); 0172 void categoryAdded( int category ); 0173 void providerAdded( Playlists::PlaylistProvider *provider, int category ); 0174 void providerRemoved( Playlists::PlaylistProvider *provider, int category ); 0175 void playlistAdded( Playlists::PlaylistPtr playlist, int category ); 0176 void playlistRemoved( Playlists::PlaylistPtr playlist, int category ); 0177 void playlistUpdated( Playlists::PlaylistPtr playlist, int category ); 0178 0179 void renamePlaylist( Playlists::PlaylistPtr playlist ); 0180 0181 private Q_SLOTS: 0182 void slotUpdated(); 0183 void slotPlaylistAdded( Playlists::PlaylistPtr playlist ); 0184 void slotPlaylistRemoved( Playlists::PlaylistPtr playlist ); 0185 void slotSyncNeeded(); 0186 0187 private: 0188 void loadPlaylists( Playlists::PlaylistProvider *provider, int category ); 0189 void removePlaylists( Playlists::PlaylistProvider *provider ); 0190 0191 void addPlaylist( Playlists::PlaylistPtr playlist, int category ); 0192 void removePlaylist( Playlists::PlaylistPtr playlist, int category ); 0193 0194 static PlaylistManager *s_instance; 0195 PlaylistManager(); 0196 ~PlaylistManager() override; 0197 0198 SyncRelationStorage *m_syncRelStore; 0199 QList<SyncedPlaylistPtr> m_syncNeeded; 0200 0201 bool hasToSync( Playlists::PlaylistPtr master, Playlists::PlaylistPtr slave ); 0202 0203 Podcasts::PodcastProvider *m_defaultPodcastProvider; 0204 Playlists::UserPlaylistProvider *m_defaultUserPlaylistProvider; 0205 Playlists::PlaylistFileProvider *m_playlistFileProvider; 0206 0207 QMultiMap<int, Playlists::PlaylistProvider*> m_providerMap; //Map PlaylistCategories to providers 0208 QMultiMap<int, Playlists::PlaylistPtr> m_playlistMap; 0209 QMultiMap<SyncedPlaylistPtr, Playlists::PlaylistPtr> m_syncedPlaylistMap; 0210 0211 QMap<int, QString> m_customCategories; 0212 0213 QMap<KJob *, Playlists::PlaylistFilePtr> m_downloadJobMap; 0214 }; 0215 0216 Q_DECLARE_METATYPE( PlaylistProviderList ) 0217 0218 #endif