Skip to content

Commit

Permalink
Only include real playlists to the playlist model
Browse files Browse the repository at this point in the history
There are for example folder markers in the playlists returned by libspotify.
We should only take into account those whose type is SP_PLAYLIST_TYPE_PLAYLIST.

Also fixes the playlist model population: the indexes weren't handled quite
correctly, they were confused with the starred tracks list.

Fixed the PlaylistModel::insertRows(). It now works as intended by Qt.
Removed needless members from PlaylistModel::Entry and added a constructor.
  • Loading branch information
Aki Koskinen committed Jul 9, 2012
1 parent 20f91f9 commit 2d04e22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
20 changes: 17 additions & 3 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,18 +875,32 @@ void MainWindow::fillPlaylistModel()
m_pc = sp_session_playlistcontainer(m_session);
sp_playlistcontainer_add_callbacks(m_pc, &SpotifyPlaylistContainer::spotifyCallbacks, this);
}
const int numPlaylists = sp_playlistcontainer_num_playlists(m_pc);

int numPlaylists = sp_playlistcontainer_num_playlists(m_pc);

// Collect the playlists that are of type "playlist", discard others
QList<sp_playlist*> playLists;
playLists.reserve(numPlaylists);
for (int i = 0; i < numPlaylists; ++i) {
if (sp_playlistcontainer_playlist_type(m_pc, i) == SP_PLAYLIST_TYPE_PLAYLIST) {
playLists.append(sp_playlistcontainer_playlist(m_pc, i));
}
}
numPlaylists = 1 + playLists.count(); // 1 place needed for the starred tracks list

m_playlistModel->removeRows(0, m_playlistModel->rowCount());
m_playlistModel->insertRows(0, numPlaylists);

int currRow = -1;

// Add the special playlist for starred tracks
{
sp_playlist *pl = sp_session_starred_create(m_session);
Q_ASSERT(pl);
if (pl == m_currentPlaylist) {
currRow = numPlaylists;
currRow = 0;
}

sp_playlist_add_callbacks(pl, &SpotifyPlaylists::spotifyCallbacks, NULL);
const QModelIndex &index = m_playlistModel->index(0);
m_playlistModel->setData(index, QChar(0x2605) + i18n("Starred tracks"));
Expand All @@ -896,7 +910,7 @@ void MainWindow::fillPlaylistModel()
static QList<sp_playlist*> playlistsWithCallbacksSet;

for (int i = 1; i < numPlaylists; ++i) {
sp_playlist *pl = sp_playlistcontainer_playlist(m_pc, i);
sp_playlist *pl = playLists.at(i - 1);
if (pl == m_currentPlaylist) {
currRow = i;
}
Expand Down
5 changes: 3 additions & 2 deletions playlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ PlaylistModel::~PlaylistModel()

bool PlaylistModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row < 0 || row > m_playLists.count() || count <= 0) {
if (row < 0 || count <= 0) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
m_playLists.reserve(m_playLists.count() + count);
for (int i = row; i < row + count; ++i) {
m_playLists << Entry();
m_playLists.insert(i, Entry());
}
endInsertRows();
return true;
Expand Down
6 changes: 4 additions & 2 deletions playlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class PlaylistModel

private:
struct Entry {
Entry() :
m_playlist(0)
{}

QString m_title;
QString m_owner;
bool m_collaborative;
sp_playlist *m_playlist;
};
QList<Entry> m_playLists;
Expand Down

0 comments on commit 2d04e22

Please sign in to comment.