Media.cpp 12.7 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, sqlite::Row& row )
47 48
    : m_dbConnection( dbConnection )
{
49 50 51 52 53 54 55 56 57 58 59 60
    row >> m_id
        >> m_type
        >> m_duration
        >> m_playCount
        >> m_showEpisodeId
        >> m_mrl
        >> m_movieId
        >> m_folderId
        >> m_lastModificationDate
        >> m_snapshot
        >> m_isParsed
        >> m_title;
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
    if ( _Cache::insert( dbConnection, self, req, type, self->m_mrl, sqlite::ForeignKey( folderId ),
85
                         self->m_lastModificationDate, self->m_title) == false )
86
        return nullptr;
87 88
    self->m_dbConnection = dbConnection;
    return self;
89 90
}

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

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

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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 );
}

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

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

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

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

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

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

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

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

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

197
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
198 199 200 201 202 203 204 205 206 207
{
    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;
    }
208
    return sqlite::Tools::executeRequest( m_dbConnection, req, track->id(), m_id );
209 210
}

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

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

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

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

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

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

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

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

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

272
bool Media::markParsed()
273
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
274 275
    if ( m_isParsed == true )
        return true;
276 277
    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
278 279 280 281
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, true, m_id ) == false )
        return false;
    m_isParsed = true;
    return true;
282 283
}

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

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

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

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

310
bool Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
311
{
312
    static const std::string req = "UPDATE " + policy::MediaTable::Name
313 314
            + " 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
315
        return false;
316
    m_title = title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
317 318 319
    return true;
}

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

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

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

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

394
std::string policy::MediaCache::key(sqlite::Row& row)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
395
{
396
    return row.load<std::string>( 5 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
397
}