Library.cpp 3.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * Library.cpp: Multimedia library
 *****************************************************************************
4
 * Copyright (C) 2008-2016 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 24
/** \file
  * This file the library contains class implementation.
25
  * It's the backend part of the Library widget of vlmc.
26
  * It can load and unload Medias (Medias.h/Media.cpp)
27
  * It can load and unload Clips (Clip.h/Clip.cpp)
28 29
  */

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include "Library.h"
35 36
#include "Media/Clip.h"
#include "Media/Media.h"
37
#include "Project/Project.h"
38
#include "Settings/Settings.h"
39
#include "Tools/VlmcDebug.h"
40

luyikei's avatar
luyikei committed
41
#include <QVariant>
42 43 44
#include <QHash>
#include <QUuid>

luyikei's avatar
luyikei committed
45
Library::Library( Settings *projectSettings )
46
    : m_cleanState( true )
luyikei's avatar
luyikei committed
47
    , m_settings( new Settings )
48
{
luyikei's avatar
luyikei committed
49
    m_settings->createVar( SettingValue::List, QString( "medias" ), QVariantList(), "", "", SettingValue::Nothing );
luyikei's avatar
luyikei committed
50 51 52
    connect( m_settings, &Settings::postLoad, this, &Library::postLoad, Qt::DirectConnection );
    connect( m_settings, &Settings::preSave, this, &Library::preSave, Qt::DirectConnection );

luyikei's avatar
luyikei committed
53 54 55
    projectSettings->addSettings( "Library", *m_settings );
}

luyikei's avatar
luyikei committed
56 57 58 59
void
Library::preSave()
{
    QVariantList l;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
    for ( auto val : m_media )
luyikei's avatar
luyikei committed
61 62
        l << val->toVariant();
    m_settings->value( "medias" )->set( l );
63
    setCleanState( true );
luyikei's avatar
luyikei committed
64 65 66 67 68 69
}

void
Library::postLoad()
{
    for ( const auto& var : m_settings->value( "medias" )->get().toList() )
70 71 72 73
    {
        auto m = Media::fromVariant( var );
        addMedia( m );
    }
luyikei's avatar
luyikei committed
74 75
}

luyikei's avatar
luyikei committed
76 77 78
Library::~Library()
{
    delete m_settings;
79 80 81
}

void
82
Library::addMedia( QSharedPointer<Media> media )
83 84
{
    setCleanState( false );
85
    if ( m_media.contains( media->id() ) )
86
        return;
87
    m_media[media->id()] = media;
88 89 90 91 92
}

bool
Library::addClip( Clip *clip )
{
93 94 95
    foreach ( Clip* c, m_clips.values() )
    {
        if ( clip->uuid() == c->uuid() ||
96
             ( clip->media()->mrl() == c->media()->mrl() &&
97 98 99 100 101 102 103 104
                    ( clip->begin() == c->begin() && clip->end() == c->end() ) ) )
        {
            vlmcWarning() << "Clip already loaded.";
            return false;
        }
    }
    setCleanState( false );
    return true;
105 106 107 108 109 110 111 112
}

bool
Library::isInCleanState() const
{
    return m_cleanState;
}

113
QSharedPointer<Media>
114
Library::media( qint64 mediaId )
115
{
116
    return m_media.value( mediaId );
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
Clip*
Library::clip( const QString& uuid )
{
    return m_clips.value( uuid );
}

Clip*
Library::clip( const QUuid& uuid )
{
    return clip( uuid.toString() );
}

void
Library::clear()
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
134
    m_media.clear();
135 136 137 138
    m_clips.clear();
    setCleanState( true );
}

139 140 141 142 143 144 145 146 147
void
Library::setCleanState( bool newState )
{
    if ( newState != m_cleanState )
    {
        m_cleanState = newState;
        emit cleanStateChanged( newState );
    }
}