Skip to content

Commit 3527b38

Browse files
authored
Merge pull request #15991 from ronso0/removeFromDisk-dont-delete-curr-history-playlist
(fix) prevent deletion of current History playlist after purging tracks
2 parents 707f786 + cb30f0c commit 3527b38

3 files changed

Lines changed: 59 additions & 6 deletions

File tree

src/library/dao/playlistdao.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#include "util/math.h"
1414

1515
PlaylistDAO::PlaylistDAO()
16-
: m_pAutoDJProcessor(nullptr) {
16+
: m_currentHistoryPlaylist(kInvalidPlaylistId),
17+
m_pAutoDJProcessor(nullptr) {
1718
}
1819

1920
void PlaylistDAO::initialize(const QSqlDatabase& database) {
@@ -277,19 +278,35 @@ bool PlaylistDAO::deleteUnlockedPlaylists(QStringList&& idStringList) {
277278
}
278279

279280
bool PlaylistDAO::deleteAllUnlockedPlaylistsWithFewerTracks(
280-
PlaylistDAO::HiddenType type, int minNumberOfTracks) {
281+
PlaylistDAO::HiddenType type,
282+
int minNumberOfTracks,
283+
bool skipCurrHistory) {
284+
// Note: this slot is also called after purging tracks in order to delete
285+
// now empty history playlists.
286+
// Though, if the current History is now also empty, we must not delete that!
287+
// Else the following session log is lost -- or rather not recorded in the
288+
// first place since the id passed to appendTrackToPlaylist() does not exist
289+
// anymore.
290+
// skipCurrHistory prevents that.
281291
VERIFY_OR_DEBUG_ASSERT(minNumberOfTracks > 0) {
282292
return false; // nothing to do, probably unintended invocation
283293
}
284294

285295
QSqlQuery query(m_database);
286-
query.prepare(QStringLiteral(
296+
QString queryString = QStringLiteral(
287297
"SELECT id FROM Playlists "
288298
"WHERE (SELECT count(playlist_id) FROM PlaylistTracks WHERE "
289299
"Playlists.ID = PlaylistTracks.playlist_id) < :length AND "
290-
"Playlists.hidden = :hidden AND Playlists.locked = 0"));
300+
"Playlists.hidden = :hidden AND Playlists.locked = 0");
301+
if (skipCurrHistory) {
302+
queryString.append(QStringLiteral(" AND Playlists.ID != :currHistoryId"));
303+
}
304+
query.prepare(queryString);
291305
query.bindValue(":hidden", static_cast<int>(type));
292306
query.bindValue(":length", minNumberOfTracks);
307+
if (skipCurrHistory) {
308+
query.bindValue(":currHistoryId", m_currentHistoryPlaylist);
309+
}
293310
if (!query.exec()) {
294311
LOG_FAILED_QUERY(query);
295312
return false;
@@ -299,6 +316,11 @@ bool PlaylistDAO::deleteAllUnlockedPlaylistsWithFewerTracks(
299316
while (query.next()) {
300317
idStringList.append(query.value(0).toString());
301318
}
319+
320+
if (idStringList.isEmpty()) {
321+
return false;
322+
}
323+
302324
qInfo() << "Prepared deletion of" << idStringList.size() << "playlists of type" << type
303325
<< "that contain fewer than" << minNumberOfTracks << "tracks";
304326

@@ -405,9 +427,29 @@ bool PlaylistDAO::removeTracksFromPlaylist(int playlistId, int startIndex) {
405427
return true;
406428
}
407429

430+
bool PlaylistDAO::playlistExists(const int playlistId) const {
431+
ScopedTransaction transaction(m_database);
432+
QSqlQuery query(m_database);
433+
query.prepare(QStringLiteral("SELECT id FROM Playlists WHERE id = :id"));
434+
query.bindValue(":id", playlistId);
435+
436+
if (!query.exec()) {
437+
LOG_FAILED_QUERY(query);
438+
return false;
439+
}
440+
441+
if (query.next()) {
442+
// id is guaranteed to be unique, so we can return here
443+
return true;
444+
}
445+
// not found
446+
return false;
447+
}
448+
408449
bool PlaylistDAO::appendTracksToPlaylist(const QList<TrackId>& trackIds, const int playlistId) {
409450
// qDebug() << "PlaylistDAO::appendTracksToPlaylist"
410451
// << QThread::currentThread() << m_database.connectionName();
452+
DEBUG_ASSERT(playlistExists(playlistId));
411453

412454
// Start the transaction
413455
ScopedTransaction transaction(m_database);
@@ -991,7 +1033,8 @@ void PlaylistDAO::removeTracksFromPlaylists(const QList<TrackId>& trackIds, bool
9911033
transaction.commit();
9921034

9931035
// We may now have empty history playlists. Remove them.
994-
deleteAllUnlockedPlaylistsWithFewerTracks(PlaylistDAO::PLHT_SET_LOG, 1);
1036+
// Note: does not delete current History playlist.
1037+
deleteAllUnlockedPlaylistsWithFewerTracks(PlaylistDAO::PLHT_SET_LOG, 1, true);
9951038

9961039
// update the sidebar
9971040
emit playlistContentChanged(playlistIds);

src/library/dao/playlistdao.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ class PlaylistDAO : public QObject, public virtual DAO {
4747
/// Needs to be called inside a transaction.
4848
/// @return true on success, false on error
4949
bool deleteAllUnlockedPlaylistsWithFewerTracks(const PlaylistDAO::HiddenType type,
50-
int minNumberOfTracks);
50+
int minNumberOfTracks,
51+
bool skipCurrHistory = false);
5152
// Rename a playlist
5253
void renamePlaylist(const int playlistId, const QString& newName);
5354
// Lock or unlock a playlist
5455
bool setPlaylistLocked(const int playlistId, const bool locked);
5556
int setPlaylistsLocked(const QSet<int>& playlistIds, const bool lock);
5657
// Find out the state of a playlist lock
5758
bool isPlaylistLocked(const int playlistId) const;
59+
// Check if a playlist exists
60+
bool playlistExists(const int playlistId) const;
5861
// Append a list of tracks to a playlist
5962
bool appendTracksToPlaylist(const QList<TrackId>& trackIds, const int playlistId);
6063
// Append a track to a playlist
@@ -115,6 +118,10 @@ class PlaylistDAO : public QObject, public virtual DAO {
115118

116119
void getPlaylistsTrackIsIn(TrackId trackId, QSet<int>* playlistSet) const;
117120

121+
void setCurrentHistoryPlaylistId(int id) {
122+
m_currentHistoryPlaylist = id;
123+
}
124+
118125
void setAutoDJProcessor(AutoDJProcessor* pAutoDJProcessor);
119126

120127
signals:
@@ -146,6 +153,7 @@ class PlaylistDAO : public QObject, public virtual DAO {
146153
void populatePlaylistMembershipCache();
147154

148155
QMultiHash<TrackId, int> m_playlistsTrackIsIn;
156+
int m_currentHistoryPlaylist;
149157
AutoDJProcessor* m_pAutoDJProcessor;
150158
DISALLOW_COPY_AND_ASSIGN(PlaylistDAO);
151159
};

src/library/trackset/setlogfeature.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ void SetlogFeature::slotGetNewPlaylist() {
383383
<< set_log_name;
384384
} else {
385385
m_recentTracks.clear();
386+
m_playlistDao.setCurrentHistoryPlaylistId(m_currentPlaylistId);
386387
}
387388

388389
// reload child model again because the 'added' signal fired by PlaylistDAO
@@ -447,6 +448,7 @@ void SetlogFeature::slotJoinWithPrevious() {
447448

448449
// Change current setlog
449450
m_currentPlaylistId = previousPlaylistId;
451+
m_playlistDao.setCurrentHistoryPlaylistId(m_currentPlaylistId);
450452
}
451453
qDebug() << "slotJoinWithPrevious() current:"
452454
<< clickedPlaylistId

0 commit comments

Comments
 (0)