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

Add a mountpoint class

parent 712f1807
......@@ -66,6 +66,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
Parser.cpp
Artist.cpp
Settings.cpp
Mountpoint.cpp
factory/FileSystem.cpp
filesystem/common/CommonFile.cpp
......
......@@ -31,6 +31,7 @@
#include "AudioTrack.h"
#include "discoverer/DiscovererWorker.h"
#include "Media.h"
#include "Mountpoint.h"
#include "Folder.h"
#include "MediaLibrary.h"
#include "IMetadataService.h"
......@@ -98,6 +99,7 @@ MediaLibrary::~MediaLibrary()
VideoTrack::clear();
AudioTrack::clear();
Artist::clear();
Mountpoint::clear();
// Explicitely release the connection's TLS
if ( m_dbConnection != nullptr )
m_dbConnection->release();
......@@ -153,7 +155,8 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
AudioTrack::createTable( m_dbConnection.get() ) &&
Artist::createTable( m_dbConnection.get() ) &&
Artist::createDefaultArtists( m_dbConnection.get() ) &&
Settings::createTable( m_dbConnection.get() ) ) == false )
Settings::createTable( m_dbConnection.get() ) &&
Mountpoint::createTable( m_dbConnection.get() ) ) == false )
{
LOG_ERROR( "Failed to create database structure" );
return false;
......@@ -264,6 +267,16 @@ bool MediaLibrary::deleteFolder( FolderPtr folder )
return true;
}
std::shared_ptr<Mountpoint> MediaLibrary::mountpoint( const std::string& uuid )
{
return Mountpoint::fromUuid( m_dbConnection.get(), uuid );
}
std::shared_ptr<Mountpoint> MediaLibrary::addMountpoint( const std::string& uuid, bool isRemovable )
{
return Mountpoint::create( m_dbConnection.get(), uuid, isRemovable );
}
LabelPtr MediaLibrary::createLabel( const std::string& label )
{
return Label::create( m_dbConnection.get(), label );
......
......@@ -38,6 +38,7 @@ class Artist;
class Media;
class Movie;
class Show;
class Mountpoint;
class MediaLibrary : public IMediaLibrary
{
......@@ -57,6 +58,8 @@ class MediaLibrary : public IMediaLibrary
virtual FolderPtr folder( const std::string& path ) override;
bool deleteFolder( FolderPtr folder );
std::shared_ptr<Mountpoint> mountpoint( const std::string& uuid );
std::shared_ptr<Mountpoint> addMountpoint( const std::string& uuid, bool isRemovable );
virtual LabelPtr createLabel( const std::string& label ) override;
virtual bool deleteLabel( LabelPtr label ) override;
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* 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.
*****************************************************************************/
#include "Mountpoint.h"
const std::string policy::MountpointTable::Name = "Mountpoint";
const std::string policy::MountpointTable::PrimaryKeyColumn = "id_mountpoint";
unsigned int Mountpoint::* const policy::MountpointTable::PrimaryKey = &Mountpoint::m_id;
Mountpoint::Mountpoint( DBConnection dbConnection, sqlite::Row& row )
: m_dbConn( dbConnection )
{
row >> m_id
>> m_uuid
>> m_isRemovable
>> m_isPresent;
//FIXME: It's probably a bad idea to load "isPresent" for DB. This field should
//only be here for sqlite triggering purposes
}
Mountpoint::Mountpoint( const std::string& uuid, bool isRemovable )
: m_uuid( uuid )
, m_isRemovable( isRemovable )
// Assume we can't add an unmounted/absent mountpoint
, m_isPresent( true )
{
}
unsigned int Mountpoint::id() const
{
return m_id;
}
const std::string&Mountpoint::uuid() const
{
return m_uuid;
}
bool Mountpoint::isRemovable() const
{
return m_isRemovable;
}
bool Mountpoint::isPresent() const
{
return m_isPresent;
}
void Mountpoint::setPresent(bool value)
{
static const std::string req = "UPDATE " + policy::MountpointTable::Name +
" SET is_present = ?";
if ( sqlite::Tools::executeUpdate( m_dbConn, req, value ) == false )
return;
m_isPresent = value;
}
std::shared_ptr<Mountpoint> Mountpoint::create(DBConnection dbConnection, const std::string& uuid, bool isRemovable )
{
static const std::string req = "INSERT INTO " + policy::MountpointTable::Name
+ "(uuid, is_removable, is_present) VALUES(?, ?, ?)";
auto self = std::make_shared<Mountpoint>( uuid, isRemovable );
if ( insert( dbConnection, self, req, uuid, isRemovable, self->isPresent() ) == false )
return nullptr;
self->m_dbConn = dbConnection;
return self;
}
bool Mountpoint::createTable(DBConnection connection)
{
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MountpointTable::Name + "("
"id_mountpoint INTEGER PRIMARY KEY AUTOINCREMENT,"
"uuid TEXT UNIQUE ON CONFLICT FAIL,"
"is_removable BOOLEAN,"
"is_present BOOLEAN"
")";
return sqlite::Tools::executeRequest( connection, req );
}
std::shared_ptr<Mountpoint> Mountpoint::fromUuid( DBConnection dbConnection, const std::string& uuid )
{
static const std::string req = "SELECT * FROM " + policy::MountpointTable::Name +
" WHERE uuid = ?";
return fetch( dbConnection, req, uuid );
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* 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 "Types.h"
#include "database/DatabaseHelpers.h"
class Mountpoint;
namespace policy
{
struct MountpointTable
{
static const std::string Name;
static const std::string PrimaryKeyColumn;
static unsigned int Mountpoint::*const PrimaryKey;
};
}
class Mountpoint : public DatabaseHelpers<Mountpoint, policy::MountpointTable>
{
public:
Mountpoint( const std::string& uuid, bool isRemovable );
Mountpoint( DBConnection dbConnection, sqlite::Row& row );
unsigned int id() const;
const std::string& uuid() const;
bool isRemovable() const;
bool isPresent() const;
void setPresent( bool value );
static std::shared_ptr<Mountpoint> create( DBConnection dbConnection, const std::string& uuid, bool isRemovable );
static bool createTable( DBConnection connection );
static std::shared_ptr<Mountpoint> fromUuid( DBConnection dbConnection, const std::string& uuid );
private:
DBConnection m_dbConn;
// This is a database ID
unsigned int m_id;
// This is a unique ID on the system side, in the /dev/disk/by-uuid sense.
// It can be a name or what not, depending on the OS.
std::string m_uuid;
bool m_isRemovable;
bool m_isPresent;
friend struct policy::MountpointTable;
};
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* 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.
*****************************************************************************/
#include "Tests.h"
#include "Mountpoint.h"
class Mountpoints : public Tests
{
};
TEST_F( Mountpoints, Create )
{
auto m = ml->addMountpoint( "dummy", true );
ASSERT_NE( nullptr, m );
ASSERT_EQ( "dummy", m->uuid() );
ASSERT_TRUE( m->isRemovable() );
ASSERT_TRUE( m->isPresent() );
Reload();
m = ml->mountpoint( "dummy" );
ASSERT_NE( nullptr, m );
ASSERT_EQ( "dummy", m->uuid() );
ASSERT_TRUE( m->isRemovable() );
ASSERT_TRUE( m->isPresent() );
}
TEST_F( Mountpoints, SetPresent )
{
auto m = ml->addMountpoint( "dummy", true );
ASSERT_NE( nullptr, m );
ASSERT_TRUE( m->isPresent() );
m->setPresent( false );
ASSERT_FALSE( m->isPresent() );
Reload();
m = ml->mountpoint( "dummy" );
ASSERT_FALSE( m->isPresent() );
}
......@@ -11,6 +11,7 @@ list(APPEND TEST_SRCS
unittest/FsUtilsTests.cpp
unittest/ArtistTests.cpp
unittest/AlbumTrackTests.cpp
unittest/MountpointTests.cpp
mocks/FileSystem.h
mocks/DiscovererCbMock.h
......
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