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

Add a Thumbnail class

parent 07c87aeb
......@@ -57,6 +57,7 @@ libmedialibrary_la_SOURCES = \
src/Playlist.cpp \
src/Settings.cpp \
src/Show.cpp \
src/Thumbnail.cpp \
src/ShowEpisode.cpp \
src/VideoTrack.cpp \
src/database/SqliteConnection.cpp \
......@@ -169,6 +170,7 @@ noinst_HEADERS = \
src/Settings.h \
src/ShowEpisode.h \
src/Show.h \
src/Thumbnail.h \
src/utils/Cache.h \
src/utils/Directory.h \
src/utils/Filename.h \
......@@ -298,6 +300,7 @@ unittest_SOURCES = \
test/unittest/Tests.cpp \
test/unittest/VideoTrackTests.cpp \
test/unittest/MiscTests.cpp \
test/unittest/ThumbnailTests.cpp \
$(NULL)
EXTRA_DIST += test/unittest/db_v3.sql
......
......@@ -51,6 +51,7 @@
#include "Playlist.h"
#include "Show.h"
#include "ShowEpisode.h"
#include "Thumbnail.h"
#include "database/SqliteTools.h"
#include "database/SqliteConnection.h"
#include "parser/Task.h"
......@@ -131,6 +132,7 @@ void MediaLibrary::clearCache()
Playlist::clear();
History::clear();
Genre::clear();
Thumbnail::clear();
}
void MediaLibrary::createAllTables()
......@@ -159,6 +161,7 @@ void MediaLibrary::createAllTables()
History::createTable( m_dbConnection.get() );
Settings::createTable( m_dbConnection.get() );
parser::Task::createTable( m_dbConnection.get() );
Thumbnail::createTable( m_dbConnection.get() );
}
void MediaLibrary::createAllTriggers()
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "Thumbnail.h"
namespace medialibrary
{
namespace policy
{
const std::string ThumbnailTable::Name = "Thumbnail";
const std::string ThumbnailTable::PrimaryKeyColumn = "id_thumbnail";
int64_t Thumbnail::*const ThumbnailTable::PrimaryKey = &Thumbnail::m_id;
}
Thumbnail::Thumbnail(MediaLibraryPtr ml, sqlite::Row& row)
{
row >> m_id
>> m_mrl
>> m_origin;
}
Thumbnail::Thumbnail( MediaLibraryPtr ml, std::string mrl, Thumbnail::Origin origin )
: m_ml( ml )
, m_id( 0 )
, m_mrl( std::move( mrl ) )
, m_origin( origin )
{
}
int64_t Thumbnail::id() const
{
return m_id;
}
const std::string& Thumbnail::mrl() const
{
return m_mrl;
}
bool Thumbnail::setMrl( std::string mrl )
{
if ( m_mrl == mrl )
return true;
static const std::string req = "UPDATE " + policy::ThumbnailTable::Name +
" SET mrl = ? WHERE id_thumbnail = ?";
if( sqlite::Tools::executeUpdate( m_ml->getConn(), req, mrl, m_id ) == false )
return false;
m_mrl = std::move( mrl );
return true;
}
Thumbnail::Origin Thumbnail::origin() const
{
return m_origin;
}
void Thumbnail::createTable( sqlite::Connection* dbConnection )
{
const std::string req = "CREATE TABLE IF NOT EXISTS " + policy::ThumbnailTable::Name +
"("
"id_thumbnail INTEGER PRIMARY KEY AUTOINCREMENT,"
"mrl TEXT NOT NULL,"
"origin INTEGER NOT NULL"
")";
sqlite::Tools::executeRequest( dbConnection, req );
}
std::shared_ptr<Thumbnail> Thumbnail::create( MediaLibraryPtr ml, std::string mrl,
Thumbnail::Origin origin )
{
static const std::string req = "INSERT INTO " + policy::ThumbnailTable::Name +
"(mrl, origin) VALUES(?,?)";
auto self = std::make_shared<Thumbnail>( ml, std::move( mrl ), origin );
if ( insert( ml, self, req, self->mrl(), origin ) == false )
return nullptr;
return self;
}
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#pragma once
#include "database/DatabaseHelpers.h"
namespace medialibrary
{
class Thumbnail;
namespace policy
{
struct ThumbnailTable
{
static const std::string Name;
static const std::string PrimaryKeyColumn;
static int64_t Thumbnail::*const PrimaryKey;
};
}
class Thumbnail : public DatabaseHelpers<Thumbnail, policy::ThumbnailTable>
{
public:
enum class Origin : uint8_t
{
Artist,
AlbumArtist,
Album,
Media,
UserProvided,
};
Thumbnail( MediaLibraryPtr ml, sqlite::Row& row );
Thumbnail( MediaLibraryPtr ml, std::string mrl, Origin origin );
int64_t id() const;
const std::string& mrl() const;
bool setMrl( std::string mrl );
Origin origin() const;
static void createTable( sqlite::Connection* dbConnection );
static std::shared_ptr<Thumbnail> create( MediaLibraryPtr ml, std::string mrl,
Origin origin );
private:
MediaLibraryPtr m_ml;
int64_t m_id;
std::string m_mrl;
Origin m_origin;
friend policy::ThumbnailTable;
};
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015-2018 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "Tests.h"
#include "Thumbnail.h"
#include "Media.h"
class Thumbnails : public Tests
{
};
TEST_F( Thumbnails, Create )
{
std::string mrl = "/path/to/thumbnail.png";
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided );
ASSERT_NE( t, nullptr );
ASSERT_EQ( t->mrl(), mrl );
ASSERT_EQ( t->origin(), Thumbnail::Origin::UserProvided );
}
TEST_F( Thumbnails, MediaSetThumbnail )
{
std::string mrl = "/path/to/thumbnail.png";
auto m = ml->addFile( "/path/to/media.mp3" );
ASSERT_FALSE( m->isThumbnailGenerated() );
auto res = m->setThumbnail( mrl );
ASSERT_TRUE( res );
ASSERT_TRUE( m->isThumbnailGenerated() );
ASSERT_EQ( m->thumbnail(), mrl );
Reload();
m = ml->media( m->id() );
ASSERT_EQ( m->thumbnail(), mrl );
ASSERT_TRUE( m->isThumbnailGenerated() );
}
TEST_F( Thumbnails, SetMrl )
{
std::string mrl = "/path/to/thumbnail.png";
auto t = Thumbnail::create( ml.get(), mrl, Thumbnail::Origin::UserProvided );
ASSERT_EQ( t->mrl(), mrl );
mrl = "/better/thumbnail.gif";
auto res = t->setMrl( mrl );
ASSERT_TRUE( res );
ASSERT_EQ( t->mrl(), mrl );
Reload();
t = Thumbnail::fetch( ml.get(), t->id() );
ASSERT_EQ( t->mrl(), mrl );
}
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