2010-11-23 23:36:00 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, 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 "icecastbackend.h"
|
|
|
|
#include "icecastmodel.h"
|
2010-11-24 20:25:41 +01:00
|
|
|
#include "playlist/songmimedata.h"
|
2010-11-23 23:36:00 +01:00
|
|
|
IcecastModel::IcecastModel(IcecastBackend* backend, QObject* parent)
|
|
|
|
: SimpleTreeModel<IcecastItem>(new IcecastItem(this), parent),
|
|
|
|
backend_(backend),
|
|
|
|
sort_mode_(SortMode_GenreByPopularity),
|
|
|
|
genre_icon_(":last.fm/icon_tag.png"),
|
|
|
|
station_icon_(":last.fm/icon_radio.png")
|
|
|
|
{
|
|
|
|
root_->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
IcecastModel::~IcecastModel() {
|
|
|
|
delete root_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IcecastModel::Init() {
|
|
|
|
connect(backend_, SIGNAL(DatabaseReset()), SLOT(Reset()));
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IcecastModel::Reset() {
|
|
|
|
delete root_;
|
|
|
|
|
|
|
|
root_ = new IcecastItem(this);
|
|
|
|
root_->lazy_loaded = false;
|
|
|
|
|
|
|
|
LazyPopulate(root_);
|
2010-11-24 00:05:42 +01:00
|
|
|
|
|
|
|
reset();
|
2010-11-23 23:36:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IcecastModel::LazyPopulate(IcecastItem* parent) {
|
|
|
|
if (parent->lazy_loaded)
|
|
|
|
return;
|
|
|
|
parent->lazy_loaded = true;
|
|
|
|
|
|
|
|
switch (parent->type) {
|
|
|
|
case IcecastItem::Type_Station:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case IcecastItem::Type_Genre:
|
2010-11-24 22:34:54 +01:00
|
|
|
PopulateGenre(parent, parent->key, false);
|
2010-11-23 23:36:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IcecastItem::Type_Root:
|
|
|
|
switch (sort_mode_) {
|
|
|
|
case SortMode_GenreAlphabetical:
|
2010-11-24 22:34:54 +01:00
|
|
|
AddGenres(backend_->GetGenresAlphabetical(filter_), true);
|
2010-11-23 23:36:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SortMode_GenreByPopularity:
|
2010-11-24 22:34:54 +01:00
|
|
|
AddGenres(backend_->GetGenresByPopularity(filter_), false);
|
2010-11-23 23:36:00 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SortMode_StationAlphabetical:
|
2010-11-24 22:34:54 +01:00
|
|
|
PopulateGenre(parent, QString(), true);
|
2010-11-23 23:36:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 22:34:54 +01:00
|
|
|
void IcecastModel::PopulateGenre(IcecastItem* parent, const QString& genre,
|
|
|
|
bool create_dividers) {
|
|
|
|
QChar last_divider;
|
|
|
|
|
2010-11-24 00:05:42 +01:00
|
|
|
IcecastBackend::StationList stations = backend_->GetStations(filter_, genre);
|
2010-11-23 23:36:00 +01:00
|
|
|
foreach (const IcecastBackend::Station& station, stations) {
|
2010-11-24 22:34:54 +01:00
|
|
|
QChar divider_char = DividerKey(station.name);
|
|
|
|
if (create_dividers && !divider_char.isNull() && divider_char != last_divider) {
|
|
|
|
last_divider = divider_char;
|
|
|
|
|
|
|
|
IcecastItem* divider = new IcecastItem(IcecastItem::Type_Divider, parent);
|
|
|
|
divider->display_text = DividerDisplayText(divider_char);
|
|
|
|
divider->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 23:36:00 +01:00
|
|
|
IcecastItem* item = new IcecastItem(IcecastItem::Type_Station, parent);
|
2010-11-24 20:25:41 +01:00
|
|
|
item->station = station;
|
2010-11-23 23:36:00 +01:00
|
|
|
item->display_text = station.name;
|
|
|
|
item->sort_text = station.name;
|
|
|
|
item->key = station.url.toString();
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 22:34:54 +01:00
|
|
|
void IcecastModel::AddGenres(const QStringList& genres, bool create_dividers) {
|
|
|
|
QChar last_divider;
|
|
|
|
|
2010-11-23 23:36:00 +01:00
|
|
|
foreach (const QString& genre, genres) {
|
2010-11-24 22:34:54 +01:00
|
|
|
QChar divider_char = DividerKey(genre);
|
|
|
|
if (create_dividers && divider_char != last_divider) {
|
|
|
|
last_divider = divider_char;
|
|
|
|
|
|
|
|
IcecastItem* divider = new IcecastItem(IcecastItem::Type_Divider, root_);
|
|
|
|
divider->display_text = DividerDisplayText(divider_char);
|
|
|
|
divider->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 23:36:00 +01:00
|
|
|
IcecastItem* item = new IcecastItem(IcecastItem::Type_Genre, root_);
|
|
|
|
item->key = genre;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 22:34:54 +01:00
|
|
|
QChar IcecastModel::DividerKey(const QString& text) {
|
|
|
|
if (text.isEmpty())
|
|
|
|
return QChar();
|
|
|
|
|
|
|
|
QChar c;
|
|
|
|
c = text[0];
|
|
|
|
|
|
|
|
if (c.isDigit())
|
|
|
|
return '0';
|
|
|
|
if (c.isPunct() || c.isSymbol())
|
|
|
|
return QChar();
|
|
|
|
|
|
|
|
if (c.decompositionTag() != QChar::NoDecomposition)
|
|
|
|
return QChar(c.decomposition()[0]);
|
|
|
|
return c.toUpper();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString IcecastModel::DividerDisplayText(const QChar& key) {
|
|
|
|
if (key == '0')
|
|
|
|
return "0-9";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2010-11-23 23:36:00 +01:00
|
|
|
QVariant IcecastModel::data(const QModelIndex& index, int role) const {
|
|
|
|
const IcecastItem* item = IndexToItem(index);
|
|
|
|
return data(item, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant IcecastModel::data(const IcecastItem* item, int role) const {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return item->DisplayText();
|
|
|
|
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
switch (item->type) {
|
|
|
|
case IcecastItem::Type_Genre: return genre_icon_;
|
|
|
|
case IcecastItem::Type_Station: return station_icon_;
|
|
|
|
}
|
|
|
|
break;
|
2010-11-24 22:34:54 +01:00
|
|
|
|
|
|
|
case Role_IsDivider:
|
|
|
|
return item->type == IcecastItem::Type_Divider;
|
2010-11-23 23:36:00 +01:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
2010-11-24 00:05:42 +01:00
|
|
|
|
|
|
|
void IcecastModel::SetFilterText(const QString& filter) {
|
|
|
|
filter_ = filter;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IcecastModel::SetSortMode(SortMode mode) {
|
|
|
|
sort_mode_ = mode;
|
|
|
|
Reset();
|
|
|
|
}
|
2010-11-24 20:25:41 +01:00
|
|
|
|
|
|
|
Qt::ItemFlags IcecastModel::flags(const QModelIndex& index) const {
|
|
|
|
switch (IndexToItem(index)->type) {
|
|
|
|
case IcecastItem::Type_Station:
|
|
|
|
return Qt::ItemIsSelectable |
|
|
|
|
Qt::ItemIsEnabled |
|
|
|
|
Qt::ItemIsDragEnabled;
|
|
|
|
case IcecastItem::Type_Genre:
|
|
|
|
case IcecastItem::Type_Root:
|
2010-11-24 22:34:54 +01:00
|
|
|
case IcecastItem::Type_Divider:
|
2010-11-24 20:25:41 +01:00
|
|
|
default:
|
2011-07-05 21:48:58 +02:00
|
|
|
return Qt::ItemIsSelectable |
|
|
|
|
Qt::ItemIsEnabled;
|
2010-11-24 20:25:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList IcecastModel::mimeTypes() const {
|
|
|
|
return QStringList() << "text/uri-list";
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* IcecastModel::mimeData(const QModelIndexList& indexes) const {
|
|
|
|
if (indexes.isEmpty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SongMimeData* data = new SongMimeData;
|
|
|
|
QList<QUrl> urls;
|
|
|
|
|
|
|
|
foreach (const QModelIndex& index, indexes) {
|
|
|
|
IcecastItem* item = IndexToItem(index);
|
2010-11-24 20:41:17 +01:00
|
|
|
if (!item || item->type != IcecastItem::Type_Station)
|
2010-11-24 20:25:41 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
data->songs << item->station.ToSong();
|
|
|
|
urls << item->station.url;
|
|
|
|
}
|
|
|
|
|
2010-11-24 20:41:17 +01:00
|
|
|
if (data->songs.isEmpty()) {
|
|
|
|
delete data;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-24 20:25:41 +01:00
|
|
|
data->setUrls(urls);
|
2011-02-16 19:29:35 +01:00
|
|
|
data->name_for_new_playlist_ = "Icecast";
|
|
|
|
|
2010-11-24 20:25:41 +01:00
|
|
|
return data;
|
|
|
|
}
|
2010-11-24 20:41:17 +01:00
|
|
|
|
|
|
|
Song IcecastModel::GetSong(const QModelIndex& index) const {
|
|
|
|
IcecastItem* item = IndexToItem(index);
|
|
|
|
if (!item || item->type != IcecastItem::Type_Station)
|
|
|
|
return Song();
|
|
|
|
|
|
|
|
return item->station.ToSong();
|
|
|
|
}
|