File indexing completed on 2024-05-12 16:21:16
0001 // SPDX-FileCopyrightText: 2022 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 "localplaylistmodel.h" 0006 0007 #include "library.h" 0008 0009 LocalPlaylistModel::LocalPlaylistModel(QObject *parent) 0010 : QAbstractListModel(parent) 0011 { 0012 connect(this, &LocalPlaylistModel::playlistIdChanged, 0013 this, &LocalPlaylistModel::refreshModel); 0014 connect(this, &LocalPlaylistModel::playlistIdChanged, 0015 this, &LocalPlaylistModel::refreshModel); 0016 } 0017 0018 int LocalPlaylistModel::rowCount(const QModelIndex &index) const 0019 { 0020 return index.isValid() ? 0 : m_entries.size(); 0021 } 0022 0023 QHash<int, QByteArray> LocalPlaylistModel::roleNames() const 0024 { 0025 return { 0026 { Roles::VideoId, "videoId" }, 0027 { Roles::Title, "title" }, 0028 { Roles::Artists, "artists"}, 0029 { Roles::ArtistsDisplayString, "artistsDisplayString"} 0030 0031 }; 0032 } 0033 0034 QVariant LocalPlaylistModel::data(const QModelIndex &index, int role) const 0035 { 0036 switch (role) { 0037 case Roles::Title: 0038 return m_entries[index.row()].title; 0039 case Roles::VideoId: 0040 return m_entries[index.row()].videoId; 0041 case Roles::Artists: 0042 return QVariant::fromValue(std::vector<meta::Artist> { 0043 { 0044 m_entries.at(index.row()).artists.toStdString(), 0045 {} 0046 } 0047 }); 0048 case Roles::ArtistsDisplayString: 0049 return m_entries.at(index.row()).artists; 0050 } 0051 0052 Q_UNREACHABLE(); 0053 } 0054 0055 QString LocalPlaylistModel::playlistId() const 0056 { 0057 return m_playlistId; 0058 } 0059 0060 void LocalPlaylistModel::setPlaylistId(const QString &playlistId) 0061 { 0062 m_playlistId = playlistId; 0063 Q_EMIT playlistIdChanged(); 0064 } 0065 0066 void LocalPlaylistModel::refreshModel() 0067 { 0068 auto future = Library::instance() 0069 .database() 0070 .getResults<PlaylistEntry>( 0071 "select video_id, title, artist, album from " 0072 "playlist_entries natural join songs where playlist_id = ?", m_playlistId); 0073 0074 QCoro::connect(std::move(future), this, [this](auto &&entries) { 0075 beginResetModel(); 0076 m_entries = std::move(entries); 0077 endResetModel(); 0078 }); 0079 } 0080 0081 const std::vector<PlaylistEntry> &LocalPlaylistModel::entries() const 0082 { 0083 return m_entries; 0084 } 0085 0086 void LocalPlaylistModel::removeSong(QString videoId, qint64 playlistId) 0087 { 0088 QCoro::connect(Library::instance().database().execute("delete from playlist_entries where playlist_id = ? and video_id = ?", playlistId, videoId), this, &LocalPlaylistModel::refreshModel); 0089 }