Library.cpp 8.17 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
  */

29
#include "Clip.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
30
#include "Library.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
31
#include "Media.h"
32
#include "MetaDataManager.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
33

34 35 36 37 38 39
#include <QDebug>
#include <QDomElement>
#include <QHash>
#include <QMessageBox>
#include <QUuid>

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
40 41 42 43
Library::Library()
{
}

44 45
Media*
Library::getMedia( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
46
{
47
    return getElementByUuid( m_medias, uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
48 49
}

50 51
Clip*
Library::getClip( const QUuid& uuid )
52
{
53 54 55 56
    Clip*   clip;
    clip = getElementByUuid( m_clips, uuid );

    return clip;
57 58
}

59 60
void
Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
{
62 63
    QHash<QUuid, Media*>::iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64
        return ;
65
    Media*  media = it.value();
66
    emit mediaRemoved( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
67 68 69
    //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;
70
    m_medias.erase( it );
71 72 73 74
    //delete media;
    deleteMedia( media );
}

75 76
void
Library::deleteMedia( Media *media )
77 78 79 80 81 82 83 84 85 86
{
    if ( media->getMetadata() == Media::ParsedWithAudioSpectrum )
        delete media;
    else
    {
        m_mediaToDelete.append( media );
        connect( media, SIGNAL( audioSpectrumComputed( Media* ) ), this, SLOT( deleteMedia( Media* ) ) );
    }
}

87 88
void
Library::audioSpectrumComputed( Media *media )
89 90 91 92 93 94 95
{
    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
96 97
}

98 99
void
Library::metaDataComputed( Media* media )
100
{
101
    emit newMediaLoaded( media );
102
    Clip* clip = new Clip( media );
103
    m_clips[media->getUuid()] = clip;
104 105 106 107 108 109 110 111 112 113
    //If the media have some clip, add then to m_clips
    const QHash<QUuid, Clip*>*    clips = media->clips();
    if ( clips->size() != 0 )
    {
        QHash<QUuid, Clip*>::const_iterator   it = clips->begin();
        QHash<QUuid, Clip*>::const_iterator   ed = clips->end();

        for ( ; it != ed; ++it )
            m_clips[it.key()] = it.value();
    }
114 115
}

116 117
void
Library::newMediaLoadingAsked( const QString& filePath, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
118
{
119
    Media*   media;
120 121
    if ( mediaAlreadyLoaded( filePath ) == true )
        return ;
122
    media = new Media( filePath, uuid );
123
    m_medias[media->getUuid()] = media;
124
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ), Qt::DirectConnection );
125
    emit metadataRequired( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
126
}
127

128 129
void
Library::addMedia( Media* media )
130
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
131 132
    QUuid id;
    foreach( id, m_medias.keys() )
133 134 135
    {
        qDebug() << m_medias.value( id )->getFileInfo()->filePath();
        qDebug() << media->getFileInfo()->filePath();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
136 137
        if ( m_medias.value( id )->getFileInfo()->filePath() == media->getFileInfo()->filePath() )
        {
138
            deleteMedia( media );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
139 140
            return;
        }
141
    }
142 143
    m_medias[media->getUuid()] = media;
    metaDataComputed( media );
144 145 146 147 148
}

void        Library::addClip( Clip* clip )
{
    m_clips[clip->getUuid()] = clip;
149 150
}

151 152
void
Library::loadMedia( const QString& path, const QUuid& uuid )
153 154 155 156 157 158 159 160 161 162 163 164
{
    Media*  it;
    foreach ( it, m_medias )
    {
        if ( it->getFileInfo()->absoluteFilePath() == path )
        {
            m_medias.remove( it->getUuid() );
            m_medias[uuid] = it;
            it->setUuid( uuid );
            return ;
        }
    }
165 166 167 168 169
    if ( QFile::exists( path ) == false )
    {
        QMessageBox::warning( NULL, tr( "Import error" ), tr( "Can't open file :" ) + path );
        return ;
    }
170 171 172 173 174 175
    Media*  media = new Media( path, uuid );
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ) );
    MetaDataManager::getInstance()->computeMediaMetadata( media );
    m_medias[uuid] = media;
}

176 177
bool
Library::mediaAlreadyLoaded( const QString& filePath )
178 179 180 181 182 183 184 185 186 187
{
    Media*   media;
    foreach ( media, m_medias )
    {
        if ( media->getFileInfo()->absoluteFilePath() == filePath )
            return true;
    }
    return false;
}

188 189
void
Library::loadProject( const QDomElement& medias )
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
    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();
        }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        //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
        {
236
            loadMedia( path, uuid );
237 238
        }

239 240
        elem = elem.nextSibling().toElement();
    }
241
    emit projectLoaded();
242 243
}

244 245
void
Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
{
    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 );
    }
270
    rootNode.appendChild( medias );
271
}
272

273 274
void
Library::clear()
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
    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();
}