Library.cpp 12 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
  * It can load and unload Clips (Clip.h/Clip.cpp)
28 29
  */

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

35
#include <QDebug>
36
#include <QDir>
37 38 39
#include <QDomElement>
#include <QHash>
#include <QMessageBox>
40
#include <QProgressDialog>
41 42
#include <QUuid>

Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
43 44
Library::Library()
{
45 46 47 48 49
    m_progressDialog = new QProgressDialog( tr("Importing files..."),
                                            tr("Cancel"), 0, m_loadingMedias, NULL);
    m_progressDialog->setWindowModality( Qt::WindowModal );
    m_progressDialog->setMinimumDuration( 1000 );
    m_nbLoadedMedias = 0;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
50 51
}

52
Media*
53
Library::media( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54
{
55
    return getElementByUuid( m_medias, uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
56 57
}

58 59 60 61 62 63
Media*
Library::temporaryMedia( const QUuid& uuid )
{
    return getElementByUuid( m_temporaryMedias, uuid );
}

64
Clip*
65
Library::clip( const QUuid& uuid )
66
{
67 68 69 70 71 72 73
    if ( m_medias.contains( uuid ) )
    {
        Media* media = m_medias.value( uuid );
        Clip* clip = new Clip( media->baseClip() );
        return clip;
    }

74 75 76 77 78 79 80 81 82
    QUuid mediaUuid;
    foreach( mediaUuid, m_medias.keys() )
    {
        Media* media = m_medias.value( mediaUuid );
        if ( media != NULL && media->clips()->contains( uuid ) )
            return dynamic_cast<Clip*>( getElementByUuid( *media->clips(), uuid ) );
    }
    return NULL;
}
83

84 85 86
Clip*
Library::clip( const QUuid& mediaUuid, const QUuid& clipUuid )
{
87 88 89 90 91 92 93 94
    if ( m_medias.contains( mediaUuid ) )
    {
        if ( m_medias.value( mediaUuid )->clips()->contains( clipUuid ) )
            return m_medias.value( mediaUuid )->clip( clipUuid );
        else
            return new Clip( m_medias.value( mediaUuid )->baseClip() );
    }
    return NULL;
95 96
}

97 98
void
Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
99
{
100
    deleteMedia( uuid );
101
    emit mediaRemoved( uuid );
102 103
}

104
void
105
Library::deleteMedia( const QUuid& uuid )
106
{
107
    if ( m_medias.contains( uuid ) )
108
    {
109 110 111 112 113
        disconnect( m_medias.value( uuid ),
                    SIGNAL( audioSpectrumComputed( const QUuid& ) ),
                    this,
                    SLOT( deleteMedia( const QUuid& ) ) );
        delete m_medias.take( uuid );
114
    }
115
    else if ( m_temporaryMedias.contains( uuid ) )
116
    {
117 118 119 120 121
        disconnect( m_medias.value( uuid ),
                    SIGNAL( audioSpectrumComputed( const QUuid& ) ),
                    this,
                    SLOT( deleteMedia( const QUuid& ) ) );
        delete m_temporaryMedias.take( uuid );
122
    }
123
    else if ( m_mediaToDelete.contains( uuid ) )
124
    {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        delete m_mediaToDelete.value( uuid );
    }
    else if ( m_medias.contains( uuid ) )
    {
        Media* media = m_medias.take( uuid );
        m_mediaToDelete.insert( uuid, media );
        connect( media,
                 SIGNAL( audioSpectrumComputed( const QUuid& ) ),
                 this,
                 SLOT( deleteMedia( const QUuid& ) ) );
    }
    else if ( m_temporaryMedias.contains( uuid ) )
    {
        Media* media = m_temporaryMedias.take( uuid );
        m_mediaToDelete.insert( uuid, media );
        connect( media,
                 SIGNAL( audioSpectrumComputed( const QUuid& ) ),
                 this,
                 SLOT( deleteMedia( const QUuid& ) ) );
144
    }
145 146
}

147
void
148
Library::addMedia( const QFileInfo& fileInfo )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
149
{
150 151
    Media* media = new Media( fileInfo.filePath() );
    m_nbLoadedMedias++;
152

Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
153 154
    QUuid id;
    foreach( id, m_medias.keys() )
155 156
        if ( m_medias.value( id )->getFileInfo()->filePath() ==
             media->getFileInfo()->filePath() )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
157
        {
158
            delete media;
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
159 160
            return;
        }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    connect( media,
             SIGNAL( metaDataComputed( Media* ) ),
             this,
             SLOT( metaDataComputed( Media* ) ), Qt::QueuedConnection );
    connect( media,
             SIGNAL( snapshotComputed( Media* ) ),
             this,
             SLOT( snapshotComputed( Media* ) ), Qt::QueuedConnection );

    MetaDataManager::getInstance()->computeMediaMetadata( media );

    m_temporaryMedias[media->getUuid()] = media;

    m_progressDialog->setValue(m_nbLoadedMedias);
    emit newMediaLoaded( media->getUuid() );
177 178
}

179 180
void
Library::addClip( Clip* clip )
181
{
182 183
    Media* media = m_medias[clip->getParent()->getUuid()];
    media->addClip( clip );
184 185
}

186 187
void
Library::loadMedia( const QString& path, const QUuid& uuid )
188 189 190 191 192 193 194 195 196 197 198 199
{
    Media*  it;
    foreach ( it, m_medias )
    {
        if ( it->getFileInfo()->absoluteFilePath() == path )
        {
            m_medias.remove( it->getUuid() );
            m_medias[uuid] = it;
            it->setUuid( uuid );
            return ;
        }
    }
200 201
    if ( QFile::exists( path ) == false )
    {
202 203
        QMessageBox::warning( NULL, tr( "Import error" ),
                              tr( "Can't open file :" ) + path );
204 205
        return ;
    }
206
    Media*  media = new Media( path, uuid );
207 208 209 210
    connect( media,
             SIGNAL( metaDataComputed( Media* ) ),
             this,
             SLOT( metaDataComputed( Media* ) ) );
211 212 213 214
    MetaDataManager::getInstance()->computeMediaMetadata( media );
    m_medias[uuid] = media;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
void
Library::metaDataComputed( Media* media )
{
    disconnect( media,
                SIGNAL( metaDataComputed( Media* ) ),
                this,
                SLOT( metaDataComputed( Media* ) ) );
}

void
Library::snapshotComputed( Media *media )
{
    disconnect( media,
                SIGNAL( snapshotComputed( Media* ) ),
                this,
                SLOT( snapshotComputed( Media* ) ) );
    emit updateMediaRequested( media->getUuid() );
}

234 235
bool
Library::mediaAlreadyLoaded( const QString& filePath )
236
{
237 238 239 240 241 242 243
    QUuid id;
    foreach( id, m_temporaryMedias.keys() )
    {
        Media* media = m_temporaryMedias.value( id );
        if ( media->getFileInfo()->filePath() == filePath )
            return true;
    }
244 245 246 247 248 249
    foreach( id, m_medias.keys() )
    {
        Media* media = m_medias.value( id );
        if ( media->getFileInfo()->filePath() == filePath )
            return true;
    }
250 251 252 253 254 255 256 257
    return false;
}

bool
Library::mediaAlreadyLoaded( const QFileInfo& fileInfo )
{
    QUuid id;
    foreach( id, m_temporaryMedias.keys() )
258
    {
259 260
        Media* media = m_temporaryMedias.value( id );
        if ( media->getFileInfo()->filePath() == fileInfo.filePath() )
261 262
            return true;
    }
263 264 265 266 267 268
    foreach( id, m_medias.keys() )
    {
        Media* media = m_medias.value( id );
        if ( media->getFileInfo()->filePath() == fileInfo.filePath() )
            return true;
    }
269 270 271
    return false;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
void
Library::loadFile( const QFileInfo& fileInfo, int loadingMedias )
{
    if ( !fileInfo.isDir() )
    {
        if ( loadingMedias == 1 )
        {
            m_progressDialog->setMaximum( 1 );
            m_progressDialog->setValue( 0 );
            m_loadingMedias = 1;
            m_nbLoadedMedias = 0;
        }

        if ( !mediaAlreadyLoaded( fileInfo ) )
            addMedia( fileInfo );
        else
288
        {
289
            m_progressDialog->setMaximum( m_loadingMedias-- );
290
            m_progressDialog->setValue( ++m_nbLoadedMedias );
291
        }
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    }
    else
    {
        QDir dir = QDir( fileInfo.filePath() );
        QFileInfoList list = dir.entryInfoList( m_filters );
        QFileInfo file;

        m_loadingMedias = list.count();
        m_nbLoadedMedias = 0;
        m_progressDialog->setMaximum( m_loadingMedias );
        m_progressDialog->setValue( 0 );
        foreach( file, list )
            loadFile( file, m_loadingMedias );
    }
}

308 309
void
Library::loadProject( const QDomElement& medias )
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
    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();
        }
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        //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
        {
356
            loadMedia( path, uuid );
357 358
        }

359 360
        elem = elem.nextSibling().toElement();
    }
361
    emit projectLoaded();
362 363
}

364 365
void
Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
366 367 368 369 370 371 372 373 374 375 376 377 378
{
    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" );

379 380
        QDomCharacterData text;
        text = doc.createTextNode( it.value()->getFileInfo()->absoluteFilePath() );
381 382

        QDomElement uuid = doc.createElement( "uuid" );
383
        QDomCharacterData text2 = doc.createTextNode( it.value()->getUuid().toString() );
384 385 386 387 388 389 390

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

        media.appendChild( mrl );
        media.appendChild( uuid );
    }
391
    rootNode.appendChild( medias );
392
}
393

394 395
void
Library::clear()
396 397 398 399 400 401 402 403 404 405 406 407
{
    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();
}
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

void
Library::deleteTemporaryMedias()
{
    QUuid uuid;
    foreach( uuid, m_temporaryMedias.keys() )
        deleteMedia( uuid );
}

void
Library::importDone()
{
    QUuid id;
    foreach( id, m_temporaryMedias.keys() )
    {
        Media* media = m_temporaryMedias.value( id );
        m_medias.insert( media->getUuid(), media );
        emit newMediaImported( media->getUuid() );
    }
    m_temporaryMedias.clear();
}
429 430 431 432 433 434 435 436 437 438 439 440 441

void
Library::removeClip( const QUuid& mediaId, const QUuid& clipId )
{
    Media*  med = 0;
    if ( m_medias.contains( mediaId ) )
        med = m_medias[mediaId];
    else
        return;

    if ( med->clips()->contains( clipId ) )
        med->removeClip( clipId );
}