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

Media: Store progress & rating as metadata

parent d95255cc
......@@ -63,6 +63,10 @@ class IMedia
{
AspectRatio,
Speed,
// The progress within the file, the range of value is up to the application.
Progress,
// A rating for this media, values are up to the application
Rating,
};
virtual ~IMedia() = default;
......@@ -76,23 +80,10 @@ class IMedia
* @brief duration Returns the media duration in ms
*/
virtual int64_t duration() const = 0;
virtual ShowEpisodePtr showEpisode() const = 0;
virtual int playCount() const = 0;
virtual bool increasePlayCount() = 0;
virtual ShowEpisodePtr showEpisode() const = 0;
virtual const std::vector<FilePtr>& files() const = 0;
///
/// \brief progress Returns the progress, in the [0;1] range
///
virtual float progress() const = 0;
virtual bool setProgress( float progress ) = 0;
///
/// \brief rating The media rating, or -1 if unset.
/// It is up to the application to determine the values it wishes to use.
/// No value is enforced, and any positive value (less or equal to INT32_MAX)
/// will be accepted.
///
virtual int rating() const = 0;
virtual bool setRating( int rating ) = 0;
virtual bool isFavorite() const = 0;
virtual bool setFavorite( bool favorite ) = 0;
virtual bool addLabel( LabelPtr label ) = 0;
......
......@@ -67,8 +67,6 @@ Media::Media( MediaLibraryPtr ml, sqlite::Row& row )
>> m_duration
>> m_playCount
>> m_lastPlayedDate
>> m_progress
>> m_rating
>> m_insertionDate
>> m_releaseDate
>> m_thumbnail
......@@ -86,8 +84,6 @@ Media::Media( MediaLibraryPtr ml, const std::string& title, Type type )
, m_duration( -1 )
, m_playCount( 0 )
, m_lastPlayedDate( 0 )
, m_progress( .0f )
, m_rating( -1 )
, m_insertionDate( time( nullptr ) )
, m_releaseDate( 0 )
, m_title( title )
......@@ -186,38 +182,6 @@ bool Media::increasePlayCount()
return true;
}
float Media::progress() const
{
return m_progress;
}
bool Media::setProgress( float progress )
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET progress = ? WHERE id_media = ?";
if ( progress == m_progress || progress < 0 || progress > 1.0 )
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, progress, m_id ) == false )
return false;
m_progress = progress;
return true;
}
int Media::rating() const
{
return m_rating;
}
bool Media::setRating( int rating )
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET rating = ? WHERE id_media = ?";
if ( m_rating == rating )
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, rating, m_id ) == false )
return false;
m_rating = rating;
return true;
}
bool Media::isFavorite() const
{
return m_isFavorite;
......@@ -382,12 +346,12 @@ void Media::setThumbnail(const std::string& thumbnail )
bool Media::save()
{
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"type = ?, subtype = ?, duration = ?, progress = ?, release_date = ?,"
"type = ?, subtype = ?, duration = ?, release_date = ?,"
"thumbnail = ?, title = ? WHERE id_media = ?";
if ( m_changed == false )
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_type, m_subType, m_duration,
m_progress, m_releaseDate, m_thumbnail, m_title, m_id ) == false )
m_releaseDate, m_thumbnail, m_title, m_id ) == false )
{
return false;
}
......@@ -501,8 +465,6 @@ bool Media::createTable( DBConnection connection )
"duration INTEGER DEFAULT -1,"
"play_count UNSIGNED INTEGER,"
"last_played_date UNSIGNED INTEGER,"
"progress REAL,"
"rating INTEGER DEFAULT -1,"
"insertion_date UNSIGNED INTEGER,"
"release_date UNSIGNED INTEGER,"
"thumbnail TEXT,"
......@@ -619,13 +581,17 @@ std::vector<MediaPtr> Media::fetchHistory( MediaLibraryPtr ml )
void Media::clearHistory( MediaLibraryPtr ml )
{
auto dbConn = ml->getConn();
// There should already be an active transaction, from MediaLibrary::clearHistory
assert( sqlite::Transaction::transactionInProgress() == true );
static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
"play_count = 0,"
"last_played_date = NULL,"
"progress = 0";
"last_played_date = NULL";
static const std::string flushProgress = "DELETE FROM " + policy::MediaMetadataTable::Name +
" WHERE type = ?";
// Clear the entire cache since quite a few items are now containing invalid info.
clear();
sqlite::Tools::executeUpdate( dbConn, req );
sqlite::Tools::executeDelete( dbConn, flushProgress, IMedia::MetadataType::Progress );
}
bool Media::MediaMetadata::isSet() const
......
......@@ -103,10 +103,6 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
virtual std::vector<LabelPtr> labels() override;
virtual int playCount() const override;
virtual bool increasePlayCount() override;
virtual float progress() const override;
virtual bool setProgress( float progress ) override;
virtual int rating() const override;
virtual bool setRating( int rating ) override;
virtual bool isFavorite() const override;
virtual bool setFavorite( bool favorite ) override;
virtual const std::vector<FilePtr>& files() const override;
......@@ -149,8 +145,6 @@ private:
int64_t m_duration;
unsigned int m_playCount;
unsigned int m_lastPlayedDate;
float m_progress;
int m_rating;
unsigned int m_insertionDate;
unsigned int m_releaseDate;
std::string m_thumbnail;
......
......@@ -123,30 +123,27 @@ TEST_F( Medias, PlayCount )
TEST_F( Medias, Progress )
{
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( .0f, f->progress() );
f->setProgress( 123.0f );
// Check that a non-sensical value is ignored
ASSERT_EQ( .0f, f->progress() );
f->setProgress( 0.666f );
ASSERT_EQ( .666f, f->progress() );
ASSERT_EQ( 0, f->metadata( Media::MetadataType::Progress ).integer() );
f->setMetadata( Media::MetadataType::Progress, 123 );
ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
Reload();
f = ml->media( f->id() );
ASSERT_EQ( .666f, f->progress() );
ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
}
TEST_F( Medias, Rating )
{
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( -1, f->rating() );
f->setRating( 12345 );
ASSERT_EQ( 12345, f->rating() );
ASSERT_FALSE( f->metadata( Media::MetadataType::Rating ).isSet() );
f->setMetadata( Media::MetadataType::Rating, 12345 );
ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
Reload();
f = ml->media( f->id() );
ASSERT_EQ( 12345, f->rating() );
ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
}
TEST_F( Medias, Search )
......
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