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 )
42 return QVariant::fromValue( video
->getId() );
44 return QVariant::fromValue( video
->getTitle() );
46 return QVariant::fromValue( video
->getThumbnail() );
48 return QVariant::fromValue( video
->getDuration() );
49 case VIDEO_DURATION_SHORT
:
50 return QVariant::fromValue( video
->getDurationShort() );
52 return QVariant::fromValue( video
->getProgress() );
54 return QVariant::fromValue( video
->getPlayCount() );
55 case VIDEO_RESOLUTION
:
56 return QVariant::fromValue( video
->getResolutionName() );
58 return QVariant::fromValue( video
->getChannel() );
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() ) );
75 QHash
<int, QByteArray
> MLVideoModel::roleNames() const
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" },
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
100 return VLC_ML_SORTING_ALPHA
;
102 return VLC_ML_SORTING_DURATION
;
103 case VIDEO_PLAYCOUNT
:
104 return VLC_ML_SORTING_PLAYCOUNT
;
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
:
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 )
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
) );