1 /****************************************************************************************
2  * Copyright (c) 2009-2010 Bart Cerneels <bart.cerneels@kde.org>                        *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #ifndef AMAROK_PLAYLISTBROWSERMODEL_H
18 #define AMAROK_PLAYLISTBROWSERMODEL_H
19 
20 #include "core/playlists/Playlist.h"
21 #include "core/playlists/PlaylistProvider.h"
22 
23 #include <QAbstractItemModel>
24 #include <QAction>
25 //Playlist & Track index differentiator macros
26 //QModelIndex::intenalId() is a qint64 to support 64-bit pointers in a union with the ID
27 #define TRACK_MASK (0x1<<31)
28 #define IS_TRACK(x) ((x.internalId()) & (TRACK_MASK))?true:false
29 #define SET_TRACK_MASK(x) ((x) | (TRACK_MASK))
30 #define REMOVE_TRACK_MASK(x) ((x) & ~(TRACK_MASK))
31 
32 class QAction;
33 
Q_DECLARE_METATYPE(QActionList)34 Q_DECLARE_METATYPE( QActionList )
35 
36 namespace PlaylistBrowserNS {
37 
38 /**
39     @author Bart Cerneels <bart.cerneels@kde.org>
40 */
41 class PlaylistBrowserModel : public QAbstractItemModel, public Playlists::PlaylistObserver
42 {
43     Q_OBJECT
44     public:
45         enum {
46             PlaylistItemColumn = 0, //Data form the playlist itself or it's tracks
47             LabelColumn, //Data from the labels. Can be used as foldernames in the view.
48             ProviderColumn, //data from the PlaylistProvider
49             CustomColumOffset //first column that can be used by subclasses for their own data
50         };
51 
52         enum
53         {
54             ProviderRole = Qt::UserRole + 21, // pointer to associated PlaylistProvider
55             PlaylistRole = Qt::UserRole + 22, // PlaylistPtr for associated playlist or null
56             TrackRole = Qt::UserRole + 23, // TrackPtr for associated track or null
57             EpisodeIsNewRole = Qt::UserRole + 24, // for podcast episodes, supports setting, type: bool
58             CustomRoleOffset = Qt::UserRole + 25 //first role that can be used by subclasses for their own data
59         };
60 
61         PlaylistBrowserModel( int PlaylistCategory );
62         ~PlaylistBrowserModel() override {}
63 
64         /* QAbstractItemModel methods */
65         QVariant data( const QModelIndex &index, int role ) const override;
66         bool setData( const QModelIndex &idx, const QVariant &value, int role ) override;
67         Qt::ItemFlags flags( const QModelIndex &index ) const override;
68         QVariant headerData( int section, Qt::Orientation orientation,
69                             int role = Qt::DisplayRole ) const override;
70         QModelIndex index( int row, int column,
71                         const QModelIndex &parent = QModelIndex() ) const override;
72         QModelIndex parent( const QModelIndex &index ) const override;
73 
74         bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
75         int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
76         int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
77 
78         bool canFetchMore( const QModelIndex &parent ) const override;
79         void fetchMore( const QModelIndex &parent ) override;
80 
81         QStringList mimeTypes() const override;
82         QMimeData* mimeData( const QModelIndexList &indexes ) const override;
83         bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row,
84                                    int column, const QModelIndex &parent ) override;
85 
86         /* Playlists::PlaylistObserver methods */
87         void metadataChanged( const Playlists::PlaylistPtr &playlist ) override;
88         void trackAdded( const Playlists::PlaylistPtr &playlist, const Meta::TrackPtr &track, int position ) override;
89         void trackRemoved( const Playlists::PlaylistPtr &playlist, int position ) override;
90 
91     public Q_SLOTS:
92         void slotRenamePlaylist( Playlists::PlaylistPtr playlist );
93         void slotUpdate( int category );
94 
95     Q_SIGNALS:
96         void renameIndex( const QModelIndex &index );
97 
98     protected:
99         virtual Playlists::PlaylistList loadPlaylists();
100 
101         Meta::TrackList tracksFromIndexes( const QModelIndexList &list ) const;
102         Meta::TrackPtr trackFromIndex( const QModelIndex &index ) const;
103         Playlists::PlaylistPtr playlistFromIndex( const QModelIndex &index ) const;
104         Playlists::PlaylistProvider *providerForIndex( const QModelIndex &index ) const;
105 
106         Playlists::PlaylistList m_playlists;
107         QMap<Playlists::PlaylistPtr,int> m_playlistTracksLoaded;
108 
109         Playlists::PlaylistProvider *getProviderByName( const QString &name );
110 
111     private Q_SLOTS:
112         void slotPlaylistAdded( Playlists::PlaylistPtr playlist, int category );
113         void slotPlaylistRemoved( Playlists::PlaylistPtr playlist, int category );
114         void slotPlaylistUpdated( Playlists::PlaylistPtr playlist, int category );
115 
116     private:
117         int m_playlistCategory;
118 };
119 
120 }
121 
122 #endif //AMAROK_PLAYLISTBROWSERMODEL_H
123