Support drag&drop of icecast streams to the playlist

This commit is contained in:
David Sansome 2010-11-24 19:25:41 +00:00
parent 9a8d01e2bd
commit c43a06c04e
6 changed files with 60 additions and 5 deletions

View File

@ -575,9 +575,9 @@ bool Playlist::dropMimeData(const QMimeData* data, Qt::DropAction action, int ro
// Dragged from a library
// We want to check if these songs are from the actual local file backend,
// if they are we treat them differently.
if (song_data->backend->songs_table() == Library::kSongsTable)
if (song_data->backend && song_data->backend->songs_table() == Library::kSongsTable)
InsertLibraryItems(song_data->songs, row);
else if (song_data->backend->songs_table() == MagnatuneService::kSongsTable)
else if (song_data->backend && song_data->backend->songs_table() == MagnatuneService::kSongsTable)
InsertMagnatuneItems(song_data->songs, row);
else
InsertSongs(song_data->songs, row);

View File

@ -172,3 +172,14 @@ void IcecastBackend::ClearAndAddStations(const StationList& stations) {
emit DatabaseReset();
}
Song IcecastBackend::Station::ToSong() const {
Song ret;
ret.set_valid(true);
ret.set_title(name);
ret.set_filename(url.toEncoded());
ret.set_bitrate(bitrate);
ret.set_samplerate(samplerate);
ret.set_genre(genre);
return ret;
}

View File

@ -18,6 +18,8 @@
#ifndef ICECASTBACKEND_H
#define ICECASTBACKEND_H
#include "core/song.h"
#include <QObject>
#include <QUrl>
@ -48,6 +50,8 @@ public:
int channels;
int samplerate;
QString genre;
Song ToSong() const;
};
typedef QList<Station> StationList;

View File

@ -18,6 +18,7 @@
#ifndef ICECASTITEM_H
#define ICECASTITEM_H
#include "icecastbackend.h"
#include "core/simpletreeitem.h"
class IcecastItem : public SimpleTreeItem<IcecastItem> {
@ -32,6 +33,8 @@ public:
: SimpleTreeItem<IcecastItem>(Type_Root, model) {}
IcecastItem(Type type, IcecastItem* parent = NULL)
: SimpleTreeItem<IcecastItem>(type, parent) {}
IcecastBackend::Station station;
};
#endif // ICECASTITEM_H

View File

@ -17,7 +17,7 @@
#include "icecastbackend.h"
#include "icecastmodel.h"
#include "playlist/songmimedata.h"
IcecastModel::IcecastModel(IcecastBackend* backend, QObject* parent)
: SimpleTreeModel<IcecastItem>(new IcecastItem(this), parent),
backend_(backend),
@ -84,6 +84,7 @@ void IcecastModel::PopulateGenre(IcecastItem* parent, const QString& genre) {
IcecastBackend::StationList stations = backend_->GetStations(filter_, genre);
foreach (const IcecastBackend::Station& station, stations) {
IcecastItem* item = new IcecastItem(IcecastItem::Type_Station, parent);
item->station = station;
item->display_text = station.name;
item->sort_text = station.name;
item->key = station.url.toString();
@ -128,3 +129,40 @@ void IcecastModel::SetSortMode(SortMode mode) {
sort_mode_ = mode;
Reset();
}
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:
default:
return Qt::ItemIsEnabled;
}
}
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);
if (!item)
continue;
data->songs << item->station.ToSong();
urls << item->station.url;
}
data->setUrls(urls);
return data;
}

View File

@ -43,10 +43,9 @@ public:
// QAbstractItemModel
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
/*Qt::ItemFlags flags(const QModelIndex& index) const;
Qt::ItemFlags flags(const QModelIndex& index) const;
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList& indexes) const;
bool canFetchMore(const QModelIndex &parent) const;*/
public slots:
void Init();