Increment play and skip counts when playing or skipping songs from the library
This commit is contained in:
parent
0d04e9e28d
commit
a13e0ba3f3
|
@ -186,6 +186,10 @@ void Player::Next() {
|
|||
}
|
||||
|
||||
void Player::NextInternal(Engine::TrackChangeType change) {
|
||||
if (change == Engine::Manual) {
|
||||
emit TrackSkipped(current_item_);
|
||||
}
|
||||
|
||||
if (playlists_->active()->stop_after_current()) {
|
||||
playlists_->active()->StopAfter(-1);
|
||||
Stop();
|
||||
|
|
|
@ -145,6 +145,7 @@ class Player : public QObject {
|
|||
void PlaylistFinished();
|
||||
void VolumeChanged(int volume);
|
||||
void Error(const QString& message);
|
||||
void TrackSkipped(PlaylistItemPtr old_track);
|
||||
|
||||
void ForceShowOSD(Song);
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <QSettings>
|
||||
#include <QtDebug>
|
||||
#include <QCoreApplication>
|
||||
#include <QDateTime>
|
||||
|
||||
LibraryBackend::LibraryBackend(QObject *parent)
|
||||
: LibraryBackendInterface(parent)
|
||||
|
@ -49,6 +50,16 @@ void LibraryBackend::UpdateTotalSongCountAsync() {
|
|||
metaObject()->invokeMethod(this, "UpdateTotalSongCount", Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
void LibraryBackend::IncrementPlayCountAsync(int id) {
|
||||
metaObject()->invokeMethod(this, "IncrementPlayCount", Qt::QueuedConnection,
|
||||
Q_ARG(int, id));
|
||||
}
|
||||
|
||||
void LibraryBackend::IncrementSkipCountAsync(int id) {
|
||||
metaObject()->invokeMethod(this, "IncrementSkipCount", Qt::QueuedConnection,
|
||||
Q_ARG(int, id));
|
||||
}
|
||||
|
||||
void LibraryBackend::LoadDirectories() {
|
||||
DirectoryList dirs = GetAllDirectories();
|
||||
|
||||
|
@ -455,6 +466,10 @@ Song LibraryBackend::GetSongById(int id) {
|
|||
QMutexLocker l(db_->Mutex());
|
||||
QSqlDatabase db(db_->Connect());
|
||||
|
||||
return GetSongById(id, db);
|
||||
}
|
||||
|
||||
Song LibraryBackend::GetSongById(int id, QSqlDatabase& db) {
|
||||
QSqlQuery q(QString("SELECT ROWID, " + Song::kColumnSpec + " FROM %1"
|
||||
" WHERE ROWID = :id").arg(songs_table_), db);
|
||||
q.bindValue(":id", id);
|
||||
|
@ -787,3 +802,49 @@ void LibraryBackend::ForceCompilation(const QString& artist, const QString& albu
|
|||
bool LibraryBackend::ExecQuery(LibraryQuery *q) {
|
||||
return !db_->CheckErrors(q->Exec(db_->Connect(), songs_table_, fts_table_));
|
||||
}
|
||||
|
||||
void LibraryBackend::IncrementPlayCount(int id) {
|
||||
if (id == -1)
|
||||
return;
|
||||
|
||||
QMutexLocker l(db_->Mutex());
|
||||
QSqlDatabase db(db_->Connect());
|
||||
|
||||
Song old_song = GetSongById(id, db);
|
||||
|
||||
QSqlQuery q(QString("UPDATE %1 SET playcount = playcount + 1,"
|
||||
" lastplayed = :now"
|
||||
" WHERE ROWID = :id").arg(songs_table_), db);
|
||||
q.bindValue(":now", QDateTime::currentDateTime().toTime_t());
|
||||
q.bindValue(":id", id);
|
||||
q.exec();
|
||||
if (db_->CheckErrors(q.lastError()))
|
||||
return;
|
||||
|
||||
Song new_song = GetSongById(id, db);
|
||||
|
||||
emit SongsDeleted(SongList() << old_song);
|
||||
emit SongsDiscovered(SongList() << new_song);
|
||||
}
|
||||
|
||||
void LibraryBackend::IncrementSkipCount(int id) {
|
||||
if (id == -1)
|
||||
return;
|
||||
|
||||
QMutexLocker l(db_->Mutex());
|
||||
QSqlDatabase db(db_->Connect());
|
||||
|
||||
Song old_song = GetSongById(id, db);
|
||||
|
||||
QSqlQuery q(QString("UPDATE %1 SET skipcount = skipcount + 1"
|
||||
" WHERE ROWID = :id").arg(songs_table_), db);
|
||||
q.bindValue(":id", id);
|
||||
q.exec();
|
||||
if (db_->CheckErrors(q.lastError()))
|
||||
return;
|
||||
|
||||
Song new_song = GetSongById(id, db);
|
||||
|
||||
emit SongsDeleted(SongList() << old_song);
|
||||
emit SongsDiscovered(SongList() << new_song);
|
||||
}
|
||||
|
|
|
@ -132,6 +132,9 @@ class LibraryBackend : public LibraryBackendInterface {
|
|||
|
||||
bool ExecQuery(LibraryQuery* q);
|
||||
|
||||
void IncrementPlayCountAsync(int id);
|
||||
void IncrementSkipCountAsync(int id);
|
||||
|
||||
public slots:
|
||||
void LoadDirectories();
|
||||
void UpdateTotalSongCount();
|
||||
|
@ -142,6 +145,8 @@ class LibraryBackend : public LibraryBackendInterface {
|
|||
void UpdateCompilations();
|
||||
void UpdateManualAlbumArt(const QString& artist, const QString& album, const QString& art);
|
||||
void ForceCompilation(const QString& artist, const QString& album, bool on);
|
||||
void IncrementPlayCount(int id);
|
||||
void IncrementSkipCount(int id);
|
||||
|
||||
signals:
|
||||
void DirectoryDiscovered(const Directory& dir, const SubdirectoryList& subdirs);
|
||||
|
@ -169,6 +174,7 @@ class LibraryBackend : public LibraryBackendInterface {
|
|||
AlbumList GetAlbums(const QString& artist, bool compilation = false,
|
||||
const QueryOptions& opt = QueryOptions());
|
||||
SubdirectoryList SubdirsInDirectory(int id, QSqlDatabase& db);
|
||||
Song GetSongById(int id, QSqlDatabase& db);
|
||||
|
||||
private:
|
||||
boost::shared_ptr<Database> db_;
|
||||
|
|
|
@ -314,12 +314,19 @@ void LibraryWatcher::ScanSubdirectory(
|
|||
song_on_disk.set_id(matching_song.id());
|
||||
song_on_disk.set_art_automatic(image);
|
||||
|
||||
// Preserve user-settable fields
|
||||
song_on_disk.set_playcount(matching_song.playcount());
|
||||
song_on_disk.set_skipcount(matching_song.skipcount());
|
||||
song_on_disk.set_lastplayed(matching_song.lastplayed());
|
||||
song_on_disk.set_rating(matching_song.rating());
|
||||
song_on_disk.set_art_manual(matching_song.art_manual());
|
||||
|
||||
if (!matching_song.IsMetadataEqual(song_on_disk)) {
|
||||
qDebug() << file << "metadata changed";
|
||||
// Update the song in the DB
|
||||
t->new_songs << song_on_disk;
|
||||
} else {
|
||||
// Only the metadata changed
|
||||
// Only the mtimes changed
|
||||
t->touched_songs << song_on_disk;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -332,6 +332,7 @@ MainWindow::MainWindow(Engine::Type engine, QWidget *parent)
|
|||
connect(player_, SIGNAL(Paused()), SLOT(MediaPaused()));
|
||||
connect(player_, SIGNAL(Playing()), SLOT(MediaPlaying()));
|
||||
connect(player_, SIGNAL(Stopped()), SLOT(MediaStopped()));
|
||||
connect(player_, SIGNAL(TrackSkipped(PlaylistItemPtr)), SLOT(TrackSkipped(PlaylistItemPtr)));
|
||||
|
||||
connect(player_, SIGNAL(Paused()), playlists_, SLOT(SetActivePaused()));
|
||||
connect(player_, SIGNAL(Playing()), playlists_, SLOT(SetActivePlaying()));
|
||||
|
@ -750,6 +751,14 @@ void MainWindow::MediaPlaying() {
|
|||
tray_icon_->SetPlaying();
|
||||
}
|
||||
|
||||
void MainWindow::TrackSkipped(PlaylistItemPtr item) {
|
||||
// If it was a library item then we have to increment its skipped count in
|
||||
// the database.
|
||||
if (item && item->type() == "Library" && item->Metadata().id() != -1) {
|
||||
library_->backend()->IncrementSkipCountAsync(item->Metadata().id());
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::ScrobblingEnabledChanged(bool value) {
|
||||
if (!player_->GetState() == Engine::Idle)
|
||||
return;
|
||||
|
@ -883,8 +892,9 @@ void MainWindow::FilePathChanged(const QString& path) {
|
|||
|
||||
void MainWindow::UpdateTrackPosition() {
|
||||
// Track position in seconds
|
||||
PlaylistItemPtr item(player_->GetCurrentItem());
|
||||
const int position = std::floor(float(player_->GetEngine()->position()) / 1000.0 + 0.5);
|
||||
const int length = player_->GetCurrentItem()->Metadata().length();
|
||||
const int length = item->Metadata().length();
|
||||
|
||||
if (length <= 0) {
|
||||
// Probably a stream that we don't know the length of
|
||||
|
@ -900,6 +910,11 @@ void MainWindow::UpdateTrackPosition() {
|
|||
position >= playlists_->active()->scrobble_point()) {
|
||||
lastfm->Scrobble();
|
||||
playlists_->active()->set_scrobbled(true);
|
||||
|
||||
// Update the play count for the song if it's from the library
|
||||
if (item->type() == "Library" && item->Metadata().id() != -1) {
|
||||
library_->backend()->IncrementPlayCountAsync(item->Metadata().id());
|
||||
}
|
||||
}
|
||||
|
||||
// Update the slider
|
||||
|
|
|
@ -104,6 +104,7 @@ class MainWindow : public QMainWindow, public PlatformInterface {
|
|||
void MediaStopped();
|
||||
void MediaPaused();
|
||||
void MediaPlaying();
|
||||
void TrackSkipped(PlaylistItemPtr item);
|
||||
void ForceShowOSD(const Song& song);
|
||||
|
||||
void PlaylistRightClick(const QPoint& global_pos, const QModelIndex& index);
|
||||
|
|
Loading…
Reference in New Issue