Media.cpp 11.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
#include "utils/Filename.h"
42

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

47
Media::Media( DBConnection dbConnection, sqlite::Row& row )
48
    : m_dbConnection( dbConnection )
49
    , m_changed( false )
50
{
51 52 53 54
    row >> m_id
        >> m_type
        >> m_duration
        >> m_playCount
55 56
        >> m_progress
        >> m_rating
57 58
        >> m_showEpisodeId
        >> m_mrl
59
        >> m_artist
60 61 62
        >> m_movieId
        >> m_folderId
        >> m_lastModificationDate
63
        >> m_insertionDate
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64
        >> m_thumbnail
65
        >> m_isParsed
66
        >> m_title
67 68
        >> m_isPresent
        >> m_isRemovable;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
69 70
}

71
Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& title, Type type, bool isRemovable )
72
    : m_id( 0 )
73
    , m_type( type )
74
    , m_duration( -1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
75
    , m_playCount( 0 )
76 77
    , m_progress( .0f )
    , m_rating( -1 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
78
    , m_showEpisodeId( 0 )
79
    , m_mrl( isRemovable == true ? file->name() : file->fullPath() )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
80
    , m_movieId( 0 )
81
    , m_folderId( folderId )
82
    , m_lastModificationDate( file->lastModificationDate() )
83
    , m_insertionDate( time( nullptr ) )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
84
    , m_isParsed( false )
85
    , m_title( title )
86
    , m_isPresent( true )
87
    , m_isRemovable( isRemovable )
88
    , m_changed( false )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
89
{
90 91
}

92
std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, const fs::IFile* file, unsigned int folderId, bool isRemovable )
93
{
94
    auto self = std::make_shared<Media>( file, folderId, file->name(), type, isRemovable );
95
    static const std::string req = "INSERT INTO " + policy::MediaTable::Name +
96
            "(type, mrl, folder_id, last_modification_date, insertion_date, title, is_removable) VALUES(?, ?, ?, ?, ?, ?, ?)";
97

98
    if ( insert( dbConnection, self, req, type, self->m_mrl, sqlite::ForeignKey( folderId ),
99
                         self->m_lastModificationDate, self->m_insertionDate, self->m_title, isRemovable ) == false )
100
        return nullptr;
101
    self->m_dbConnection = dbConnection;
102
    self->m_fullPath = file->fullPath();
103
    return self;
104 105
}

106
AlbumTrackPtr Media::albumTrack()
107
{
108
    if ( m_albumTrack == nullptr )
109
    {
110 111
        std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
                " WHERE media_id = ?";
112
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, req, m_id );
113 114
    }
    return m_albumTrack;
115 116
}

117
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
118 119 120 121 122
{
    m_albumTrack = albumTrack;
    return true;
}

123
const std::string& Media::artist() const
124
{
125
    return m_artist;
126 127
}

128
void Media::setArtist(const std::string& artist)
129
{
130
    if ( m_artist == artist )
131
        return;
132
    m_artist = artist;
133
    m_changed = true;
134 135
}

136
int64_t Media::duration() const
137 138 139 140
{
    return m_duration;
}

141
void Media::setDuration( int64_t duration )
142
{
143 144
    if ( m_duration == duration )
        return;
145
    m_duration = duration;
146
    m_changed = true;
147 148
}

149
std::shared_ptr<IShowEpisode> Media::showEpisode()
150
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
151
    if ( m_showEpisode == nullptr && m_showEpisodeId != 0 )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
152
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
153
        m_showEpisode = ShowEpisode::fetch( m_dbConnection, m_showEpisodeId );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
154 155
    }
    return m_showEpisode;
156 157
}

158
void Media::setShowEpisode( ShowEpisodePtr showEpisode )
159
{
160 161
    if ( showEpisode == nullptr || m_showEpisodeId == showEpisode->id() )
        return;
162 163
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
164
    m_changed = true;
165 166
}

167
std::vector<LabelPtr> Media::labels()
168
{
169
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
170
            "LEFT JOIN LabelFileRelation lfr ON lfr.id_label = l.id_label "
171
            "WHERE lfr.id_media = ?";
172
    return Label::fetchAll<ILabel>( m_dbConnection, req, m_id );
173 174
}

175
int Media::playCount() const
176
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
177
    return m_playCount;
178 179
}

180 181 182 183 184 185
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
float Media::progress() const
{
    return m_progress;
}

void Media::setProgress( float progress )
{
    if ( progress == m_progress || progress < 0 || progress > 1.0 )
        return;
    m_progress = progress;
    m_changed = true;
}

int Media::rating() const
{
    return m_rating;
}

void Media::setRating( int rating )
{
    if ( m_rating == rating )
        return;
    m_rating = rating;
    m_changed = true;
}

212
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
213
{
214 215 216
    if ( m_isRemovable == false )
        return m_mrl;

217 218 219 220 221 222 223 224
    auto lock = m_fullPath.lock();
    if ( m_fullPath.isCached() )
        return m_fullPath;
    auto folder = Folder::fetch( m_dbConnection, m_folderId );
    if ( folder == nullptr )
        return m_mrl;
    m_fullPath = folder->path() + m_mrl;
    return m_fullPath;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
225 226
}

227
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
228 229 230 231 232 233 234 235
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

236
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
237
{
238 239
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
240 241
    m_movie = movie;
    m_movieId = movie->id();
242
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
243 244
}

245
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
246
{
247
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
248 249
}

250
std::vector<VideoTrackPtr> Media::videoTracks()
251
{
252 253
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
254
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
255 256
}

257
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
258 259
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
260
{
261
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
262 263
}

264
std::vector<AudioTrackPtr> Media::audioTracks()
265
{
266 267
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
268
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
269 270
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
271
const std::string &Media::thumbnail()
272
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
273
    return m_thumbnail;
274 275
}

276 277 278 279 280
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
281
void Media::setThumbnail(const std::string& thumbnail )
282
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
283
    if ( m_thumbnail == thumbnail )
284
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
285
    m_thumbnail = thumbnail;
286 287 288 289 290 291
    m_changed = true;
}

bool Media::save()
{
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
292
            "type = ?, duration = ?, play_count = ?, progress = ?, rating = ?, show_episode_id = ?,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
293
            "artist = ?, movie_id = ?, last_modification_date = ?, thumbnail = ?, parsed = ?,"
294
            "title = ? WHERE id_media = ?";
295 296 297
    if ( m_changed == false )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_type, m_duration, m_playCount,
298
                                       m_progress, m_rating,
299 300
                                       sqlite::ForeignKey{ m_showEpisodeId }, m_artist,
                                       sqlite::ForeignKey{ m_movieId }, m_lastModificationDate,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
301
                                       m_thumbnail, m_isParsed, m_title, m_id ) == false )
302 303 304 305
    {
        return false;
    }
    m_changed = false;
306 307 308
    return true;
}

309
unsigned int Media::lastModificationDate()
310 311 312 313
{
    return m_lastModificationDate;
}

314
bool Media::isParsed() const
315
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
316
    return m_isParsed;
317 318
}

319
void Media::markParsed()
320
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
321
    if ( m_isParsed == true )
322
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
323
    m_isParsed = true;
324
    m_changed = true;
325 326
}

327
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
328 329 330 331
{
    return m_id;
}

332
IMedia::Type Media::type()
333 334 335 336
{
    return m_type;
}

337
void Media::setType( Type type )
338
{
339 340
    if ( m_type != type )
        return;
341
    m_type = type;
342
    m_changed = true;
343 344
}

345
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
346
{
347
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
348 349
}

350
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
351
{
352 353
    if ( m_title == title )
        return;
354
    m_title = title;
355
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
356 357
}

358
bool Media::createTable( DBConnection connection )
359
{
360 361
    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
362
            "type INTEGER,"
363
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
364
            "play_count UNSIGNED INTEGER,"
365 366
            "progress REAL,"
            "rating INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
367
            "show_episode_id UNSIGNED INTEGER,"
368
            "mrl TEXT,"
369
            "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
370
            "movie_id UNSIGNED INTEGER,"
371
            "folder_id UNSIGNED INTEGER,"
372
            "last_modification_date UNSIGNED INTEGER,"
373
            "insertion_date UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
374
            "thumbnail TEXT,"
375
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
376
            "title TEXT,"
377
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
378
            "is_removable BOOLEAN NOT NULL,"
379
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
380 381
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
382 383
            + "(id_movie) ON DELETE CASCADE,"
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
384 385
            + "(id_folder) ON DELETE CASCADE,"
            "UNIQUE( mrl, folder_id ) ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
386
            ")";
387 388 389 390 391 392 393
    std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_folder_present AFTER UPDATE OF is_present ON "
            + policy::FolderTable::Name +
            " BEGIN"
            " UPDATE " + policy::MediaTable::Name + " SET is_present = new.is_present WHERE folder_id = new.id_folder;"
            " END";
    return sqlite::Tools::executeRequest( connection, req ) &&
            sqlite::Tools::executeRequest( connection, triggerReq );
394
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
395

396
bool Media::addLabel( LabelPtr label )
397
{
398
    if ( m_id == 0 || label->id() == 0 )
399
    {
400
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
401 402 403
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
404
    return sqlite::Tools::insert( m_dbConnection, req, label->id(), m_id ) != 0;
405 406
}

407
bool Media::removeLabel( LabelPtr label )
408
{
409
    if ( m_id == 0 || label->id() == 0 )
410
    {
411
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
412 413
        return false;
    }
414
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_media = ?";
415
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
416
}