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

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

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

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

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

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

122
int64_t Media::duration() const
123 124 125 126
{
    return m_duration;
}

127
void Media::setDuration( int64_t duration )
128
{
129 130
    if ( m_duration == duration )
        return;
131
    m_duration = duration;
132
    m_changed = true;
133 134
}

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

144
void Media::setShowEpisode( ShowEpisodePtr showEpisode )
145
{
146 147
    if ( showEpisode == nullptr || m_showEpisodeId == showEpisode->id() )
        return;
148 149
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
150
    m_changed = true;
151 152
}

153
std::vector<LabelPtr> Media::labels()
154
{
155
    static const std::string req = "SELECT l.* FROM " + policy::LabelTable::Name + " l "
156 157
            "LEFT JOIN LabelFileRelation lfr ON lfr.label_id = l.id_label "
            "WHERE lfr.media_id = ?";
158
    return Label::fetchAll<ILabel>( m_dbConnection, req, m_id );
159 160
}

161
int Media::playCount() const
162
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
163
    return m_playCount;
164 165
}

166 167 168 169 170 171
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
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;
}

198
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
199
{
200 201 202
    if ( m_isRemovable == false )
        return m_mrl;

203 204 205 206 207 208 209 210
    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
211 212
}

213
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
214 215 216 217 218 219 220 221
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

222
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
223
{
224 225
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
226 227
    m_movie = movie;
    m_movieId = movie->id();
228
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
229 230
}

231
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
232
{
233
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
234 235
}

236
std::vector<VideoTrackPtr> Media::videoTracks()
237
{
238 239
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
240
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
241 242
}

243
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
244 245
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
246
{
247
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
248 249
}

250
std::vector<AudioTrackPtr> Media::audioTracks()
251
{
252 253
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
254
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
255 256
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
257
const std::string &Media::thumbnail()
258
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
259
    return m_thumbnail;
260 261
}

262 263 264 265 266
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
267
void Media::setThumbnail(const std::string& thumbnail )
268
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
269
    if ( m_thumbnail == thumbnail )
270
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
271
    m_thumbnail = thumbnail;
272 273 274 275 276 277
    m_changed = true;
}

bool Media::save()
{
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
278
            "type = ?, duration = ?, play_count = ?, progress = ?, rating = ?, show_episode_id = ?,"
279
            "movie_id = ?, last_modification_date = ?, thumbnail = ?, parsed = ?,"
280
            "title = ? WHERE id_media = ?";
281 282 283
    if ( m_changed == false )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_type, m_duration, m_playCount,
284
                                       m_progress, m_rating,
285
                                       sqlite::ForeignKey{ m_showEpisodeId },
286
                                       sqlite::ForeignKey{ m_movieId }, m_lastModificationDate,
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
287
                                       m_thumbnail, m_isParsed, m_title, m_id ) == false )
288 289 290 291
    {
        return false;
    }
    m_changed = false;
292 293 294
    return true;
}

295
unsigned int Media::lastModificationDate()
296 297 298 299
{
    return m_lastModificationDate;
}

300
bool Media::isParsed() const
301
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
302
    return m_isParsed;
303 304
}

305
void Media::markParsed()
306
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
307
    if ( m_isParsed == true )
308
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
309
    m_isParsed = true;
310
    m_changed = true;
311 312
}

313
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
314 315 316 317
{
    return m_id;
}

318
IMedia::Type Media::type()
319 320 321 322
{
    return m_type;
}

323
void Media::setType( Type type )
324
{
325 326
    if ( m_type != type )
        return;
327
    m_type = type;
328
    m_changed = true;
329 330
}

331
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
332
{
333
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
334 335
}

336
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
337
{
338 339
    if ( m_title == title )
        return;
340
    m_title = title;
341
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
342 343
}

344
bool Media::createTable( DBConnection connection )
345
{
346 347
    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
348
            "type INTEGER,"
349
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
350
            "play_count UNSIGNED INTEGER,"
351 352
            "progress REAL,"
            "rating INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
353
            "show_episode_id UNSIGNED INTEGER,"
354
            "mrl TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
355
            "movie_id UNSIGNED INTEGER,"
356
            "folder_id UNSIGNED INTEGER,"
357
            "last_modification_date UNSIGNED INTEGER,"
358
            "insertion_date UNSIGNED INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
359
            "thumbnail TEXT,"
360
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
361
            "title TEXT,"
362
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
363
            "is_removable BOOLEAN NOT NULL,"
364
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
365 366
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
367 368
            + "(id_movie) ON DELETE CASCADE,"
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
369 370
            + "(id_folder) ON DELETE CASCADE,"
            "UNIQUE( mrl, folder_id ) ON CONFLICT FAIL"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
371
            ")";
372 373 374 375 376 377 378
    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 );
379
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
380

381
bool Media::addLabel( LabelPtr label )
382
{
383
    if ( m_id == 0 || label->id() == 0 )
384
    {
385
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
386 387 388
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
389
    return sqlite::Tools::insert( m_dbConnection, req, label->id(), m_id ) != 0;
390 391
}

392
bool Media::removeLabel( LabelPtr label )
393
{
394
    if ( m_id == 0 || label->id() == 0 )
395
    {
396
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
397 398
        return false;
    }
399
    const char* req = "DELETE FROM LabelFileRelation WHERE label_id = ? AND media_id = ?";
400
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
401
}