2 * Copyright (C) 2016 The Qt Company Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "playlistwithmetadata.h"
19 #include <QtCore/QDebug>
20 #include <QtCore/QBuffer>
21 #include <QtGui/QImage>
22 #include <QtGui/QImageWriter>
23 #include <QtMultimedia/QMediaPlayer>
24 #include <QtMultimedia/QMediaMetaData>
26 class PlaylistWithMetadata::Private
29 Private(PlaylistWithMetadata *parent);
35 void loadMetadata(int row);
38 PlaylistWithMetadata *q;
41 QAbstractListModel *source;
42 QList<QMetaObject::Connection> connections;
44 QHash<QUrl, QString> title;
45 QHash<QUrl, QString> artist;
46 QHash<QUrl, QUrl> coverArt;
47 QHash<QUrl, qint64> duration;
48 QHash<QUrl, QMediaPlayer *> players;
51 PlaylistWithMetadata::Private::Private(PlaylistWithMetadata *parent)
57 void PlaylistWithMetadata::Private::disconnect()
60 for (const auto &connection : connections)
61 q->disconnect(connection);
66 void PlaylistWithMetadata::Private::connect()
69 connections.append(q->connect(source, &QAbstractListModel::rowsAboutToBeInserted, [&](const QModelIndex &parent, int first, int last) {
71 q->beginInsertRows(QModelIndex(), first, last);
73 connections.append(q->connect(source, &QAbstractListModel::rowsInserted, [&](const QModelIndex &parent, int first, int last) {
75 for (int i = first; i <= last; i++) {
81 int count = source->rowCount();
83 q->beginInsertRows(QModelIndex(), 0, count);
84 for (int i = 0; i < count; i++) {
92 void PlaylistWithMetadata::Private::loadMetadata(int row)
94 QUrl url = source->data(source->index(row), Qt::UserRole + 1).toUrl();
95 QMediaPlayer *player = new QMediaPlayer(q);
97 players.insert(url, player);
98 q->connect(player, &QMediaPlayer::mediaStatusChanged, [this, url](QMediaPlayer::MediaStatus mediaStatus) {
99 switch (mediaStatus) {
100 case QMediaPlayer::NoMedia:
101 case QMediaPlayer::LoadedMedia: {
102 QMediaPlayer *player = players.take(url);
103 title.insert(url, player->metaData(QMediaMetaData::Title).toString());
104 artist.insert(url, player->metaData(QMediaMetaData::ContributingArtist).toString());
105 QVariant coverArtImage = player->metaData(QMediaMetaData::CoverArtImage);
106 if (coverArtImage.type() == QVariant::Image) {
107 QImage image = coverArtImage.value<QImage>();
109 QBuffer buffer(&data);
110 buffer.open(QBuffer::WriteOnly);
111 QImageWriter png(&buffer, "png");
112 if (png.write(image)) {
114 coverArt.insert(url, QUrl(QStringLiteral("data:image/png;base64,") + data.toBase64()));
117 duration.insert(url, player->duration());
118 QModelIndex index = q->index(urls.indexOf(url));
119 q->dataChanged(index, index, QVector<int>() << TitleRole << ArtistRole << CoverArtRole << DurationRole);
120 player->deleteLater();
127 player->setMedia(url);
130 PlaylistWithMetadata::PlaylistWithMetadata(QObject *parent)
131 : QAbstractListModel(parent)
132 , d(new Private(this))
136 PlaylistWithMetadata::~PlaylistWithMetadata()
141 int PlaylistWithMetadata::rowCount(const QModelIndex &parent) const
144 if (parent.isValid())
147 ret = d->source->rowCount(QModelIndex());
151 QVariant PlaylistWithMetadata::data(const QModelIndex &index, int role) const
154 if (!index.isValid())
156 int row = index.row();
157 if (row < 0 || rowCount() <= row)
159 QUrl url = d->urls.at(row);
162 ret = d->title.value(url);
163 if (ret.toString().isEmpty())
164 ret = QVariant(url.fileName());
167 ret = d->artist.value(url);
170 ret = d->coverArt.value(url);
176 ret = d->duration.value(url);
185 QHash<int, QByteArray> PlaylistWithMetadata::roleNames() const
188 {TitleRole, "title"},
189 {ArtistRole, "artist"},
190 {CoverArtRole, "coverArt"},
191 {SourceRole, "source"},
192 {DurationRole, "duration"}
196 QAbstractListModel *PlaylistWithMetadata::source() const
201 void PlaylistWithMetadata::setSource(QAbstractListModel *source)
203 if (d->source == source) return;
207 emit sourceChanged(source);