From 8cc971fa93c3ee6b399db43f36b63d7e148c3607 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sun, 11 Mar 2012 12:27:48 +0000 Subject: [PATCH] Mark podcast episodes as listened when they're listened to --- data/schema/schema-37.sql | 4 ++++ src/podcasts/podcastbackend.cpp | 23 +++++++++++++++++++++-- src/podcasts/podcastbackend.h | 1 + src/podcasts/podcastepisode.h | 2 ++ src/podcasts/podcastservice.cpp | 15 +++++++++++++++ src/podcasts/podcastservice.h | 2 ++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/data/schema/schema-37.sql b/data/schema/schema-37.sql index c8d13e890..27ea81a6f 100644 --- a/data/schema/schema-37.sql +++ b/data/schema/schema-37.sql @@ -37,4 +37,8 @@ CREATE INDEX podcast_idx_url ON podcasts(url); CREATE INDEX podcast_episodes_idx_podcast_id ON podcast_episodes(podcast_id); +CREATE INDEX podcast_episodes_idx_url ON podcast_episodes(url); + +CREATE INDEX podcast_episodes_idx_local_url ON podcast_episodes(local_url); + UPDATE schema_version SET version=37; diff --git a/src/podcasts/podcastbackend.cpp b/src/podcasts/podcastbackend.cpp index 2345e906d..49a564be1 100644 --- a/src/podcasts/podcastbackend.cpp +++ b/src/podcasts/podcastbackend.cpp @@ -143,7 +143,7 @@ void PodcastBackend::UpdateEpisodes(const PodcastEpisodeList& episodes) { foreach (const PodcastEpisode& episode, episodes) { q.bindValue(":listened", episode.listened()); q.bindValue(":downloaded", episode.downloaded()); - q.bindValue(":local_url", episode.local_url()); + q.bindValue(":local_url", episode.local_url().toEncoded()); q.bindValue(":id", episode.database_id()); q.exec(); db_->CheckErrors(q); @@ -260,7 +260,26 @@ PodcastEpisode PodcastBackend::GetEpisodeByUrl(const QUrl& url) { QSqlQuery q("SELECT ROWID, " + PodcastEpisode::kColumnSpec + " FROM podcast_episodes" " WHERE url = :url", db); - q.bindValue(":url", url); + q.bindValue(":url", url.toEncoded()); + q.exec(); + if (!db_->CheckErrors(q) && q.next()) { + ret.InitFromQuery(q); + } + + return ret; +} + +PodcastEpisode PodcastBackend::GetEpisodeByUrlOrLocalUrl(const QUrl& url) { + PodcastEpisode ret; + + QMutexLocker l(db_->Mutex()); + QSqlDatabase db(db_->Connect()); + + QSqlQuery q("SELECT ROWID, " + PodcastEpisode::kColumnSpec + + " FROM podcast_episodes" + " WHERE url = :url" + " OR local_url = :url", db); + q.bindValue(":url", url.toEncoded()); q.exec(); if (!db_->CheckErrors(q) && q.next()) { ret.InitFromQuery(q); diff --git a/src/podcasts/podcastbackend.h b/src/podcasts/podcastbackend.h index 9cd19f231..3d5dc39b6 100644 --- a/src/podcasts/podcastbackend.h +++ b/src/podcasts/podcastbackend.h @@ -52,6 +52,7 @@ public: PodcastEpisodeList GetEpisodes(int podcast_id); PodcastEpisode GetEpisodeById(int id); PodcastEpisode GetEpisodeByUrl(const QUrl& url); + PodcastEpisode GetEpisodeByUrlOrLocalUrl(const QUrl& url); // Adds episodes to the database. Every episode must have a valid // podcast_database_id set already. diff --git a/src/podcasts/podcastepisode.h b/src/podcasts/podcastepisode.h index 2c51cf3c3..429be2d09 100644 --- a/src/podcasts/podcastepisode.h +++ b/src/podcasts/podcastepisode.h @@ -44,6 +44,8 @@ public: Song ToSong(const Podcast& podcast) const; + bool is_valid() const { return database_id() != -1; } + int database_id() const; int podcast_database_id() const; const QString& title() const; diff --git a/src/podcasts/podcastservice.cpp b/src/podcasts/podcastservice.cpp index 530e3bc59..992276af6 100644 --- a/src/podcasts/podcastservice.cpp +++ b/src/podcasts/podcastservice.cpp @@ -64,6 +64,8 @@ PodcastService::PodcastService(Application* app, InternetModel* parent) connect(backend_, SIGNAL(SubscriptionRemoved(Podcast)), SLOT(SubscriptionRemoved(Podcast))); connect(backend_, SIGNAL(EpisodesAdded(QList)), SLOT(EpisodesAdded(QList))); connect(backend_, SIGNAL(EpisodesUpdated(QList)), SLOT(EpisodesUpdated(QList))); + + connect(app_->playlist_manager(), SIGNAL(CurrentSongChanged(Song)), SLOT(CurrentSongChanged(Song))); } PodcastService::~PodcastService() { @@ -434,3 +436,16 @@ void PodcastService::DownloadProgressChanged(const PodcastEpisode& episode, void PodcastService::ShowConfig() { app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Podcasts); } + +void PodcastService::CurrentSongChanged(const Song& metadata) { + // Check whether this song is one of our podcast episodes. + PodcastEpisode episode = backend_->GetEpisodeByUrlOrLocalUrl(metadata.url()); + if (!episode.is_valid()) + return; + + // Mark it as listened if it's not already + if (!episode.listened()) { + episode.set_listened(true); + backend_->UpdateEpisodes(PodcastEpisodeList() << episode); + } +} diff --git a/src/podcasts/podcastservice.h b/src/podcasts/podcastservice.h index ac846419e..a573787b0 100644 --- a/src/podcasts/podcastservice.h +++ b/src/podcasts/podcastservice.h @@ -78,6 +78,8 @@ private slots: PodcastDownloader::State state, int percent); + void CurrentSongChanged(const Song& metadata); + private: void PopulatePodcastList(QStandardItem* parent); void UpdatePodcastText(QStandardItem* item, int unlistened_count) const;