Archive / / / / / Playlist.cc
2008-10-21 13:56:51 UTC
previous next
/* Echo Media Player * Copyright (C) 2008 Shane O'Connell * * [ The original file includes a copyright header in this location describing * the file as being released under the terms of the GNU General Public * License. It has been removed in order to display the file as part of the * archive. ] */ #include "Playlist.h" #include "Database.h" #include <string> #include <cassert> #include <memory> #include <iostream> using namespace std; Playlist::Playlist() : db(*Database::m_instance) { assert(Database::m_instance); db.run("INSERT INTO MemoryPlaylists (PlaylistID, PlaylistName) VALUES(NULL, 'Untitled')"); m_playlist_id = db.last_rowid(); Glib::ustring sql; // 'remove' sql commands db.prepare(m_remove1_stmt, Glib::ustring::compose( "DELETE FROM MemoryPlaylistTracks " "WHERE PlaylistID = %1 AND PlaylistIndex = ?", m_playlist_id)); db.prepare(m_remove2_stmt, Glib::ustring::compose( "UPDATE MemoryPlaylistTracks SET PlaylistIndex = (PlaylistIndex - 1) " "WHERE PlaylistID = %1 AND PlaylistIndex >= ?", m_playlist_id)); // 'insert' sql commands db.prepare(m_insert1_stmt, Glib::ustring::compose( "UPDATE MemoryPlaylistTracks SET PlaylistIndex = (PlaylistIndex + 1) " "WHERE PlaylistID = %1 AND PlaylistIndex >= ?", m_playlist_id)); db.prepare(m_insert2_stmt, Glib::ustring::compose( "INSERT INTO MemoryPlaylistTracks (PlaylistID, PlaylistIndex, ArtistName, AlbumName, " "AlbumReleaseDate, TrackName, TrackNumber, TrackURI) " "VALUES(%1, ?, ?, ?, ?, ?, ?, ?)", m_playlist_id)); /* "CREATE TEMPORARY TABLE MemoryPlaylistTracks (PlaylistID INTEGER, " "PlaylistOrder INTEGER, ArtistName TEXT, AlbumName TEXT, AlbumReleaseDate INTEGER, " "TrackName TEXT, TrackNumber INTEGER, TrackURI TEXT)" */ // 'get_track' sql command db.prepare(m_get_track_stmt, Glib::ustring::compose( "SELECT ArtistName, AlbumName, AlbumReleaseDate, TrackName, " "TrackNumber, TrackURI FROM MemoryPlaylistTracks " "WHERE PlaylistID = %1 ORDER BY PlaylistIndex LIMIT %2 OFFSET ?", m_playlist_id, m_track_cache.size())); m_size = 0; } void Playlist::insert_track(int index, TrackRef track) { assert(index >= 0); assert(index <= get_num_tracks()); // '<=' because could be inserting at the end db.run("BEGIN TRANSACTION"); m_insert1_stmt.bind(1, index); m_insert1_stmt.step(); m_insert1_stmt.reset(); /* "INSERT INTO MemoryPlaylistTracks (PlaylistID, PlaylistIndex, ArtistName, AlbumName, " "AlbumReleaseDate, TrackName, TrackNumber, TrackURI) " "VALUES(%1, ?, ?, ?, ?, ?, ?, ?)" */ m_insert2_stmt.bind(1, index); m_insert2_stmt.bind(2, track->get_artist()); m_insert2_stmt.bind(3, track->get_album()); m_insert2_stmt.bind(4, 0); m_insert2_stmt.bind(5, track->get_title()); m_insert2_stmt.bind(6, track->get_track_number()); m_insert2_stmt.bind(7, track->get_uri()); m_insert2_stmt.step(); m_insert2_stmt.reset(); db.run("END TRANSACTION"); m_size++; invalidate_cache(); signal_track_inserted().emit(index); } void Playlist::remove_track(int index) { assert(index >= 0); assert(index < get_num_tracks()); db.run("BEGIN TRANSACTION"); m_remove1_stmt.bind(1, index); m_remove1_stmt.step(); m_remove1_stmt.reset(); m_remove2_stmt.bind(1, index); m_remove2_stmt.step(); m_remove2_stmt.reset(); db.run("END TRANSACTION"); invalidate_cache(); signal_track_removed().emit(index); } void Playlist::move_track(int old_index, int new_index) { assert(old_index >= 0); assert(old_index < get_num_tracks()); assert(new_index >= 0); assert(new_index < get_num_tracks()); TrackRef track_info = get_track(old_index); remove_track(old_index); if (old_index < new_index) new_index--; // Adjust for the removed track insert_track(new_index, track_info); } int Playlist::get_num_tracks() const { return m_size; }