File indexing completed on 2025-01-05 04:26:01

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 IPODPLAYLIST_H
0018 #define IPODPLAYLIST_H
0019 
0020 #include "core/playlists/Playlist.h"
0021 
0022 #include <QPointer>
0023 #include <QReadWriteLock>
0024 
0025 
0026 class IpodCollection;
0027 struct _Itdb_Playlist;
0028 typedef struct _Itdb_Playlist Itdb_Playlist;
0029 
0030 // we cannot use QMap<Track, int> because it doesn't preserve order
0031 typedef QPair<Meta::TrackPtr, int> TrackPosition;
0032 typedef QList<TrackPosition> TrackPositionList;
0033 
0034 /**
0035  * Represents playlist on the iPod. Takes ownership of the m_playlist pointer.
0036  */
0037 class IpodPlaylist : public Playlists::Playlist
0038 {
0039     public:
0040         enum Type {
0041             Normal,  // regular iPod playlist
0042             Stale,  // playlist containing stale iTunes database entries
0043             Orphaned,  // playlist containing track on iPod filesystem that are not it database
0044         };
0045 
0046         /**
0047          * Create Amarok iPod playlist out of existing itdb playlist
0048          */
0049         IpodPlaylist( Itdb_Playlist *ipodPlaylist, IpodCollection *collection );
0050 
0051         /**
0052          * Create new Amarok iPod playlist. Some @p tracks may not be in corresponding
0053          * iPod collection, these are copied to iPod (unless not matched by meta tags)
0054          *
0055          * @param tracks the tracks
0056          * @param name the playlist name
0057          * @param collection iPod collection
0058          * @param type whether this playlist is an ordinatory one or a kind of special
0059          */
0060         IpodPlaylist( const Meta::TrackList &tracks, const QString &name,
0061                       IpodCollection *collection, Type type = Normal );
0062 
0063         virtual ~IpodPlaylist();
0064 
0065         QUrl uidUrl() const override;
0066         QString name() const override;
0067         void setName( const QString &name ) override;
0068 
0069         Playlists::PlaylistProvider *provider() const override;
0070 
0071         int trackCount() const override;
0072         Meta::TrackList tracks() override;
0073         void addTrack( const Meta::TrackPtr &track, int position = -1 ) override;
0074         void removeTrack( int position ) override;
0075 
0076         // IpodPlaylist specific:
0077         Itdb_Playlist *itdbPlaylist();
0078 
0079         /**
0080          * Return tracks that should be copied to iPod and then added to this playlist,
0081          * clearing this map.
0082          */
0083         TrackPositionList takeTracksToCopy();
0084 
0085         /**
0086          * Return type of this playlist. Can be used to determine whether this one is
0087          * special or not.
0088          */
0089         Type type() const { return m_type; }
0090 
0091     private:
0092         Q_DISABLE_COPY( IpodPlaylist )
0093 
0094         /**
0095          * Copy m_tracksToCopy to iPod and them add them to this playlist. The actual call
0096          * to start copying tracks is deferred to next eventloop iteration to pickup
0097          * multiple successive addTrack() calls
0098          */
0099         void scheduleCopyAndInsert();
0100 
0101         /**
0102          * Does the dirty job of adding @p track to this playlist, both to m_tracks
0103          * and to underlying libgpoid playlist. @p position must be a valid position
0104          * otherwise this method asserts out.
0105          * @param track the track
0106          * @param position the position
0107          */
0108         void addIpodTrack( Meta::TrackPtr track, int position );
0109 
0110         Itdb_Playlist *m_playlist;
0111         mutable QReadWriteLock m_playlistLock;
0112         QPointer<IpodCollection> m_coll;
0113         Type m_type;
0114         Meta::TrackList m_tracks; // playlists tracks, in fact MemoryMeta::Track objects
0115         TrackPositionList m_tracksToCopy;
0116 };
0117 
0118 #endif // IPODPLAYLIST_H