2012-03-05 19:15:45 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "addpodcastdialog.h"
|
2012-03-11 18:57:15 +01:00
|
|
|
#include "opmlcontainer.h"
|
2012-03-06 19:37:46 +01:00
|
|
|
#include "podcastbackend.h"
|
2012-03-10 16:32:36 +01:00
|
|
|
#include "podcastdownloader.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
#include "podcastservice.h"
|
2012-03-11 00:39:09 +01:00
|
|
|
#include "podcastservicemodel.h"
|
2012-03-09 13:15:24 +01:00
|
|
|
#include "podcastupdater.h"
|
2012-03-06 19:37:46 +01:00
|
|
|
#include "core/application.h"
|
2012-03-06 22:24:41 +01:00
|
|
|
#include "core/logging.h"
|
2012-03-07 12:04:47 +01:00
|
|
|
#include "core/mergedproxymodel.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
#include "internet/internetmodel.h"
|
2012-03-06 22:24:41 +01:00
|
|
|
#include "library/libraryview.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
#include "ui/iconloader.h"
|
2012-03-06 22:24:41 +01:00
|
|
|
#include "ui/standarditemiconloader.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
|
|
|
|
#include <QMenu>
|
2012-03-07 12:04:47 +01:00
|
|
|
#include <QSortFilterProxyModel>
|
2012-03-05 19:15:45 +01:00
|
|
|
|
|
|
|
const char* PodcastService::kServiceName = "Podcasts";
|
|
|
|
const char* PodcastService::kSettingsGroup = "Podcasts";
|
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
|
|
|
|
class PodcastSortProxyModel : public QSortFilterProxyModel {
|
|
|
|
public:
|
|
|
|
PodcastSortProxyModel(QObject* parent = NULL);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
PodcastService::PodcastService(Application* app, InternetModel* parent)
|
|
|
|
: InternetService(kServiceName, app, parent, parent),
|
2012-03-06 22:24:41 +01:00
|
|
|
use_pretty_covers_(true),
|
|
|
|
icon_loader_(new StandardItemIconLoader(app->album_cover_loader(), this)),
|
2012-03-07 12:04:47 +01:00
|
|
|
backend_(app->podcast_backend()),
|
2012-03-11 00:39:09 +01:00
|
|
|
model_(new PodcastServiceModel(this)),
|
2012-03-09 13:15:24 +01:00
|
|
|
proxy_(new PodcastSortProxyModel(this)),
|
2012-03-05 19:15:45 +01:00
|
|
|
context_menu_(NULL),
|
2012-03-07 12:04:47 +01:00
|
|
|
root_(NULL)
|
2012-03-05 19:15:45 +01:00
|
|
|
{
|
2012-03-07 12:04:47 +01:00
|
|
|
icon_loader_->SetModel(model_);
|
|
|
|
proxy_->setSourceModel(model_);
|
|
|
|
proxy_->setDynamicSortFilter(true);
|
|
|
|
proxy_->sort(0);
|
|
|
|
|
|
|
|
connect(backend_, SIGNAL(SubscriptionAdded(Podcast)), SLOT(SubscriptionAdded(Podcast)));
|
|
|
|
connect(backend_, SIGNAL(SubscriptionRemoved(Podcast)), SLOT(SubscriptionRemoved(Podcast)));
|
2012-03-09 13:15:24 +01:00
|
|
|
connect(backend_, SIGNAL(EpisodesAdded(QList<PodcastEpisode>)), SLOT(EpisodesAdded(QList<PodcastEpisode>)));
|
2012-03-10 22:05:57 +01:00
|
|
|
connect(backend_, SIGNAL(EpisodesUpdated(QList<PodcastEpisode>)), SLOT(EpisodesUpdated(QList<PodcastEpisode>)));
|
2012-03-11 13:27:48 +01:00
|
|
|
|
|
|
|
connect(app_->playlist_manager(), SIGNAL(CurrentSongChanged(Song)), SLOT(CurrentSongChanged(Song)));
|
2012-03-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PodcastService::~PodcastService() {
|
|
|
|
}
|
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
PodcastSortProxyModel::PodcastSortProxyModel(QObject* parent)
|
|
|
|
: QSortFilterProxyModel(parent) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PodcastSortProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
|
|
|
|
const int left_type = left.data(InternetModel::Role_Type).toInt();
|
|
|
|
const int right_type = right.data(InternetModel::Role_Type).toInt();
|
|
|
|
|
|
|
|
// The special Add Podcast item comes first
|
|
|
|
if (left_type == PodcastService::Type_AddPodcast)
|
|
|
|
return true;
|
|
|
|
else if (right_type == PodcastService::Type_AddPodcast)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise we only compare identical typed items.
|
|
|
|
if (left_type != right_type)
|
|
|
|
return QSortFilterProxyModel::lessThan(left, right);
|
|
|
|
|
|
|
|
switch (left_type) {
|
|
|
|
case PodcastService::Type_Podcast:
|
|
|
|
return left.data().toString().localeAwareCompare(right.data().toString()) < 0;
|
|
|
|
|
|
|
|
case PodcastService::Type_Episode: {
|
2012-03-10 22:05:57 +01:00
|
|
|
const PodcastEpisode left_episode = left.data(PodcastService::Role_Episode).value<PodcastEpisode>();
|
|
|
|
const PodcastEpisode right_episode = right.data(PodcastService::Role_Episode).value<PodcastEpisode>();
|
2012-03-09 13:15:24 +01:00
|
|
|
|
|
|
|
return left_episode.publication_date() > right_episode.publication_date();
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QSortFilterProxyModel::lessThan(left, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
QStandardItem* PodcastService::CreateRootItem() {
|
|
|
|
root_ = new QStandardItem(QIcon(":providers/podcast16.png"), tr("Podcasts"));
|
2012-03-06 19:37:46 +01:00
|
|
|
root_->setData(true, InternetModel::Role_CanLazyLoad);
|
2012-03-05 19:15:45 +01:00
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::LazyPopulate(QStandardItem* parent) {
|
2012-03-06 19:37:46 +01:00
|
|
|
switch (parent->data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case InternetModel::Type_Service:
|
2012-03-07 12:04:47 +01:00
|
|
|
PopulatePodcastList(model_->invisibleRootItem());
|
|
|
|
model()->merged_model()->AddSubModel(parent->index(), proxy_);
|
2012-03-06 19:37:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::PopulatePodcastList(QStandardItem* parent) {
|
2012-03-10 22:05:57 +01:00
|
|
|
// Do this here since the downloader won't be created yet in the ctor.
|
|
|
|
connect(app_->podcast_downloader(),
|
|
|
|
SIGNAL(ProgressChanged(PodcastEpisode,PodcastDownloader::State,int)),
|
|
|
|
SLOT(DownloadProgressChanged(PodcastEpisode,PodcastDownloader::State,int)));
|
|
|
|
|
2012-03-06 22:24:41 +01:00
|
|
|
if (default_icon_.isNull()) {
|
|
|
|
default_icon_ = QIcon(":providers/podcast16.png");
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:37:46 +01:00
|
|
|
foreach (const Podcast& podcast, backend_->GetAllSubscriptions()) {
|
2012-03-07 12:04:47 +01:00
|
|
|
parent->appendRow(CreatePodcastItem(podcast));
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
void PodcastService::UpdatePodcastText(QStandardItem* item, int unlistened_count) const {
|
|
|
|
const Podcast podcast = item->data(Role_Podcast).value<Podcast>();
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
QString title = podcast.title();
|
|
|
|
QFont font;
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
if (unlistened_count > 0) {
|
|
|
|
// Add the number of new episodes after the title.
|
|
|
|
title.append(QString(" (%1)").arg(unlistened_count));
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
// Set a bold font
|
2012-03-10 22:05:57 +01:00
|
|
|
font.setBold(true);
|
2012-03-09 13:15:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
item->setFont(font);
|
|
|
|
item->setText(title);
|
|
|
|
}
|
|
|
|
|
2012-03-10 22:05:57 +01:00
|
|
|
void PodcastService::UpdateEpisodeText(QStandardItem* item,
|
|
|
|
PodcastDownloader::State state,
|
|
|
|
int percent) {
|
|
|
|
const PodcastEpisode episode = item->data(Role_Episode).value<PodcastEpisode>();
|
|
|
|
|
|
|
|
QString title = episode.title();
|
|
|
|
QString tooltip;
|
|
|
|
QFont font;
|
|
|
|
QIcon icon;
|
|
|
|
|
2012-03-11 00:39:09 +01:00
|
|
|
// Unlistened episodes are bold
|
2012-03-10 22:05:57 +01:00
|
|
|
if (!episode.listened()) {
|
|
|
|
font.setBold(true);
|
|
|
|
}
|
|
|
|
|
2012-03-11 00:39:09 +01:00
|
|
|
// Downloaded episodes get an icon
|
2012-03-10 22:05:57 +01:00
|
|
|
if (episode.downloaded()) {
|
|
|
|
if (downloaded_icon_.isNull()) {
|
|
|
|
downloaded_icon_ = IconLoader::Load("document-save");
|
|
|
|
}
|
|
|
|
icon = downloaded_icon_;
|
|
|
|
}
|
|
|
|
|
2012-03-11 00:39:09 +01:00
|
|
|
// Queued or downloading episodes get icons, tooltips, and maybe a title.
|
2012-03-10 22:05:57 +01:00
|
|
|
switch (state) {
|
|
|
|
case PodcastDownloader::Queued:
|
|
|
|
if (queued_icon_.isNull()) {
|
|
|
|
queued_icon_ = QIcon(":icons/22x22/user-away.png");
|
|
|
|
}
|
|
|
|
icon = queued_icon_;
|
|
|
|
tooltip = tr("Download queued");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PodcastDownloader::Downloading:
|
|
|
|
if (downloading_icon_.isNull()) {
|
|
|
|
downloading_icon_ = IconLoader::Load("go-down");
|
|
|
|
}
|
|
|
|
icon = downloading_icon_;
|
|
|
|
tooltip = tr("Downloading (%1%)...").arg(percent);
|
2012-03-11 00:39:09 +01:00
|
|
|
title = QString("[ %1% ] %2").arg(QString::number(percent), episode.title());
|
2012-03-10 22:05:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PodcastDownloader::Finished:
|
|
|
|
case PodcastDownloader::NotDownloading:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->setFont(font);
|
|
|
|
item->setText(title);
|
|
|
|
item->setIcon(icon);
|
|
|
|
}
|
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
QStandardItem* PodcastService::CreatePodcastItem(const Podcast& podcast) {
|
|
|
|
QStandardItem* item = new QStandardItem;
|
|
|
|
|
|
|
|
// Add the episodes in this podcast and gather aggregate stats.
|
|
|
|
int unlistened_count = 0;
|
|
|
|
foreach (const PodcastEpisode& episode, backend_->GetEpisodes(podcast.database_id())) {
|
|
|
|
if (!episode.listened()) {
|
|
|
|
unlistened_count ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->appendRow(CreatePodcastEpisodeItem(episode));
|
2012-03-07 12:04:47 +01:00
|
|
|
}
|
2012-03-06 22:24:41 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
item->setIcon(default_icon_);
|
2012-03-09 13:15:24 +01:00
|
|
|
item->setData(Type_Podcast, InternetModel::Role_Type);
|
2012-03-07 12:22:43 +01:00
|
|
|
item->setData(QVariant::fromValue(podcast), Role_Podcast);
|
2012-03-11 00:39:09 +01:00
|
|
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable);
|
2012-03-09 13:15:24 +01:00
|
|
|
UpdatePodcastText(item, unlistened_count);
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
// Load the podcast's image if it has one
|
2012-03-07 13:27:31 +01:00
|
|
|
if (podcast.ImageUrlSmall().isValid()) {
|
|
|
|
icon_loader_->LoadIcon(podcast.ImageUrlSmall().toString(), QString(), item);
|
2012-03-06 19:37:46 +01:00
|
|
|
}
|
2012-03-07 12:04:47 +01:00
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
podcasts_by_database_id_[podcast.database_id()] = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItem* PodcastService::CreatePodcastEpisodeItem(const PodcastEpisode& episode) {
|
|
|
|
QStandardItem* item = new QStandardItem;
|
|
|
|
item->setText(episode.title());
|
|
|
|
item->setData(Type_Episode, InternetModel::Role_Type);
|
|
|
|
item->setData(QVariant::fromValue(episode), Role_Episode);
|
2012-03-11 00:39:09 +01:00
|
|
|
item->setData(InternetModel::PlayBehaviour_UseSongLoader, InternetModel::Role_PlayBehaviour);
|
|
|
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable);
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-10 22:05:57 +01:00
|
|
|
UpdateEpisodeText(item);
|
|
|
|
|
|
|
|
episodes_by_database_id_[episode.database_id()] = item;
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
return item;
|
2012-03-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:44:43 +01:00
|
|
|
void PodcastService::ShowContextMenu(const QPoint& global_pos) {
|
2012-03-05 19:15:45 +01:00
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu;
|
2012-03-10 23:39:24 +01:00
|
|
|
context_menu_->addAction(
|
|
|
|
IconLoader::Load("list-add"), tr("Add podcast..."),
|
|
|
|
this, SLOT(AddPodcast()));
|
|
|
|
context_menu_->addAction(
|
|
|
|
IconLoader::Load("view-refresh"), tr("Update all podcasts"),
|
|
|
|
app_->podcast_updater(), SLOT(UpdateAllPodcastsNow()));
|
2012-03-12 17:21:05 +01:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addActions(GetPlaylistActions());
|
2012-03-09 21:02:12 +01:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
2012-03-10 23:39:24 +01:00
|
|
|
update_selected_action_ = context_menu_->addAction(
|
|
|
|
IconLoader::Load("view-refresh"), tr("Update this podcast"),
|
|
|
|
this, SLOT(UpdateSelectedPodcast()));
|
2012-03-11 16:36:35 +01:00
|
|
|
download_selected_action_ = context_menu_->addAction(
|
|
|
|
IconLoader::Load("download"), "",
|
|
|
|
this, SLOT(DownloadSelectedEpisode()));
|
|
|
|
delete_downloaded_action_ = context_menu_->addAction(
|
|
|
|
IconLoader::Load("edit-delete"), tr("Delete downloaded data"),
|
|
|
|
this, SLOT(DeleteDownloadedData()));
|
2012-03-10 23:39:24 +01:00
|
|
|
remove_selected_action_ = context_menu_->addAction(
|
|
|
|
IconLoader::Load("list-remove"), tr("Unsubscribe"),
|
|
|
|
this, SLOT(RemoveSelectedPodcast()));
|
2012-03-11 16:36:35 +01:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
|
|
|
set_new_action_ = context_menu_->addAction(
|
|
|
|
tr("Mark as new"), this, SLOT(SetNew()));
|
|
|
|
set_listened_action_ = context_menu_->addAction(
|
|
|
|
tr("Mark as listened"), this, SLOT(SetListened()));
|
2012-03-10 23:39:24 +01:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addAction(
|
|
|
|
IconLoader::Load("configure"), tr("Configure podcasts..."),
|
|
|
|
this, SLOT(ShowConfig()));
|
2012-03-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 16:36:35 +01:00
|
|
|
selected_episodes_.clear();
|
|
|
|
selected_podcasts_.clear();
|
|
|
|
QSet<int> podcast_ids;
|
|
|
|
|
|
|
|
foreach (const QModelIndex& index, model()->selected_indexes()) {
|
|
|
|
switch (index.data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case Type_Podcast: {
|
|
|
|
const int id = index.data(Role_Podcast).value<Podcast>().database_id();
|
|
|
|
if (!podcast_ids.contains(id)) {
|
|
|
|
selected_podcasts_.append(index);
|
|
|
|
podcast_ids.insert(id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-11 16:36:35 +01:00
|
|
|
case Type_Episode: {
|
|
|
|
selected_episodes_.append(index);
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-11 16:36:35 +01:00
|
|
|
// Add the parent podcast as well.
|
|
|
|
const QModelIndex parent = index.parent();
|
|
|
|
const int id = parent.data(Role_Podcast).value<Podcast>().database_id();
|
|
|
|
if (!podcast_ids.contains(id)) {
|
|
|
|
selected_podcasts_.append(parent);
|
|
|
|
podcast_ids.insert(id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-11 16:36:35 +01:00
|
|
|
const bool episodes = !selected_episodes_.isEmpty();
|
|
|
|
const bool podcasts = !selected_podcasts_.isEmpty();
|
|
|
|
|
|
|
|
update_selected_action_->setEnabled(podcasts);
|
|
|
|
remove_selected_action_->setEnabled(podcasts);
|
|
|
|
|
|
|
|
if (selected_episodes_.count() == 1) {
|
|
|
|
const PodcastEpisode episode = selected_episodes_[0].data(Role_Episode).value<PodcastEpisode>();
|
|
|
|
const bool downloaded = episode.downloaded();
|
|
|
|
const bool listened = episode.listened();
|
|
|
|
|
|
|
|
download_selected_action_->setEnabled(!downloaded);
|
|
|
|
delete_downloaded_action_->setEnabled(downloaded);
|
|
|
|
set_new_action_->setEnabled(listened);
|
|
|
|
set_listened_action_->setEnabled(!listened);
|
|
|
|
} else {
|
|
|
|
download_selected_action_->setEnabled(episodes);
|
|
|
|
delete_downloaded_action_->setEnabled(episodes);
|
|
|
|
set_new_action_->setEnabled(episodes);
|
|
|
|
set_listened_action_->setEnabled(episodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selected_episodes_.count() > 1) {
|
|
|
|
download_selected_action_->setText(tr("Download %n episodes", "", selected_episodes_.count()));
|
|
|
|
} else {
|
|
|
|
download_selected_action_->setText(tr("Download this episode"));
|
2012-03-09 13:15:24 +01:00
|
|
|
}
|
2012-03-12 17:21:05 +01:00
|
|
|
|
|
|
|
GetAppendToPlaylistAction()->setEnabled(episodes || podcasts);
|
|
|
|
GetReplacePlaylistAction()->setEnabled(episodes || podcasts);
|
|
|
|
GetOpenInNewPlaylistAction()->setEnabled(episodes || podcasts);
|
2012-03-09 13:15:24 +01:00
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
context_menu_->popup(global_pos);
|
|
|
|
}
|
|
|
|
|
2012-03-09 13:15:24 +01:00
|
|
|
void PodcastService::UpdateSelectedPodcast() {
|
2012-03-11 16:36:35 +01:00
|
|
|
foreach (const QModelIndex& index, selected_podcasts_) {
|
|
|
|
app_->podcast_updater()->UpdatePodcastNow(
|
|
|
|
index.data(Role_Podcast).value<Podcast>());
|
|
|
|
}
|
2012-03-09 13:15:24 +01:00
|
|
|
}
|
|
|
|
|
2012-03-09 21:02:12 +01:00
|
|
|
void PodcastService::RemoveSelectedPodcast() {
|
2012-03-11 16:36:35 +01:00
|
|
|
foreach (const QModelIndex& index, selected_podcasts_) {
|
|
|
|
backend_->Unsubscribe(index.data(Role_Podcast).value<Podcast>());
|
|
|
|
}
|
2012-03-09 21:02:12 +01:00
|
|
|
}
|
|
|
|
|
2012-03-06 22:24:41 +01:00
|
|
|
void PodcastService::ReloadSettings() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(LibraryView::kSettingsGroup);
|
|
|
|
|
|
|
|
use_pretty_covers_ = s.value("pretty_covers", true).toBool();
|
|
|
|
// TODO: reload the podcast icons that are already loaded?
|
|
|
|
}
|
|
|
|
|
2012-03-11 18:57:15 +01:00
|
|
|
void PodcastService::EnsureAddPodcastDialogCreated() {
|
2012-03-05 19:15:45 +01:00
|
|
|
if (!add_podcast_dialog_) {
|
|
|
|
add_podcast_dialog_.reset(new AddPodcastDialog(app_));
|
|
|
|
}
|
2012-03-11 18:57:15 +01:00
|
|
|
}
|
2012-03-05 19:15:45 +01:00
|
|
|
|
2012-03-11 18:57:15 +01:00
|
|
|
void PodcastService::AddPodcast() {
|
|
|
|
EnsureAddPodcastDialogCreated();
|
2012-03-05 19:15:45 +01:00
|
|
|
add_podcast_dialog_->show();
|
|
|
|
}
|
2012-03-07 12:04:47 +01:00
|
|
|
|
|
|
|
void PodcastService::SubscriptionAdded(const Podcast& podcast) {
|
2012-03-12 15:11:24 +01:00
|
|
|
// Ensure the root item is lazy loaded already
|
|
|
|
LazyLoadRoot();
|
|
|
|
|
|
|
|
// The podcast might already be in the list - maybe the LazyLoadRoot() above
|
|
|
|
// added it.
|
|
|
|
QStandardItem* item = podcasts_by_database_id_[podcast.database_id()];
|
|
|
|
if (!item) {
|
|
|
|
item = CreatePodcastItem(podcast);
|
|
|
|
model_->appendRow(item);
|
2012-03-07 12:04:47 +01:00
|
|
|
}
|
|
|
|
|
2012-03-12 15:11:24 +01:00
|
|
|
emit ScrollToIndex(MapToMergedModel(item->index()));
|
2012-03-07 12:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::SubscriptionRemoved(const Podcast& podcast) {
|
2012-03-09 13:15:24 +01:00
|
|
|
QStandardItem* item = podcasts_by_database_id_.take(podcast.database_id());
|
|
|
|
if (item) {
|
2012-03-10 22:05:57 +01:00
|
|
|
// 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<PodcastEpisode>().database_id();
|
|
|
|
|
|
|
|
episodes_by_database_id_.remove(episode_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove this episode's row
|
2012-03-09 19:57:54 +01:00
|
|
|
model_->removeRow(item->row());
|
2012-03-09 13:15:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::EpisodesAdded(const QList<PodcastEpisode>& episodes) {
|
|
|
|
QSet<int> seen_podcast_ids;
|
|
|
|
|
|
|
|
foreach (const PodcastEpisode& episode, episodes) {
|
|
|
|
const int database_id = episode.podcast_database_id();
|
|
|
|
QStandardItem* parent = podcasts_by_database_id_[database_id];
|
|
|
|
if (!parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
parent->appendRow(CreatePodcastEpisodeItem(episode));
|
|
|
|
|
|
|
|
if (!seen_podcast_ids.contains(database_id)) {
|
|
|
|
// Update the unlistened count text once for each podcast
|
|
|
|
int unlistened_count = 0;
|
|
|
|
foreach (const PodcastEpisode& episode, backend_->GetEpisodes(database_id)) {
|
|
|
|
if (!episode.listened()) {
|
|
|
|
unlistened_count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdatePodcastText(parent, unlistened_count);
|
|
|
|
seen_podcast_ids.insert(database_id);
|
2012-03-07 12:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-07 12:04:47 +01:00
|
|
|
}
|
2012-03-10 16:32:36 +01:00
|
|
|
|
2012-03-10 22:05:57 +01:00
|
|
|
void PodcastService::EpisodesUpdated(const QList<PodcastEpisode>& episodes) {
|
|
|
|
QSet<int> seen_podcast_ids;
|
|
|
|
|
|
|
|
foreach (const PodcastEpisode& episode, episodes) {
|
|
|
|
const int podcast_database_id = episode.podcast_database_id();
|
|
|
|
QStandardItem* item = episodes_by_database_id_[episode.database_id()];
|
|
|
|
QStandardItem* parent = podcasts_by_database_id_[podcast_database_id];
|
|
|
|
if (!item || !parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update the episode data on the item, and update the item's text.
|
|
|
|
item->setData(QVariant::fromValue(episode), Role_Episode);
|
|
|
|
UpdateEpisodeText(item);
|
|
|
|
|
|
|
|
// Update the parent podcast's text too.
|
|
|
|
if (!seen_podcast_ids.contains(podcast_database_id)) {
|
|
|
|
// Update the unlistened count text once for each podcast
|
|
|
|
int unlistened_count = 0;
|
|
|
|
foreach (const PodcastEpisode& episode, backend_->GetEpisodes(podcast_database_id)) {
|
|
|
|
if (!episode.listened()) {
|
|
|
|
unlistened_count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdatePodcastText(parent, unlistened_count);
|
|
|
|
seen_podcast_ids.insert(podcast_database_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 16:32:36 +01:00
|
|
|
void PodcastService::DownloadSelectedEpisode() {
|
2012-03-11 16:36:35 +01:00
|
|
|
foreach (const QModelIndex& index, selected_episodes_) {
|
|
|
|
app_->podcast_downloader()->DownloadEpisode(
|
|
|
|
index.data(Role_Episode).value<PodcastEpisode>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::DeleteDownloadedData() {
|
|
|
|
foreach (const QModelIndex& index, selected_episodes_) {
|
|
|
|
app_->podcast_downloader()->DeleteEpisode(
|
|
|
|
index.data(Role_Episode).value<PodcastEpisode>());
|
|
|
|
}
|
2012-03-10 16:32:36 +01:00
|
|
|
}
|
2012-03-10 22:05:57 +01:00
|
|
|
|
|
|
|
void PodcastService::DownloadProgressChanged(const PodcastEpisode& episode,
|
|
|
|
PodcastDownloader::State state,
|
|
|
|
int percent) {
|
|
|
|
QStandardItem* item = episodes_by_database_id_[episode.database_id()];
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UpdateEpisodeText(item, state, percent);
|
|
|
|
}
|
2012-03-10 23:39:24 +01:00
|
|
|
|
|
|
|
void PodcastService::ShowConfig() {
|
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Podcasts);
|
|
|
|
}
|
2012-03-11 13:27:48 +01:00
|
|
|
|
|
|
|
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);
|
2012-03-12 20:35:47 +01:00
|
|
|
episode.set_listened_date(QDateTime::currentDateTime());
|
2012-03-11 13:27:48 +01:00
|
|
|
backend_->UpdateEpisodes(PodcastEpisodeList() << episode);
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 16:36:35 +01:00
|
|
|
|
|
|
|
void PodcastService::SetNew() {
|
|
|
|
SetListened(selected_episodes_, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::SetListened() {
|
|
|
|
SetListened(selected_episodes_, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::SetListened(const QModelIndexList& indexes, bool listened) {
|
|
|
|
PodcastEpisodeList episodes;
|
|
|
|
|
2012-03-12 20:35:47 +01:00
|
|
|
QDateTime current_date_time = QDateTime::currentDateTime();
|
|
|
|
|
2012-03-11 16:36:35 +01:00
|
|
|
foreach (const QModelIndex& index, indexes) {
|
|
|
|
PodcastEpisode episode = index.data(Role_Episode).value<PodcastEpisode>();
|
|
|
|
episode.set_listened(listened);
|
2012-03-12 20:35:47 +01:00
|
|
|
if (listened) {
|
|
|
|
episode.set_listened_date(current_date_time);
|
|
|
|
}
|
2012-03-11 16:36:35 +01:00
|
|
|
episodes << episode;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend_->UpdateEpisodes(episodes);
|
|
|
|
}
|
2012-03-11 18:57:15 +01:00
|
|
|
|
|
|
|
QModelIndex PodcastService::MapToMergedModel(const QModelIndex& index) const {
|
|
|
|
return model()->merged_model()->mapFromSource(proxy_->mapFromSource(index));
|
|
|
|
}
|
|
|
|
|
2012-03-12 15:11:24 +01:00
|
|
|
void PodcastService::LazyLoadRoot() {
|
|
|
|
if (root_->data(InternetModel::Role_CanLazyLoad).toBool()) {
|
|
|
|
root_->setData(false, InternetModel::Role_CanLazyLoad);
|
|
|
|
LazyPopulate(root_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 18:57:15 +01:00
|
|
|
void PodcastService::SubscribeAndShow(const QVariant& podcast_or_opml) {
|
|
|
|
if (podcast_or_opml.canConvert<Podcast>()) {
|
|
|
|
Podcast podcast(podcast_or_opml.value<Podcast>());
|
|
|
|
backend_->Subscribe(&podcast);
|
|
|
|
|
|
|
|
// Lazy load the root item if it hasn't been already
|
2012-03-12 15:11:24 +01:00
|
|
|
LazyLoadRoot();
|
2012-03-11 18:57:15 +01:00
|
|
|
|
|
|
|
QStandardItem* item = podcasts_by_database_id_[podcast.database_id()];
|
|
|
|
if (item) {
|
2012-03-12 15:11:24 +01:00
|
|
|
// There will be an item already if this podcast was already there,
|
|
|
|
// otherwise it'll be scrolled to when the item is created.
|
2012-03-11 18:57:15 +01:00
|
|
|
emit ScrollToIndex(MapToMergedModel(item->index()));
|
|
|
|
}
|
|
|
|
} else if (podcast_or_opml.canConvert<OpmlContainer>()) {
|
|
|
|
EnsureAddPodcastDialogCreated();
|
|
|
|
|
|
|
|
add_podcast_dialog_->ShowWithOpml(podcast_or_opml.value<OpmlContainer>());
|
|
|
|
}
|
|
|
|
}
|