qt: medialib: implement separate data loaders
[vlc.git] / modules / gui / qt / medialibrary / mlvideomodel.cpp
blob27e1414e4bbd5226c2171d346e81e4d3f517f3f7
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 "mlvideomodel.hpp"
21 QHash<QByteArray, vlc_ml_sorting_criteria_t> MLVideoModel::M_names_to_criteria = {
22 {"id", VLC_ML_SORTING_DEFAULT},
23 {"title", VLC_ML_SORTING_ALPHA},
24 {"duration", VLC_ML_SORTING_DURATION},
25 {"duration_short", VLC_ML_SORTING_DURATION},
26 {"playcount", VLC_ML_SORTING_PLAYCOUNT},
29 MLVideoModel::MLVideoModel(QObject* parent)
30 : MLSlidingWindowModel<MLVideo>(parent)
34 QVariant MLVideoModel::data(const QModelIndex& index, int role) const
36 const auto video = item(index.row());
37 if ( video == nullptr )
38 return {};
39 switch (role)
41 case VIDEO_ID:
42 return QVariant::fromValue( video->getId() );
43 case VIDEO_TITLE:
44 return QVariant::fromValue( video->getTitle() );
45 case VIDEO_THUMBNAIL:
46 return QVariant::fromValue( video->getThumbnail() );
47 case VIDEO_DURATION:
48 return QVariant::fromValue( video->getDuration() );
49 case VIDEO_DURATION_SHORT:
50 return QVariant::fromValue( video->getDurationShort() );
51 case VIDEO_PROGRESS:
52 return QVariant::fromValue( video->getProgress() );
53 case VIDEO_PLAYCOUNT:
54 return QVariant::fromValue( video->getPlayCount() );
55 case VIDEO_RESOLUTION:
56 return QVariant::fromValue( video->getResolutionName() );
57 case VIDEO_CHANNEL:
58 return QVariant::fromValue( video->getChannel() );
59 case VIDEO_MRL:
60 return QVariant::fromValue( video->getMRL() );
61 case VIDEO_DISPLAY_MRL:
62 return QVariant::fromValue( video->getDisplayMRL() );
63 case VIDEO_VIDEO_TRACK:
64 return QVariant::fromValue( video->getVideoDesc() );
65 case VIDEO_AUDIO_TRACK:
66 return QVariant::fromValue( video->getAudioDesc() );
67 case VIDEO_TITLE_FIRST_SYMBOL:
68 return QVariant::fromValue( getFirstSymbol( video->getTitle() ) );
70 default:
71 return {};
75 QHash<int, QByteArray> MLVideoModel::roleNames() const
77 return {
78 { VIDEO_ID, "id" },
79 { VIDEO_TITLE, "title" },
80 { VIDEO_THUMBNAIL, "thumbnail" },
81 { VIDEO_DURATION, "duration" },
82 { VIDEO_DURATION_SHORT, "duration_short" },
83 { VIDEO_PROGRESS, "progress" },
84 { VIDEO_PLAYCOUNT, "playcount" },
85 { VIDEO_RESOLUTION, "resolution_name" },
86 { VIDEO_CHANNEL, "channel" },
87 { VIDEO_MRL, "mrl" },
88 { VIDEO_DISPLAY_MRL, "display_mrl" },
89 { VIDEO_AUDIO_TRACK, "audioDesc" },
90 { VIDEO_VIDEO_TRACK, "videoDesc" },
91 { VIDEO_TITLE_FIRST_SYMBOL, "title_first_symbol"},
95 vlc_ml_sorting_criteria_t MLVideoModel::roleToCriteria(int role) const
97 switch(role)
99 case VIDEO_TITLE:
100 return VLC_ML_SORTING_ALPHA;
101 case VIDEO_DURATION:
102 return VLC_ML_SORTING_DURATION;
103 case VIDEO_PLAYCOUNT:
104 return VLC_ML_SORTING_PLAYCOUNT;
105 default:
106 return VLC_ML_SORTING_DEFAULT;
110 vlc_ml_sorting_criteria_t MLVideoModel::nameToCriteria(QByteArray name) const
112 return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
115 QByteArray MLVideoModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const
117 return M_names_to_criteria.key(criteria, "");
120 void MLVideoModel::onVlcMlEvent(const MLEvent &event)
122 switch (event.i_type)
124 case VLC_ML_EVENT_MEDIA_ADDED:
125 case VLC_ML_EVENT_MEDIA_UPDATED:
126 case VLC_ML_EVENT_MEDIA_DELETED:
127 m_need_reset = true;
128 break;
129 default:
130 break;
132 MLSlidingWindowModel::onVlcMlEvent( event );
135 void MLVideoModel::thumbnailUpdated(int idx)
137 emit dataChanged(index(idx), index(idx), {VIDEO_THUMBNAIL});
140 ListCacheLoader<std::unique_ptr<MLVideo>> *
141 MLVideoModel::createLoader() const
143 return new Loader(*this);
146 size_t MLVideoModel::Loader::count() const
148 MLQueryParams params = getParams();
149 auto queryParams = params.toCQueryParams();
151 return vlc_ml_count_video_media(m_ml, &queryParams);
154 std::vector<std::unique_ptr<MLVideo>>
155 MLVideoModel::Loader::load(size_t index, size_t count) const
157 MLQueryParams params = getParams(index, count);
158 auto queryParams = params.toCQueryParams();
160 ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_video_media(
161 m_ml, &queryParams ) };
162 if ( media_list == nullptr )
163 return {};
164 std::vector<std::unique_ptr<MLVideo>> res;
165 for( vlc_ml_media_t &media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
166 res.emplace_back( std::make_unique<MLVideo>(m_ml, &media) );
167 return res;