qt: medialib: implement separate data loaders
[vlc.git] / modules / gui / qt / medialibrary / mlgenremodel.cpp
blobb9c562a5f8b90edda28815d708e3156c77c6242a
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 "mlgenremodel.hpp"
21 #include "mlartistmodel.hpp"
24 QHash<QByteArray, vlc_ml_sorting_criteria_t> MLGenreModel::M_names_to_criteria = {
25 {"title", VLC_ML_SORTING_ALPHA}
28 MLGenreModel::MLGenreModel(QObject *parent)
29 : MLSlidingWindowModel<MLGenre>(parent)
33 QVariant MLGenreModel::data(const QModelIndex &index, int role) const
35 if (!index.isValid() || index.row() < 0)
36 return QVariant();
38 const MLGenre* ml_genre = item(index.row());
39 if (!ml_genre)
40 return QVariant();
42 switch (role)
44 // Genres
45 case GENRE_ID:
46 return QVariant::fromValue( ml_genre->getId() );
47 case GENRE_NAME:
48 return QVariant::fromValue( ml_genre->getName() );
49 case GENRE_NB_TRACKS:
50 return QVariant::fromValue( ml_genre->getNbTracks() );
51 case GENRE_COVER:
52 return QVariant::fromValue( ml_genre->getCover() );
53 default :
54 return QVariant();
58 QHash<int, QByteArray> MLGenreModel::roleNames() const
60 return {
61 { GENRE_ID, "id" },
62 { GENRE_NAME, "name" },
63 { GENRE_NB_TRACKS, "nb_tracks" },
64 { GENRE_ARTISTS, "artists" },
65 { GENRE_TRACKS, "tracks" },
66 { GENRE_ALBUMS, "albums" },
67 { GENRE_COVER, "cover" }
71 void MLGenreModel::onVlcMlEvent(const MLEvent &event)
73 switch (event.i_type)
75 case VLC_ML_EVENT_GENRE_ADDED:
76 case VLC_ML_EVENT_GENRE_UPDATED:
77 case VLC_ML_EVENT_GENRE_DELETED:
78 m_need_reset = true;
79 break;
81 MLSlidingWindowModel::onVlcMlEvent(event);
84 void MLGenreModel::thumbnailUpdated(int idx)
86 emit dataChanged(index(idx), index(idx), {GENRE_COVER});
89 vlc_ml_sorting_criteria_t MLGenreModel::roleToCriteria(int role) const
91 switch (role)
93 case GENRE_NAME:
94 return VLC_ML_SORTING_ALPHA;
95 default :
96 return VLC_ML_SORTING_DEFAULT;
100 vlc_ml_sorting_criteria_t MLGenreModel::nameToCriteria(QByteArray name) const
102 return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
105 ListCacheLoader<std::unique_ptr<MLGenre>> *
106 MLGenreModel::createLoader() const
108 return new Loader(*this);
111 size_t MLGenreModel::Loader::count() const
113 MLQueryParams params = getParams();
114 auto queryParams = params.toCQueryParams();
116 return vlc_ml_count_genres( m_ml, &queryParams );
119 std::vector<std::unique_ptr<MLGenre>>
120 MLGenreModel::Loader::load(size_t index, size_t count) const
122 MLQueryParams params = getParams(index, count);
123 auto queryParams = params.toCQueryParams();
125 ml_unique_ptr<vlc_ml_genre_list_t> genre_list(
126 vlc_ml_list_genres(m_ml, &queryParams)
128 if ( genre_list == nullptr )
129 return {};
130 std::vector<std::unique_ptr<MLGenre>> res;
131 for( const vlc_ml_genre_t& genre: ml_range_iterate<vlc_ml_genre_t>( genre_list ) )
132 res.emplace_back( std::make_unique<MLGenre>( m_ml, &genre ) );
133 return res;