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"
24 VIDEO_ID
= Qt::UserRole
+ 1,
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 )
61 return QVariant::fromValue( video
->getId() );
63 return QVariant::fromValue( video
->getTitle() );
65 return QVariant::fromValue( video
->getThumbnail() );
67 return QVariant::fromValue( video
->getDuration() );
69 return QVariant::fromValue( video
->getProgress() );
71 return QVariant::fromValue( video
->getPlayCount() );
72 case VIDEO_RESOLUTION
:
73 return QVariant::fromValue( video
->getResolutionName() );
75 return QVariant::fromValue( video
->getChannel() );
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() ) );
90 QHash
<int, QByteArray
> MLVideoModel::roleNames() const
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 )
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
) );
120 size_t MLVideoModel::countTotalElements() const
122 vlc_ml_query_params_t params
= m_query_param
;
124 params
.i_nbResults
= 0;
125 return vlc_ml_count_video_media(m_ml
, ¶ms
);
128 vlc_ml_sorting_criteria_t
MLVideoModel::roleToCriteria(int role
) const
133 return VLC_ML_SORTING_ALPHA
;
135 return VLC_ML_SORTING_DURATION
;
136 case VIDEO_PLAYCOUNT
:
137 return VLC_ML_SORTING_PLAYCOUNT
;
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
:
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
)
176 if ( str
.length() > 0 && str
[0].isLetter() )
177 ret
= str
[0].toUpper();