Library.cpp 6.08 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 50
    QHash<QUuid, Media*>::iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
51
        return ;
52
    Media*  media = it.value();
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 );
58
    delete media;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
59 60
}

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

77
void        Library::newMediaLoadingAsked( const QString& filePath, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
78
{
79
    Media*   media;
80 81
    if ( mediaAlreadyLoaded( filePath ) == true )
        return ;
82
    media = new Media( filePath, uuid );
83
    m_medias[media->getUuid()] = media;
84
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ), Qt::DirectConnection );
85
    emit metadataRequired( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
86
}
87

88 89
void        Library::addMedia( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
90 91 92 93 94 95 96
    QUuid id;
    foreach( id, m_medias.keys() )
        if ( m_medias.value( id )->getFileInfo()->filePath() == media->getFileInfo()->filePath() )
        {
            delete media;
            return;
        }
97 98 99 100
    m_medias[media->getUuid()] = media;
    metaDataComputed( media );
}

101 102
bool        Library::mediaAlreadyLoaded( const QString& filePath )
{
103
    //FIXME: Is this necessary ??
104 105 106 107 108 109 110 111 112
    Media*   media;
    foreach ( media, m_medias )
    {
        if ( media->getFileInfo()->absoluteFilePath() == filePath )
            return true;
    }
    return false;
}

113 114
void        Library::loadProject( const QDomElement& medias )
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
115
    qDebug() << "Loading (library part) project";
116 117 118 119 120 121 122
    if ( medias.isNull() == true || medias.tagName() != "medias" )
    {
        qWarning() << "Invalid medias node";
        return ;
    }

    QDomElement elem = medias.firstChild().toElement();
123
    m_nbMediasToLoad = 0;
124 125
    while ( elem.isNull() == false )
    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126
        qDebug() << "Iterating over element";
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        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();
        }
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        //FIXME: This is verry redondant...
        if ( mediaAlreadyLoaded( path ) == true )
        {
            Media*   media;
            QHash<QUuid, Media*>::iterator   it = m_medias.begin();
            QHash<QUuid, Media*>::iterator   end = m_medias.end();

            for ( ; it != end; ++it )
            {
                if ( it.value()->getFileInfo()->absoluteFilePath() == path )
                {
                    media = it.value();
                    media->setUuid( QUuid( uuid ) );
                    m_medias.erase( it );
                    m_medias[media->getUuid()] = media;
                    break ;
                }
            }
        }
        else
        {
            m_nbMediasToLoad.fetchAndAddAcquire( 1 );
            newMediaLoadingAsked( path, uuid );
        }

167 168 169 170
        elem = elem.nextSibling().toElement();
    }
}

171
void        Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
{
    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 );
    }
196
    rootNode.appendChild( medias );
197
}