Commit 9220a4df authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Hugo Beauzée-Luyssen
Browse files

Add a title to History items


Signed-off-by: Hugo Beauzée-Luyssen's avatarHugo Beauzée-Luyssen <hugo@beauzee.fr>
parent e87f3576
......@@ -32,6 +32,7 @@ class IHistoryEntry
public:
virtual ~IHistoryEntry() = default;
virtual const std::string& mrl() const = 0;
virtual const std::string& title() const = 0;
virtual unsigned int insertionDate() const = 0;
virtual bool isFavorite() const = 0;
virtual bool setFavorite( bool isFavorite ) = 0;
......
......@@ -194,7 +194,7 @@ class IMediaLibrary
/**
* History
*/
virtual bool addToHistory( const std::string& mrl ) = 0;
virtual bool addToHistory( const std::string& mrl, const std::string& title ) = 0;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const = 0;
virtual std::vector<MediaPtr> lastMediaPlayed() const = 0;
/**
......
......@@ -45,6 +45,7 @@ History::History( MediaLibraryPtr ml, sqlite::Row& row )
{
row >> m_id
>> m_mrl
>> m_title
>> m_date
>> m_favorite;
}
......@@ -55,6 +56,7 @@ bool History::createTable( DBConnection dbConnection )
"("
"id_record INTEGER PRIMARY KEY AUTOINCREMENT,"
"mrl TEXT UNIQUE ON CONFLICT FAIL,"
"title TEXT,"
"insertion_date UNSIGNED INT NOT NULL DEFAULT (strftime('%s', 'now')),"
"favorite BOOLEAN NOT NULL DEFAULT 0"
")";
......@@ -69,17 +71,17 @@ bool History::createTable( DBConnection dbConnection )
sqlite::Tools::executeRequest( dbConnection, triggerReq );
}
bool History::insert( DBConnection dbConn, const std::string& mrl )
bool History::insert( DBConnection dbConn, const std::string& mrl, const std::string& title )
{
History::clear();
static const std::string req = "INSERT OR REPLACE INTO " + policy::HistoryTable::Name +
"(id_record, mrl, insertion_date, favorite)"
" SELECT id_record, mrl, strftime('%s', 'now'), favorite FROM " +
"(id_record, mrl, title, insertion_date, favorite)"
" SELECT id_record, mrl, ?, strftime('%s', 'now'), favorite FROM " +
policy::HistoryTable::Name + " WHERE mrl = ?"
" UNION SELECT NULL, ?, NULL, NULL"
" UNION SELECT NULL, ?, ?, NULL, NULL"
" ORDER BY id_record DESC"
" LIMIT 1";
return sqlite::Tools::executeInsert( dbConn, req, mrl, mrl ) != 0;
return sqlite::Tools::executeInsert( dbConn, req, title, mrl, mrl, title ) != 0;
}
std::vector<HistoryPtr> History::fetch( MediaLibraryPtr ml )
......@@ -105,6 +107,11 @@ const std::string& History::mrl() const
return m_mrl;
}
const std::string& History::title() const
{
return m_title;
}
unsigned int History::insertionDate() const
{
return m_date;
......
......@@ -50,11 +50,12 @@ class History : public IHistoryEntry, public DatabaseHelpers<History, policy::Hi
public:
History( MediaLibraryPtr ml, sqlite::Row& row );
static bool createTable( DBConnection dbConnection );
static bool insert( DBConnection dbConn, const std::string& mrl );
static bool insert( DBConnection dbConn, const std::string& mrl, const std::string& title );
static std::vector<HistoryPtr> fetch( MediaLibraryPtr ml );
static bool clearStreams( MediaLibraryPtr ml );
virtual const std::string& mrl() const override;
virtual const std::string& title() const override;
virtual unsigned int insertionDate() const override;
virtual bool isFavorite() const override;
virtual bool setFavorite( bool isFavorite ) override;
......@@ -66,6 +67,7 @@ private:
int64_t m_id;
std::string m_mrl;
std::string m_title;
unsigned int m_date;
bool m_favorite;
......
......@@ -480,9 +480,9 @@ bool MediaLibrary::deletePlaylist( int64_t playlistId )
return Playlist::destroy( this, playlistId );
}
bool MediaLibrary::addToHistory( const std::string& mrl )
bool MediaLibrary::addToHistory( const std::string& mrl, const std::string& title )
{
return History::insert( getConn(), mrl );
return History::insert( getConn(), mrl, title );
}
std::vector<HistoryPtr> MediaLibrary::lastStreamsPlayed() const
......
......@@ -101,7 +101,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual PlaylistPtr playlist( int64_t id ) const override;
virtual bool deletePlaylist( int64_t playlistId ) override;
virtual bool addToHistory( const std::string& mrl ) override;
virtual bool addToHistory( const std::string& mrl, const std::string& title ) override;
virtual std::vector<HistoryPtr> lastStreamsPlayed() const override;
virtual std::vector<MediaPtr> lastMediaPlayed() const override;
virtual bool clearHistory() override;
......
......@@ -39,32 +39,44 @@ class HistoryTest : public Tests
TEST_F( HistoryTest, InsertMrl )
{
ml->addToHistory( "upnp://stream" );
ml->addToHistory( "upnp://stream", "title" );
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( 1u, hList.size() );
auto h = hList[0];
ASSERT_EQ( h->mrl(), "upnp://stream" );
ASSERT_EQ( h->title(), "title" );
ASSERT_NE( 0u, h->insertionDate() );
}
TEST_F( HistoryTest, updateTitle )
{
ml->addToHistory( "upnp://stream", "title" );
ml->addToHistory( "upnp://stream", "title2" );
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( 1u, hList.size() );
auto h = hList[0];
ASSERT_EQ( h->mrl(), "upnp://stream" );
ASSERT_EQ( h->title(), "title2" );
}
TEST_F( HistoryTest, MaxEntries )
{
for ( auto i = 0u; i < History::MaxEntries; ++i )
{
ml->addToHistory( std::to_string( i ) );
ml->addToHistory( std::to_string( i ), std::to_string( i ) );
}
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( History::MaxEntries, hList.size() );
ml->addToHistory( "new-media" );
ml->addToHistory( "new-media", "title" );
hList = ml->lastStreamsPlayed();
ASSERT_EQ( History::MaxEntries, hList.size() );
}
TEST_F( HistoryTest, Ordering )
{
ml->addToHistory( "first-stream" );
ml->addToHistory( "first-stream", "title 1" );
compat::this_thread::sleep_for( std::chrono::seconds( 1 ) );
ml->addToHistory( "second-stream" );
ml->addToHistory( "second-stream", "title 2" );
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( 2u, hList.size() );
ASSERT_EQ( hList[0]->mrl(), "second-stream" );
......@@ -73,12 +85,12 @@ TEST_F( HistoryTest, Ordering )
TEST_F( HistoryTest, UpdateInsertionDate )
{
ml->addToHistory( "stream" );
ml->addToHistory( "stream", "title" );
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( 1u, hList.size() );
auto date = hList[0]->insertionDate();
compat::this_thread::sleep_for( std::chrono::seconds( 1 ) );
ml->addToHistory( "stream" );
ml->addToHistory( "stream", "title" );
hList = ml->lastStreamsPlayed();
ASSERT_EQ( 1u, hList.size() );
ASSERT_NE( date, hList[0]->insertionDate() );
......@@ -86,7 +98,7 @@ TEST_F( HistoryTest, UpdateInsertionDate )
TEST_F( HistoryTest, FavoriteMrl )
{
ml->addToHistory( "stream" );
ml->addToHistory( "stream", "title" );
auto hList = ml->lastStreamsPlayed();
ASSERT_EQ( 1u, hList.size() );
auto item = hList[0];
......@@ -97,13 +109,13 @@ TEST_F( HistoryTest, FavoriteMrl )
TEST_F( HistoryTest, ReinsertFavorited )
{
ml->addToHistory( "stream" );
ml->addToHistory( "stream", "title" );
auto hList = ml->lastStreamsPlayed();
auto item = hList[0];
auto date = item->insertionDate();
item->setFavorite( true );
compat::this_thread::sleep_for( std::chrono::seconds{ 1 } );
ml->addToHistory( "stream" );
ml->addToHistory( "stream", "title" );
hList = ml->lastStreamsPlayed();
item = hList[0];
ASSERT_NE( date, item->insertionDate() );
......@@ -112,8 +124,8 @@ TEST_F( HistoryTest, ReinsertFavorited )
TEST_F( HistoryTest, ClearStreamHistory )
{
ml->addToHistory( "f00" );
ml->addToHistory( "bar" );
ml->addToHistory( "f00", "f00" );
ml->addToHistory( "bar", "bar" );
auto history = ml->lastStreamsPlayed();
ASSERT_EQ( 2u, history.size() );
......
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