Media.cpp 10.8 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
const std::string policy::MediaTable::Name = "Media";
43
const std::string policy::MediaTable::PrimaryKeyColumn = "id_media";
44
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
    : m_dbConnection( dbConnection )
48
    , m_changed( false )
49
{
50 51 52 53 54 55
    row >> m_id
        >> m_type
        >> m_duration
        >> m_playCount
        >> m_showEpisodeId
        >> m_mrl
56
        >> m_artist
57 58 59
        >> m_movieId
        >> m_folderId
        >> m_lastModificationDate
60
        >> m_insertionDate
61 62
        >> m_snapshot
        >> m_isParsed
63 64
        >> m_title
        >> m_isPresent;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
65 66
}

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

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

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

99
AlbumTrackPtr Media::albumTrack()
100
{
101
    if ( m_albumTrack == nullptr )
102
    {
103 104
        std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
                " WHERE media_id = ?";
105
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, req, m_id );
106 107
    }
    return m_albumTrack;
108 109
}

110
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
111 112 113 114 115
{
    m_albumTrack = albumTrack;
    return true;
}

116
const std::string& Media::artist() const
117
{
118
    return m_artist;
119 120
}

121
void Media::setArtist(const std::string& artist)
122
{
123
    if ( m_artist == artist )
124
        return;
125
    m_artist = artist;
126
    m_changed = true;
127 128
}

129
int64_t Media::duration() const
130 131 132 133
{
    return m_duration;
}

134
void Media::setDuration( int64_t duration )
135
{
136 137
    if ( m_duration == duration )
        return;
138
    m_duration = duration;
139
    m_changed = true;
140 141
}

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

151
void Media::setShowEpisode( ShowEpisodePtr showEpisode )
152
{
153 154
    if ( showEpisode == nullptr || m_showEpisodeId == showEpisode->id() )
        return;
155 156
    m_showEpisodeId = showEpisode->id();
    m_showEpisode = showEpisode;
157
    m_changed = true;
158 159
}

160
std::vector<LabelPtr> 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<ILabel>( 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 174 175 176 177 178
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

179
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
180
{
181 182 183 184 185 186 187 188
    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
189 190
}

191
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
192 193 194 195 196 197 198 199
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

200
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
201
{
202 203
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
204 205
    m_movie = movie;
    m_movieId = movie->id();
206
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
207 208
}

209
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
210
{
211
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
212 213
}

214
std::vector<VideoTrackPtr> Media::videoTracks()
215
{
216 217
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
218
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
219 220
}

221
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
222 223
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
224
{
225
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
226 227
}

228
std::vector<AudioTrackPtr> Media::audioTracks()
229
{
230 231
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
232
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
233 234
}

235
const std::string &Media::snapshot()
236 237 238 239
{
    return m_snapshot;
}

240 241 242 243 244
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

245
void Media::setSnapshot( const std::string& snapshot )
246
{
247 248
    if ( m_snapshot == snapshot )
        return;
249
    m_snapshot = snapshot;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    m_changed = true;
}

bool Media::save()
{
    static const std::string req = "UPDATE " + policy::MediaTable::Name + " SET "
            "type = ?, duration = ?, play_count = ?, show_episode_id = ?, artist = ?,"
            "movie_id = ?, last_modification_date = ?, snapshot = ?, parsed = ?, title = ? "
            "WHERE id_media = ?";
    if ( m_changed == false )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConnection, req, m_type, m_duration, m_playCount,
                                       sqlite::ForeignKey{ m_showEpisodeId }, m_artist,
                                       sqlite::ForeignKey{ m_movieId }, m_lastModificationDate,
                                       m_snapshot, m_isParsed, m_title, m_id ) == false )
    {
        return false;
    }
    m_changed = false;
269 270 271
    return true;
}

272
bool Media::isStandAlone()
273 274 275 276
{
    return m_folderId == 0;
}

277
unsigned int Media::lastModificationDate()
278 279 280 281
{
    return m_lastModificationDate;
}

282
bool Media::isParsed() const
283
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
284
    return m_isParsed;
285 286
}

287
void Media::markParsed()
288
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
289
    if ( m_isParsed == true )
290
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
291
    m_isParsed = true;
292
    m_changed = true;
293 294
}

295
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
296 297 298 299
{
    return m_id;
}

300
IMedia::Type Media::type()
301 302 303 304
{
    return m_type;
}

305
void Media::setType( Type type )
306
{
307 308
    if ( m_type != type )
        return;
309
    m_type = type;
310
    m_changed = true;
311 312
}

313
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
314
{
315
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
316 317
}

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

326
bool Media::createTable( DBConnection connection )
327
{
328 329
    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
330
            "type INTEGER,"
331
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
332 333
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
334
            "mrl TEXT,"
335
            "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
336
            "movie_id UNSIGNED INTEGER,"
337
            "folder_id UNSIGNED INTEGER,"
338
            "last_modification_date UNSIGNED INTEGER,"
339
            "insertion_date UNSIGNED INTEGER,"
340
            "snapshot TEXT,"
341
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
342
            "title TEXT,"
343
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
344
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
345 346
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
347 348
            + "(id_movie) ON DELETE CASCADE,"
            "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 id_label = ? AND id_media = ?";
380
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
381
}