Library.cpp 7.7 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
#include <QtDebug>
30 31
#include <QMessageBox>

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
32
#include "Library.h"
33
#include "MetaDataManager.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
34 35 36 37 38

Library::Library()
{
}

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

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

49
void        Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
50
{
51 52
    QHash<QUuid, Media*>::iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
53
        return ;
54
    Media*  media = it.value();
55
    emit mediaRemoved( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57 58
    //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;
59
    m_medias.erase( it );
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    //delete media;
    deleteMedia( media );
}

void        Library::deleteMedia( Media *media )
{
    if ( media->getMetadata() == Media::ParsedWithAudioSpectrum )
        delete media;
    else
    {
        m_mediaToDelete.append( media );
        connect( media, SIGNAL( audioSpectrumComputed( Media* ) ), this, SLOT( deleteMedia( Media* ) ) );
    }
}

void        Library::audioSpectrumComputed( Media *media )
{
    disconnect( media, SIGNAL( audioSpectrumComputed( Media* ) ), this, SLOT( audioSpectrumComputed( Media* ) ) );
    if ( m_mediaToDelete.contains( media ) )
    {
        m_mediaToDelete.removeAll( media );
        delete media;
    }
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
83 84
}

85 86
void        Library::metaDataComputed( Media* media )
{
87
    emit newMediaLoaded( media );
88
    Clip* clip = new Clip( media );
89
    m_clips[media->getUuid()] = clip;
90 91
}

92
void        Library::newMediaLoadingAsked( const QString& filePath, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
93
{
94
    Media*   media;
95 96
    if ( mediaAlreadyLoaded( filePath ) == true )
        return ;
97
    media = new Media( filePath, uuid );
98
    m_medias[media->getUuid()] = media;
99
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ), Qt::DirectConnection );
100
    emit metadataRequired( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
101
}
102

103 104
void        Library::addMedia( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
105 106
    QUuid id;
    foreach( id, m_medias.keys() )
107 108 109
    {
        qDebug() << m_medias.value( id )->getFileInfo()->filePath();
        qDebug() << media->getFileInfo()->filePath();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
110 111
        if ( m_medias.value( id )->getFileInfo()->filePath() == media->getFileInfo()->filePath() )
        {
112
            deleteMedia( media );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
113 114
            return;
        }
115
    }
116 117 118 119
    m_medias[media->getUuid()] = media;
    metaDataComputed( media );
}

120 121 122 123 124 125 126 127 128 129 130 131 132
void        Library::loadMedia( const QString& path, const QUuid& uuid )
{
    Media*  it;
    foreach ( it, m_medias )
    {
        if ( it->getFileInfo()->absoluteFilePath() == path )
        {
            m_medias.remove( it->getUuid() );
            m_medias[uuid] = it;
            it->setUuid( uuid );
            return ;
        }
    }
133 134 135 136 137
    if ( QFile::exists( path ) == false )
    {
        QMessageBox::warning( NULL, tr( "Import error" ), tr( "Can't open file :" ) + path );
        return ;
    }
138 139 140 141 142 143
    Media*  media = new Media( path, uuid );
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ) );
    MetaDataManager::getInstance()->computeMediaMetadata( media );
    m_medias[uuid] = media;
}

144 145 146 147 148 149 150 151 152 153 154
bool        Library::mediaAlreadyLoaded( const QString& filePath )
{
    Media*   media;
    foreach ( media, m_medias )
    {
        if ( media->getFileInfo()->absoluteFilePath() == filePath )
            return true;
    }
    return false;
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
void        Library::loadProject( const QDomElement& medias )
{
    if ( medias.isNull() == true || medias.tagName() != "medias" )
    {
        qWarning() << "Invalid medias node";
        return ;
    }

    QDomElement elem = medias.firstChild().toElement();
    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();
        }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        //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
        {
202
            loadMedia( path, uuid );
203 204
        }

205 206
        elem = elem.nextSibling().toElement();
    }
207
    emit projectLoaded();
208 209
}

210
void        Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
    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 );
    }
235
    rootNode.appendChild( medias );
236
}
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

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

    while ( it != end )
    {
        emit mediaRemoved( it.key() );
        delete it.value();
        ++it;
    }
    m_medias.clear();
    QHash<QUuid, Clip*>::iterator  it2 = m_clips.begin();
    QHash<QUuid, Clip*>::iterator  end2 = m_clips.end();
    while ( it2 != end2 )
    {
        delete it2.value();
        ++it2;
    }
    m_clips.clear();
}