Library.cpp 8.6 KB
Newer Older
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
1 2 3
/*****************************************************************************
 * Library.cpp: Multimedia library
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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 45 46
Library::Library()
{
}

47
Media*
48
Library::media( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
49
{
50
    return getElementByUuid( m_medias, uuid );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
51 52
}

53
Clip*
54
Library::clip( const QUuid& uuid )
55
{
56 57 58
    if ( m_medias.contains( uuid ) )
    {
        Media* media = m_medias.value( uuid );
59 60
        if ( media->baseClip() == NULL )
            return NULL;
61 62 63 64
        Clip* clip = new Clip( media->baseClip() );
        return clip;
    }

65 66 67 68 69
    QUuid mediaUuid;
    foreach( mediaUuid, m_medias.keys() )
    {
        Media* media = m_medias.value( mediaUuid );
        if ( media != NULL && media->clips()->contains( uuid ) )
70
            return getElementByUuid( *media->clips(), uuid );
71 72 73
    }
    return NULL;
}
74

75 76 77
Clip*
Library::clip( const QUuid& mediaUuid, const QUuid& clipUuid )
{
78 79 80 81 82 83 84 85
    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;
86 87
}

88 89
void
Library::removingMediaAsked( const QUuid& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
90
{
91
    deleteMedia( uuid );
92
    emit mediaRemoved( uuid );
93 94
}

95
void
96
Library::deleteMedia( const QUuid& uuid )
97
{
98
    if ( m_medias.contains( uuid ) )
99
        delete m_medias.take( uuid );
100 101
}

102
void
103
Library::addMedia( const QFileInfo& fileInfo, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
104
{
105
    Media* media = new Media( fileInfo.filePath(), uuid );
106

107 108
    foreach( Media* it, m_medias.values() )
    {
109
        if ( it->fileInfo()->filePath() == media->fileInfo()->filePath() )
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
110
        {
111
            delete media;
Geoffroy Lacarriere's avatar
Geoffroy Lacarriere committed
112 113
            return;
        }
114
    }
115
    MetaDataManager::getInstance()->computeMediaMetadata( media );
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
116 117 118 119 120 121 122
    addMedia( media );
}


void
Library::addMedia( Media *media )
{
123
    m_medias[media->uuid()] = media;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
124
    emit newMediaLoaded( media );
125 126
}

127 128
void
Library::addClip( Clip* clip )
129
{
130
    Media* media = m_medias[clip->getParent()->uuid()];
131
    media->addClip( clip );
132 133
}

134 135 136
bool
Library::mediaAlreadyLoaded( const QFileInfo& fileInfo )
{
137
    foreach( Media* media, m_medias.values() )
138
    {
139
        if ( media->fileInfo()->filePath() == fileInfo.filePath() )
140 141
            return true;
    }
142 143 144
    return false;
}

145 146
void
Library::loadProject( const QDomElement& medias )
147 148 149 150 151 152 153 154 155 156
{
    if ( medias.isNull() == true || medias.tagName() != "medias" )
    {
        qWarning() << "Invalid medias node";
        return ;
    }

    QDomElement elem = medias.firstChild().toElement();
    while ( elem.isNull() == false )
    {
157
        QList<QDomElement>  clipList;
158 159 160 161 162 163 164 165 166 167 168
        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();
169 170 171 172 173 174 175 176 177
            else if ( tagName == "clips" )
            {
                QDomElement clip = mediaProperty.firstChild().toElement();
                while ( clip.isNull() == false )
                {
                    clipList.push_back( clip );
                    clip = clip.nextSibling().toElement();
                }
            }
178 179 180 181
            else
                qWarning() << "Unknown field" << tagName;
            mediaProperty = mediaProperty.nextSibling().toElement();
        }
182 183 184 185 186 187 188 189 190
        //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 )
            {
191
                if ( it.value()->fileInfo()->absoluteFilePath() == path )
192 193 194 195
                {
                    media = it.value();
                    media->setUuid( QUuid( uuid ) );
                    m_medias.erase( it );
196
                    m_medias[media->uuid()] = media;
197 198 199 200 201 202
                    break ;
                }
            }
        }
        else
        {
203
            addMedia( path, uuid );
204
        }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        if ( clipList.size() != 0 )
        {
            foreach( QDomElement clip, clipList )
            {
                QString parentUuid = clip.attribute( "parentUuid", "");
                if ( parentUuid != "" && parentUuid == uuid )
                {
                    QString beg = clip.attribute( "begin", "" );
                    QString end = clip.attribute( "end", "" );
                    QString clipUuid = clip.attribute( "uuid", "" );
                    if ( beg != "" && end != "" && uuid != "" )
                    {
                        Media* media = m_medias[QUuid( uuid )];
                        if ( media != 0 )
                        {
                            Clip* clip = new Clip( media, beg.toInt(), end.toInt(), QUuid( clipUuid ) );
                            media->addClip( clip );
                        }
                    }
                }
            }
        }
227

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

233 234
void
Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
235 236 237 238 239 240 241 242 243 244 245 246 247
{
    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" );

248
        QDomCharacterData text;
249
        text = doc.createTextNode( it.value()->fileInfo()->absoluteFilePath() );
250 251

        QDomElement uuid = doc.createElement( "uuid" );
252
        QDomCharacterData text2 = doc.createTextNode( it.value()->uuid().toString() );
253 254 255 256 257 258

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

        media.appendChild( mrl );
        media.appendChild( uuid );
259 260 261 262 263 264 265
        //Creating the clip branch
        if ( it.value()->clips()->size() != 0 )
        {
            QDomElement clips = doc.createElement( "clips" );
            foreach( Clip* c, it.value()->clips()->values() )
            {
                QDomElement clip = doc.createElement( "clip" );
266 267 268
                clip.setAttribute( "begin", c->begin() );
                clip.setAttribute( "end", c->end() );
                clip.setAttribute( "uuid", c->uuid() );
269
                clip.setAttribute( "parentUuid", c->getParent()->uuid() );
270 271 272 273
                clips.appendChild( clip );
            }
            media.appendChild( clips );
        }
274
    }
275
    rootNode.appendChild( medias );
276
}
277

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

293 294 295 296 297 298 299 300 301 302 303 304
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 );
}