[vlc-commits] [Git][videolan/vlc][master] 4 commits: qt: MLQueryParams.toCQueryParams can't be used with temporary objects

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sat Aug 5 22:08:14 UTC 2023



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
90468efd by Pierre Lamot at 2023-08-05T21:47:59+00:00
qt: MLQueryParams.toCQueryParams can't be used with temporary objects

this causes a use after free

- - - - -
e60618a3 by Pierre Lamot at 2023-08-05T21:47:59+00:00
qt: add pagination to MLBaseModel

Adds `limit` and `offset` properties to the model, this allows to show only a
subpart of the model.

- - - - -
eb0755f9 by Pierre Lamot at 2023-08-05T21:47:59+00:00
qt: use MLBaseModel::limit instead of RecentXXXModel::numberOfItemsToShow

MLBaseModel::limit is the generic implementation

- - - - -
fa0e5dec by Pierre Lamot at 2023-08-05T21:47:59+00:00
qt: remove implementation of model limit in MLRecentModel and MLRecentsVideoModel

- - - - -


16 changed files:

- modules/gui/qt/medialibrary/mlbasemodel.cpp
- modules/gui/qt/medialibrary/mlbasemodel.hpp
- modules/gui/qt/medialibrary/mllistcache.cpp
- modules/gui/qt/medialibrary/mllistcache.hpp
- modules/gui/qt/medialibrary/mlplaylistlistmodel.cpp
- modules/gui/qt/medialibrary/mlplaylistmodel.cpp
- modules/gui/qt/medialibrary/mlrecentsmodel.cpp
- modules/gui/qt/medialibrary/mlrecentsmodel.hpp
- modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
- modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
- modules/gui/qt/medialibrary/mlvideofoldersmodel.cpp
- modules/gui/qt/medialibrary/mlvideogroupsmodel.cpp
- modules/gui/qt/medialibrary/mlvideomodel.cpp
- modules/gui/qt/medialibrary/qml/VideoDisplayRecentVideos.qml
- modules/gui/qt/menus/menus.cpp
- modules/gui/qt/menus/qml/GlobalShortcutsMedialib.qml


Changes:

=====================================
modules/gui/qt/medialibrary/mlbasemodel.cpp
=====================================
@@ -216,9 +216,10 @@ void MLBaseModel::onResetRequested()
     invalidateCache();
 }
 
-void MLBaseModel::onLocalSizeChanged(size_t size)
+void MLBaseModel::onLocalSizeChanged(size_t queryCount, size_t maximumCount)
 {
-    emit countChanged(size);
+    emit countChanged(queryCount);
+    emit maximumCountChanged(maximumCount);
 }
 
 void MLBaseModel::onVlcMlEvent(const MLEvent &event)
@@ -238,7 +239,7 @@ void MLBaseModel::onVlcMlEvent(const MLEvent &event)
                 if (!m_cache)
                     break;
 
-                ssize_t stotal = m_cache->count();
+                ssize_t stotal = m_cache->queryCount();
                 if (stotal == COUNT_UNINITIALIZED)
                     break;
 
@@ -396,22 +397,60 @@ void MLBaseModel::unsetSortCriteria()
     emit sortCriteriaChanged();
 }
 
+
+unsigned int MLBaseModel::getLimit() const
+{
+    return m_limit;
+}
+
+void MLBaseModel::setLimit(unsigned int limit)
+{
+    if (m_limit == limit)
+        return;
+    m_limit = limit;
+    resetCache();
+    emit limitChanged();
+}
+
+unsigned int MLBaseModel::getOffset() const
+{
+    return m_offset;
+}
+
+void MLBaseModel::setOffset(unsigned int offset)
+{
+    if (m_offset == offset)
+        return;
+    m_offset = offset;
+    resetCache();
+    emit offsetChanged();
+}
+
+
 int MLBaseModel::rowCount(const QModelIndex &parent) const
 {
     if (!m_cache || parent.isValid())
         return 0;
 
-    return m_cache->count();
+    return m_cache->queryCount();
 }
 
 //-------------------------------------------------------------------------------------------------
 
 unsigned MLBaseModel::getCount() const
 {
-    if (!m_cache || m_cache->count() == COUNT_UNINITIALIZED)
+    if (!m_cache || m_cache->queryCount() == COUNT_UNINITIALIZED)
+        return 0;
+
+    return static_cast<unsigned>(m_cache->queryCount());
+}
+
+unsigned MLBaseModel::getMaximumCount() const
+{
+    if (!m_cache || m_cache->maximumCount() == COUNT_UNINITIALIZED)
         return 0;
 
-    return static_cast<unsigned>(m_cache->count());
+    return static_cast<unsigned>(m_cache->maximumCount());
 }
 
 
@@ -444,7 +483,7 @@ void MLBaseModel::validateCache() const
         return;
 
     auto loader = createLoader();
-    m_cache = std::make_unique<MLListCache>(m_mediaLib, std::move(loader), false);
+    m_cache = std::make_unique<MLListCache>(m_mediaLib, std::move(loader), false, m_limit, m_offset);
     connect(m_cache.get(), &MLListCache::localSizeChanged,
             this, &MLBaseModel::onLocalSizeChanged);
 
@@ -498,7 +537,7 @@ MLItem *MLBaseModel::item(int signedidx) const
     if (!m_cache)
         return nullptr;
 
-    ssize_t count = m_cache->count();
+    ssize_t count = m_cache->queryCount();
 
     if (count == 0 || signedidx < 0 || signedidx >= count)
         return nullptr;
@@ -628,5 +667,5 @@ MLQueryParams MLBaseModel::BaseLoader::getParams(size_t index, size_t count) con
 
 bool MLBaseModel::loading() const
 {
-    return !(m_mediaLib && m_cache && (m_cache->count() != COUNT_UNINITIALIZED));
+    return !(m_mediaLib && m_cache && (m_cache->queryCount() != COUNT_UNINITIALIZED));
 }


=====================================
modules/gui/qt/medialibrary/mlbasemodel.hpp
=====================================
@@ -58,8 +58,18 @@ class MLBaseModel : public QAbstractListModel, public QQmlParserStatus
     Q_PROPERTY(QString sortCriteria READ getSortCriteria WRITE setSortCriteria
                NOTIFY sortCriteriaChanged RESET unsetSortCriteria FINAL)
 
+    //maximum number of element to load
+    //limit = 0 means all elements are loaded
+    Q_PROPERTY(unsigned int limit READ getLimit WRITE setLimit NOTIFY limitChanged FINAL)
+    //skip in N first elements
+    Q_PROPERTY(unsigned int offset READ getOffset WRITE setOffset NOTIFY offsetChanged FINAL)
+
+    //number of elements in the model (limit is accounted)
     Q_PROPERTY(unsigned int count READ getCount NOTIFY countChanged FINAL)
 
+    //number of elements in the model (limit not accounted)
+    Q_PROPERTY(unsigned int maximumCount READ getMaximumCount NOTIFY maximumCountChanged FINAL)
+
     /**
      * @brief loading
      * @return true till no data is available
@@ -104,7 +114,14 @@ public:
     void setSortCriteria(const QString& criteria);
     void unsetSortCriteria();
 
+    unsigned int getLimit() const;
+    void setLimit(unsigned int limit);
+    unsigned int getOffset() const;
+    void setOffset(unsigned int offset);
+
     virtual unsigned int getCount() const;
+    virtual unsigned int getMaximumCount() const;
+
 
     bool loading() const;
 
@@ -117,13 +134,16 @@ signals:
     void resetRequested();
     void sortOrderChanged();
     void sortCriteriaChanged();
+    void limitChanged() const;
+    void offsetChanged() const;
     void countChanged(unsigned int) const;
+    void maximumCountChanged(unsigned int) const;
     void loadingChanged() const;
 
 
 protected slots:
     void onResetRequested();
-    void onLocalSizeChanged(size_t size);
+    void onLocalSizeChanged(size_t queryCount, size_t maximumCount);
 
 
 protected:
@@ -208,6 +228,8 @@ protected:
     QString m_search_pattern;
     vlc_ml_sorting_criteria_t m_sort = VLC_ML_SORTING_DEFAULT;
     bool m_sort_desc = false;
+    unsigned int m_limit = 0;
+    unsigned int m_offset = 0;
 
     std::unique_ptr<vlc_ml_event_callback_t,
                     std::function<void(vlc_ml_event_callback_t*)>> m_ml_event_handle;


=====================================
modules/gui/qt/medialibrary/mllistcache.cpp
=====================================
@@ -43,11 +43,13 @@ bool cacheDataCompare(const void* dataOld, uint32_t oldIndex, const void* dataNe
 
 }
 
-MLListCache::MLListCache(MediaLib* medialib, std::unique_ptr<ListCacheLoader<MLListCache::ItemType>>&& loader, bool useMove, size_t chunkSize)
+MLListCache::MLListCache(MediaLib* medialib, std::unique_ptr<ListCacheLoader<MLListCache::ItemType>>&& loader, bool useMove, size_t limit, size_t offset, size_t chunkSize)
     : m_medialib(medialib)
     , m_useMove(useMove)
     , m_loader(loader.release())
     , m_chunkSize(chunkSize)
+    , m_limit(limit)
+    , m_offset(offset)
 {
     assert(medialib);
 }
@@ -123,7 +125,7 @@ const MLListCache::ItemType* MLListCache::get(size_t index) const
 const MLListCache::ItemType* MLListCache::find(const std::function<bool (const MLListCache::ItemType&)> &&f, int *index) const
 {
 
-    if (!m_cachedData || m_cachedData->totalCount == 0)
+    if (!m_cachedData || m_cachedData->queryCount == 0)
         return nullptr;
 
     auto it = std::find_if(m_cachedData->list.cbegin(), m_cachedData->list.cend(), f);
@@ -187,9 +189,10 @@ int MLListCache::deleteItem(const MLItemId& mlid)
     m_cachedData->list.erase(it, it+1);
     size_t delta = m_cachedData->loadedCount - m_cachedData->list.size();
     m_cachedData->loadedCount -= delta;
-    m_cachedData->totalCount -= delta;
+    m_cachedData->maximumCount -= delta;
+    m_cachedData->queryCount -= delta;
     emit endRemoveRows();
-    emit localSizeChanged(m_cachedData->totalCount);
+    emit localSizeChanged(m_cachedData->queryCount, m_cachedData->maximumCount);
 
     return pos;
 }
@@ -232,7 +235,7 @@ void MLListCache::deleteRange(int first, int last)
         return;
     assert(first <= last);
     assert(first >= 0);
-    assert(static_cast<size_t>(last) < m_cachedData->totalCount);
+    assert(static_cast<size_t>(last) < m_cachedData->queryCount);
     emit beginRemoveRows(first, last);
     if (static_cast<size_t>(first) < m_cachedData->loadedCount)
     {
@@ -249,17 +252,30 @@ void MLListCache::deleteRange(int first, int last)
     }
     //else data are not loaded, just mark them as deleted
 
-    m_cachedData->totalCount -= (last + 1) - first;
+    int rangeSize =  (last + 1) - first;
+    m_cachedData->queryCount -= rangeSize;
+    m_cachedData->maximumCount -= rangeSize;
     emit endRemoveRows();
-    emit localSizeChanged(m_cachedData->totalCount);
+    emit localSizeChanged(m_cachedData->queryCount, m_cachedData->maximumCount);
 }
 
-ssize_t MLListCache::count() const
+ssize_t MLListCache::queryCount() const
 {
     if (m_cachedData)
-        return m_cachedData->totalCount;
+        return m_cachedData->queryCount;
     else if (m_oldData)
-        return m_oldData->totalCount;
+        return m_oldData->queryCount;
+    else
+        return COUNT_UNINITIALIZED;
+}
+
+
+ssize_t MLListCache::maximumCount() const
+{
+    if (m_cachedData)
+        return m_cachedData->maximumCount;
+    else if (m_oldData)
+        return m_oldData->maximumCount;
     else
         return COUNT_UNINITIALIZED;
 }
@@ -278,7 +294,7 @@ void MLListCache::refer(size_t index)
     if (!m_cachedData)
         return;
 
-    if (index > m_cachedData->totalCount)
+    if (index > m_cachedData->queryCount)
         return;
 
    /* index is already in the list */
@@ -349,7 +365,8 @@ void MLListCache::partialUpdate()
 
     m_partialIndex = 0;
     m_partialLoadedCount = m_oldData->loadedCount;
-    size_t partialTotalCount = m_oldData->totalCount;
+    size_t partialQueryCount = m_oldData->queryCount;
+    size_t partialMaximumCount = m_oldData->maximumCount;
     for (size_t i = 0; i < changes->size; i++)
     {
         vlc_diffutil_change_t& op = changes->data[i];
@@ -363,9 +380,10 @@ void MLListCache::partialUpdate()
             emit beginInsertRows(op.op.insert.index, op.op.insert.index + op.count - 1);
             m_partialIndex += op.count;
             m_partialLoadedCount += op.count;
-            partialTotalCount += op.count;
+            partialQueryCount += op.count;
+            partialMaximumCount += op.count;
             emit endInsertRows();
-            emit localSizeChanged(partialTotalCount);
+            emit localSizeChanged(partialQueryCount, partialMaximumCount);
             break;
         case VLC_DIFFUTIL_OP_REMOVE:
             m_partialX = op.op.remove.x;
@@ -373,9 +391,10 @@ void MLListCache::partialUpdate()
             emit beginRemoveRows(op.op.remove.index, op.op.remove.index + op.count - 1);
             m_partialLoadedCount -= op.count;
             m_partialX += op.count;
-            partialTotalCount -= op.count;
+            partialQueryCount -= op.count;
+            partialMaximumCount -= op.count;
             emit endRemoveRows();
-            emit localSizeChanged(partialTotalCount);
+            emit localSizeChanged(partialQueryCount, partialMaximumCount);
             break;
         case VLC_DIFFUTIL_OP_MOVE:
             m_partialX = op.op.move.x;
@@ -408,19 +427,19 @@ void MLListCache::partialUpdate()
 
     //if we have change outside our cache
     //just notify for addition/removal at a the end of the list
-    if (partialTotalCount != m_cachedData->totalCount)
+    if (partialQueryCount != m_cachedData->queryCount)
     {
-        if (partialTotalCount > m_cachedData->totalCount)
+        if (partialQueryCount > m_cachedData->queryCount)
         {
-            emit beginRemoveRows(m_cachedData->totalCount - 1, partialTotalCount - 1);
+            emit beginRemoveRows(m_cachedData->queryCount - 1, partialQueryCount - 1);
             emit endRemoveRows();
-            emit localSizeChanged(m_cachedData->totalCount);
+            emit localSizeChanged(m_cachedData->queryCount, m_cachedData->maximumCount);
         }
         else
         {
-            emit beginInsertRows(partialTotalCount - 1, m_cachedData->totalCount - 1);
+            emit beginInsertRows(partialQueryCount - 1, m_cachedData->queryCount - 1);
             emit endInsertRows();
-            emit localSizeChanged(m_cachedData->totalCount);
+            emit localSizeChanged(m_cachedData->queryCount, m_cachedData->maximumCount);
         }
     }
 }
@@ -433,16 +452,16 @@ void MLListCache::asyncCountAndLoad()
     size_t count = std::max(m_maxReferedIndex, m_chunkSize);
 
     struct Ctx {
-        size_t totalCount;
+        size_t maximumCount;
         std::vector<ItemType> list;
     };
 
     m_countTask = m_medialib->runOnMLThread<Ctx>(this,
         //ML thread
-        [loader = m_loader, count = count](vlc_medialibrary_t* ml, Ctx& ctx)
+        [loader = m_loader, offset = m_offset, count = count](vlc_medialibrary_t* ml, Ctx& ctx)
         {
-            ctx.list = loader->load(ml, 0, count);
-            ctx.totalCount = loader->count(ml);
+            ctx.list = loader->load(ml, offset, count);
+            ctx.maximumCount = loader->count(ml);
         },
         //UI thread
         [this](quint64 taskId, Ctx& ctx)
@@ -451,13 +470,19 @@ void MLListCache::asyncCountAndLoad()
                 return;
 
             //quite unlikley but model may change between count and load
-            if (unlikely(ctx.list.size() > ctx.totalCount))
+            if (unlikely(ctx.list.size() > ctx.maximumCount))
             {
-                ctx.totalCount = ctx.list.size();
+                ctx.maximumCount = ctx.list.size();
                 m_needReload = true;
             }
 
-            m_cachedData = std::make_unique<CacheData>(std::move(ctx.list), ctx.totalCount);
+            size_t queryCount = (m_limit > 0)
+                ? std::min(m_limit, ctx.maximumCount)
+                : ctx.maximumCount;
+            //note: should we drop items past queryCount?
+            m_cachedData = std::make_unique<CacheData>(std::move(ctx.list),
+                                                       queryCount,
+                                                       ctx.maximumCount);
 
             if (m_oldData)
             {
@@ -465,15 +490,15 @@ void MLListCache::asyncCountAndLoad()
             }
             else
             {
-                if (m_cachedData->totalCount > 0)
+                if (m_cachedData->queryCount > 0)
                 {
                     //no previous data, we insert everything
-                    emit beginInsertRows(0, m_cachedData->totalCount - 1);
+                    emit beginInsertRows(0, m_cachedData->queryCount - 1);
                     emit endInsertRows();
-                    emit localSizeChanged(m_cachedData->totalCount);
+                    emit localSizeChanged(m_cachedData->queryCount, m_cachedData->maximumCount);
                 }
                 else
-                    emit localSizeChanged(0);
+                    emit localSizeChanged(0, m_cachedData->maximumCount);
             }
 
             m_countTask = 0;
@@ -489,7 +514,7 @@ void MLListCache::asyncCountAndLoad()
                 m_maxReferedIndex = m_cachedData->loadedCount;
             }
             else if (m_maxReferedIndex > m_cachedData->loadedCount
-                && m_maxReferedIndex <= m_cachedData->totalCount)
+                && m_maxReferedIndex <= m_cachedData->queryCount)
             {
                 asyncFetchMore();
             }
@@ -506,7 +531,7 @@ void MLListCache::asyncFetchMore()
     if (m_appendTask)
         m_medialib->cancelMLTask(this, m_appendTask);
 
-    m_maxReferedIndex = std::min(m_cachedData->totalCount, m_maxReferedIndex);
+    m_maxReferedIndex = std::min(m_cachedData->queryCount, m_maxReferedIndex);
     size_t count = ((m_maxReferedIndex - m_cachedData->loadedCount) / m_chunkSize + 1 ) * m_chunkSize;
 
     struct Ctx {
@@ -514,7 +539,7 @@ void MLListCache::asyncFetchMore()
     };
     m_appendTask = m_medialib->runOnMLThread<Ctx>(this,
         //ML thread
-        [loader = m_loader, offset = m_cachedData->loadedCount, count]
+        [loader = m_loader, offset = m_offset + m_cachedData->loadedCount, count]
         (vlc_medialibrary_t* ml, Ctx& ctx)
         {
             ctx.list = loader->load(ml, offset, count);


=====================================
modules/gui/qt/medialibrary/mllistcache.hpp
=====================================
@@ -124,7 +124,7 @@ public:
     static constexpr ssize_t COUNT_UNINITIALIZED = -1;
 
     MLListCache(MediaLib* medialib, std::unique_ptr<ListCacheLoader<ItemType>>&& loader,
-        bool useMove, size_t chunkSize = 100);
+        bool useMove, size_t limit, size_t offset, size_t chunkSize = 100);
 
     /**
      * Return the item at specified index
@@ -170,12 +170,20 @@ public:
     void deleteRange(int first, int last);
 
     /**
-     * Return the number of items or `COUNT_UNINITIALIZED`
+     * @return the number of items or `COUNT_UNINITIALIZED`
      *
      * This returns the local count, and does not retrieve anything from the
      * loader.
      */
-    ssize_t count() const;
+    ssize_t queryCount() const;
+
+    /**
+     * @return the total number of elements in the list, without taking @ref m_limit in account.
+     * COUNT_UNINITIALIZED is returned if the list isn't initialized
+     *
+     * This may be usefull to know whether there are additionnal elements past the limit
+     */
+    ssize_t maximumCount() const;
 
     /**
      * Init the list size
@@ -196,7 +204,7 @@ public:
     void invalidate();
 
 signals:
-    void localSizeChanged(size_t size);
+    void localSizeChanged(size_t querySize, size_t maximumSize);
 
     void localDataChanged(int sourceFirst, int sourceLast);
 
@@ -224,6 +232,10 @@ private:
     QSharedPointer<ListCacheLoader<ItemType>> m_loader;
     size_t m_chunkSize;
 
+    //0 limit means no limit
+    size_t m_limit = 0;
+    size_t m_offset = 0;
+
     //highest index requested by the view (1 based, 0 is nothing referenced)
     size_t m_maxReferedIndex = 0;
 
@@ -233,15 +245,22 @@ private:
     uint64_t m_countTask = 0;
 
     struct CacheData {
-        explicit CacheData(std::vector<ItemType>&& list_, size_t totalCount_)
+        explicit CacheData(std::vector<ItemType>&& list_,
+                           size_t queryCount_,
+                           size_t maximumCount_)
             : list(std::move(list_))
-            , totalCount(totalCount_)
+            , queryCount(queryCount_)
+            , maximumCount(maximumCount_)
         {
             loadedCount = list.size();
         }
 
         std::vector<ItemType> list;
-        size_t totalCount = 0;
+        //How many items are does the query returns min(maximumCount - offset, limit)
+        size_t queryCount = 0;
+        //how many items in the table
+        size_t maximumCount = 0;
+        //how many items are loaded (list.size)
         size_t loadedCount = 0;
     };
 


=====================================
modules/gui/qt/medialibrary/mlplaylistlistmodel.cpp
=====================================
@@ -478,19 +478,21 @@ MLPlaylistListModel::Loader::Loader(const MLPlaylistListModel & model, PlaylistT
 
 size_t MLPlaylistListModel::Loader::count(vlc_medialibrary_t* ml) const /* override */
 {
-    vlc_ml_query_params_t params = getParams().toCQueryParams();
+    MLQueryParams params = getParams();
+    vlc_ml_query_params_t queryParams = params.toCQueryParams();
     vlc_ml_playlist_type_t mlPlaylistType = qmlToMLPlaylistType(m_playlistType);
-    return vlc_ml_count_playlists(ml, &params, mlPlaylistType);
+    return vlc_ml_count_playlists(ml, &queryParams, mlPlaylistType);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLPlaylistListModel::Loader::load(vlc_medialibrary_t* ml, size_t index, size_t count) const /* override */
 {
-    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+    MLQueryParams params = getParams(index, count);
+    vlc_ml_query_params_t queryParams = params.toCQueryParams();
 
     vlc_ml_playlist_type_t mlPlaylistType = qmlToMLPlaylistType(m_playlistType);
 
-    ml_unique_ptr<vlc_ml_playlist_list_t> list(vlc_ml_list_playlists(ml, &params, mlPlaylistType));
+    ml_unique_ptr<vlc_ml_playlist_list_t> list(vlc_ml_list_playlists(ml, &queryParams, mlPlaylistType));
 
     if (list == nullptr)
         return {};


=====================================
modules/gui/qt/medialibrary/mlplaylistmodel.cpp
=====================================
@@ -512,18 +512,20 @@ MLPlaylistModel::Loader::Loader(const MLPlaylistModel & model) : MLBaseModel::Ba
 
 size_t MLPlaylistModel::Loader::count(vlc_medialibrary_t* ml) const /* override */
 {
-    vlc_ml_query_params_t params = getParams().toCQueryParams();
+    MLQueryParams params = getParams();
+    auto queryParams = params.toCQueryParams();
 
-    return vlc_ml_count_playlist_media(ml, &params, m_parent.id);
+    return vlc_ml_count_playlist_media(ml, &queryParams, m_parent.id);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLPlaylistModel::Loader::load(vlc_medialibrary_t* ml, size_t index, size_t count) const /* override */
 {
-    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+    MLQueryParams params = getParams(index, count);
+    auto queryParams = params.toCQueryParams();
 
     ml_unique_ptr<vlc_ml_media_list_t> list {
-        vlc_ml_list_playlist_media(ml, &params, m_parent.id)
+        vlc_ml_list_playlist_media(ml, &queryParams, m_parent.id)
     };
 
     if (list == nullptr)


=====================================
modules/gui/qt/medialibrary/mlrecentsmodel.cpp
=====================================
@@ -91,23 +91,15 @@ void MLRecentsModel::onVlcMlEvent( const MLEvent &event )
     }
     MLBaseModel::onVlcMlEvent( event );
 }
-void MLRecentsModel::setNumberOfItemsToShow( int n ){
-    m_numberOfItemsToShow = n;
-    invalidateCache();
-}
-int MLRecentsModel::getNumberOfItemsToShow() const {
-    return m_numberOfItemsToShow;
-}
 
 std::unique_ptr<MLBaseModel::BaseLoader>
 MLRecentsModel::createLoader() const
 {
-    return std::make_unique<Loader>(*this, m_numberOfItemsToShow);
+    return std::make_unique<Loader>(*this);
 }
 
-MLRecentsModel::Loader::Loader(const MLRecentsModel &model, int numberOfItemsToShow)
+MLRecentsModel::Loader::Loader(const MLRecentsModel &model)
     : BaseLoader(model)
-    , m_numberOfItemsToShow(numberOfItemsToShow)
 {
 }
 
@@ -116,10 +108,7 @@ size_t MLRecentsModel::Loader::count(vlc_medialibrary_t* ml) const
     MLQueryParams params = getParams();
     auto queryParams = params.toCQueryParams();
 
-    size_t realCount = vlc_ml_count_history( ml, &queryParams );
-    if (m_numberOfItemsToShow >= 0)
-        return std::min( realCount, static_cast<size_t>(m_numberOfItemsToShow) );
-    return realCount;
+    return vlc_ml_count_history( ml, &queryParams );
 }
 
 std::vector<std::unique_ptr<MLItem>>
@@ -128,21 +117,16 @@ MLRecentsModel::Loader::load(vlc_medialibrary_t* ml, size_t index, size_t count)
     MLQueryParams params = getParams(index, count);
     auto queryParams = params.toCQueryParams();
 
-    std::vector<std::unique_ptr<MLItem>> res;
-    if (m_numberOfItemsToShow >= 0)
-    {
-        if (queryParams.i_offset <= static_cast<uint32_t>(m_numberOfItemsToShow))
-           queryParams.i_nbResults = static_cast<uint32_t>(m_numberOfItemsToShow) - queryParams.i_offset;
-        else
-            return res;
-    }
-
     ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_history(
                 ml, &queryParams ) };
     if ( media_list == nullptr )
         return {};
+
+    std::vector<std::unique_ptr<MLItem>> res;
+
     for( vlc_ml_media_t &media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
         res.emplace_back( std::make_unique<MLRecentMedia>( &media ) );
+
     return res;
 }
 


=====================================
modules/gui/qt/medialibrary/mlrecentsmodel.hpp
=====================================
@@ -47,8 +47,6 @@ private:
 class MLRecentsModel : public MLBaseModel
 {
     Q_OBJECT
-    Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow WRITE setNumberOfItemsToShow FINAL)
-
 public:
     enum Roles {
         RECENT_MEDIA_ID = Qt::UserRole + 1,
@@ -61,13 +59,9 @@ public:
     virtual ~MLRecentsModel() = default;
 
     QHash<int, QByteArray> roleNames() const override;
-    int m_numberOfItemsToShow = -1;
 
     Q_INVOKABLE void clearHistory();
 
-    void setNumberOfItemsToShow(int);
-    int getNumberOfItemsToShow() const;
-
 protected:
     QVariant itemRoleData(MLItem *item, int role) const override;
 
@@ -84,13 +78,11 @@ private:
 
     struct Loader : public BaseLoader
     {
-        Loader(const MLRecentsModel &model, int numberOfItemsToShow);
+        Loader(const MLRecentsModel &model);
 
         size_t count(vlc_medialibrary_t* ml) const override;
         std::vector<std::unique_ptr<MLItem>> load(vlc_medialibrary_t* ml, size_t index, size_t count) const override;
         std::unique_ptr<MLItem> loadItemById(vlc_medialibrary_t* ml, MLItemId itemId) const override;
-    private:
-        int m_numberOfItemsToShow;
     };
 };
 


=====================================
modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
=====================================
@@ -29,21 +29,7 @@ std::unique_ptr<MLBaseModel::BaseLoader>
 MLRecentsVideoModel::createLoader() const
 /* override */
 {
-    return std::make_unique<Loader>(*this, m_numberOfItemsToShow);
-}
-
-// Private functions
-
-int MLRecentsVideoModel::getNumberOfItemsToShow()
-{
-    return m_numberOfItemsToShow;
-}
-
-void MLRecentsVideoModel::setNumberOfItemsToShow(int number)
-{
-    m_numberOfItemsToShow = number;
-
-    invalidateCache();
+    return std::make_unique<Loader>(*this);
 }
 
 // Private MLVideoModel reimplementation
@@ -64,9 +50,8 @@ void MLRecentsVideoModel::onVlcMlEvent(const MLEvent & event) /* override */
 
 // Loader
 
-MLRecentsVideoModel::Loader::Loader(const MLRecentsVideoModel & model, int numberOfItemsToShow)
+MLRecentsVideoModel::Loader::Loader(const MLRecentsVideoModel & model)
     : MLBaseModel::BaseLoader(model)
-    , m_numberOfItemsToShow(numberOfItemsToShow)
 {}
 
 size_t MLRecentsVideoModel::Loader::count(vlc_medialibrary_t* ml) const /* override */
@@ -74,44 +59,24 @@ size_t MLRecentsVideoModel::Loader::count(vlc_medialibrary_t* ml) const /* overr
     MLQueryParams params = getParams();
 
     auto queryParams = params.toCQueryParams();
-
-    size_t realCount = vlc_ml_count_history_by_type(ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO);
-
-    if (m_numberOfItemsToShow >= 0)
-    {
-        return std::min(realCount, static_cast<size_t> (m_numberOfItemsToShow));
-    }
-
-    return realCount;
+    return vlc_ml_count_history_by_type(ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLRecentsVideoModel::Loader::load(vlc_medialibrary_t* ml, size_t index, size_t count) const /* override */
 {
     MLQueryParams params = getParams(index, count);
-
     auto queryParams = params.toCQueryParams();
 
-    std::vector<std::unique_ptr<MLItem>> res;
-
-    if (m_numberOfItemsToShow >= 0)
-    {
-        uint32_t count = static_cast<uint32_t> (m_numberOfItemsToShow);
-
-        if (queryParams.i_offset > count)
-            return res;
-
-        queryParams.i_nbResults = count - queryParams.i_offset;
-    }
-
-    ml_unique_ptr<vlc_ml_media_list_t> media_list
-    {
+    ml_unique_ptr<vlc_ml_media_list_t> media_list {
         vlc_ml_list_history_by_type(ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO)
     };
 
     if (media_list == nullptr)
         return {};
 
+    std::vector<std::unique_ptr<MLItem>> res;
+
     for (const vlc_ml_media_t & media : ml_range_iterate<vlc_ml_media_t>(media_list))
     {
         res.emplace_back( std::make_unique<MLVideo>(&media));


=====================================
modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
=====================================
@@ -29,9 +29,6 @@ class MLRecentsVideoModel : public MLVideoModel
 {
     Q_OBJECT
 
-    Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow
-               WRITE setNumberOfItemsToShow FINAL)
-
 public:
     explicit MLRecentsVideoModel(QObject * parent = nullptr);
 
@@ -40,28 +37,19 @@ public:
 protected: // MLBaseModel implementation
     std::unique_ptr<MLBaseModel::BaseLoader> createLoader() const override;
 
-private: // Functions
-    int  getNumberOfItemsToShow();
-    void setNumberOfItemsToShow(int number);
-
 protected: // MLVideoModel reimplementation
     void onVlcMlEvent(const MLEvent & event) override;
 
-private: // Variables
-    int m_numberOfItemsToShow = 10;
-
 private:
     struct Loader : public BaseLoader
     {
-        Loader(const MLRecentsVideoModel & model, int numberOfItemsToShow);
+        Loader(const MLRecentsVideoModel & model);
 
         size_t count(vlc_medialibrary_t* ml) const override;
 
         std::vector<std::unique_ptr<MLItem>> load(vlc_medialibrary_t* ml, size_t index, size_t count) const override;
 
         std::unique_ptr<MLItem> loadItemById(vlc_medialibrary_t* ml, MLItemId itemId) const override;
-    private:
-        int m_numberOfItemsToShow;
     };
 };
 


=====================================
modules/gui/qt/medialibrary/mlvideofoldersmodel.cpp
=====================================
@@ -168,18 +168,20 @@ MLVideoFoldersModel::Loader::Loader(const MLVideoFoldersModel & model)
 
 size_t MLVideoFoldersModel::Loader::count(vlc_medialibrary_t * ml) const /* override */
 {
-    vlc_ml_query_params_t params = getParams().toCQueryParams();
+    MLQueryParams params = getParams();
+    auto queryParams = params.toCQueryParams();
 
-    return vlc_ml_count_folders_by_type(ml, &params, VLC_ML_MEDIA_TYPE_VIDEO);
+    return vlc_ml_count_folders_by_type(ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLVideoFoldersModel::Loader::load(vlc_medialibrary_t * ml,
                                   size_t index, size_t count) const /* override */
 {
-    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+    MLQueryParams params = getParams(index, count);
+    auto queryParams = params.toCQueryParams();
 
-    ml_unique_ptr<vlc_ml_folder_list_t> list(vlc_ml_list_folders_by_type(ml, &params,
+    ml_unique_ptr<vlc_ml_folder_list_t> list(vlc_ml_list_folders_by_type(ml, &queryParams,
                                                                          VLC_ML_MEDIA_TYPE_VIDEO));
 
     if (list == nullptr)


=====================================
modules/gui/qt/medialibrary/mlvideogroupsmodel.cpp
=====================================
@@ -223,18 +223,21 @@ MLVideoGroupsModel::Loader::Loader(const MLVideoGroupsModel & model)
 
 size_t MLVideoGroupsModel::Loader::count(vlc_medialibrary_t* ml) const /* override */
 {
-    vlc_ml_query_params_t params = getParams().toCQueryParams();
+    MLQueryParams params = getParams();
+    auto queryParams = params.toCQueryParams();
 
-    return vlc_ml_count_groups(ml, &params);
+    return vlc_ml_count_groups(ml, &queryParams);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLVideoGroupsModel::Loader::load(vlc_medialibrary_t* ml,
                                  size_t index, size_t count) const /* override */
 {
-    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+    MLQueryParams params = getParams(index, count);
+    auto queryParams = params.toCQueryParams();
 
-    ml_unique_ptr<vlc_ml_group_list_t> list(vlc_ml_list_groups(ml, &params));
+
+    ml_unique_ptr<vlc_ml_group_list_t> list(vlc_ml_list_groups(ml, &queryParams));
 
     if (list == nullptr)
         return {};


=====================================
modules/gui/qt/medialibrary/mlvideomodel.cpp
=====================================
@@ -261,29 +261,31 @@ MLVideoModel::createLoader() const
 
 size_t MLVideoModel::Loader::count(vlc_medialibrary_t* ml) const /* override */
 {
-    vlc_ml_query_params_t params = getParams().toCQueryParams();
+    MLQueryParams params = getParams();
+    auto queryParams = params.toCQueryParams();
 
     int64_t id = m_parent.id;
 
     if (id <= 0)
-        return vlc_ml_count_video_media(ml, &params);
+        return vlc_ml_count_video_media(ml, &queryParams);
     else
-        return vlc_ml_count_video_of(ml, &params, m_parent.type, id);
+        return vlc_ml_count_video_of(ml, &queryParams, m_parent.type, id);
 }
 
 std::vector<std::unique_ptr<MLItem>>
 MLVideoModel::Loader::load(vlc_medialibrary_t* ml, size_t index, size_t count) const /* override */
 {
-    vlc_ml_query_params_t params = getParams(index, count).toCQueryParams();
+    MLQueryParams params = getParams(index, count);
+    auto queryParams = params.toCQueryParams();
 
     ml_unique_ptr<vlc_ml_media_list_t> list;
 
     int64_t id = m_parent.id;
 
     if (id <= 0)
-        list.reset(vlc_ml_list_video_media(ml, &params));
+        list.reset(vlc_ml_list_video_media(ml, &queryParams));
     else
-        list.reset(vlc_ml_list_video_of(ml, &params, m_parent.type, id));
+        list.reset(vlc_ml_list_video_of(ml, &queryParams, m_parent.type, id));
 
     if (list == nullptr)
         return {};


=====================================
modules/gui/qt/medialibrary/qml/VideoDisplayRecentVideos.qml
=====================================
@@ -117,6 +117,7 @@ FocusScope {
                 id: recentModel
 
                 ml: MediaLib
+                limit: 10
             }
 
             delegate: VideoGridItem {


=====================================
modules/gui/qt/menus/menus.cpp
=====================================
@@ -197,7 +197,7 @@ void VLCMenuBar::FileMenu(qt_intf_t *p_intf, QMenu *menu)
     {
         MLRecentsModel* recentModel = new MLRecentsModel(nullptr);
         recentModel->setMl(mi->getMediaLibrary());
-        recentModel->setNumberOfItemsToShow(10);
+        recentModel->setLimit(10);
         QMenu* recentsMenu = new RecentMenu(recentModel, mi->getMediaLibrary(), menu);
         recentsMenu->setTitle(qtr( "Open &Recent Media" ) );
         recentModel->setParent(recentsMenu);


=====================================
modules/gui/qt/menus/qml/GlobalShortcutsMedialib.qml
=====================================
@@ -25,7 +25,7 @@ Item {
 
     MLRecentModel {
         id: recentModel
-        numberOfItemsToShow: 10
+        limit: 10
         ml: MediaLib
     }
 



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/47a6a9ea155c10607a7b54febc75ec78b6c169fd...fa0e5dec76d139f3f22210a38cea639dc682450e

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/47a6a9ea155c10607a7b54febc75ec78b6c169fd...fa0e5dec76d139f3f22210a38cea639dc682450e
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list