Library.cpp 8.26 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
#include <QMessageBox>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
31 32 33
#include <QDomElement>
#include <QUuid>
#include <QHash>
34

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
35
#include "Library.h"
Ludovic Fauvet's avatar
Ludovic Fauvet committed
36 37
#include "Media.h"
#include "Clip.h"
38
#include "MetaDataManager.h"
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
39 40 41 42 43

Library::Library()
{
}

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

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

    return clip;
55 56
}

57
void        Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
58
{
59 60
    QHash<QUuid, Media*>::iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
61
        return ;
62
    Media*  media = it.value();
63
    emit mediaRemoved( uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
64 65 66
    //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;
67
    m_medias.erase( it );
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    //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
91 92
}

93 94
void        Library::metaDataComputed( Media* media )
{
95
    emit newMediaLoaded( media );
96
    Clip* clip = new Clip( media );
97
    m_clips[media->getUuid()] = clip;
98 99 100 101 102 103 104 105 106 107
    //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();
    }
108 109
}

110
void        Library::newMediaLoadingAsked( const QString& filePath, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
111
{
112
    Media*   media;
113 114
    if ( mediaAlreadyLoaded( filePath ) == true )
        return ;
115
    media = new Media( filePath, uuid );
116
    m_medias[media->getUuid()] = media;
117
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ), Qt::DirectConnection );
118
    emit metadataRequired( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
119
}
120

121 122
void        Library::addMedia( Media* media )
{
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
123 124
    QUuid id;
    foreach( id, m_medias.keys() )
125 126 127
    {
        qDebug() << m_medias.value( id )->getFileInfo()->filePath();
        qDebug() << media->getFileInfo()->filePath();
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
128 129
        if ( m_medias.value( id )->getFileInfo()->filePath() == media->getFileInfo()->filePath() )
        {
130
            deleteMedia( media );
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
131 132
            return;
        }
133
    }
134 135
    m_medias[media->getUuid()] = media;
    metaDataComputed( media );
136 137 138 139 140
}

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

143 144 145 146 147 148 149 150 151 152 153 154 155
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 ;
        }
    }
156 157 158 159 160
    if ( QFile::exists( path ) == false )
    {
        QMessageBox::warning( NULL, tr( "Import error" ), tr( "Can't open file :" ) + path );
        return ;
    }
161 162 163 164 165 166
    Media*  media = new Media( path, uuid );
    connect( media, SIGNAL( metaDataComputed( Media* ) ), this, SLOT( metaDataComputed( Media* ) ) );
    MetaDataManager::getInstance()->computeMediaMetadata( media );
    m_medias[uuid] = media;
}

167 168 169 170 171 172 173 174 175 176 177
bool        Library::mediaAlreadyLoaded( const QString& filePath )
{
    Media*   media;
    foreach ( media, m_medias )
    {
        if ( media->getFileInfo()->absoluteFilePath() == filePath )
            return true;
    }
    return false;
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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();
        }
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        //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
        {
225
            loadMedia( path, uuid );
226 227
        }

228 229
        elem = elem.nextSibling().toElement();
    }
230
    emit projectLoaded();
231 232
}

233
void        Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
    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 );
    }
258
    rootNode.appendChild( medias );
259
}
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

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();
}