Media.cpp 10.6 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 59
        >> m_mrl
        >> m_folderId
        >> m_lastModificationDate
60
        >> m_insertionDate
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61
        >> m_thumbnail
62
        >> m_isParsed
63
        >> m_title
64 65
        >> m_isPresent
        >> m_isRemovable;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
66 67
}

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

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

93
    if ( insert( dbConnection, self, req, type, self->m_mrl, sqlite::ForeignKey( folderId ),
94
                         self->m_lastModificationDate, self->m_insertionDate, self->m_title, isRemovable ) == false )
95
        return nullptr;
96
    self->m_dbConnection = dbConnection;
97
    self->m_fullPath = file->fullPath();
98
    return self;
99 100
}

101
AlbumTrackPtr Media::albumTrack() const
102
{
103 104 105 106 107
    auto lock = m_albumTrack.lock();

    if ( m_albumTrack.isCached() == false )
        m_albumTrack = AlbumTrack::fromMedia( m_dbConnection, m_id );
    return m_albumTrack.get();
108 109
}

110
void Media::setAlbumTrack( AlbumTrackPtr albumTrack )
111
{
112
    auto lock = m_albumTrack.lock();
113 114 115
    m_albumTrack = albumTrack;
}

116
int64_t Media::duration() const
117 118 119 120
{
    return m_duration;
}

121
void Media::setDuration( int64_t duration )
122
{
123 124
    if ( m_duration == duration )
        return;
125
    m_duration = duration;
126
    m_changed = true;
127 128
}

129
ShowEpisodePtr Media::showEpisode() const
130
{
131 132 133 134 135
    auto lock = m_showEpisode.lock();

    if ( m_showEpisode.isCached() == false )
        m_showEpisode = ShowEpisode::fromMedia( m_dbConnection, m_id );
    return m_showEpisode.get();
136 137
}

138
void Media::setShowEpisode( ShowEpisodePtr episode )
139
{
140 141
    auto lock = m_showEpisode.lock();
    m_showEpisode = episode;
142 143
}

144
std::vector<LabelPtr> Media::labels()
145
{
146
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
147 148
            "LEFT JOIN LabelFileRelation lfr ON lfr.label_id = l.id_label "
            "WHERE lfr.media_id = ?";
149
    return Label::fetchAll<ILabel>( m_dbConnection, req, m_id );
150 151
}

152
int Media::playCount() const
153
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
154
    return m_playCount;
155 156
}

157 158 159 160 161 162
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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;
}

189
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
190
{
191 192 193
    if ( m_isRemovable == false )
        return m_mrl;

194 195 196 197 198 199 200 201
    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
202 203
}

204
MoviePtr Media::movie() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
205
{
206 207 208 209 210
    auto lock = m_movie.lock();

    if ( m_movie.isCached() == false )
        m_movie = Movie::fromMedia( m_dbConnection, m_id );
    return m_movie.get();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
211 212
}

213
void Media::setMovie(MoviePtr movie)
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
214
{
215
    auto lock = m_movie.lock();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
216 217 218
    m_movie = movie;
}

219
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
220
{
221
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
222 223
}

224
std::vector<VideoTrackPtr> Media::videoTracks()
225
{
226 227
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
228
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
229 230
}

231
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
232 233
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
234
{
235
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
236 237
}

238
std::vector<AudioTrackPtr> Media::audioTracks()
239
{
240 241
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
242
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
243 244
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
245
const std::string &Media::thumbnail()
246
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
247
    return m_thumbnail;
248 249
}

250 251 252 253 254
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
255
void Media::setThumbnail(const std::string& thumbnail )
256
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
257
    if ( m_thumbnail == thumbnail )
258
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
259
    m_thumbnail = thumbnail;
260 261 262 263 264 265
    m_changed = true;
}

bool Media::save()
{
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
266 267
            "type = ?, duration = ?, play_count = ?, progress = ?, rating = ?,"
            "last_modification_date = ?, thumbnail = ?, parsed = ?,"
268
            "title = ? WHERE id_media = ?";
269 270 271
    if ( m_changed == false )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_type, m_duration, m_playCount,
272
                                       m_progress, m_rating, m_lastModificationDate,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
273
                                       m_thumbnail, m_isParsed, m_title, m_id ) == false )
274 275 276 277
    {
        return false;
    }
    m_changed = false;
278 279 280
    return true;
}

281
unsigned int Media::lastModificationDate()
282 283 284 285
{
    return m_lastModificationDate;
}

286
bool Media::isParsed() const
287
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
288
    return m_isParsed;
289 290
}

291
void Media::markParsed()
292
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
293
    if ( m_isParsed == true )
294
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
295
    m_isParsed = true;
296
    m_changed = true;
297 298
}

299
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
300 301 302 303
{
    return m_id;
}

304
IMedia::Type Media::type()
305 306 307 308
{
    return m_type;
}

309
void Media::setType( Type type )
310
{
311 312
    if ( m_type != type )
        return;
313
    m_type = type;
314
    m_changed = true;
315 316
}

317
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
318
{
319
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
320 321
}

322
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
323
{
324 325
    if ( m_title == title )
        return;
326
    m_title = title;
327
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
328 329
}

330
bool Media::createTable( DBConnection connection )
331
{
332 333
    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
334
            "type INTEGER,"
335
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
336
            "play_count UNSIGNED INTEGER,"
337 338
            "progress REAL,"
            "rating INTEGER DEFAULT -1,"
339
            "mrl TEXT,"
340
            "folder_id UNSIGNED INTEGER,"
341
            "last_modification_date UNSIGNED INTEGER,"
342
            "insertion_date UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
343
            "thumbnail TEXT,"
344
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
345
            "title TEXT,"
346
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
347
            "is_removable BOOLEAN NOT NULL,"
348
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
349 350
            + "(id_folder) ON DELETE CASCADE,"
            "UNIQUE( mrl, folder_id ) ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
351
            ")";
352 353 354 355 356 357 358
    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 );
359
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
360

361
bool Media::addLabel( LabelPtr label )
362
{
363
    if ( m_id == 0 || label->id() == 0 )
364
    {
365
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
366 367 368
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
369
    return sqlite::Tools::insert( m_dbConnection, req, label->id(), m_id ) != 0;
370 371
}

372
bool Media::removeLabel( LabelPtr label )
373
{
374
    if ( m_id == 0 || label->id() == 0 )
375
    {
376
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
377 378
        return false;
    }
379
    const char* req = "DELETE FROM LabelFileRelation WHERE label_id = ? AND media_id = ?";
380
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
381
}