[vlmc-devel] MediaLibrary: Create the models early, and refresh them on demand

Hugo Beauzée-Luyssen git at videolan.org
Tue Jul 12 00:26:40 CEST 2016


vlmc | branch: medialibrary | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Mon Jul 11 21:59:33 2016 +0200| [d1453c130f36ad9ec90f1ae9e4597cc944b79c66] | committer: Hugo Beauzée-Luyssen

MediaLibrary: Create the models early, and refresh them on demand

> https://code.videolan.org/videolan/vlmc/commit/d1453c130f36ad9ec90f1ae9e4597cc944b79c66
---

 src/Library/MediaLibrary.cpp      | 12 ++++++++----
 src/Library/MediaLibraryModel.cpp | 39 ++++++++++++++++++++++-----------------
 src/Library/MediaLibraryModel.h   |  1 +
 3 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/src/Library/MediaLibrary.cpp b/src/Library/MediaLibrary.cpp
index dda5f71..5639c5d 100644
--- a/src/Library/MediaLibrary.cpp
+++ b/src/Library/MediaLibrary.cpp
@@ -25,10 +25,11 @@
 #include "MediaLibraryModel.h"
 
 MediaLibrary::MediaLibrary( Settings* settings )
-    : m_videoModel( nullptr )
-    , m_initialized( false )
+    : m_initialized( false )
 {
     m_ml.reset( NewMediaLibrary() );
+    m_videoModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::VideoType, this );
+    m_audioModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::AudioType, this );
     auto s = settings->createVar( SettingValue::List, QStringLiteral( "vlmc/mlDirs" ), QVariantList(),
                         "Media Library folders", "List of folders VLMC will search for media files",
                          SettingValue::Folders );
@@ -75,8 +76,6 @@ MediaLibrary::workspaceChanged( const QVariant& workspace )
         // Initializing the medialibrary doesn't start new folders discovery.
         // This will happen after the first call to IMediaLibrary::discover()
         m_ml->initialize( w + "/ml.db", w + "/thumbnails/", this );
-        m_videoModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::VideoType, this );
-        m_audioModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::AudioType, this );
         m_initialized = true;
     }
     //else FIXME, and relocate the media library
@@ -169,6 +168,11 @@ void MediaLibrary::onDiscoveryStarted( const std::string& entryPoint )
 
 void MediaLibrary::onDiscoveryCompleted( const std::string& entryPoint )
 {
+    if ( entryPoint.empty() == true )
+    {
+        m_videoModel->refresh();
+        m_audioModel->refresh();
+    }
     emit discoveryCompleted( QString::fromStdString( entryPoint ) );
 }
 
diff --git a/src/Library/MediaLibraryModel.cpp b/src/Library/MediaLibraryModel.cpp
index 7a283be..9710831 100644
--- a/src/Library/MediaLibraryModel.cpp
+++ b/src/Library/MediaLibraryModel.cpp
@@ -32,24 +32,8 @@ MediaLibraryModel::MediaLibraryModel( medialibrary::IMediaLibrary& ml, medialibr
     : QAbstractListModel(parent)
     , m_ml( ml )
     , m_mediaType( type )
+    , m_rowCount( 0 )
 {
-    switch (type)
-    {
-    case medialibrary::IMedia::Type::AudioType:
-        m_media = m_ml.audioFiles();
-        break;
-    case medialibrary::IMedia::Type::VideoType:
-        m_media = m_ml.videoFiles();
-        break;
-    default:
-        Q_UNREACHABLE();
-    }
-    if ( m_media.size() == 0 )
-        return;
-    beginInsertRows( QModelIndex(), 0, m_media.size() );
-    m_rowCount = m_media.size();
-    insertRows( 0, m_media.size() );
-    endInsertRows();
 }
 
 void MediaLibraryModel::addMedia( medialibrary::MediaPtr media )
@@ -134,3 +118,24 @@ MediaLibraryModel::roleNames() const
         { Roles::Duration, "duration" }
     };
 }
+
+void MediaLibraryModel::refresh()
+{
+    std::lock_guard<std::mutex> lock( m_mediaMutex );
+
+    beginResetModel();
+
+    switch ( m_mediaType )
+    {
+    case medialibrary::IMedia::Type::AudioType:
+        m_media = m_ml.audioFiles();
+        break;
+    case medialibrary::IMedia::Type::VideoType:
+        m_media = m_ml.videoFiles();
+        break;
+    default:
+        Q_UNREACHABLE();
+    }
+    m_rowCount = m_media.size();
+    endResetModel();
+}
diff --git a/src/Library/MediaLibraryModel.h b/src/Library/MediaLibraryModel.h
index 28c2940..479de85 100644
--- a/src/Library/MediaLibraryModel.h
+++ b/src/Library/MediaLibraryModel.h
@@ -52,6 +52,7 @@ public:
     int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
     QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
     QHash<int, QByteArray> roleNames() const;
+    void refresh();
 
 private:
     medialibrary::IMediaLibrary& m_ml;



More information about the Vlmc-devel mailing list