File indexing completed on 2024-05-12 16:21:15

0001 // SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "albummodel.h"
0006 #include "playlistutils.h"
0007 
0008 #include <QStringBuilder>
0009 
0010 AlbumModel::AlbumModel(QObject *parent)
0011     : AbstractYTMusicModel(parent)
0012 {
0013     connect(this, &AlbumModel::browseIdChanged, this, [this] {
0014         setLoading(true);
0015         auto future = YTMusicThread::instance()->fetchAlbum(m_browseId);
0016         QCoro::connect(std::move(future), this, [=, this](album::Album &&album) {
0017             setLoading(false);
0018 
0019             beginResetModel();
0020             m_album = std::move(album);
0021             endResetModel();
0022             std::sort(m_album.thumbnails.begin(), m_album.thumbnails.end());
0023 
0024             Q_EMIT titleChanged();
0025             Q_EMIT artistsChanged();
0026             Q_EMIT thumbnailUrlChanged();
0027             Q_EMIT playlistIdChanged();
0028         });
0029     });
0030 
0031     connect(&YTMusicThread::instance().get(), &AsyncYTMusic::errorOccurred, this, [=, this] {
0032         setLoading(false);
0033     });
0034 }
0035 
0036 int AlbumModel::rowCount(const QModelIndex &parent) const
0037 {
0038     return parent.isValid() ? 0 : int(m_album.tracks.size());
0039 }
0040 
0041 QVariant AlbumModel::data(const QModelIndex &index, int role) const
0042 {
0043     switch (role) {
0044     case Title:
0045         return QString::fromStdString(m_album.tracks[index.row()].title);
0046     case VideoId:
0047         return QString::fromStdString(m_album.tracks[index.row()].video_id.value_or(std::string()));
0048     case Artists:
0049         return QVariant::fromValue(m_album.tracks[index.row()].artists);
0050     case ThumbnailUrl:
0051         if (!m_album.thumbnails.empty()) {
0052             return QString::fromStdString(m_album.thumbnails.front().url);
0053         } else {
0054             return {};
0055         }
0056     case ArtistsDisplayString:
0057         return PlaylistUtils::artistsToString(m_album.tracks[index.row()].artists);
0058     }
0059 
0060     Q_UNREACHABLE();
0061 
0062     return {};
0063 }
0064 
0065 QHash<int, QByteArray> AlbumModel::roleNames() const
0066 {
0067     return {
0068         {Title, "title"},
0069         {VideoId, "videoId"},
0070         {Artists, "artists"},
0071         {ThumbnailUrl, "thumbnailUrl"},
0072         {ArtistsDisplayString, "artistsDisplayString"}
0073     };
0074 }
0075 
0076 QString AlbumModel::browseId() const
0077 {
0078     return m_browseId;
0079 }
0080 
0081 void AlbumModel::setBrowseId(const QString &value)
0082 {
0083     m_browseId = value;
0084     Q_EMIT browseIdChanged();
0085 }
0086 
0087 QString AlbumModel::title() const
0088 {
0089     return QString::fromStdString(m_album.title);
0090 }
0091 
0092 QString AlbumModel::artists() const
0093 {
0094     return PlaylistUtils::artistsToString(m_album.artists);
0095 }
0096 
0097 
0098 QUrl AlbumModel::thumbnailUrl() const
0099 {
0100     if (m_album.thumbnails.empty()) {
0101         return QUrl();
0102     }
0103 
0104     return QUrl(QString::fromStdString(m_album.thumbnails.back().url));
0105 }
0106 
0107 QString AlbumModel::playlistId() const
0108 {
0109     return QString::fromStdString(m_album.audio_playlist_id);
0110 }
0111 
0112 QUrl AlbumModel::webUrl() const
0113 {
0114     return QUrl(YTMUSIC_WEB_BASE_URL % "/playlist?list=" % QString::fromStdString(m_album.audio_playlist_id));
0115 }
0116 
0117 const album::Album &AlbumModel::album() const
0118 {
0119     return m_album;
0120 }