MediaLibrary.cpp 1.9 KB
Newer Older
1
#include "MediaLibrary.h"
2 3 4 5 6 7 8
#include "SqliteTools.h"
#include "File.h"
#include "Label.h"
#include "Album.h"
#include "AlbumTrack.h"
#include "Show.h"
#include "ShowEpisode.h"
9 10

MediaLibrary::MediaLibrary()
11
    : m_dbConnection( NULL )
12 13 14
{
}

15 16 17 18 19 20 21 22 23 24
MediaLibrary::~MediaLibrary()
{
    File::clear();
    Label::clear();
    Album::clear();
    AlbumTrack::clear();
    Show::clear();
    ShowEpisode::clear();
}

25 26 27
bool MediaLibrary::initialize(const std::string& dbPath)
{
    int res = sqlite3_open( dbPath.c_str(), &m_dbConnection );
28 29
    if ( res != SQLITE_OK )
        return false;
30
    if ( SqliteTools::executeRequest( m_dbConnection, "PRAGMA foreign_keys = ON" ) == false )
31 32 33 34 35 36 37
        return false;
    return ( File::createTable( m_dbConnection ) &&
        Label::createTable( m_dbConnection ) &&
        Album::createTable( m_dbConnection ) &&
        AlbumTrack::createTable( m_dbConnection ) &&
        Show::createTable( m_dbConnection ) &&
        ShowEpisode::createTable( m_dbConnection ) );
38 39 40
}


Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
41
bool MediaLibrary::files( std::vector<FilePtr>& res )
42
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
    return File::fetchAll( m_dbConnection, res );
44
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
45

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46
FilePtr MediaLibrary::file( const std::string& path )
47
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
48
    return File::fetch( m_dbConnection, path );
49 50
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
51
FilePtr MediaLibrary::addFile( const std::string& path )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52
{
53
    return File::create( m_dbConnection, path );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54
}
55

56 57 58 59 60 61 62 63 64 65
bool MediaLibrary::deleteFile( const std::string& mrl )
{
    return File::destroy( m_dbConnection, mrl );
}

bool MediaLibrary::deleteFile( FilePtr file )
{
    return File::destroy( m_dbConnection, std::static_pointer_cast<File>( file ) );
}

66 67
LabelPtr MediaLibrary::createLabel( const std::string& label )
{
68
    return Label::create( m_dbConnection, label );
69
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70 71 72

bool MediaLibrary::deleteLabel( const std::string& text )
{
73
    return Label::destroy( m_dbConnection, text );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
74 75 76 77 78 79
}

bool MediaLibrary::deleteLabel( LabelPtr label )
{
    return Label::destroy( m_dbConnection, std::static_pointer_cast<Label>( label ) );
}