Library.cpp 4.48 KB
Newer Older
1 2 3
/*****************************************************************************
 * Library.cpp: Multimedia library
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * 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 25
/** \file
  * This file the library contains class implementation.
  * It's the 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
#include "Clip.h"
31
#include "Library.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
32
#include "Media.h"
33
#include "MetaDataManager.h"
34 35
#include "SettingsManager.h"
#include "Workspace.h"
36

37
#include <QtDebug>
38 39 40 41
#include <QDomElement>
#include <QHash>
#include <QUuid>

42 43 44 45
Library::Library() : m_cleanState( true )
{
}

46
void
47
Library::loadProject( const QDomElement& doc )
48
{
49 50 51
    const QDomElement   medias = doc.firstChildElement( "medias" );

    if ( medias.isNull() == true )
52 53
        return ;

54 55 56
    //Add a virtual media, which represents all the clip.
    //This avoid emitting projectLoaded(); before all the clip are actually loaded.
    m_nbMediaToLoad = 1;
57
    QDomElement media = medias.firstChildElement();
58
    while ( media.isNull() == false )
59
    {
60
        if ( media.hasAttribute( "mrl" ) == true )
61
        {
62
            QString mrl = media.attribute( "mrl" );
63 64 65 66

            //If in workspace: compute the path in workspace
            if ( mrl.startsWith( Workspace::workspacePrefix ) == true )
            {
67 68
                //Transforming the workspace://[path] into [project-path]/[path]
                QString     projectPath = VLMC_PROJECT_GET_STRING( "general/Workspace" );
69
                mrl = projectPath + mrl.mid( Workspace::workspacePrefix.length() );
70 71
            }
            Media*  m = addMedia( mrl );
72 73
            connect( m, SIGNAL( metaDataComputed( const Media* ) ),
                     this, SLOT( mediaLoaded( const Media* ) ), Qt::QueuedConnection );
74
            m_medias[mrl] = m;
75
            m_nbMediaToLoad.fetchAndAddAcquire( 1 );
76
        }
77
        media = media.nextSiblingElement();
78
    }
79 80 81 82
    const QDomElement   clips = doc.firstChildElement( "clips" );
    if ( clips.isNull() == true )
        return ;
    load( clips, this );
83
    mediaLoaded( NULL );
84
}
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

void
Library::saveProject( QXmlStreamWriter& project )
{
    QHash<QUuid, Clip*>::const_iterator     it = m_clips.begin();
    QHash<QUuid, Clip*>::const_iterator     end = m_clips.end();

    project.writeStartElement( "medias" );
    while ( it != end )
    {
        it.value()->getMedia()->save( project );
        ++it;
    }
    project.writeEndElement();
    project.writeStartElement( "clips" );
    save( project );
    project.writeEndElement();
102
    setCleanState( true );
103
}
104 105 106 107

void
Library::mediaLoaded( const Media* media )
{
108 109 110 111 112
    if ( media != NULL )
    {
        disconnect( media, SIGNAL( metaDataComputed( const Media* ) ),
                 this, SLOT( mediaLoaded( const Media* ) ) );
    }
113
    m_nbMediaToLoad.fetchAndAddAcquire( -1 );
114 115 116
    if ( m_nbMediaToLoad == 0 )
        emit projectLoaded();
}
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

void
Library::addMedia( Media* media )
{
    setCleanState( false );
    MediaContainer::addMedia( media );
}

Media*
Library::addMedia( const QFileInfo &fileInfo )
{
    Media* media = MediaContainer::addMedia( fileInfo );
    if ( media != NULL )
        setCleanState( false );
    return media;
}

bool
Library::addClip( Clip *clip )
{
    bool    ret = MediaContainer::addClip( clip );
    if ( ret != false )
        setCleanState( false );
    return ret;
}

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

void
Library::setCleanState( bool newState )
{
    if ( newState != m_cleanState )
    {
        m_cleanState = newState;
        emit cleanStateChanged( newState );
    }
}