2012-03-05 19:15:45 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2014-11-02 19:50:39 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-11-01 19:38:39 +01:00
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-03-07 16:11:56 +01:00
|
|
|
#include "opmlcontainer.h"
|
2012-03-05 19:15:45 +01:00
|
|
|
#include "podcast.h"
|
|
|
|
#include "podcastdiscoverymodel.h"
|
|
|
|
#include "core/application.h"
|
2012-03-06 01:35:55 +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 <QIcon>
|
|
|
|
#include <QSet>
|
|
|
|
|
|
|
|
PodcastDiscoveryModel::PodcastDiscoveryModel(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QStandardItemModel(parent),
|
|
|
|
app_(app),
|
|
|
|
icon_loader_(new StandardItemIconLoader(app->album_cover_loader(), this)),
|
|
|
|
default_icon_(":providers/podcast16.png") {
|
2012-03-06 22:24:41 +01:00
|
|
|
icon_loader_->SetModel(this);
|
2012-03-05 19:15:45 +01:00
|
|
|
}
|
|
|
|
|
2012-03-06 17:37:15 +01:00
|
|
|
QVariant PodcastDiscoveryModel::data(const QModelIndex& index, int role) const {
|
2012-03-07 16:11:56 +01:00
|
|
|
if (index.isValid() && role == Qt::DecorationRole &&
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItemModel::data(index, Role_StartedLoadingImage).toBool() ==
|
|
|
|
false) {
|
|
|
|
const QUrl image_url =
|
|
|
|
QStandardItemModel::data(index, Role_ImageUrl).toUrl();
|
2012-03-07 16:11:56 +01:00
|
|
|
if (image_url.isValid()) {
|
|
|
|
const_cast<PodcastDiscoveryModel*>(this)->LazyLoadImage(image_url, index);
|
|
|
|
}
|
2012-03-06 17:37:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return QStandardItemModel::data(index, role);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* PodcastDiscoveryModel::CreatePodcastItem(
|
|
|
|
const Podcast& podcast) {
|
2012-03-05 19:15:45 +01:00
|
|
|
QStandardItem* item = new QStandardItem;
|
|
|
|
item->setIcon(default_icon_);
|
|
|
|
item->setText(podcast.title());
|
|
|
|
item->setData(QVariant::fromValue(podcast), Role_Podcast);
|
|
|
|
item->setData(Type_Podcast, Role_Type);
|
2012-03-07 16:11:56 +01:00
|
|
|
item->setData(podcast.ImageUrlSmall(), Role_ImageUrl);
|
2012-03-05 19:15:45 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:35:55 +01:00
|
|
|
QStandardItem* PodcastDiscoveryModel::CreateFolder(const QString& name) {
|
|
|
|
if (folder_icon_.isNull()) {
|
|
|
|
folder_icon_ = IconLoader::Load("folder");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItem* item = new QStandardItem;
|
|
|
|
item->setIcon(folder_icon_);
|
|
|
|
item->setText(name);
|
|
|
|
item->setData(Type_Folder, Role_Type);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* PodcastDiscoveryModel::CreateOpmlContainerItem(
|
|
|
|
const OpmlContainer& container) {
|
2012-03-07 16:11:56 +01:00
|
|
|
QStandardItem* item = CreateFolder(container.name);
|
|
|
|
CreateOpmlContainerItems(container, item);
|
|
|
|
return item;
|
|
|
|
}
|
2012-03-06 17:37:15 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PodcastDiscoveryModel::CreateOpmlContainerItems(
|
|
|
|
const OpmlContainer& container, QStandardItem* parent) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const OpmlContainer& child : container.containers) {
|
2012-03-07 16:11:56 +01:00
|
|
|
QStandardItem* child_item = CreateOpmlContainerItem(child);
|
|
|
|
parent->appendRow(child_item);
|
|
|
|
}
|
2012-03-06 17:37:15 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Podcast& child : container.feeds) {
|
2012-03-07 16:11:56 +01:00
|
|
|
QStandardItem* child_item = CreatePodcastItem(child);
|
|
|
|
parent->appendRow(child_item);
|
2012-03-06 17:37:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PodcastDiscoveryModel::LazyLoadImage(const QUrl& url,
|
|
|
|
const QModelIndex& index) {
|
2012-03-07 16:11:56 +01:00
|
|
|
QStandardItem* item = itemFromIndex(index);
|
|
|
|
item->setData(true, Role_StartedLoadingImage);
|
|
|
|
icon_loader_->LoadIcon(url.toString(), QString(), item);
|
|
|
|
}
|
|
|
|
|
2012-03-06 17:37:15 +01:00
|
|
|
QStandardItem* PodcastDiscoveryModel::CreateLoadingIndicator() {
|
|
|
|
QStandardItem* item = new QStandardItem;
|
|
|
|
item->setText(tr("Loading..."));
|
|
|
|
item->setData(Type_LoadingIndicator, Role_Type);
|
|
|
|
return item;
|
|
|
|
}
|