Library.cpp 4.74 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Library.cpp: Multimedia library
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * 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 28
  */

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
29 30 31 32 33
#include <QtDebug>
#include "Library.h"

Library::Library()
{
34
    m_nbMediasToLoad = -1;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
35 36
}

37
Media*          Library::getMedia( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
38
{
39
    return getElementByUuid( m_medias, uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
40 41
}

42
Clip*           Library::getClip( const QUuid& uuid )
43
{
44
    return getElementByUuid( m_clips, uuid );
45 46
}

47
void        Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
48
{
49
    QMutexLocker locker( &m_mutex );
50 51
    QHash<QUuid, Media*>::iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
52
        return ;
53
    emit mediaRemoved( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54 55 56
    //TODO: this is obviously a memleak, but at the moment, the library depends on the clip to work,
    //and won't be abble to remove the ListViewMediaItem without it.
    //delete *it;
57
    m_medias.erase( it );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
58 59
}

60 61
void        Library::metaDataComputed( Media* media )
{
62
    Clip* clip = new Clip( media );
63
    m_clips[media->getUuid()] = clip;
64
    emit newClipLoaded( clip );
65 66 67 68
    if ( m_nbMediasToLoad >= 0 )
    {
        m_nbMediasToLoad.fetchAndAddAcquire( -1 );
        if ( m_nbMediasToLoad <= 0 )
69
        {
70
            emit projectLoaded();
71
        }
72
    }
73 74
}

75
void        Library::newMediaLoadingAsked( const QString& filePath, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
76
{
77
    //FIXME: Is this necessary ??
78
    QMutexLocker locker( &m_mutex );
79 80
    Media*   media;
    foreach ( media, m_medias )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81
    {
82
        if ( media->getFileInfo()->absoluteFilePath() == filePath )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83 84
            return ;
    }
85
    media = new Media( filePath, uuid );
86
    m_medias[media->getUuid()] = media;
87
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ), Qt::DirectConnection );
88
    emit newMediaLoaded( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
89
}
90 91 92 93 94 95 96 97 98 99

void        Library::loadProject( const QDomElement& medias )
{
    if ( medias.isNull() == true || medias.tagName() != "medias" )
    {
        qWarning() << "Invalid medias node";
        return ;
    }

    QDomElement elem = medias.firstChild().toElement();
100
    m_nbMediasToLoad = 0;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    while ( elem.isNull() == false )
    {
        QDomElement mediaProperty = elem.firstChild().toElement();
        QString     path;
        QString     uuid;

        while ( mediaProperty.isNull() == false )
        {
            QString tagName = mediaProperty.tagName();
            if ( tagName == "path" )
                path = mediaProperty.text();
            else if ( tagName == "uuid" )
                uuid = mediaProperty.text();
            else
                qWarning() << "Unknown field" << tagName;
            mediaProperty = mediaProperty.nextSibling().toElement();
        }
118
        m_nbMediasToLoad.fetchAndAddAcquire( 1 );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
119
        newMediaLoadingAsked( path, uuid );
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
        elem = elem.nextSibling().toElement();
    }
}

void        Library::saveProject( QDomDocument& doc )
{
    QHash<QUuid, Media*>::iterator          it = m_medias.begin();
    QHash<QUuid, Media*>::iterator          end = m_medias.end();

    QDomElement medias = doc.createElement( "medias" );

    for ( ; it != end; ++it )
    {
        QDomElement media = doc.createElement( "media" );

        medias.appendChild( media );
        QDomElement mrl = doc.createElement( "path" );

        QDomCharacterData   text = doc.createTextNode( it.value()->getFileInfo()->absoluteFilePath() );

        QDomElement uuid = doc.createElement( "uuid" );
        QDomCharacterData   text2 = doc.createTextNode( it.value()->getUuid().toString() );

        mrl.appendChild( text );
        uuid.appendChild( text2 );

        media.appendChild( mrl );
        media.appendChild( uuid );
    }
    doc.appendChild( medias );
}