Media.cpp 10 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 63
        >> m_snapshot
        >> m_isParsed
        >> m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64 65
}

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

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

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

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

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

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

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

126
int64_t Media::duration() const
127 128 129 130
{
    return m_duration;
}

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

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

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

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

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

170 171 172 173 174 175
void Media::increasePlayCount()
{
    m_playCount++;
    m_changed = true;
}

176
const std::string& Media::mrl() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
177 178 179 180
{
    return m_mrl;
}

181
MoviePtr Media::movie()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
182 183 184 185 186 187 188 189
{
    if ( m_movie == nullptr && m_movieId != 0 )
    {
        m_movie = Movie::fetch( m_dbConnection, m_movieId );
    }
    return m_movie;
}

190
void Media::setMovie( MoviePtr movie )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
191
{
192 193
    if ( movie == nullptr || m_movieId == movie->id() )
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
194 195
    m_movie = movie;
    m_movieId = movie->id();
196
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
197 198
}

199
bool Media::addVideoTrack(const std::string& codec, unsigned int width, unsigned int height, float fps)
200
{
201
    return VideoTrack::create( m_dbConnection, codec, width, height, fps, m_id ) != nullptr;
202 203
}

204
std::vector<VideoTrackPtr> Media::videoTracks()
205
{
206 207
    static const std::string req = "SELECT * FROM " + policy::VideoTrackTable::Name +
            " WHERE media_id = ?";
208
    return VideoTrack::fetchAll<IVideoTrack>( m_dbConnection, req, m_id );
209 210
}

211
bool Media::addAudioTrack( const std::string& codec, unsigned int bitrate,
212 213
                          unsigned int sampleRate, unsigned int nbChannels,
                          const std::string& language, const std::string& desc )
214
{
215
    return AudioTrack::create( m_dbConnection, codec, bitrate, sampleRate, nbChannels, language, desc, m_id ) != nullptr;
216 217
}

218
std::vector<AudioTrackPtr> Media::audioTracks()
219
{
220 221
    static const std::string req = "SELECT * FROM " + policy::AudioTrackTable::Name +
            " WHERE media_id = ?";
222
    return AudioTrack::fetchAll<IAudioTrack>( m_dbConnection, req, m_id );
223 224
}

225
const std::string &Media::snapshot()
226 227 228 229
{
    return m_snapshot;
}

230 231 232 233 234
unsigned int Media::insertionDate() const
{
    return m_insertionDate;
}

235
void Media::setSnapshot( const std::string& snapshot )
236
{
237 238
    if ( m_snapshot == snapshot )
        return;
239
    m_snapshot = snapshot;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    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;
259 260 261
    return true;
}

262
bool Media::isStandAlone()
263 264 265 266
{
    return m_folderId == 0;
}

267
unsigned int Media::lastModificationDate()
268 269 270 271
{
    return m_lastModificationDate;
}

272
bool Media::isParsed() const
273
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
274
    return m_isParsed;
275 276
}

277
void Media::markParsed()
278
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
279
    if ( m_isParsed == true )
280
        return;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
281
    m_isParsed = true;
282
    m_changed = true;
283 284
}

285
unsigned int Media::id() const
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
286 287 288 289
{
    return m_id;
}

290
IMedia::Type Media::type()
291 292 293 294
{
    return m_type;
}

295
void Media::setType( Type type )
296
{
297 298
    if ( m_type != type )
        return;
299
    m_type = type;
300
    m_changed = true;
301 302
}

303
const std::string &Media::title()
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
304
{
305
    return m_title;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
306 307
}

308
void Media::setTitle( const std::string &title )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
309
{
310 311
    if ( m_title == title )
        return;
312
    m_title = title;
313
    m_changed = true;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
314 315
}

316
bool Media::createTable( DBConnection connection )
317
{
318 319
    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
320
            "type INTEGER,"
321
            "duration INTEGER DEFAULT -1,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
322 323
            "play_count UNSIGNED INTEGER,"
            "show_episode_id UNSIGNED INTEGER,"
324
            "mrl TEXT UNIQUE ON CONFLICT FAIL,"
325
            "artist TEXT,"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
326
            "movie_id UNSIGNED INTEGER,"
327
            "folder_id UNSIGNED INTEGER,"
328
            "last_modification_date UNSIGNED INTEGER,"
329
            "insertion_date UNSIGNED INTEGER,"
330
            "snapshot TEXT,"
331
            "parsed BOOLEAN NOT NULL DEFAULT 0,"
332
            "title TEXT,"
333
            "FOREIGN KEY (show_episode_id) REFERENCES " + policy::ShowEpisodeTable::Name
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
334 335
            + "(id_episode) ON DELETE CASCADE,"
            "FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
336 337 338
            + "(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
339
            ")";
340
    return sqlite::Tools::executeRequest( connection, req );
341
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
342

343
bool Media::addLabel( LabelPtr label )
344
{
345
    if ( m_id == 0 || label->id() == 0 )
346
    {
347
        LOG_ERROR( "Both file & label need to be inserted in database before being linked together" );
348 349 350
        return false;
    }
    const char* req = "INSERT INTO LabelFileRelation VALUES(?, ?)";
351
    return sqlite::Tools::insert( m_dbConnection, req, label->id(), m_id ) != 0;
352 353
}

354
bool Media::removeLabel( LabelPtr label )
355
{
356
    if ( m_id == 0 || label->id() == 0 )
357
    {
358
        LOG_ERROR( "Can't unlink a label/file not inserted in database" );
359 360
        return false;
    }
361
    const char* req = "DELETE FROM LabelFileRelation WHERE id_label = ? AND id_media = ?";
362
    return sqlite::Tools::executeDelete( m_dbConnection, req, label->id(), m_id );
363
}