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-06 19:37:46 +01:00
|
|
|
#include "podcastbackend.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
#include "podcastservice.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";
|
|
|
|
|
|
|
|
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()),
|
|
|
|
model_(new QStandardItemModel(this)),
|
|
|
|
proxy_(new QSortFilterProxyModel(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-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PodcastService::~PodcastService() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-07 12:04:47 +01:00
|
|
|
QStandardItem* PodcastService::CreatePodcastItem(const Podcast& podcast) {
|
|
|
|
const int unlistened_count = podcast.extra("db:unlistened_count").toInt();
|
|
|
|
QString title = podcast.title();
|
2012-03-06 19:37:46 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
QStandardItem* item = new QStandardItem;
|
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
|
|
|
|
QFont font(item->font());
|
|
|
|
font.setBold(true);
|
|
|
|
item->setFont(font);
|
|
|
|
}
|
2012-03-06 22:24:41 +01:00
|
|
|
|
2012-03-07 12:04:47 +01:00
|
|
|
item->setText(podcast.title());
|
|
|
|
item->setIcon(default_icon_);
|
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
|
|
|
|
if (podcast.image_url().isValid()) {
|
|
|
|
icon_loader_->LoadIcon(podcast.image_url().toString(), QString(), item);
|
2012-03-06 19:37:46 +01:00
|
|
|
}
|
2012-03-07 12:04:47 +01:00
|
|
|
|
|
|
|
return item;
|
2012-03-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::ShowContextMenu(const QModelIndex& index,
|
|
|
|
const QPoint& global_pos) {
|
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu;
|
|
|
|
context_menu_->addAction(IconLoader::Load("list-add"), tr("Add podcast..."),
|
|
|
|
this, SLOT(AddPodcast()));
|
|
|
|
}
|
|
|
|
|
|
|
|
context_menu_->popup(global_pos);
|
|
|
|
}
|
|
|
|
|
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-05 19:15:45 +01:00
|
|
|
QModelIndex PodcastService::GetCurrentIndex() {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::AddPodcast() {
|
|
|
|
if (!add_podcast_dialog_) {
|
|
|
|
add_podcast_dialog_.reset(new AddPodcastDialog(app_));
|
|
|
|
}
|
|
|
|
|
|
|
|
add_podcast_dialog_->show();
|
|
|
|
}
|
2012-03-07 12:04:47 +01:00
|
|
|
|
|
|
|
void PodcastService::SubscriptionAdded(const Podcast& podcast) {
|
|
|
|
// If the user hasn't expanded the root node yet we don't need to do anything
|
|
|
|
if (root_->data(InternetModel::Role_CanLazyLoad).toBool()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
model_->appendRow(CreatePodcastItem(podcast));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastService::SubscriptionRemoved(const Podcast& podcast) {
|
|
|
|
}
|