Skip to content

Commit

Permalink
Merge pull request #2996 from poelzi/history
Browse files Browse the repository at this point in the history
History improvements
  • Loading branch information
uklotzde authored Dec 2, 2020
2 parents e78101f + 4d69a40 commit 049311a
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 171 deletions.
48 changes: 48 additions & 0 deletions src/library/dao/playlistdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "library/trackcollection.h"
#include "track/track.h"
#include "util/compatibility.h"
#include "util/db/fwdsqlquery.h"
#include "util/math.h"

PlaylistDAO::PlaylistDAO()
Expand Down Expand Up @@ -227,6 +228,53 @@ void PlaylistDAO::deletePlaylist(const int playlistId) {
}
}

int PlaylistDAO::deleteAllPlaylistsWithFewerTracks(
PlaylistDAO::HiddenType type, int minNumberOfTracks) {
VERIFY_OR_DEBUG_ASSERT(minNumberOfTracks > 0) {
return 0; // nothing to do, probably unintended invocation
}

QSqlQuery query(m_database);
query.prepare(QStringLiteral(
"SELECT id FROM Playlists "
"WHERE (SELECT count(playlist_id) FROM PlaylistTracks WHERE "
"Playlists.ID = PlaylistTracks.playlist_id) < :length AND "
"Playlists.hidden = :hidden"));
query.bindValue(":hidden", static_cast<int>(type));
query.bindValue(":length", minNumberOfTracks);
if (!query.exec()) {
LOG_FAILED_QUERY(query);
return -1;
}

QStringList idStringList;
while (query.next()) {
idStringList.append(query.value(0).toString());
}
if (idStringList.isEmpty()) {
return 0;
}
QString idString = idStringList.join(",");

qInfo() << "Deleting" << idStringList.size() << "playlists of type" << type
<< "that contain fewer than" << minNumberOfTracks << "tracks";

auto deleteTracks = FwdSqlQuery(m_database,
QString("DELETE FROM PlaylistTracks WHERE playlist_id IN (%1)")
.arg(idString));
if (!deleteTracks.execPrepared()) {
return -1;
}

auto deletePlaylists = FwdSqlQuery(m_database,
QString("DELETE FROM Playlists WHERE id IN (%1)").arg(idString));
if (!deletePlaylists.execPrepared()) {
return -1;
}

return idStringList.length();
}

void PlaylistDAO::renamePlaylist(const int playlistId, const QString& newName) {
QSqlQuery query(m_database);
query.prepare(QStringLiteral(
Expand Down
4 changes: 4 additions & 0 deletions src/library/dao/playlistdao.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class PlaylistDAO : public QObject, public virtual DAO {
int createUniquePlaylist(QString* pName, const HiddenType type = PLHT_NOT_HIDDEN);
// Delete a playlist
void deletePlaylist(const int playlistId);
/// Delete Playlists with fewer entries then "length"
/// Needs to be called inside a transaction.
/// @return number of deleted playlists, -1 on error
int deleteAllPlaylistsWithFewerTracks(PlaylistDAO::HiddenType type, int minNumberOfTracks);
// Rename a playlist
void renamePlaylist(const int playlistId, const QString& newName);
// Lock or unlock a playlist
Expand Down
11 changes: 8 additions & 3 deletions src/library/queryutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class ScopedTransaction {
return false;
}
bool result = m_database.commit();
qDebug() << "Committing transaction on"
<< m_database.connectionName()
<< "result:" << result;
if (result) {
qDebug() << "Committing transaction successfully on"
<< m_database.connectionName();
} else {
qInfo() << "Committing transaction failed on"
<< m_database.connectionName()
<< ":" << m_database.lastError();
}
m_active = false;
return result;
}
Expand Down
9 changes: 7 additions & 2 deletions src/library/sidebarmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ bool SidebarModel::dragMoveAccept(const QModelIndex& index, const QUrl& url) {

// Translates an index from the child models to an index of the sidebar models
QModelIndex SidebarModel::translateSourceIndex(const QModelIndex& index) {
QModelIndex translatedIndex;

/* These method is called from the slot functions below.
* QObject::sender() return the object which emitted the signal
* handled by the slot functions.
Expand All @@ -388,6 +386,13 @@ QModelIndex SidebarModel::translateSourceIndex(const QModelIndex& index) {
return QModelIndex();
}

return translateIndex(index, model);
}

QModelIndex SidebarModel::translateIndex(
const QModelIndex& index, const QAbstractItemModel* model) {
QModelIndex translatedIndex;

if (index.isValid()) {
TreeItem* item = (TreeItem*)index.internalPointer();
translatedIndex = createIndex(index.row(), index.column(), item);
Expand Down
4 changes: 4 additions & 0 deletions src/library/sidebarmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class SidebarModel : public QAbstractItemModel {
bool dragMoveAccept(const QModelIndex& index, const QUrl& url);
bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
bool hasTrackTable(const QModelIndex& index) const;
QModelIndex translateChildIndex(const QModelIndex& index) {
return translateIndex(index, index.model());
}

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

private:
QModelIndex translateSourceIndex(const QModelIndex& parent);
QModelIndex translateIndex(const QModelIndex& index, const QAbstractItemModel* model);
void featureRenamed(LibraryFeature*);
QList<LibraryFeature*> m_sFeatures;
unsigned int m_iDefaultSelectedIndex; /** Index of the item in the sidebar model to select at startup. */
Expand Down
Loading

0 comments on commit 049311a

Please sign in to comment.