Media.cpp 9.73 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 60 61 62
        >> m_movieId
        >> m_folderId
        >> m_lastModificationDate
        >> m_snapshot
        >> m_isParsed
        >> m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
63 64
}

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

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

87
    if ( insert( dbConnection, self, req, type, self->m_mrl, sqlite::ForeignKey( folderId ),
88
                         self->m_lastModificationDate, self->m_title) == false )
89
        return nullptr;
90 91
    self->m_dbConnection = dbConnection;
    return self;
92 93
}

94
AlbumTrackPtr Media::albumTrack()
95
{
96
    if ( m_albumTrack == nullptr )
97
    {
98 99
        std::string req = "SELECT * FROM " + policy::AlbumTrackTable::Name +
                " WHERE media_id = ?";
100
        m_albumTrack = AlbumTrack::fetch( m_dbConnection, req, m_id );
101 102
    }
    return m_albumTrack;
103 104
}

105
bool Media::setAlbumTrack( AlbumTrackPtr albumTrack )
106 107 108 109 110
{
    m_albumTrack = albumTrack;
    return true;
}

111
const std::string& Media::artist() const
112
{
113
    return m_artist;
114 115
}

116
void Media::setArtist(const std::string& artist)
117
{
118
    if ( m_artist == artist )
119
        return;
120
    m_artist = artist;
121
    m_changed = true;
122 123
}

124
int64_t Media::duration() const
125 126 127 128
{
    return m_duration;
}

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

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

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

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

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

168
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
169 170 171 172
{
    return m_mrl;
}

173
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
174 175 176 177 178 179 180 181
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

182
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
183
{
184 185
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
186 187
    m_movie = movie;
    m_movieId = movie->id();
188
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
189 190
}

191
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
192
{
193
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
194 195
}

196
std::vector<VideoTrackPtr> Media::videoTracks()
197
{
198 199
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
200
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
201 202
}

203
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
204 205
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
206
{
207
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
208 209
}

210
std::vector<AudioTrackPtr> Media::audioTracks()
211
{
212 213
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
214
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
215 216
}

217
const std::string &Media::snapshot()
218 219 220 221
{
    return m_snapshot;
}

222
void Media::setSnapshot( const std::string& snapshot )
223
{
224 225
    if ( m_snapshot == snapshot )
        return;
226
    m_snapshot = snapshot;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    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;
246 247 248
    return true;
}

249
bool Media::isStandAlone()
250 251 252 253
{
    return m_folderId == 0;
}

254
unsigned int Media::lastModificationDate()
255 256 257 258
{
    return m_lastModificationDate;
}

259
bool Media::isParsed() const
260
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
261
    return m_isParsed;
262 263
}

264
void Media::markParsed()
265
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
266
    if ( m_isParsed == true )
267
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
268
    m_isParsed = true;
269
    m_changed = true;
270 271
}

272
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
273 274 275 276
{
    return m_id;
}

277
IMedia::Type Media::type()
278 279 280 281
{
    return m_type;
}

282
void Media::setType( Type type )
283
{
284 285
    if ( m_type != type )
        return;
286
    m_type = type;
287
    m_changed = true;
288 289
}

290
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
291
{
292
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
293 294
}

295
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
296
{
297 298
    if ( m_title == title )
        return;
299
    m_title = title;
300
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
301 302
}

303
bool Media::createTable( DBConnection connection )
304
{
305 306
    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
307
            "type INTEGER,"
308
            "duration INTEGER,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
309 310
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
311
            "mrl TEXT UNIQUE ON CONFLICT FAIL,"
312
            "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
313
            "movie_id UNSIGNED INTEGER,"
314
            "folder_id UNSIGNED INTEGER,"
315
            "last_modification_date UNSIGNED INTEGER,"
316
            "snapshot TEXT,"
317
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
318
            "title TEXT,"
319
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
320 321
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
322 323 324
            + "(id_movie) ON DELETE CASCADE,"
            "FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
            + "(id_folder) ON DELETE CASCADE"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
325
            ")";
326
    return sqlite::Tools::executeRequest( connection, req );
327
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
328

329
bool Media::addLabel( LabelPtr label )
330
{
331
    if ( m_id == 0 || label->id() == 0 )
332
    {
333
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
334 335 336
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
337
    return sqlite::Tools::executeInsert( m_dbConnection, req, label->id(), m_id );
338 339
}

340
bool Media::removeLabel( LabelPtr label )
341
{
342
    if ( m_id == 0 || label->id() == 0 )
343
    {
344
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
345 346
        return false;
    }
347
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_media = ?";
348
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
349
}