[Amarok] step 1 in my secret plan: provide a wrapper around

Leo Franchi lfranchi at kde.org
Sat Sep 26 20:34:43 CEST 2009


Hi max,

we're really really really deep in feature freeze. like, 2 days   
before final 2.2. tagging.

please revert :)

leo


On 26 Sep 2009, at 14:24, Maximilian Kossick wrote:

> commit c47fad1197995562169ec19325a1e76e39caca0b
> Author:     Maximilian Kossick <maximilian.kossick at googlemail.com>
> AuthorDate: Sat Sep 26 20:20:09 2009 +0200
> Commit:     Maximilian Kossick <maximilian.kossick at googlemail.com>
> CommitDate: Sat Sep 26 20:22:26 2009 +0200
>
>    step 1 in my secret plan: provide a wrapper around playlists that  
> makes them behave like collections
>    provide a querymaker to query collections and a  
> CollectionLocation that allows adding and removing of tracks.
>
> diff --git a/src/collection/support/PlaylistCollection.cpp b/src/ 
> collection/support/PlaylistCollection.cpp
> new file mode 100644
> index 0000000..6842bc0
> --- /dev/null
> +++ b/src/collection/support/PlaylistCollection.cpp
> @@ -0,0 +1,132 @@
> +/ 
> ****************************************************************************************
> + * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick at googlemail.com 
> >            *
> +  
> *                                                                                      *
> + * This program is free software; you can redistribute it and/or  
> modify it under        *
> + * the terms of the GNU General Public License as published by the  
> Free Software        *
> + * Foundation; either version 2 of the License, or (at your option)  
> any later           *
> + *  
> version 
> .                                                                             *
> +  
> *                                                                                      *
> + * This program is distributed in the hope that it will be useful,  
> but WITHOUT ANY      *
> + * WARRANTY; without even the implied warranty of MERCHANTABILITY  
> or FITNESS FOR A      *
> + * PARTICULAR PURPOSE. See the GNU General Public License for more  
> details.             *
> +  
> *                                                                                      *
> + * You should have received a copy of the GNU General Public  
> License along with         *
> + * this program.  If not, see <http://www.gnu.org/licenses/ 
> >.                           *
> +  
> ****************************************************************************************/
> +
> +#include "PlaylistCollection.h"
> +
> +#include "MemoryQueryMaker.h"
> +#include "PlaylistCollectionLocation.h"
> +#include "playlistmanager/PlaylistProvider.h"
> +
> +PlaylistCollection::PlaylistCollection( const Meta::PlaylistPtr  
> &playlist )
> +        : Amarok::Collection()
> +        , MemoryCollection()
> +        , Meta::PlaylistObserver()
> +        , m_playlist( playlist )
> +{
> +    subscribeTo( playlist );
> +    acquireWriteLock();
> +    foreach( const Meta::TrackPtr &track, m_playlist->tracks() )
> +    {
> +        insertTrack( track );
> +    }
> +    releaseLock();
> +}
> +
> +PlaylistCollection::~PlaylistCollection()
> +{
> +    //nothing to do?
> +}
> +
> +QueryMaker*
> +PlaylistCollection::queryMaker()
> +{
> +    return new MemoryQueryMaker( this, collectionId() );
> +}
> +
> +CollectionLocation*
> +PlaylistCollection::location() const
> +{
> +    return new PlaylistCollectionLocation( this );
> +}
> +
> +QString
> +PlaylistCollection::collectionId() const
> +{
> +    return m_playlist->name(); //add prefix?
> +}
> +
> +QString
> +PlaylistCollection::prettyName() const
> +{
> +    return m_playlist->prettyName();
> +}
> +
> +KIcon
> +PlaylistCollection::icon() const
> +{
> +    if( m_playlist->provider() )
> +        return m_playlist->provider()->icon();
> +    else
> +        return KIcon();
> +}
> +
> +void
> +PlaylistCollection::trackAdded( Meta::PlaylistPtr playlist,  
> Meta::TrackPtr track, int position )
> +{
> +    Q_UNUSED( position );
> +    if( playlist != m_playlist )
> +        return;
> +
> +    if( track )
> +    {
> +        acquireWriteLock();
> +        insertTrack( track );
> +        releaseLock();
> +    }
> +}
> +
> +void
> +PlaylistCollection::trackRemoved( Meta::PlaylistPtr playlist, int  
> position )
> +{
> +    //ok, what now? is the removed track still availabe at position
> +    //as this is not clear from the API, and apparently not used
> +    //anywhere, do it the hard way...
> +    acquireWriteLock();
> +    setTrackMap( TrackMap() );
> +    setArtistMap( ArtistMap() );
> +    setAlbumMap( AlbumMap() );
> +    setGenreMap( GenreMap() );
> +    setComposerMap( ComposerMap() );
> +    setYearMap( YearMap() );
> +    foreach( const Meta::TrackPtr &track, playlist->tracks() )
> +    {
> +        insertTrack( track );
> +    }
> +    releaseLock();
> +}
> +
> +//should be moved to MemoryCollection I guess
> +void
> +PlaylistCollection::insertTrack( const Meta::TrackPtr &track )
> +{
> +    addTrack( track );
> +    if( track->artist() )
> +        addArtist( track->artist() );
> +    if( track->album() )
> +        addAlbum( track->album() );
> +    if( track->composer() )
> +        addComposer( track->composer() );
> +    if( track->genre() )
> +        addGenre( track->genre() );
> +    if( track->year() )
> +        addYear( track->year() );
> +}
> +
> +Meta::PlaylistPtr
> +PlaylistCollection::playlist() const
> +{
> +    return m_playlist;
> +}
> diff --git a/src/collection/support/PlaylistCollection.h b/src/ 
> collection/support/PlaylistCollection.h
> new file mode 100644
> index 0000000..f92564a
> --- /dev/null
> +++ b/src/collection/support/PlaylistCollection.h
> @@ -0,0 +1,51 @@
> +/ 
> ****************************************************************************************
> + * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick at googlemail.com 
> >            *
> +  
> *                                                                                      *
> + * This program is free software; you can redistribute it and/or  
> modify it under        *
> + * the terms of the GNU General Public License as published by the  
> Free Software        *
> + * Foundation; either version 2 of the License, or (at your option)  
> any later           *
> + *  
> version 
> .                                                                             *
> +  
> *                                                                                      *
> + * This program is distributed in the hope that it will be useful,  
> but WITHOUT ANY      *
> + * WARRANTY; without even the implied warranty of MERCHANTABILITY  
> or FITNESS FOR A      *
> + * PARTICULAR PURPOSE. See the GNU General Public License for more  
> details.             *
> +  
> *                                                                                      *
> + * You should have received a copy of the GNU General Public  
> License along with         *
> + * this program.  If not, see <http://www.gnu.org/licenses/ 
> >.                           *
> +  
> ****************************************************************************************/
> +
> +#ifndef PLAYLISTCOLLECTION_H
> +#define PLAYLISTCOLLECTION_H
> +
> +#include "collection/Collection.h"
> +#include "MemoryCollection.h"
> +#include "meta/Playlist.h"
> +
> +/**
> +  * Utility class that wraps a playlist as collection and makes it  
> possible to
> +  * query the content of the playlist using QueryMaker.
> +  */
> +class PlaylistCollection : public Amarok::Collection, public  
> MemoryCollection, public Meta::PlaylistObserver
> +{
> +public:
> +    PlaylistCollection( const Meta::PlaylistPtr &playlist );
> +    virtual ~PlaylistCollection();
> +
> +    virtual QString collectionId() const;
> +    virtual QString prettyName() const;
> +    virtual QueryMaker* queryMaker();
> +    virtual CollectionLocation* location() const;
> +
> +    KIcon icon() const; //why is this pure virtual?
> +
> +    virtual void trackAdded( Meta::PlaylistPtr playlist,  
> Meta::TrackPtr track, int position ) = 0;
> +    virtual void trackRemoved( Meta::PlaylistPtr playlist, int  
> position ) = 0;
> +
> +    Meta::PlaylistPtr playlist() const;
> +private:
> +    void insertTrack( const Meta::TrackPtr &track );
> +
> +    Meta::PlaylistPtr m_playlist;
> +};
> +
> +#endif // PLAYLISTCOLLECTION_H
> diff --git a/src/collection/support/PlaylistCollectionLocation.cpp b/ 
> src/collection/support/PlaylistCollectionLocation.cpp
> new file mode 100644
> index 0000000..a5787d8
> --- /dev/null
> +++ b/src/collection/support/PlaylistCollectionLocation.cpp
> @@ -0,0 +1,71 @@
> +/ 
> ****************************************************************************************
> + * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick at googlemail.com 
> >            *
> +  
> *                                                                                      *
> + * This program is free software; you can redistribute it and/or  
> modify it under        *
> + * the terms of the GNU General Public License as published by the  
> Free Software        *
> + * Foundation; either version 2 of the License, or (at your option)  
> any later           *
> + *  
> version 
> .                                                                             *
> +  
> *                                                                                      *
> + * This program is distributed in the hope that it will be useful,  
> but WITHOUT ANY      *
> + * WARRANTY; without even the implied warranty of MERCHANTABILITY  
> or FITNESS FOR A      *
> + * PARTICULAR PURPOSE. See the GNU General Public License for more  
> details.             *
> +  
> *                                                                                      *
> + * You should have received a copy of the GNU General Public  
> License along with         *
> + * this program.  If not, see <http://www.gnu.org/licenses/ 
> >.                           *
> +  
> ****************************************************************************************/
> +
> +#include "PlaylistCollectionLocation.h"
> +
> +#include "meta/Playlist.h"
> +#include "PlaylistCollection.h"
> +
> +PlaylistCollectionLocation::PlaylistCollectionLocation( const  
> PlaylistCollection *collection )
> +        : CollectionLocation()
> +        , m_collection( collection )
> +{
> +}
> +
> +QString
> +PlaylistCollectionLocation::prettyLocation() const
> +{
> +    //think of something better
> +    return m_collection->prettyName();
> +}
> +
> +bool
> +PlaylistCollectionLocation::isWritable() const
> +{
> +    return true;
> +}
> +
> +bool
> +PlaylistCollectionLocation::remove( const Meta::TrackPtr &track )
> +{
> +    Meta::PlaylistPtr playlist = m_collection->playlist();
> +    int index = playlist->tracks().indexOf( track );
> +    if( index != -1 )
> +    {
> +        playlist->removeTrack( index );
> +    }
> +    //always succeed as we are not doing anything dangerous
> +    return true;
> +}
> +
> +void
> +PlaylistCollectionLocation::copyUrlsToCollection( const  
> QMap<Meta::TrackPtr, KUrl> &sources )
> +{
> +    Meta::PlaylistPtr playlist = m_collection->playlist();
> +    foreach( const Meta::TrackPtr &track, sources.keys() )
> +    {
> +        playlist->addTrack( track );
> +    }
> +}
> +
> +void
> +PlaylistCollectionLocation::removeUrlsFromCollection( const  
> Meta::TrackList &tracks )
> +{
> +    foreach( const Meta::TrackPtr &track, tracks )
> +    {
> +        remove( track );
> +    }
> +}
> diff --git a/src/collection/support/PlaylistCollectionLocation.h b/ 
> src/collection/support/PlaylistCollectionLocation.h
> new file mode 100644
> index 0000000..3dd1dde
> --- /dev/null
> +++ b/src/collection/support/PlaylistCollectionLocation.h
> @@ -0,0 +1,50 @@
> +/ 
> ****************************************************************************************
> + * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick at googlemail.com 
> >            *
> +  
> *                                                                                      *
> + * This program is free software; you can redistribute it and/or  
> modify it under        *
> + * the terms of the GNU General Public License as published by the  
> Free Software        *
> + * Foundation; either version 2 of the License, or (at your option)  
> any later           *
> + *  
> version 
> .                                                                             *
> +  
> *                                                                                      *
> + * This program is distributed in the hope that it will be useful,  
> but WITHOUT ANY      *
> + * WARRANTY; without even the implied warranty of MERCHANTABILITY  
> or FITNESS FOR A      *
> + * PARTICULAR PURPOSE. See the GNU General Public License for more  
> details.             *
> +  
> *                                                                                      *
> + * You should have received a copy of the GNU General Public  
> License along with         *
> + * this program.  If not, see <http://www.gnu.org/licenses/ 
> >.                           *
> +  
> ****************************************************************************************/
> +
> +#ifndef PLAYLISTCOLLECTIONLOCATION_H
> +#define PLAYLISTCOLLECTIONLOCATION_H
> +
> +#include "collection/CollectionLocation.h"
> +
> +class PlaylistCollection;
> +
> +/**
> +  * Utility class that allows modification of playlists using the  
> standard
> +  * CollectionLocation API.
> +  * caveat: although it is writable, moving tracks to this  
> "collection" does not
> +  * work, as it is just a wrapper around a playlist, which only  
> holds references to tracks
> +  *
> +  * It is safe though as this collection does not signal that it  
> has copied tracks successfully
> +  */
> +class PlaylistCollectionLocation : public CollectionLocation
> +{
> +public:
> +    PlaylistCollectionLocation( const PlaylistCollection  
> *collection );
> +
> +    QString prettyLocation() const;
> +    bool isWritable() const;
> +    bool remove( const Meta::TrackPtr &track );
> +
> +protected:
> +    void copyUrlsToCollection( const QMap<Meta::TrackPtr, KUrl>  
> &sources );
> +    //why is this called "removeUrls" if the argument are only  
> tracks?
> +    void removeUrlsFromCollection( const Meta::TrackList &tracks );
> +
> +private:
> +    const PlaylistCollection *m_collection;
> +};
> +
> +#endif // PLAYLISTCOLLECTIONLOCATION_H
>
>

---
Leo Franchi				(512) 775 5637
Tufts University 2010

lfranchi at kde.org
leonardo.franchi at tufts.edu



More information about the Amarok-devel mailing list