Media.cpp 11.1 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 55 56
    row >> m_id
        >> m_type
        >> m_duration
        >> m_playCount
        >> m_showEpisodeId
        >> m_mrl
57
        >> m_artist
58 59 60
        >> m_movieId
        >> m_folderId
        >> m_lastModificationDate
61
        >> m_insertionDate
62 63
        >> m_snapshot
        >> m_isParsed
64
        >> m_title
65 66
        >> m_isPresent
        >> m_isRemovable;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67 68
}

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

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

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

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

113
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
114 115 116 117 118
{
    m_albumTrack = albumTrack;
    return true;
}

119
const std::string& Media::artist() const
120
{
121
    return m_artist;
122 123
}

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

132
int64_t Media::duration() const
133 134 135 136
{
    return m_duration;
}

137
void Media::setDuration( int64_t duration )
138
{
139 140
    if ( m_duration == duration )
        return;
141
    m_duration = duration;
142
    m_changed = true;
143 144
}

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

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

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

171
int Media::playCount() const
172
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
173
    return m_playCount;
174 175
}

176 177 178 179 180 181
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

182
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
183
{
184 185 186
    if ( m_isRemovable == false )
        return m_mrl;

187 188 189 190 191 192 193 194
    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
195 196
}

197
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
198 199 200 201 202 203 204 205
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

206
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
207
{
208 209
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
210 211
    m_movie = movie;
    m_movieId = movie->id();
212
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
213 214
}

215
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
216
{
217
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
218 219
}

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

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

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

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

246 247 248 249 250
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

251
void Media::setSnapshot( const std::string& snapshot )
252
{
253 254
    if ( m_snapshot == snapshot )
        return;
255
    m_snapshot = snapshot;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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;
275 276 277
    return true;
}

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

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

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

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

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

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

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

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

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

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

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