Debian Codegraph @

src/playlist/navigators/AlbumNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2008 Nikolaj Hald Nielsen <[email protected]> *
3 * Copyright (c) 2008 Soren Harward <[email protected]> *
4 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
5 * Copyright (c) 2010 Nanno Langstraat <[email protected]> *
6 * *
7 * This program is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) version 3 or *
10 * any later version accepted by the membership of KDE e.V. (or its successor approved *
11 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of *
12 * version 3 of the license. *
13 * *
14 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License along with *
19 * this program. If not, see <http://www.gnu.org/licenses/>. *
20 ****************************************************************************************/
21
22 #define DEBUG_PREFIX "Playlist::AlbumNavigator"
23
24 #include "AlbumNavigator.h"
25
26 #include "playlist/PlaylistModelStack.h"
27
28 #include "core/support/Debug.h"
29 #include "core/meta/Meta.h"
30
31 #include <algorithm>
32
33
34 void
35 Playlist::AlbumNavigator::notifyItemsInserted( const QSet<quint64> &insertedItems )
36 {
37 DEBUG_BLOCK
38
39 QList<AlbumId> oldAlbumList = m_itemsPerAlbum.keys();
40 QSet<AlbumId> oldAlbums(oldAlbumList.begin(), oldAlbumList.end());
41 QSet<AlbumId> modifiedAlbums;
42
43 for( quint64 insertedItem : insertedItems )
44 {
45 AlbumId album = albumForItem( insertedItem );
46 m_itemsPerAlbum[album].append( insertedItem ); // conveniently creates an empty list if none exists
47 modifiedAlbums.insert( album );
48 }
49
50 for( AlbumId album : modifiedAlbums )
51 std::stable_sort( m_itemsPerAlbum[album].begin(), m_itemsPerAlbum[album].end(), itemLessThan );
52
53 notifyAlbumsInserted( ( modifiedAlbums - oldAlbums ).values() );
54 }
55
56 void
57 Playlist::AlbumNavigator::notifyItemsRemoved( const QSet<quint64> &removedItems )
58 {
59 DEBUG_BLOCK
60
61 for( quint64 removedItem : removedItems )
62 {
63 AlbumId album = albumForItem( removedItem );
64
65 // Try not to lose our position in the playlist: if we're losing 'currentItem()', substitute the next "planned item".
66 if ( removedItem == currentItem() )
67 {
68 planOne(); // Could select 'removedItem' again; in that case our parent will 'setCurrentItem( 0 )'.
69 if ( !m_plannedItems.isEmpty() )
70 setCurrentItem( m_plannedItems.first() );
71 }
72
73 m_plannedItems.removeAll( removedItem ); // We only need to do this because we call 'planOne()' in this loop.
74
75 // Maintain 'm_itemsPerAlbum'
76 ItemList itemsInAlbum = m_itemsPerAlbum.value( album );
77 itemsInAlbum.removeAll( removedItem );
78 if ( itemsInAlbum.isEmpty() )
79 {
80 m_itemsPerAlbum.remove( album );
81 m_plannedAlbums.removeAll( album );
82 }
83 else
84 m_itemsPerAlbum.insert( album, itemsInAlbum ); // Replace old list with the edited copy.
85
86 // Maintain 'm_albumForItem'.
87 m_albumForItem.remove( removedItem );
88 }
89 }
90
91 Playlist::AlbumNavigator::AlbumId
92 Playlist::AlbumNavigator::albumForItem( const quint64 &item )
93 {
94 if ( m_albumForItem.contains( item ) )
95 return m_albumForItem.value( item );
96 else
97 {
98 AlbumId album;
99
100 Meta::TrackPtr track = m_model->trackForId( item );
101 if ( track )
102 {
103 Meta::AlbumPtr metaAlbum = track->album();
104 if ( metaAlbum )
105 album = metaAlbum->name(); // See comment for 'typedef AlbumId'.
106 }
107
108 m_albumForItem.insert( item, album );
109 return album;
110 }
111 }
112
113 bool
114 Playlist::AlbumNavigator::itemLessThan( const quint64 &item1, const quint64 &item2 )
115 {
116 // Somewhat nasty to hard-code the model like this, but 'qStableSort()' doesn't give us a way to pass 'm_model'.
117
118 AbstractModel *model = The::playlist();
119
120 Meta::TrackPtr track1 = model->trackForId( item1 );
121 Meta::TrackPtr track2 = model->trackForId( item2 );
122
123 return Meta::Track::lessThan( track1, track2 );
124 }