Skip to content

Commit 049311a

Browse files
authored
Merge pull request #2996 from poelzi/history
History improvements
2 parents e78101f + 4d69a40 commit 049311a

17 files changed

+434
-171
lines changed

src/library/dao/playlistdao.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "library/trackcollection.h"
1212
#include "track/track.h"
1313
#include "util/compatibility.h"
14+
#include "util/db/fwdsqlquery.h"
1415
#include "util/math.h"
1516

1617
PlaylistDAO::PlaylistDAO()
@@ -227,6 +228,53 @@ void PlaylistDAO::deletePlaylist(const int playlistId) {
227228
}
228229
}
229230

231+
int PlaylistDAO::deleteAllPlaylistsWithFewerTracks(
232+
PlaylistDAO::HiddenType type, int minNumberOfTracks) {
233+
VERIFY_OR_DEBUG_ASSERT(minNumberOfTracks > 0) {
234+
return 0; // nothing to do, probably unintended invocation
235+
}
236+
237+
QSqlQuery query(m_database);
238+
query.prepare(QStringLiteral(
239+
"SELECT id FROM Playlists "
240+
"WHERE (SELECT count(playlist_id) FROM PlaylistTracks WHERE "
241+
"Playlists.ID = PlaylistTracks.playlist_id) < :length AND "
242+
"Playlists.hidden = :hidden"));
243+
query.bindValue(":hidden", static_cast<int>(type));
244+
query.bindValue(":length", minNumberOfTracks);
245+
if (!query.exec()) {
246+
LOG_FAILED_QUERY(query);
247+
return -1;
248+
}
249+
250+
QStringList idStringList;
251+
while (query.next()) {
252+
idStringList.append(query.value(0).toString());
253+
}
254+
if (idStringList.isEmpty()) {
255+
return 0;
256+
}
257+
QString idString = idStringList.join(",");
258+
259+
qInfo() << "Deleting" << idStringList.size() << "playlists of type" << type
260+
<< "that contain fewer than" << minNumberOfTracks << "tracks";
261+
262+
auto deleteTracks = FwdSqlQuery(m_database,
263+
QString("DELETE FROM PlaylistTracks WHERE playlist_id IN (%1)")
264+
.arg(idString));
265+
if (!deleteTracks.execPrepared()) {
266+
return -1;
267+
}
268+
269+
auto deletePlaylists = FwdSqlQuery(m_database,
270+
QString("DELETE FROM Playlists WHERE id IN (%1)").arg(idString));
271+
if (!deletePlaylists.execPrepared()) {
272+
return -1;
273+
}
274+
275+
return idStringList.length();
276+
}
277+
230278
void PlaylistDAO::renamePlaylist(const int playlistId, const QString& newName) {
231279
QSqlQuery query(m_database);
232280
query.prepare(QStringLiteral(

src/library/dao/playlistdao.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ class PlaylistDAO : public QObject, public virtual DAO {
5959
int createUniquePlaylist(QString* pName, const HiddenType type = PLHT_NOT_HIDDEN);
6060
// Delete a playlist
6161
void deletePlaylist(const int playlistId);
62+
/// Delete Playlists with fewer entries then "length"
63+
/// Needs to be called inside a transaction.
64+
/// @return number of deleted playlists, -1 on error
65+
int deleteAllPlaylistsWithFewerTracks(PlaylistDAO::HiddenType type, int minNumberOfTracks);
6266
// Rename a playlist
6367
void renamePlaylist(const int playlistId, const QString& newName);
6468
// Lock or unlock a playlist

src/library/queryutil.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ class ScopedTransaction {
4242
return false;
4343
}
4444
bool result = m_database.commit();
45-
qDebug() << "Committing transaction on"
46-
<< m_database.connectionName()
47-
<< "result:" << result;
45+
if (result) {
46+
qDebug() << "Committing transaction successfully on"
47+
<< m_database.connectionName();
48+
} else {
49+
qInfo() << "Committing transaction failed on"
50+
<< m_database.connectionName()
51+
<< ":" << m_database.lastError();
52+
}
4853
m_active = false;
4954
return result;
5055
}

src/library/sidebarmodel.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,6 @@ bool SidebarModel::dragMoveAccept(const QModelIndex& index, const QUrl& url) {
374374

375375
// Translates an index from the child models to an index of the sidebar models
376376
QModelIndex SidebarModel::translateSourceIndex(const QModelIndex& index) {
377-
QModelIndex translatedIndex;
378-
379377
/* These method is called from the slot functions below.
380378
* QObject::sender() return the object which emitted the signal
381379
* handled by the slot functions.
@@ -388,6 +386,13 @@ QModelIndex SidebarModel::translateSourceIndex(const QModelIndex& index) {
388386
return QModelIndex();
389387
}
390388

389+
return translateIndex(index, model);
390+
}
391+
392+
QModelIndex SidebarModel::translateIndex(
393+
const QModelIndex& index, const QAbstractItemModel* model) {
394+
QModelIndex translatedIndex;
395+
391396
if (index.isValid()) {
392397
TreeItem* item = (TreeItem*)index.internalPointer();
393398
translatedIndex = createIndex(index.row(), index.column(), item);

src/library/sidebarmodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class SidebarModel : public QAbstractItemModel {
4040
bool dragMoveAccept(const QModelIndex& index, const QUrl& url);
4141
bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
4242
bool hasTrackTable(const QModelIndex& index) const;
43+
QModelIndex translateChildIndex(const QModelIndex& index) {
44+
return translateIndex(index, index.model());
45+
}
4346

4447
public slots:
4548
void pressed(const QModelIndex& index);
@@ -76,6 +79,7 @@ class SidebarModel : public QAbstractItemModel {
7679

7780
private:
7881
QModelIndex translateSourceIndex(const QModelIndex& parent);
82+
QModelIndex translateIndex(const QModelIndex& index, const QAbstractItemModel* model);
7983
void featureRenamed(LibraryFeature*);
8084
QList<LibraryFeature*> m_sFeatures;
8185
unsigned int m_iDefaultSelectedIndex; /** Index of the item in the sidebar model to select at startup. */

0 commit comments

Comments
 (0)