From ef93e8211d2ac70c7895aa9f1e6e0235546dccbb Mon Sep 17 00:00:00 2001 From: Jim Broadus Date: Sun, 25 Jul 2021 23:51:43 -0700 Subject: [PATCH] internet/podcasts: Fix crash when updating podcasts. When a podcast is updated and the number of visible items is set in the podcast settings, child items that disappear from the view, and are deleted, are still referenced by the database id map. Move the removal code from SubscriptionRemoved to a common method and use that for this case. --- src/internet/podcasts/podcastservice.cpp | 29 +++++++++++++----------- src/internet/podcasts/podcastservice.h | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/internet/podcasts/podcastservice.cpp b/src/internet/podcasts/podcastservice.cpp index 496e9427c..fa91da60d 100644 --- a/src/internet/podcasts/podcastservice.cpp +++ b/src/internet/podcasts/podcastservice.cpp @@ -395,6 +395,20 @@ QStandardItem* PodcastService::CreatePodcastItem(const Podcast& podcast) { return item; } +void PodcastService::RemovePodcastItem(QStandardItem* item) { + // Remove any episode ID -> item mappings for the episodes in this podcast. + for (int i = 0; i < item->rowCount(); ++i) { + QStandardItem* episode_item = item->child(i); + const int episode_id = + episode_item->data(Role_Episode).value().database_id(); + + episodes_by_database_id_.remove(episode_id); + } + + // Remove this podcast's row + model_->removeRow(item->row()); +} + QStandardItem* PodcastService::CreatePodcastEpisodeItem( const PodcastEpisode& episode) { QStandardItem* item = new QStandardItem; @@ -621,18 +635,7 @@ void PodcastService::SubscriptionAdded(const Podcast& podcast) { void PodcastService::SubscriptionRemoved(const Podcast& podcast) { QStandardItem* item = podcasts_by_database_id_.take(podcast.database_id()); if (item) { - // Remove any episode ID -> item mappings for the episodes in this podcast. - for (int i = 0; i < item->rowCount(); ++i) { - QStandardItem* episode_item = item->child(i); - const int episode_id = episode_item->data(Role_Episode) - .value() - .database_id(); - - episodes_by_database_id_.remove(episode_id); - } - - // Remove this episode's row - model_->removeRow(item->row()); + RemovePodcastItem(item); } } @@ -859,6 +862,6 @@ void PodcastService::ReloadPodcast(const Podcast& podcast) { } QStandardItem* item = podcasts_by_database_id_[podcast.database_id()]; - model_->invisibleRootItem()->removeRow(item->row()); + RemovePodcastItem(item); model_->invisibleRootItem()->appendRow(CreatePodcastItem(podcast)); } diff --git a/src/internet/podcasts/podcastservice.h b/src/internet/podcasts/podcastservice.h index b5452f9be..579f3ea2d 100644 --- a/src/internet/podcasts/podcastservice.h +++ b/src/internet/podcasts/podcastservice.h @@ -119,6 +119,7 @@ class PodcastService : public InternetService { QStandardItem* CreatePodcastItem(const Podcast& podcast); QStandardItem* CreatePodcastEpisodeItem(const PodcastEpisode& episode); + void RemovePodcastItem(QStandardItem* item); QModelIndex MapToMergedModel(const QModelIndex& index) const;