Commit 5c24588c authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

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

parent 19c2e922
......@@ -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 ) );
}
......
......@@ -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();
}
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment