qt: medialib: implement separate data loaders
[vlc.git] / modules / gui / qt / medialibrary / mlalbumtrackmodel.cpp
blobfab64930f8265491c4c41337146f6012ef452179
1 /*****************************************************************************
2 * Copyright (C) 2019 VLC authors and VideoLAN
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * ( at your option ) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *****************************************************************************/
19 #include "mlalbumtrackmodel.hpp"
21 QHash<QByteArray, vlc_ml_sorting_criteria_t> MLAlbumTrackModel::M_names_to_criteria = {
22 {"id", VLC_ML_SORTING_DEFAULT},
23 {"title", VLC_ML_SORTING_ALPHA},
24 {"album_title", VLC_ML_SORTING_ALBUM},
25 {"track_number", VLC_ML_SORTING_TRACKNUMBER},
26 {"release_year", VLC_ML_SORTING_RELEASEDATE},
27 {"main_artist", VLC_ML_SORTING_ARTIST},
28 {"duration", VLC_ML_SORTING_DURATION},
29 {"duration_short", VLC_ML_SORTING_DURATION}
32 MLAlbumTrackModel::MLAlbumTrackModel(QObject *parent)
33 : MLSlidingWindowModel<MLAlbumTrack>(parent)
37 QVariant MLAlbumTrackModel::data(const QModelIndex &index, int role) const
39 if (!index.isValid() || index.row() < 0)
40 return QVariant();
42 const MLAlbumTrack* ml_track = item(index.row());
43 if ( !ml_track )
44 return QVariant();
46 switch (role)
48 // Tracks
49 case TRACK_ID:
50 return QVariant::fromValue( ml_track->getId() );
51 case TRACK_TITLE :
52 return QVariant::fromValue( ml_track->getTitle() );
53 case TRACK_COVER :
54 return QVariant::fromValue( ml_track->getCover() );
55 case TRACK_NUMBER :
56 return QVariant::fromValue( ml_track->getTrackNumber() );
57 case TRACK_DISC_NUMBER:
58 return QVariant::fromValue( ml_track->getDiscNumber() );
59 case TRACK_DURATION :
60 return QVariant::fromValue( ml_track->getDuration() );
61 case TRACK_DURATION_SHORT :
62 return QVariant::fromValue( ml_track->getDurationShort() );
63 case TRACK_ALBUM:
64 return QVariant::fromValue( ml_track->getAlbumTitle() );
65 case TRACK_ARTIST:
66 return QVariant::fromValue( ml_track->getArtist() );
67 case TRACK_TITLE_FIRST_SYMBOL:
68 return QVariant::fromValue( getFirstSymbol( ml_track->getTitle() ) );
69 case TRACK_ALBUM_FIRST_SYMBOL:
70 return QVariant::fromValue( getFirstSymbol( ml_track->getAlbumTitle() ) );
71 case TRACK_ARTIST_FIRST_SYMBOL:
72 return QVariant::fromValue( getFirstSymbol( ml_track->getArtist() ) );
73 default :
74 return QVariant();
78 QHash<int, QByteArray> MLAlbumTrackModel::roleNames() const
80 return {
81 { TRACK_ID, "id" },
82 { TRACK_TITLE, "title" },
83 { TRACK_COVER, "cover" },
84 { TRACK_NUMBER, "track_number" },
85 { TRACK_DISC_NUMBER, "disc_number" },
86 { TRACK_DURATION, "duration" },
87 { TRACK_DURATION_SHORT, "duration_short" },
88 { TRACK_ALBUM, "album_title"},
89 { TRACK_ARTIST, "main_artist"},
90 { TRACK_TITLE_FIRST_SYMBOL, "title_first_symbol"},
91 { TRACK_ALBUM_FIRST_SYMBOL, "album_title_first_symbol"},
92 { TRACK_ARTIST_FIRST_SYMBOL, "main_artist_first_symbol"},
96 vlc_ml_sorting_criteria_t MLAlbumTrackModel::roleToCriteria(int role) const
98 switch (role) {
99 case TRACK_TITLE :
100 return VLC_ML_SORTING_ALPHA;
101 case TRACK_NUMBER :
102 return VLC_ML_SORTING_TRACKNUMBER;
103 case TRACK_DURATION :
104 case TRACK_DURATION_SHORT :
105 return VLC_ML_SORTING_DURATION;
106 default:
107 return VLC_ML_SORTING_DEFAULT;
111 vlc_ml_sorting_criteria_t MLAlbumTrackModel::nameToCriteria(QByteArray name) const
113 return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
116 QByteArray MLAlbumTrackModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const
118 return M_names_to_criteria.key(criteria, "");
121 void MLAlbumTrackModel::onVlcMlEvent(const MLEvent &event)
123 switch (event.i_type)
125 case VLC_ML_EVENT_MEDIA_ADDED:
126 if ( event.creation.media.i_subtype == VLC_ML_MEDIA_SUBTYPE_ALBUMTRACK )
127 m_need_reset = true;
128 break;
129 case VLC_ML_EVENT_MEDIA_UPDATED:
130 case VLC_ML_EVENT_MEDIA_DELETED:
131 // FIXME: Not optimal, this will trigger a clean/refresh for video
132 // media as well, but this needs fixing in the medialibrary
133 m_need_reset = true;
134 break;
135 case VLC_ML_EVENT_ALBUM_UPDATED:
136 if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_ALBUM &&
137 m_parent.id == event.modification.i_entity_id )
138 m_need_reset = true;
139 break;
140 case VLC_ML_EVENT_ALBUM_DELETED:
141 if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_ALBUM &&
142 m_parent.id == event.deletion.i_entity_id )
143 m_need_reset = true;
144 break;
145 case VLC_ML_EVENT_GENRE_DELETED:
146 if ( m_parent.id != 0 && m_parent.type == VLC_ML_PARENT_GENRE &&
147 m_parent.id == event.deletion.i_entity_id )
148 m_need_reset = true;
149 break;
151 MLBaseModel::onVlcMlEvent( event );
154 ListCacheLoader<std::unique_ptr<MLAlbumTrack>> *
155 MLAlbumTrackModel::createLoader() const
157 return new Loader(*this);
160 size_t MLAlbumTrackModel::Loader::count() const
162 MLQueryParams params = getParams();
163 auto queryParams = params.toCQueryParams();
165 if ( m_parent.id <= 0 )
166 return vlc_ml_count_audio_media(m_ml, &queryParams);
167 return vlc_ml_count_media_of(m_ml, &queryParams, m_parent.type, m_parent.id );
170 std::vector<std::unique_ptr<MLAlbumTrack>>
171 MLAlbumTrackModel::Loader::load(size_t index, size_t count) const
173 MLQueryParams params = getParams(index, count);
174 auto queryParams = params.toCQueryParams();
176 ml_unique_ptr<vlc_ml_media_list_t> media_list;
178 if ( m_parent.id <= 0 )
179 media_list.reset( vlc_ml_list_audio_media(m_ml, &queryParams) );
180 else
181 media_list.reset( vlc_ml_list_media_of(m_ml, &queryParams, m_parent.type, m_parent.id ) );
182 if ( media_list == nullptr )
183 return {};
184 std::vector<std::unique_ptr<MLAlbumTrack>> res;
185 for( const vlc_ml_media_t& media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
186 res.emplace_back( std::make_unique<MLAlbumTrack>( m_ml, &media ) );
187 return res;