MediaLibrary.cpp 3.7 KB
Newer Older
1 2 3
#include <algorithm>
#include <functional>

4
#include "MediaLibrary.h"
5
#include "IMetadataService.h"
6 7 8 9 10
#include "SqliteTools.h"
#include "File.h"
#include "Label.h"
#include "Album.h"
#include "AlbumTrack.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
11
#include "Movie.h"
12 13
#include "Show.h"
#include "ShowEpisode.h"
14 15 16 17 18

MediaLibrary::MediaLibrary()
{
}

19 20 21 22 23 24 25 26
MediaLibrary::~MediaLibrary()
{
    File::clear();
    Label::clear();
    Album::clear();
    AlbumTrack::clear();
    Show::clear();
    ShowEpisode::clear();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27
    Movie::clear();
28 29
}

30 31
bool MediaLibrary::initialize(const std::string& dbPath)
{
32 33
    sqlite3* dbConnection;
    int res = sqlite3_open( dbPath.c_str(), &dbConnection );
34 35
    if ( res != SQLITE_OK )
        return false;
36 37
    m_dbConnection.reset( dbConnection, &sqlite3_close );
    if ( SqliteTools::executeRequest( DBConnection(m_dbConnection), "PRAGMA foreign_keys = ON" ) == false )
38 39 40 41 42 43
        return false;
    return ( File::createTable( m_dbConnection ) &&
        Label::createTable( m_dbConnection ) &&
        Album::createTable( m_dbConnection ) &&
        AlbumTrack::createTable( m_dbConnection ) &&
        Show::createTable( m_dbConnection ) &&
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44 45
        ShowEpisode::createTable( m_dbConnection ) &&
        Movie::createTable( m_dbConnection ) );
46 47 48
}


Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
49
bool MediaLibrary::files( std::vector<FilePtr>& res )
50
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
51
    return File::fetchAll( m_dbConnection, res );
52
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
54
FilePtr MediaLibrary::file( const std::string& path )
55
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
56
    return File::fetch( m_dbConnection, path );
57 58
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
FilePtr MediaLibrary::addFile( const std::string& path )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
{
61
    return File::create( m_dbConnection, path );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
62
}
63

64 65 66 67 68 69 70 71 72 73
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 ) );
}

74 75
LabelPtr MediaLibrary::createLabel( const std::string& label )
{
76
    return Label::create( m_dbConnection, label );
77
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
78 79 80

bool MediaLibrary::deleteLabel( const std::string& text )
{
81
    return Label::destroy( m_dbConnection, text );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
82 83 84 85 86 87
}

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

89 90 91 92 93
AlbumPtr MediaLibrary::album( const std::string& id3Tag )
{
    // We can't use Cache helper, since albums are cached by primary keys
    static const std::string req = "SELECT * FROM " + policy::AlbumTable::Name +
            " WHERE id3tag = ?";
94
    return SqliteTools::fetchOne<Album>( DBConnection( m_dbConnection ), req, id3Tag );
95 96 97 98 99 100 101
}

AlbumPtr MediaLibrary::createAlbum( const std::string& id3Tag )
{
    return Album::create( m_dbConnection, id3Tag );
}

102 103 104 105 106 107 108 109 110 111 112 113
ShowPtr MediaLibrary::show(const std::string& name)
{
    static const std::string req = "SELECT * FROM " + policy::ShowTable::Name
            + " WHERE name = ?";
    return SqliteTools::fetchOne<Show>( m_dbConnection, req, name );
}

ShowPtr MediaLibrary::createShow(const std::string& name)
{
    return Show::create( m_dbConnection, name );
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
114 115 116 117 118 119 120 121 122 123 124 125
MoviePtr MediaLibrary::movie( const std::string& title )
{
    static const std::string req = "SELECT * FROM " + policy::MovieTable::Name
            + " WHERE title = ?";
    return SqliteTools::fetchOne<Movie>( m_dbConnection, req, title );
}

MoviePtr MediaLibrary::createMovie( const std::string& title )
{
    return Movie::create( m_dbConnection, title );
}

126 127 128 129 130 131 132 133 134 135 136
void MediaLibrary::addMetadataService(IMetadataService* service)
{
    typedef std::unique_ptr<IMetadataService> MdsPtr;
    std::function<bool(const MdsPtr&, const MdsPtr&)> comp = []( const MdsPtr& a, const MdsPtr& b )
    {
        // We want higher priority first
        return a->priority() > b->priority();
    };
    m_mdServices.push_back( MdsPtr( service ) );
    std::push_heap( m_mdServices.begin(), m_mdServices.end(), comp );
}