Media.cpp 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * 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.
 *****************************************************************************/

23
#include <cassert>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
24 25
#include <cstdlib>
#include <cstring>
26
#include <ctime>
27

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28
#include "Album.h"
29
#include "AlbumTrack.h"
30
#include "Artist.h"
31
#include "AudioTrack.h"
32
#include "Media.h"
33
#include "Folder.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34
#include "Label.h"
35
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
#include "Movie.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
37
#include "ShowEpisode.h"
38
#include "database/SqliteTools.h"
39
#include "VideoTrack.h"
40
#include "filesystem/IFile.h"
41

42 43 44
const std::string policy::MediaTable::Name = "Media";
const std::string policy::MediaTable::CacheColumn = "mrl";
unsigned int Media::* const policy::MediaTable::PrimaryKey = &Media::m_id;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
45

46
Media::Media( DBConnection dbConnection, sqlite3_stmt* stmt )
47 48 49 50
    : m_dbConnection( dbConnection )
{
    m_id = sqlite3_column_int( stmt, 0 );
    m_type = (Type)sqlite3_column_int( stmt, 1 );
51
    m_duration = sqlite::Traits<int64_t>::Load( stmt, 2 );
52 53 54 55 56 57 58 59
    m_playCount = sqlite3_column_int( stmt, 3 );
    m_showEpisodeId = sqlite3_column_int( stmt, 4 );
    m_mrl = (const char*)sqlite3_column_text( stmt, 5 );
    m_movieId = sqlite::Traits<unsigned int>::Load( stmt, 6 );
    m_folderId = sqlite::Traits<unsigned int>::Load( stmt, 7 );
    m_lastModificationDate = sqlite::Traits<unsigned int>::Load( stmt, 8 );
    m_snapshot = sqlite::Traits<std::string>::Load( stmt, 9 );
    m_isParsed = sqlite::Traits<bool>::Load( stmt, 10 );
60
    m_title = sqlite::Traits<std::string>::Load( stmt, 11 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61 62
}

63
Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& title, Type type )
64
    : m_id( 0 )
65
    , m_type( type )
66
    , m_duration( -1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67 68
    , m_playCount( 0 )
    , m_showEpisodeId( 0 )
69
    , m_mrl( file->fullPath() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
70
    , m_movieId( 0 )
71
    , m_folderId( folderId )
72
    , m_lastModificationDate( file->lastModificationDate() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
73
    , m_isParsed( false )
74
    , m_title( title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
75
{
76 77
}

78
std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, const fs::IFile* file, unsigned int folderId )
79
{
80 81
    auto self = std::make_shared<Media>( file, folderId, file->name(), type );
    static const std::string req = "INSERT INTO " + policy::MediaTable::Name +
82
            "(type, mrl, folder_id, last_modification_date, title) VALUES(?, ?, ?, ?, ?)";
83

84 85
    using type_t = std::underlying_type<Type>::type;
    if ( _Cache::insert( dbConnection, self, req, static_cast<type_t>( type ), self->m_mrl, sqlite::ForeignKey( folderId ),
86
                         self->m_lastModificationDate, self->m_title) == false )
87
        return nullptr;
88 89
    self->m_dbConnection = dbConnection;
    return self;
90 91
}

92
AlbumTrackPtr Media::albumTrack()
93
{
94
    if ( m_albumTrack == nullptr )
95
    {
96 97 98
        std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
                " WHERE media_id = ?";
        m_albumTrack = AlbumTrack::fetchOne( m_dbConnection, req, m_id );
99 100
    }
    return m_albumTrack;
101 102
}

103
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
104 105 106 107 108
{
    m_albumTrack = albumTrack;
    return true;
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
bool Media::addArtist( ArtistPtr artist )
{
    static const std::string req = "INSERT INTO MediaArtistRelation VALUES(?, ?)";
    // If track's ID is 0, the request will fail due to table constraints
    sqlite::ForeignKey artistForeignKey( artist != nullptr ? artist->id() : 0 );
    return sqlite::Tools::executeRequest( m_dbConnection, req, m_id, artistForeignKey );
}

std::vector<ArtistPtr> Media::artists() const
{
    static const std::string req = "SELECT art.* FROM " + policy::ArtistTable::Name + " art "
            "LEFT JOIN MediaArtistRelation mar ON mar.id_artist = art.id_artist "
            "WHERE mar.id_media = ?";
    return Artist::fetchAll( m_dbConnection, req, m_id );
}

125
int64_t Media::duration() const
126 127 128 129
{
    return m_duration;
}

130
bool Media::setDuration( int64_t duration )
131
{
132 133
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET duration = ? "
            "WHERE id_media = ?";
134 135 136 137 138 139
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, duration, m_id ) == false )
        return false;
    m_duration = duration;
    return true;
}

140
std::shared_ptr<IShowEpisode> Media::showEpisode()
141
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
142
    if ( m_showEpisode == nullptr && m_showEpisodeId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
143
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
144
        m_showEpisode = ShowEpisode::fetch( m_dbConnection, m_showEpisodeId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
145 146
    }
    return m_showEpisode;
147 148
}

149
bool Media::setShowEpisode(ShowEpisodePtr showEpisode)
150
{
151 152
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET show_episode_id = ?  WHERE id_media = ?";
153
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, showEpisode->id(), m_id ) == false )
154 155 156 157 158 159
        return false;
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
    return true;
}

160
std::vector<std::shared_ptr<ILabel> > Media::labels()
161
{
162
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
163
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
164
            "WHERE lfr.id_media = ?";
165
    return Label::fetchAll( m_dbConnection, req, m_id );
166 167
}

168
int Media::playCount() const
169
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
170
    return m_playCount;
171 172
}

173
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
174 175 176 177
{
    return m_mrl;
}

178
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
179 180 181 182 183 184 185 186
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

187
bool Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
188
{
189 190
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET movie_id = ? WHERE id_media = ?";
191
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, movie->id(), m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
192 193 194 195 196 197
        return false;
    m_movie = movie;
    m_movieId = movie->id();
    return true;
}

198
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
199 200 201 202 203 204 205 206 207 208
{
    static const std::string req = "INSERT INTO VideoTrackFileRelation VALUES(?, ?)";

    auto track = VideoTrack::fetch( m_dbConnection, codec, width, height, fps );
    if ( track == nullptr )
    {
        track = VideoTrack::create( m_dbConnection, codec, width, height, fps );
        if ( track == nullptr )
            return false;
    }
209
    return sqlite::Tools::executeRequest( m_dbConnection, req, track->id(), m_id );
210 211
}

212
std::vector<VideoTrackPtr> Media::videoTracks()
213 214 215
{
    static const std::string req = "SELECT t.* FROM " + policy::VideoTrackTable::Name +
            " t LEFT JOIN VideoTrackFileRelation vtfr ON vtfr.id_track = t.id_track"
216
            " WHERE vtfr.id_media = ?";
217
    return VideoTrack::fetchAll( m_dbConnection, req, m_id );
218 219
}

220
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
221
                          unsigned int sampleRate, unsigned int nbChannels )
222 223 224
{
    static const std::string req = "INSERT INTO AudioTrackFileRelation VALUES(?, ?)";

225
    auto track = AudioTrack::fetch( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
226 227
    if ( track == nullptr )
    {
228
        track = AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels );
229 230 231
        if ( track == nullptr )
            return false;
    }
232
    return sqlite::Tools::executeRequest( m_dbConnection, req, track->id(), m_id );
233 234
}

235
std::vector<AudioTrackPtr> Media::audioTracks()
236 237 238
{
    static const std::string req = "SELECT t.* FROM " + policy::AudioTrackTable::Name +
            " t LEFT JOIN AudioTrackFileRelation atfr ON atfr.id_track = t.id_track"
239
            " WHERE atfr.id_media = ?";
240
    return AudioTrack::fetchAll( m_dbConnection, req, m_id );
241 242
}

243
const std::string &Media::snapshot()
244 245 246 247
{
    return m_snapshot;
}

248
bool Media::setSnapshot(const std::string &snapshot)
249
{
250 251
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET snapshot = ? WHERE id_media = ?";
252 253 254 255 256 257
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, snapshot, m_id ) == false )
        return false;
    m_snapshot = snapshot;
    return true;
}

258
bool Media::isStandAlone()
259 260 261 262
{
    return m_folderId == 0;
}

263
unsigned int Media::lastModificationDate()
264 265 266 267
{
    return m_lastModificationDate;
}

268
bool Media::isParsed() const
269
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
270
    return m_isParsed;
271 272
}

273
bool Media::markParsed()
274
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
275 276
    if ( m_isParsed == true )
        return true;
277 278
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET parsed = ? WHERE id_media = ?";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
279 280 281 282
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, true, m_id ) == false )
        return false;
    m_isParsed = true;
    return true;
283 284
}

285
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
286 287 288 289
{
    return m_id;
}

290
IMedia::Type Media::type()
291 292 293 294
{
    return m_type;
}

295
bool Media::setType( Type type )
296
{
297 298
    static const std::string req = "UPDATE " + policy::MediaTable::Name
            + " SET type = ? WHERE id_media = ?";
299 300 301 302 303 304 305 306
    // We need to convert to an integer representation for the sqlite traits to work properly
    using type_t = std::underlying_type<Type>::type;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, static_cast<type_t>( type ), m_id ) == false )
        return false;
    m_type = type;
    return true;
}

307
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
308
{
309
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
310 311
}

312
bool Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
313
{
314
    static const std::string req = "UPDATE " + policy::MediaTable::Name
315 316
            + " SET title = ? WHERE id_media = ?";
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, title, m_id ) == false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
317
        return false;
318
    m_title = title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
319 320 321
    return true;
}

322
bool Media::createTable( DBConnection connection )
323
{
324 325
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MediaTable::Name + "("
            "id_media INTEGER PRIMARY KEY AUTOINCREMENT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
326
            "type INTEGER,"
327
            "duration INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
328 329
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
330
            "mrl TEXT UNIQUE ON CONFLICT FAIL,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
331
            "movie_id UNSIGNED INTEGER,"
332
            "folder_id UNSIGNED INTEGER,"
333
            "last_modification_date UNSIGNED INTEGER,"
334
            "snapshot TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
335
            "parsed BOOLEAN,"
336
            "title TEXT,"
337
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
338 339
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
340 341 342
            + "(id_movie) ON DELETE CASCADE,"
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
            + "(id_folder) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
343
            ")";
344
    if ( sqlite::Tools::executeRequest( connection, req ) == false )
345 346 347
        return false;
    req = "CREATE TABLE IF NOT EXISTS VideoTrackFileRelation("
                "id_track INTEGER,"
348 349
                "id_media INTEGER,"
                "PRIMARY KEY ( id_track, id_media ), "
350 351
                "FOREIGN KEY ( id_track ) REFERENCES " + policy::VideoTrackTable::Name +
                    "(id_track) ON DELETE CASCADE,"
352 353
                "FOREIGN KEY ( id_media ) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
354
            ")";
355
    if ( sqlite::Tools::executeRequest( connection, req ) == false )
356 357 358
        return false;
    req = "CREATE TABLE IF NOT EXISTS AudioTrackFileRelation("
                "id_track INTEGER,"
359 360
                "id_media INTEGER,"
                "PRIMARY KEY ( id_track, id_media ), "
361 362
                "FOREIGN KEY ( id_track ) REFERENCES " + policy::AudioTrackTable::Name +
                    "(id_track) ON DELETE CASCADE,"
363 364
                "FOREIGN KEY ( id_media ) REFERENCES " + policy::MediaTable::Name
                + "(id_media) ON DELETE CASCADE"
365
            ")";
366
    return sqlite::Tools::executeRequest( connection, req );
367
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
368

369
bool Media::addLabel( LabelPtr label )
370
{
371
    if ( m_id == 0 || label->id() == 0 )
372
    {
373
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
374 375 376
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
377
    return sqlite::Tools::executeRequest( m_dbConnection, req, label->id(), m_id );
378 379
}

380
bool Media::removeLabel( LabelPtr label )
381
{
382
    if ( m_id == 0 || label->id() == 0 )
383
    {
384
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
385 386
        return false;
    }
387
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_media = ?";
388
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
389
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
390

391
const std::string& policy::MediaCache::key(const std::shared_ptr<Media> self )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
392 393 394 395
{
    return self->mrl();
}

396
std::string policy::MediaCache::key(sqlite3_stmt* stmt)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
397
{
398
    return sqlite::Traits<std::string>::Load( stmt, 5 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
399
}