Skip to content

Commit

Permalink
Playlists: fix table update after track purge, enable RemoveFromDisk …
Browse files Browse the repository at this point in the history
…in menu
  • Loading branch information
ronso0 committed Apr 18, 2024
1 parent 95bda63 commit 75abb60
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
52 changes: 35 additions & 17 deletions src/library/dao/playlistdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ bool PlaylistDAO::removeTracksFromPlaylist(int playlistId, int startIndex) {
return false;
}
transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
emit tracksRemoved(QSet<int>{playlistId});
return true;
}

Expand Down Expand Up @@ -440,7 +441,8 @@ bool PlaylistDAO::appendTracksToPlaylist(const QList<TrackId>& trackIds, const i
// TODO(XXX) don't emit if the track didn't add successfully.
emit trackAdded(playlistId, trackId, insertPosition++);
}
emit tracksChanged(QSet<int>{playlistId});
emit tracksAdded(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
return true;
}

Expand Down Expand Up @@ -551,7 +553,7 @@ bool PlaylistDAO::isHidden(const int playlistId) const {

void PlaylistDAO::removeHiddenTracks(const int playlistId) {
ScopedTransaction transaction(m_database);
// This query deletes all tracks marked as deleted and all
// This query deletes all tracks marked as hidden and all
// phantom track_ids with no match in the library table
QSqlQuery query(m_database);
query.prepare(QStringLiteral(
Expand All @@ -576,14 +578,16 @@ void PlaylistDAO::removeHiddenTracks(const int playlistId) {
}

transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
emit tracksRemoved(QSet<int>{playlistId});
}

void PlaylistDAO::removeTracksFromPlaylistById(int playlistId, TrackId trackId) {
ScopedTransaction transaction(m_database);
removeTracksFromPlaylistByIdInner(playlistId, trackId);
transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
emit tracksRemoved(QSet<int>{playlistId});
}

void PlaylistDAO::removeTracksFromPlaylistByIdInner(int playlistId, TrackId trackId) {
Expand Down Expand Up @@ -612,7 +616,8 @@ void PlaylistDAO::removeTrackFromPlaylist(int playlistId, int position) {
ScopedTransaction transaction(m_database);
removeTracksFromPlaylistInner(playlistId, position);
transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
emit tracksRemoved(QSet<int>{playlistId});
}

void PlaylistDAO::removeTracksFromPlaylist(int playlistId, const QList<int>& positions) {
Expand All @@ -627,7 +632,8 @@ void PlaylistDAO::removeTracksFromPlaylist(int playlistId, const QList<int>& pos
removeTracksFromPlaylistInner(playlistId, position);
}
transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
emit tracksRemoved(QSet<int>{playlistId});
}

void PlaylistDAO::removeTracksFromPlaylistInner(int playlistId, int position) {
Expand Down Expand Up @@ -722,7 +728,8 @@ bool PlaylistDAO::insertTrackIntoPlaylist(TrackId trackId, const int playlistId,

m_playlistsTrackIsIn.insert(trackId, playlistId);
emit trackAdded(playlistId, trackId, position);
emit tracksChanged(QSet<int>{playlistId});
emit tracksAdded(QSet<int>{playlistId});
emit playlistContentChanged(QSet<int>{playlistId});
return true;
}

Expand All @@ -733,7 +740,7 @@ int PlaylistDAO::insertTracksIntoPlaylist(const QList<TrackId>& trackIds,
return 0;
}

int tracksAdded = 0;
int numTracksAdded = 0;
ScopedTransaction transaction(m_database);

int max_position = getMaxPosition(playlistId) + 1;
Expand Down Expand Up @@ -777,7 +784,7 @@ int PlaylistDAO::insertTracksIntoPlaylist(const QList<TrackId>& trackIds,

// Increment the insert position for the track.
++insertPositon;
++tracksAdded;
++numTracksAdded;
}

transaction.commit();
Expand All @@ -788,8 +795,8 @@ int PlaylistDAO::insertTracksIntoPlaylist(const QList<TrackId>& trackIds,
// TODO(XXX) The position is wrong if any track failed to insert.
emit trackAdded(playlistId, trackId, insertPositon++);
}
emit tracksChanged(QSet<int>{playlistId});
return tracksAdded;
emit tracksAdded(QSet<int>{playlistId});
return numTracksAdded;
}

void PlaylistDAO::addPlaylistToAutoDJQueue(const int playlistId, AutoDJSendLoc loc) {
Expand Down Expand Up @@ -912,7 +919,8 @@ bool PlaylistDAO::copyPlaylistTracks(const int sourcePlaylistID, const int targe
m_playlistsTrackIsIn.insert(copiedTrackId, targetPlaylistID);
emit trackAdded(targetPlaylistID, copiedTrackId, copiedPosition);
}
emit tracksChanged(QSet<int>{targetPlaylistID});
emit tracksAdded(QSet<int>{targetPlaylistID});
emit playlistContentChanged(QSet<int>{targetPlaylistID});
return true;
}

Expand All @@ -936,7 +944,7 @@ int PlaylistDAO::getMaxPosition(const int playlistId) const {
return position;
}

void PlaylistDAO::removeTracksFromPlaylists(const QList<TrackId>& trackIds) {
void PlaylistDAO::removeTracksFromPlaylists(const QList<TrackId>& trackIds, bool purged) {
// copy the hash, because there is no guarantee that "it" is valid after remove
QMultiHash<TrackId, int> playlistsTrackIsInCopy = m_playlistsTrackIsIn;
QSet<int> playlistIds;
Expand All @@ -959,7 +967,17 @@ void PlaylistDAO::removeTracksFromPlaylists(const QList<TrackId>& trackIds) {
}
transaction.commit();

emit tracksChanged(playlistIds);
// update the sidebar
emit playlistContentChanged(playlistIds);
// If this is called by TrackCollection::purgeTracks() it will call
// TrackDAO::afterPurgingTracks() afterwards which will enforce a model update
// (select()), so we don't need to signal PlaylistTableModel to select(),
// Also, this double select() can cause issues in BaseTrackCache.
// See https://github.com/mixxxdj/mixxx/issues/13111
if (purged) {
return;
}
emit tracksRemoved(playlistIds);
}

int PlaylistDAO::tracksInPlaylist(const int playlistId) const {
Expand Down Expand Up @@ -1045,7 +1063,7 @@ void PlaylistDAO::moveTrack(const int playlistId, const int oldPosition, const i
qDebug() << query.lastError();
}

emit tracksChanged(QSet<int>{playlistId});
emit tracksMoved(QSet<int>{playlistId});
}

void PlaylistDAO::searchForDuplicateTrack(const int fromPosition,
Expand Down Expand Up @@ -1251,7 +1269,7 @@ void PlaylistDAO::shuffleTracks(const int playlistId,
}

transaction.commit();
emit tracksChanged(QSet<int>{playlistId});
emit tracksMoved(QSet<int>{playlistId});
}

bool PlaylistDAO::isTrackInPlaylist(TrackId trackId, const int playlistId) const {
Expand Down
9 changes: 7 additions & 2 deletions src/library/dao/playlistdao.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PlaylistDAO : public QObject, public virtual DAO {
// Returns the maximum position of the given playlist
int getMaxPosition(const int playlistId) const;
// Remove a track from all playlists
void removeTracksFromPlaylists(const QList<TrackId>& trackIds);
void removeTracksFromPlaylists(const QList<TrackId>& trackIds, bool purged = false);
// removes all hidden and purged Tracks from the playlist
void removeHiddenTracks(const int playlistId);
// Remove a track from a playlist
Expand Down Expand Up @@ -139,7 +139,12 @@ class PlaylistDAO : public QObject, public virtual DAO {
void lockChanged(const QSet<int>& playlistIds);
void trackAdded(int playlistId, TrackId trackId, int position);
void trackRemoved(int playlistId, TrackId trackId, int position);
void tracksChanged(const QSet<int>& playlistIds); // added/removed/reordered
// added / removed / un/locked. Triggers playlist features to update the sidebar
void playlistContentChanged(const QSet<int>& playlistIds);
// Separate signals for PlaylistTableModel
void tracksAdded(const QSet<int>& playlistIds);
void tracksMoved(const QSet<int>& playlistIds);
void tracksRemoved(const QSet<int>& playlistIds);
void tracksRemovedFromPlayedHistory(const QSet<TrackId>& playedTrackIds);

private:
Expand Down
11 changes: 10 additions & 1 deletion src/library/playlisttablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ PlaylistTableModel::PlaylistTableModel(QObject* parent,
m_iPlaylistId(kInvalidPlaylistId),
m_keepHiddenTracks(keepHiddenTracks) {
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::tracksChanged,
&PlaylistDAO::tracksAdded,
this,
&PlaylistTableModel::playlistsChanged);
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::tracksMoved,
this,
&PlaylistTableModel::playlistsChanged);
connect(&m_pTrackCollectionManager->internalCollection()->getPlaylistDAO(),
&PlaylistDAO::tracksRemoved,
this,
&PlaylistTableModel::playlistsChanged);
}
Expand Down Expand Up @@ -359,6 +367,7 @@ TrackModel::Capabilities PlaylistTableModel::getCapabilities() const {
Capability::LoadToSampler |
Capability::LoadToPreviewDeck |
Capability::ResetPlayed |
Capability::RemoveFromDisk |
Capability::Analyze;

if (m_iPlaylistId !=
Expand Down
2 changes: 1 addition & 1 deletion src/library/trackcollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ bool TrackCollection::purgeTracks(
}
// TODO(XXX): Move reversible actions inside transaction
m_cueDao.deleteCuesForTracks(trackIds);
m_playlistDao.removeTracksFromPlaylists(trackIds);
m_playlistDao.removeTracksFromPlaylists(trackIds, true);
m_analysisDao.deleteAnalyses(trackIds);

// Post-processing
Expand Down
2 changes: 1 addition & 1 deletion src/library/trackset/baseplaylistfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void BasePlaylistFeature::connectPlaylistDAO() {
this,
&BasePlaylistFeature::slotPlaylistTableChanged);
connect(&m_playlistDao,
&PlaylistDAO::tracksChanged,
&PlaylistDAO::playlistContentChanged,
this,
&BasePlaylistFeature::slotPlaylistContentOrLockChanged);
connect(&m_playlistDao,
Expand Down

0 comments on commit 75abb60

Please sign in to comment.