[vlc-devel] [PATCH v2 01/13] qt: medialib: make fetch() const

Romain Vimont rom1v at videolabs.io
Thu Nov 26 17:10:33 CET 2020


The fetch() methods returns the data it loads, it does not need access
(except possibly to mutable fields for caching).

This makes it consistent with countTotalElements().
---
 modules/gui/qt/medialibrary/mlalbummodel.cpp        | 2 +-
 modules/gui/qt/medialibrary/mlalbummodel.hpp        | 2 +-
 modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp   | 2 +-
 modules/gui/qt/medialibrary/mlalbumtrackmodel.hpp   | 2 +-
 modules/gui/qt/medialibrary/mlartistmodel.cpp       | 2 +-
 modules/gui/qt/medialibrary/mlartistmodel.hpp       | 2 +-
 modules/gui/qt/medialibrary/mlbasemodel.hpp         | 6 +++---
 modules/gui/qt/medialibrary/mlgenremodel.cpp        | 2 +-
 modules/gui/qt/medialibrary/mlgenremodel.hpp        | 2 +-
 modules/gui/qt/medialibrary/mlrecentsmodel.cpp      | 2 +-
 modules/gui/qt/medialibrary/mlrecentsmodel.hpp      | 2 +-
 modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp | 5 +++--
 modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp | 4 ++--
 modules/gui/qt/medialibrary/mlurlmodel.cpp          | 2 +-
 modules/gui/qt/medialibrary/mlurlmodel.hpp          | 2 +-
 modules/gui/qt/medialibrary/mlvideomodel.cpp        | 2 +-
 modules/gui/qt/medialibrary/mlvideomodel.hpp        | 2 +-
 17 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/modules/gui/qt/medialibrary/mlalbummodel.cpp b/modules/gui/qt/medialibrary/mlalbummodel.cpp
index acb6e7fbec..423081a0d0 100644
--- a/modules/gui/qt/medialibrary/mlalbummodel.cpp
+++ b/modules/gui/qt/medialibrary/mlalbummodel.cpp
@@ -88,7 +88,7 @@ QHash<int, QByteArray> MLAlbumModel::roleNames() const
     };
 }
 
-std::vector<std::unique_ptr<MLAlbum>> MLAlbumModel::fetch( )
+std::vector<std::unique_ptr<MLAlbum>> MLAlbumModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_album_list_t> album_list;
     if ( m_parent.id <= 0 )
diff --git a/modules/gui/qt/medialibrary/mlalbummodel.hpp b/modules/gui/qt/medialibrary/mlalbummodel.hpp
index 804f32fe5b..2ffe82c5ee 100644
--- a/modules/gui/qt/medialibrary/mlalbummodel.hpp
+++ b/modules/gui/qt/medialibrary/mlalbummodel.hpp
@@ -58,7 +58,7 @@ public:
     Q_INVOKABLE QHash<int, QByteArray> roleNames() const override;
 
 private:
-    std::vector<std::unique_ptr<MLAlbum>> fetch() override;
+    std::vector<std::unique_ptr<MLAlbum>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
diff --git a/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp b/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
index 4d07c93757..cd091129f0 100644
--- a/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlalbumtrackmodel.cpp
@@ -103,7 +103,7 @@ size_t MLAlbumTrackModel::countTotalElements() const
     return vlc_ml_count_media_of(m_ml, &queryParams, m_parent.type, m_parent.id );
 }
 
-std::vector<std::unique_ptr<MLAlbumTrack>> MLAlbumTrackModel::fetch()
+std::vector<std::unique_ptr<MLAlbumTrack>> MLAlbumTrackModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_media_list_t> media_list;
 
diff --git a/modules/gui/qt/medialibrary/mlalbumtrackmodel.hpp b/modules/gui/qt/medialibrary/mlalbumtrackmodel.hpp
index 93785660ce..4289c58766 100644
--- a/modules/gui/qt/medialibrary/mlalbumtrackmodel.hpp
+++ b/modules/gui/qt/medialibrary/mlalbumtrackmodel.hpp
@@ -58,7 +58,7 @@ public:
     QHash<int, QByteArray> roleNames() const override;
 
 private:
-    std::vector<std::unique_ptr<MLAlbumTrack>> fetch() override;
+    std::vector<std::unique_ptr<MLAlbumTrack>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
diff --git a/modules/gui/qt/medialibrary/mlartistmodel.cpp b/modules/gui/qt/medialibrary/mlartistmodel.cpp
index 8bc65fb6b0..a3f18f67f3 100644
--- a/modules/gui/qt/medialibrary/mlartistmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlartistmodel.cpp
@@ -67,7 +67,7 @@ QHash<int, QByteArray> MLArtistModel::roleNames() const
     };
 }
 
-std::vector<std::unique_ptr<MLArtist>> MLArtistModel::fetch()
+std::vector<std::unique_ptr<MLArtist>> MLArtistModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_artist_list_t> artist_list;
     if ( m_parent.id <= 0 )
diff --git a/modules/gui/qt/medialibrary/mlartistmodel.hpp b/modules/gui/qt/medialibrary/mlartistmodel.hpp
index af495d969d..e85a9b9149 100644
--- a/modules/gui/qt/medialibrary/mlartistmodel.hpp
+++ b/modules/gui/qt/medialibrary/mlartistmodel.hpp
@@ -49,7 +49,7 @@ public:
     QHash<int, QByteArray> roleNames() const override;
 
 private:
-    std::vector<std::unique_ptr<MLArtist>> fetch() override;
+    std::vector<std::unique_ptr<MLArtist>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp
index c25fb948b3..bac19005ed 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.hpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp
@@ -235,7 +235,7 @@ protected:
         {
             m_total_count = countTotalElements();
             if ( m_total_count > 0 )
-                m_item_list = const_cast<MLSlidingWindowModel<T>*>(this)->fetch();
+                m_item_list = fetch();
             m_initialized = true;
             emit countChanged( static_cast<unsigned int>(m_total_count) );
         }
@@ -249,7 +249,7 @@ protected:
                 m_query_param.i_offset = 0;
             else
                 m_query_param.i_offset = idx - idx % m_query_param.i_nbResults;
-            m_item_list = const_cast<MLSlidingWindowModel<T>*>(this)->fetch();
+            m_item_list = fetch();
         }
 
         //db has changed
@@ -284,7 +284,7 @@ protected:
 
 private:
     virtual size_t countTotalElements() const = 0;
-    virtual std::vector<std::unique_ptr<T>> fetch() = 0;
+    virtual std::vector<std::unique_ptr<T>> fetch() const = 0;
     virtual void thumbnailUpdated( int ) {}
 
     mutable std::vector<std::unique_ptr<T>> m_item_list;
diff --git a/modules/gui/qt/medialibrary/mlgenremodel.cpp b/modules/gui/qt/medialibrary/mlgenremodel.cpp
index 55bf5e78a9..339cd49384 100644
--- a/modules/gui/qt/medialibrary/mlgenremodel.cpp
+++ b/modules/gui/qt/medialibrary/mlgenremodel.cpp
@@ -68,7 +68,7 @@ QHash<int, QByteArray> MLGenreModel::roleNames() const
     };
 }
 
-std::vector<std::unique_ptr<MLGenre>> MLGenreModel::fetch()
+std::vector<std::unique_ptr<MLGenre>> MLGenreModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_genre_list_t> genre_list(
         vlc_ml_list_genres(m_ml, &m_query_param)
diff --git a/modules/gui/qt/medialibrary/mlgenremodel.hpp b/modules/gui/qt/medialibrary/mlgenremodel.hpp
index 6943207d03..3bc4928c3c 100644
--- a/modules/gui/qt/medialibrary/mlgenremodel.hpp
+++ b/modules/gui/qt/medialibrary/mlgenremodel.hpp
@@ -52,7 +52,7 @@ public:
     QVariant data(const QModelIndex &index, int role) const override;
 
 private:
-    std::vector<std::unique_ptr<MLGenre>> fetch() override;
+    std::vector<std::unique_ptr<MLGenre>> fetch() const override;
     size_t countTotalElements() const override;
     void onVlcMlEvent(const MLEvent &event) override;
     void thumbnailUpdated(int idx) override;
diff --git a/modules/gui/qt/medialibrary/mlrecentsmodel.cpp b/modules/gui/qt/medialibrary/mlrecentsmodel.cpp
index 8cdcbfacbf..5d0ce5bdde 100644
--- a/modules/gui/qt/medialibrary/mlrecentsmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlrecentsmodel.cpp
@@ -79,7 +79,7 @@ void MLRecentsModel::clearHistory()
     vlc_ml_clear_history(m_ml);
 }
 
-std::vector<std::unique_ptr<MLRecentMedia> > MLRecentsModel::fetch()
+std::vector<std::unique_ptr<MLRecentMedia> > MLRecentsModel::fetch() const
 {
     std::vector<std::unique_ptr<MLRecentMedia>> res;
     auto queryParams = m_query_param;
diff --git a/modules/gui/qt/medialibrary/mlrecentsmodel.hpp b/modules/gui/qt/medialibrary/mlrecentsmodel.hpp
index 8040c2918f..171132b84a 100644
--- a/modules/gui/qt/medialibrary/mlrecentsmodel.hpp
+++ b/modules/gui/qt/medialibrary/mlrecentsmodel.hpp
@@ -76,7 +76,7 @@ public:
     int getNumberOfItemsToShow() const;
 
 private:
-    std::vector<std::unique_ptr<MLRecentMedia>> fetch() override;
+    std::vector<std::unique_ptr<MLRecentMedia>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria( int /* role */ ) const override{
         return VLC_ML_SORTING_DEFAULT;
diff --git a/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp b/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
index 2e7c1947f6..9c09945c50 100644
--- a/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
+++ b/modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
@@ -92,7 +92,7 @@ QHash<int, QByteArray> MLRecentsVideoModel::roleNames() const
     };
 }
 
-std::vector<std::unique_ptr<MLVideo> > MLRecentsVideoModel::fetch()
+std::vector<std::unique_ptr<MLVideo> > MLRecentsVideoModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_history(
                 m_ml, &m_query_param ) };
@@ -111,7 +111,8 @@ std::vector<std::unique_ptr<MLVideo> > MLRecentsVideoModel::fetch()
 
 size_t MLRecentsVideoModel::countTotalElements() const
 {
-
+    // FIXME: countTotalElements() may not depend on fetch(), since the call to
+    // fetch() depends on countTotalElements()
     if(numberOfItemsToShow == -1){
         return m_video_count;
     }
diff --git a/modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp b/modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
index 65c923b8bd..0b8e4be0ab 100644
--- a/modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
+++ b/modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
@@ -45,7 +45,7 @@ public:
     int numberOfItemsToShow = 10;
 
 private:
-    std::vector<std::unique_ptr<MLVideo>> fetch() override;
+    std::vector<std::unique_ptr<MLVideo>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria( int /* role */ ) const override{
         return VLC_ML_SORTING_DEFAULT;
@@ -56,7 +56,7 @@ private:
     virtual void onVlcMlEvent( const MLEvent &event ) override;
     void setNumberOfItemsToShow(int);
     int getNumberOfItemsToShow();
-    int m_video_count;
+    mutable int m_video_count;
 };
 
 #endif // ML_RECENTS_VIDEO_MODEL_H
diff --git a/modules/gui/qt/medialibrary/mlurlmodel.cpp b/modules/gui/qt/medialibrary/mlurlmodel.cpp
index 92a114a4fe..4dd949adba 100644
--- a/modules/gui/qt/medialibrary/mlurlmodel.cpp
+++ b/modules/gui/qt/medialibrary/mlurlmodel.cpp
@@ -81,7 +81,7 @@ size_t MLUrlModel::countTotalElements() const
     return s;
 }
 
-std::vector<std::unique_ptr<MLUrl>> MLUrlModel::fetch()
+std::vector<std::unique_ptr<MLUrl>> MLUrlModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_media_list_t> media_list;
     media_list.reset( vlc_ml_list_stream_history(m_ml, &m_query_param) );
diff --git a/modules/gui/qt/medialibrary/mlurlmodel.hpp b/modules/gui/qt/medialibrary/mlurlmodel.hpp
index 425edb48e5..bbc3bdc173 100644
--- a/modules/gui/qt/medialibrary/mlurlmodel.hpp
+++ b/modules/gui/qt/medialibrary/mlurlmodel.hpp
@@ -70,7 +70,7 @@ public:
     Q_INVOKABLE void addAndPlay( const QString& url );
 
 private:
-    std::vector<std::unique_ptr<MLUrl>> fetch() override;
+    std::vector<std::unique_ptr<MLUrl>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     virtual void onVlcMlEvent( const MLEvent &event ) override;
diff --git a/modules/gui/qt/medialibrary/mlvideomodel.cpp b/modules/gui/qt/medialibrary/mlvideomodel.cpp
index 21332e298a..63338b2f96 100644
--- a/modules/gui/qt/medialibrary/mlvideomodel.cpp
+++ b/modules/gui/qt/medialibrary/mlvideomodel.cpp
@@ -92,7 +92,7 @@ QHash<int, QByteArray> MLVideoModel::roleNames() const
     };
 }
 
-std::vector<std::unique_ptr<MLVideo> > MLVideoModel::fetch()
+std::vector<std::unique_ptr<MLVideo> > MLVideoModel::fetch() const
 {
     ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_video_media(
                 m_ml, &m_query_param ) };
diff --git a/modules/gui/qt/medialibrary/mlvideomodel.hpp b/modules/gui/qt/medialibrary/mlvideomodel.hpp
index 653c6e164c..f373d26b6b 100644
--- a/modules/gui/qt/medialibrary/mlvideomodel.hpp
+++ b/modules/gui/qt/medialibrary/mlvideomodel.hpp
@@ -63,7 +63,7 @@ public:
     QHash<int, QByteArray> roleNames() const override;
 
 private:
-    std::vector<std::unique_ptr<MLVideo>> fetch() override;
+    std::vector<std::unique_ptr<MLVideo>> fetch() const override;
     size_t countTotalElements() const override;
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
-- 
2.29.2



More information about the vlc-devel mailing list