[vlmc-devel] MediaLibrary: Propagate media deletion to the model

Hugo Beauzée-Luyssen git at videolan.org
Fri Jul 8 22:56:50 CEST 2016


vlmc | branch: medialibrary | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Jul  8 16:47:38 2016 +0200| [3225291d12c942b12f0e670cb4985c801ceb9362] | committer: Hugo Beauzée-Luyssen

MediaLibrary: Propagate media deletion to the model

> https://code.videolan.org/videolan/vlmc/commit/3225291d12c942b12f0e670cb4985c801ceb9362
---

 src/Library/MediaLibrary.cpp      | 10 +++++++++-
 src/Library/MediaLibraryModel.cpp | 11 +++++------
 src/Library/MediaLibraryModel.h   |  2 +-
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/Library/MediaLibrary.cpp b/src/Library/MediaLibrary.cpp
index 7065f26..2512baa 100644
--- a/src/Library/MediaLibrary.cpp
+++ b/src/Library/MediaLibrary.cpp
@@ -118,8 +118,16 @@ void MediaLibrary::onMediaUpdated( std::vector<medialibrary::MediaPtr> mediaList
     }
 }
 
-void MediaLibrary::onMediaDeleted( std::vector<int64_t> )
+void MediaLibrary::onMediaDeleted( std::vector<int64_t> mediaList )
 {
+    for ( auto id : mediaList )
+    {
+        // We can't know the media type, however ID are unique regardless of the type
+        // so we are sure that we will remove the correct media.
+        if ( m_videoModel->removeMedia( id ) == true )
+            continue;
+        m_audioModel->removeMedia( id );
+    }
 }
 
 void MediaLibrary::onArtistsAdded( std::vector<medialibrary::ArtistPtr> )
diff --git a/src/Library/MediaLibraryModel.cpp b/src/Library/MediaLibraryModel.cpp
index f60d85c..e9a1d33 100644
--- a/src/Library/MediaLibraryModel.cpp
+++ b/src/Library/MediaLibraryModel.cpp
@@ -71,22 +71,21 @@ void MediaLibraryModel::updateMedia( medialibrary::MediaPtr media )
     emit dataChanged( m, m );
 }
 
-void MediaLibraryModel::removeMedia( medialibrary::MediaPtr media )
+bool MediaLibraryModel::removeMedia( int64_t mediaId )
 {
-    if ( media->type() != m_mediaType )
-        return;
     std::lock_guard<std::mutex> lock( m_mediaMutex );
-    auto it = std::find_if( begin( m_media ), end( m_media ), [media](medialibrary::MediaPtr m) {
-        return m->id() == media->id();
+    auto it = std::find_if( begin( m_media ), end( m_media ), [mediaId](medialibrary::MediaPtr m) {
+        return m->id() == mediaId;
     });
     if ( it == end( m_media ) )
-        return;
+        return false;
     auto idx = it - begin( m_media );
     beginRemoveRows(QModelIndex(), idx, idx );
     m_media.erase( it );
     m_rowCount.fetch_sub( 1, std::memory_order_relaxed );
     removeRow( idx );
     endRemoveRows();
+    return true;
 }
 
 int MediaLibraryModel::rowCount( const QModelIndex& ) const
diff --git a/src/Library/MediaLibraryModel.h b/src/Library/MediaLibraryModel.h
index 00ad4b8..28c2940 100644
--- a/src/Library/MediaLibraryModel.h
+++ b/src/Library/MediaLibraryModel.h
@@ -48,7 +48,7 @@ public:
 
     void addMedia( medialibrary::MediaPtr media );
     void updateMedia( medialibrary::MediaPtr media );
-    void removeMedia( medialibrary::MediaPtr media );
+    bool removeMedia( int64_t media );
     int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
     QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
     QHash<int, QByteArray> roleNames() const;



More information about the Vlmc-devel mailing list