qt: notify dataChanged when a thumbnail is updated from the medialibrary
[vlc.git] / modules / gui / qt / medialibrary / mlvideomodel.cpp
blob3338970730f2fa7cfbea255c6f4aaa4e67a215d8
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 namespace {
23 enum Role {
24 VIDEO_ID = Qt::UserRole + 1,
25 VIDEO_TITLE,
26 VIDEO_THUMBNAIL,
27 VIDEO_DURATION,
28 VIDEO_PROGRESS,
29 VIDEO_PLAYCOUNT,
30 VIDEO_RESOLUTION,
31 VIDEO_CHANNEL,
32 VIDEO_MRL,
33 VIDEO_VIDEO_TRACK,
34 VIDEO_AUDIO_TRACK,
36 VIDEO_TITLE_FIRST_SYMBOL,
41 QHash<QByteArray, vlc_ml_sorting_criteria_t> MLVideoModel::M_names_to_criteria = {
42 {"id", VLC_ML_SORTING_DEFAULT},
43 {"title", VLC_ML_SORTING_ALPHA},
44 {"duration", VLC_ML_SORTING_DURATION},
45 {"playcount", VLC_ML_SORTING_PLAYCOUNT},
48 MLVideoModel::MLVideoModel(QObject* parent)
49 : MLSlidingWindowModel<MLVideo>(parent)
53 QVariant MLVideoModel::data(const QModelIndex& index, int role) const
55 const auto video = item(static_cast<unsigned int>(index.row()));
56 if ( video == nullptr )
57 return {};
58 switch (role)
60 case VIDEO_ID:
61 return QVariant::fromValue( video->getId() );
62 case VIDEO_TITLE:
63 return QVariant::fromValue( video->getTitle() );
64 case VIDEO_THUMBNAIL:
65 return QVariant::fromValue( video->getThumbnail() );
66 case VIDEO_DURATION:
67 return QVariant::fromValue( video->getDuration() );
68 case VIDEO_PROGRESS:
69 return QVariant::fromValue( video->getProgress() );
70 case VIDEO_PLAYCOUNT:
71 return QVariant::fromValue( video->getPlayCount() );
72 case VIDEO_RESOLUTION:
73 return QVariant::fromValue( video->getResolutionName() );
74 case VIDEO_CHANNEL:
75 return QVariant::fromValue( video->getChannel() );
76 case VIDEO_MRL:
77 return QVariant::fromValue( video->getMRL() );
78 case VIDEO_VIDEO_TRACK:
79 return QVariant::fromValue( video->getVideoDesc() );
80 case VIDEO_AUDIO_TRACK:
81 return QVariant::fromValue( video->getAudioDesc() );
82 case VIDEO_TITLE_FIRST_SYMBOL:
83 return QVariant::fromValue( getFirstSymbol( video->getTitle() ) );
85 default:
86 return {};
90 QHash<int, QByteArray> MLVideoModel::roleNames() const
92 return {
93 { VIDEO_ID, "id" },
94 { VIDEO_TITLE, "title" },
95 { VIDEO_THUMBNAIL, "thumbnail" },
96 { VIDEO_DURATION, "duration" },
97 { VIDEO_PROGRESS, "progress" },
98 { VIDEO_PLAYCOUNT, "playcount" },
99 { VIDEO_RESOLUTION, "resolution_name" },
100 { VIDEO_CHANNEL, "channel" },
101 { VIDEO_MRL, "mrl" },
102 { VIDEO_AUDIO_TRACK, "audioDesc" },
103 { VIDEO_VIDEO_TRACK, "videoDesc" },
104 { VIDEO_TITLE_FIRST_SYMBOL, "title_first_symbol"},
108 std::vector<std::unique_ptr<MLVideo> > MLVideoModel::fetch()
110 ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_video_media(
111 m_ml, &m_query_param ) };
112 if ( media_list == nullptr )
113 return {};
114 std::vector<std::unique_ptr<MLVideo>> res;
115 for( vlc_ml_media_t &media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
116 res.emplace_back( std::make_unique<MLVideo>(m_ml, &media) );
117 return res;
120 size_t MLVideoModel::countTotalElements() const
122 vlc_ml_query_params_t params = m_query_param;
123 params.i_offset = 0;
124 params.i_nbResults = 0;
125 return vlc_ml_count_video_media(m_ml, &params);
128 vlc_ml_sorting_criteria_t MLVideoModel::roleToCriteria(int role) const
130 switch(role)
132 case VIDEO_TITLE:
133 return VLC_ML_SORTING_ALPHA;
134 case VIDEO_DURATION:
135 return VLC_ML_SORTING_DURATION;
136 case VIDEO_PLAYCOUNT:
137 return VLC_ML_SORTING_PLAYCOUNT;
138 default:
139 return VLC_ML_SORTING_DEFAULT;
143 vlc_ml_sorting_criteria_t MLVideoModel::nameToCriteria(QByteArray name) const
145 return M_names_to_criteria.value(name, VLC_ML_SORTING_DEFAULT);
148 QByteArray MLVideoModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) const
150 return M_names_to_criteria.key(criteria, "");
153 void MLVideoModel::onVlcMlEvent(const vlc_ml_event_t* event)
155 switch (event->i_type)
157 case VLC_ML_EVENT_MEDIA_ADDED:
158 case VLC_ML_EVENT_MEDIA_UPDATED:
159 case VLC_ML_EVENT_MEDIA_DELETED:
160 m_need_reset = true;
161 break;
162 default:
163 break;
165 MLSlidingWindowModel::onVlcMlEvent( event );
168 void MLVideoModel::thumbnailUpdated(int idx)
170 emit dataChanged(index(idx), index(idx), {VIDEO_THUMBNAIL});
173 QString MLVideoModel::getFirstSymbol( const QString& str )
175 QString ret("#");
176 if ( str.length() > 0 && str[0].isLetter() )
177 ret = str[0].toUpper();
178 return ret;