Commit 57ea9939 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IMediaLibrary: Allow history to be cleared

parent b4169b56
......@@ -192,6 +192,15 @@ class IMediaLibrary
virtual bool addToHistory( const std::string& mrl ) = 0;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const = 0;
virtual std::vector<MediaPtr> lastMediaPlayed() const = 0;
/**
* @brief clearHistory will clear both streams history & media history.
* @return true in case of success, false otherwise. The database will stay untouched in case
* of failure.
*
* This will flush the entity cache, but will not edit any existing instance of a media entity,
* meaning any instance of media you're holding will outdated fields.
*/
virtual bool clearHistory() = 0;
/**
* Search
......
......@@ -88,6 +88,18 @@ std::vector<HistoryPtr> History::fetch( MediaLibraryPtr ml )
return fetchAll<IHistoryEntry>( ml, req );
}
bool History::clearStreams( MediaLibraryPtr ml )
{
static const std::string req = "DROP TABLE " + policy::HistoryTable::Name;
auto dbConn = ml->getConn();
if ( sqlite::Tools::executeRequest( dbConn, req ) == false )
return false;
DatabaseHelpers<History, policy::HistoryTable>::clear();
if ( createTable( dbConn ) == false )
return false;
return true;
}
const std::string& History::mrl() const
{
return m_mrl;
......
......@@ -52,6 +52,7 @@ public:
static bool createTable( DBConnection dbConnection );
static bool insert( DBConnection dbConn, const std::string& mrl );
static std::vector<HistoryPtr> fetch( MediaLibraryPtr ml );
static bool clearStreams( MediaLibraryPtr ml );
virtual const std::string& mrl() const override;
virtual unsigned int insertionDate() const override;
......
......@@ -549,4 +549,16 @@ std::vector<MediaPtr> Media::fetchHistory( MediaLibraryPtr ml )
return fetchAll<IMedia>( ml, req );
}
bool Media::clearHistory(MediaLibraryPtr ml)
{
auto dbConn = ml->getConn();
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"play_count = 0,"
"last_played_date = NULL,"
"progress = 0";
// Clear the entire cache since quite a few items are now containing invalid info.
clear();
return sqlite::Tools::executeUpdate( dbConn, req );
}
}
......@@ -110,6 +110,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
static std::vector<MediaPtr> listAll(MediaLibraryPtr ml, Type type , SortingCriteria sort, bool desc);
static std::vector<MediaPtr> search( MediaLibraryPtr ml, const std::string& title );
static std::vector<MediaPtr> fetchHistory( MediaLibraryPtr ml );
static bool clearHistory( MediaLibraryPtr ml );
private:
......
......@@ -449,6 +449,17 @@ std::vector<MediaPtr> MediaLibrary::lastMediaPlayed() const
return Media::fetchHistory( this );
}
bool MediaLibrary::clearHistory()
{
auto t = getConn()->newTransaction();
if ( Media::clearHistory( this ) == false )
return false;
if ( History::clearStreams( this ) == false )
return false;
t->commit();
return true;
}
MediaSearchAggregate MediaLibrary::searchMedia( const std::string& title ) const
{
if ( validateSearchPattern( title ) == false )
......
......@@ -103,6 +103,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual bool addToHistory( const std::string& mrl ) override;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const override;
virtual std::vector<MediaPtr> lastMediaPlayed() const override;
virtual bool clearHistory() override;
virtual MediaSearchAggregate searchMedia( const std::string& title ) const override;
virtual std::vector<PlaylistPtr> searchPlaylists( const std::string& name ) const override;
......
......@@ -109,3 +109,20 @@ TEST_F( HistoryTest, ReinsertFavorited )
ASSERT_NE( date, item->insertionDate() );
ASSERT_TRUE( item->isFavorite() );
}
TEST_F( HistoryTest, ClearStreamHistory )
{
ml->addToHistory( "f00" );
ml->addToHistory( "bar" );
auto history = ml->lastStreamsPlayed();
ASSERT_EQ( 2u, history.size() );
ml->clearHistory();
history = ml->lastStreamsPlayed();
ASSERT_EQ( 0u, history.size() );
Reload();
history = ml->lastStreamsPlayed();
ASSERT_EQ( 0u, history.size() );
}
......@@ -303,6 +303,29 @@ TEST_F( Medias, History )
ASSERT_EQ( m->id(), history[1]->id() );
}
TEST_F( Medias, ClearHistory )
{
auto m = ml->addFile( "media.mkv" );
auto history = ml->lastMediaPlayed();
ASSERT_EQ( 0u, history.size() );
m->increasePlayCount();
m->save();
history = ml->lastMediaPlayed();
ASSERT_EQ( 1u, history.size() );
ASSERT_TRUE( ml->clearHistory() );
history = ml->lastMediaPlayed();
ASSERT_EQ( 0u, history.size() );
Reload();
history = ml->lastMediaPlayed();
ASSERT_EQ( 0u, history.size() );
}
TEST_F( Medias, SetReleaseDate )
{
auto m = ml->addFile( "movie.mkv" );
......
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