diff --git a/modules/gui/qt/network/networkdevicemodel.cpp b/modules/gui/qt/network/networkdevicemodel.cpp index 836f23342fc29bc1c482191fdf77f357ef6d4143..3124cb563df742b94837e74dd6b26a928e997918 100644 --- a/modules/gui/qt/network/networkdevicemodel.cpp +++ b/modules/gui/qt/network/networkdevicemodel.cpp @@ -289,7 +289,7 @@ bool NetworkDeviceModel::initializeMediaSources() if ( mediaSource == nullptr ) continue; std::unique_ptr<NetworkSourceListener> l{ new NetworkSourceListener( - MediaSourcePtr{ mediaSource, false }, this ) }; + MediaSourcePtr{ mediaSource, false }, std::make_unique<ListenerCb>(this) ) }; if ( l->listener == nullptr ) return false; m_listeners.push_back( std::move( l ) ); @@ -298,25 +298,25 @@ bool NetworkDeviceModel::initializeMediaSources() } -void NetworkDeviceModel::onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) +void NetworkDeviceModel::ListenerCb::onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) { if (node != &mediaSource->tree->root) return; - refreshDeviceList( std::move( mediaSource), node->pp_children, node->i_children, true ); + model->refreshDeviceList( std::move( mediaSource ), node->pp_children, node->i_children, true ); } -void NetworkDeviceModel::onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, - input_item_node_t *const children[], - size_t count ) +void NetworkDeviceModel::ListenerCb::onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, + input_item_node_t *const children[], + size_t count ) { if (parent != &mediaSource->tree->root) return; - refreshDeviceList( std::move( mediaSource ), children, count, false ); + model->refreshDeviceList( std::move( mediaSource ), children, count, false ); } -void NetworkDeviceModel::onItemRemoved(MediaSourcePtr mediaSource, input_item_node_t* node, - input_item_node_t *const children[], - size_t count ) +void NetworkDeviceModel::ListenerCb::onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t* node, + input_item_node_t *const children[], + size_t count ) { if (node != &mediaSource->tree->root) return; @@ -326,15 +326,15 @@ void NetworkDeviceModel::onItemRemoved(MediaSourcePtr mediaSource, input_item_no for ( auto i = 0u; i < count; ++i ) itemList.emplace_back( children[i]->p_item ); - QMetaObject::invokeMethod(this, [this, itemList=std::move(itemList)]() { + QMetaObject::invokeMethod(model, [model=model, itemList=std::move(itemList)]() { for (auto p_item : itemList) { QUrl itemUri = QUrl::fromEncoded(p_item->psz_uri); - auto it = std::find_if( begin( m_items ), end( m_items ), [p_item, itemUri](const Item& i) { + auto it = std::find_if( begin( model->m_items ), end( model->m_items ), [p_item, itemUri](const Item& i) { return QString::compare( qfu(p_item->psz_name), i.name, Qt::CaseInsensitive ) == 0 && itemUri.scheme() == i.mainMrl.scheme(); }); - if ( it == end( m_items ) ) + if ( it == end( model->m_items ) ) continue; auto mrlIt = std::find_if( begin( (*it).mrls ), end( (*it).mrls), @@ -347,11 +347,11 @@ void NetworkDeviceModel::onItemRemoved(MediaSourcePtr mediaSource, input_item_no (*it).mrls.erase( mrlIt ); if ( (*it).mrls.empty() == false ) continue; - auto idx = std::distance( begin( m_items ), it ); - beginRemoveRows({}, idx, idx ); - m_items.erase( it ); - endRemoveRows(); - emit countChanged(); + auto idx = std::distance( begin( model->m_items ), it ); + model->beginRemoveRows({}, idx, idx ); + model->m_items.erase( it ); + model->endRemoveRows(); + model->emit countChanged(); } }, Qt::QueuedConnection); } diff --git a/modules/gui/qt/network/networkdevicemodel.hpp b/modules/gui/qt/network/networkdevicemodel.hpp index 8e08fc310ae61d2cbe410fad1f2989f558c014d1..5937918e0b739f6ee64635bb0cbf25ec60f35575 100644 --- a/modules/gui/qt/network/networkdevicemodel.hpp +++ b/modules/gui/qt/network/networkdevicemodel.hpp @@ -36,7 +36,7 @@ #include <memory> class MainCtx; -class NetworkDeviceModel : public QAbstractListModel, public NetworkSourceListener::SourceListenerCb +class NetworkDeviceModel : public QAbstractListModel { Q_OBJECT public: @@ -139,14 +139,21 @@ private: }; bool initializeMediaSources(); - void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override; - void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override; - void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override; - inline void onItemPreparseEnded( MediaSourcePtr, input_item_node_t *, enum input_item_preparse_status ) override {} void refreshDeviceList(MediaSourcePtr mediaSource, input_item_node_t* const children[], size_t count , bool clear); private: + struct ListenerCb : public NetworkSourceListener::SourceListenerCb { + ListenerCb(NetworkDeviceModel *model) : model(model) {} + + void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override; + void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override; + void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override; + inline void onItemPreparseEnded( MediaSourcePtr, input_item_node_t *, enum input_item_preparse_status ) override {} + + NetworkDeviceModel *model; + }; + std::vector<Item> m_items; MainCtx* m_ctx = nullptr; SDCatType m_sdSource = CAT_UNDEFINED; diff --git a/modules/gui/qt/network/networkmediamodel.cpp b/modules/gui/qt/network/networkmediamodel.cpp index 6bc16fa4a4fc32b4dbf24a6c45bbceb56642b96d..b808d8e82fb18e784a73b8e0e930b7d8c56fc911 100644 --- a/modules/gui/qt/network/networkmediamodel.cpp +++ b/modules/gui/qt/network/networkmediamodel.cpp @@ -383,7 +383,7 @@ bool NetworkMediaModel::initializeMediaSources() return false; auto tree = m_treeItem.source->tree; - auto l = std::make_unique<NetworkSourceListener>( m_treeItem.source, this ); + auto l = std::make_unique<NetworkSourceListener>( m_treeItem.source, std::make_unique<ListenerCb>(this) ); if ( l->listener == nullptr ) return false; @@ -459,18 +459,18 @@ bool NetworkMediaModel::initializeMediaSources() return true; } -void NetworkMediaModel::onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node) +void NetworkMediaModel::ListenerCb::onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node) { InputItemPtr p_node { node->p_item }; - QMetaObject::invokeMethod(this, [this, p_node = std::move(p_node), mediaSource = std::move(mediaSource)]() { - if (p_node != m_treeItem.media) + QMetaObject::invokeMethod(model, [model=model, p_node = std::move(p_node), mediaSource = std::move(mediaSource)]() { + if (p_node != model->m_treeItem.media) return; input_item_node_t *res; input_item_node_t *parent; - vlc_media_tree_Lock( m_treeItem.source->tree ); - bool found = vlc_media_tree_Find( m_treeItem.source->tree, m_treeItem.media.get(), + vlc_media_tree_Lock( model->m_treeItem.source->tree ); + bool found = vlc_media_tree_Find( model->m_treeItem.source->tree, model->m_treeItem.media.get(), &res, &parent ); - vlc_media_tree_Unlock( m_treeItem.source->tree ); + vlc_media_tree_Unlock( model->m_treeItem.source->tree ); if (!found) return; @@ -479,13 +479,13 @@ void NetworkMediaModel::onItemCleared( MediaSourcePtr mediaSource, input_item_no for (int i = 0; i < res->i_children; i++) itemList.emplace_back(res->pp_children[i]->p_item); - refreshMediaList( std::move( mediaSource ), std::move( itemList ), true ); + model->refreshMediaList( std::move( mediaSource ), std::move( itemList ), true ); }, Qt::QueuedConnection); } -void NetworkMediaModel::onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, - input_item_node_t *const children[], - size_t count ) +void NetworkMediaModel::ListenerCb::onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, + input_item_node_t *const children[], + size_t count ) { InputItemPtr p_parent { parent->p_item }; std::vector<InputItemPtr> itemList; @@ -493,15 +493,15 @@ void NetworkMediaModel::onItemAdded( MediaSourcePtr mediaSource, input_item_node for (size_t i = 0; i < count; i++) itemList.emplace_back(children[i]->p_item); - QMetaObject::invokeMethod(this, [this, p_parent = std::move(p_parent), mediaSource = std::move(mediaSource), itemList=std::move(itemList)]() { - if ( p_parent == m_treeItem.media ) - refreshMediaList( std::move( mediaSource ), std::move( itemList ), false ); + QMetaObject::invokeMethod(model, [model=model, p_parent = std::move(p_parent), mediaSource = std::move(mediaSource), itemList=std::move(itemList)]() { + if ( p_parent == model->m_treeItem.media ) + model->refreshMediaList( std::move( mediaSource ), std::move( itemList ), false ); }, Qt::QueuedConnection); } -void NetworkMediaModel::onItemRemoved(MediaSourcePtr, input_item_node_t * node, - input_item_node_t *const children[], - size_t count ) +void NetworkMediaModel::ListenerCb::onItemRemoved( MediaSourcePtr, input_item_node_t * node, + input_item_node_t *const children[], + size_t count ) { std::vector<InputItemPtr> itemList; itemList.reserve( count ); @@ -509,18 +509,18 @@ void NetworkMediaModel::onItemRemoved(MediaSourcePtr, input_item_node_t * node, itemList.emplace_back( children[i]->p_item ); InputItemPtr p_node { node->p_item }; - QMetaObject::invokeMethod(this, [this, p_node=std::move(p_node), itemList=std::move(itemList)]() { - if (p_node != m_treeItem.media) + QMetaObject::invokeMethod(model, [model=model, p_node=std::move(p_node), itemList=std::move(itemList)]() { + if (p_node != model->m_treeItem.media) return; for (auto p_item : itemList) { QUrl itemUri = QUrl::fromEncoded(p_item->psz_uri); - auto it = std::find_if( begin( m_items ), end( m_items ), [&](const Item& i) { + auto it = std::find_if( begin( model->m_items ), end( model->m_items ), [&](const Item& i) { return QString::compare( qfu(p_item->psz_name), i.name, Qt::CaseInsensitive ) == 0 && itemUri.scheme() == i.mainMrl.scheme(); }); - if ( it == end( m_items ) ) + if ( it == end( model->m_items ) ) continue; auto mrlIt = std::find_if( begin( (*it).mrls ), end( (*it).mrls), @@ -532,25 +532,25 @@ void NetworkMediaModel::onItemRemoved(MediaSourcePtr, input_item_node_t * node, (*it).mrls.erase( mrlIt ); if ( (*it).mrls.empty() == false ) continue; - auto idx = std::distance( begin( m_items ), it ); - beginRemoveRows({}, idx, idx ); - m_items.erase( it ); - endRemoveRows(); - emit countChanged(); + auto idx = std::distance( begin( model->m_items ), it ); + model->beginRemoveRows({}, idx, idx ); + model->m_items.erase( it ); + model->endRemoveRows(); + model->emit countChanged(); } }, Qt::QueuedConnection); } -void NetworkMediaModel::onItemPreparseEnded(MediaSourcePtr, input_item_node_t* node, enum input_item_preparse_status ) +void NetworkMediaModel::ListenerCb::onItemPreparseEnded(MediaSourcePtr, input_item_node_t* node, enum input_item_preparse_status ) { - m_preparseSem.release(); + model->m_preparseSem.release(); InputItemPtr p_node { node->p_item }; - QMetaObject::invokeMethod(this, [this, p_node=std::move(p_node)]() { - if (p_node != m_treeItem.media) + QMetaObject::invokeMethod(model, [model=model, p_node=std::move(p_node)]() { + if (p_node != model->m_treeItem.media) return; - m_parsingPending = false; - emit parsingPendingChanged(false); + model->m_parsingPending = false; + model->emit parsingPendingChanged(false); }); } diff --git a/modules/gui/qt/network/networkmediamodel.hpp b/modules/gui/qt/network/networkmediamodel.hpp index 62d3d272508080d95db97a712b6e48fd5f9d24ca..d7482e4bc18fec2d5a0a5dc3202cfb317d959190 100644 --- a/modules/gui/qt/network/networkmediamodel.hpp +++ b/modules/gui/qt/network/networkmediamodel.hpp @@ -102,7 +102,7 @@ private: Q_DECLARE_METATYPE(PathNode) -class NetworkMediaModel : public QAbstractListModel, public NetworkSourceListener::SourceListenerCb +class NetworkMediaModel : public QAbstractListModel { Q_OBJECT @@ -217,16 +217,23 @@ private: }; bool initializeMediaSources(); - void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override; - void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override; - void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override; - void onItemPreparseEnded( MediaSourcePtr mediaSource, input_item_node_t* node, enum input_item_preparse_status status ) override; void refreshMediaList(MediaSourcePtr s, std::vector<InputItemPtr> children , bool clear); bool canBeIndexed(const QUrl& url , ItemType itemType ); private: + struct ListenerCb : public NetworkSourceListener::SourceListenerCb { + ListenerCb(NetworkMediaModel *model) : model(model) {} + + void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) override; + void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) override; + void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t * node, input_item_node_t *const children[], size_t count ) override; + void onItemPreparseEnded( MediaSourcePtr mediaSource, input_item_node_t* node, enum input_item_preparse_status status ) override; + + NetworkMediaModel *model; + }; + //properties of the current node QString m_name; QUrl m_url; diff --git a/modules/gui/qt/network/networksourcelistener.cpp b/modules/gui/qt/network/networksourcelistener.cpp index bc0d2b5633fd415efb1eb4960b05cc6140efd8d4..81e98a538fd0ef59759af8f39725f4fa08129686 100644 --- a/modules/gui/qt/network/networksourcelistener.cpp +++ b/modules/gui/qt/network/networksourcelistener.cpp @@ -32,12 +32,12 @@ static void onItemPreparseEnded(vlc_media_tree_t *, input_item_node_t * node, self->cb->onItemPreparseEnded( self->source, node, status ); } -NetworkSourceListener::NetworkSourceListener(MediaSourcePtr s, SourceListenerCb* m) +NetworkSourceListener::NetworkSourceListener(MediaSourcePtr s, std::unique_ptr<SourceListenerCb> &&cb) : source( s ) , listener( nullptr, [s]( vlc_media_tree_listener_id* l ) { vlc_media_tree_RemoveListener( s->tree, l ); } ) - , cb( m ) + , cb( std::move( cb ) ) { static const vlc_media_tree_callbacks cbs { &onItemCleared, diff --git a/modules/gui/qt/network/networksourcelistener.hpp b/modules/gui/qt/network/networksourcelistener.hpp index 0b49b37c72a9b24261f1b30ae7098d674f1e01cf..24ea329849f6c38f765e748b000e37e807b23c58 100644 --- a/modules/gui/qt/network/networksourcelistener.hpp +++ b/modules/gui/qt/network/networksourcelistener.hpp @@ -43,6 +43,7 @@ public: class SourceListenerCb { public: + virtual ~SourceListenerCb() = default; virtual void onItemCleared( MediaSourcePtr mediaSource, input_item_node_t* node ) = 0; virtual void onItemAdded( MediaSourcePtr mediaSource, input_item_node_t* parent, input_item_node_t *const children[], size_t count ) = 0; virtual void onItemRemoved( MediaSourcePtr mediaSource, input_item_node_t* node, input_item_node_t *const children[], size_t count ) = 0; @@ -50,7 +51,7 @@ public: }; public: - NetworkSourceListener( MediaSourcePtr s, SourceListenerCb* m ); + NetworkSourceListener( MediaSourcePtr s, std::unique_ptr<SourceListenerCb> &&cb ); NetworkSourceListener( NetworkSourceListener&& ) = default; NetworkSourceListener& operator=( NetworkSourceListener&& ) = default; @@ -60,6 +61,6 @@ public: MediaSourcePtr source; ListenerPtr listener = nullptr; - SourceListenerCb *cb = nullptr; + std::unique_ptr<SourceListenerCb> cb; }; #endif // MLNETWORKSOURCELISTENER_HPP