Library.cpp 6.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 <QtDebug>
36 37 38
#include <QDomElement>
#include <QHash>
#include <QUuid>
39
#include <QXmlStreamWriter>
40 41 42

void
Library::loadProject( const QDomElement& medias )
43 44 45 46 47 48 49 50 51 52
{
    if ( medias.isNull() == true || medias.tagName() != "medias" )
    {
        qWarning() << "Invalid medias node";
        return ;
    }

    QDomElement elem = medias.firstChild().toElement();
    while ( elem.isNull() == false )
    {
53
        QList<QDomElement>  clipList;
54 55 56 57 58 59 60 61 62 63 64
        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();
65 66 67 68 69 70 71 72 73
            else if ( tagName == "clips" )
            {
                QDomElement clip = mediaProperty.firstChild().toElement();
                while ( clip.isNull() == false )
                {
                    clipList.push_back( clip );
                    clip = clip.nextSibling().toElement();
                }
            }
74 75 76 77
            else
                qWarning() << "Unknown field" << tagName;
            mediaProperty = mediaProperty.nextSibling().toElement();
        }
78 79 80
        //FIXME: This is verry redondant...
        if ( mediaAlreadyLoaded( path ) == true )
        {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
81 82
            QHash<QUuid, Clip*>::iterator   it = m_clips.begin();
            QHash<QUuid, Clip*>::iterator   end = m_clips.end();
83 84 85

            for ( ; it != end; ++it )
            {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
86
                if ( it.value()->getMedia()->fileInfo()->absoluteFilePath() == path )
87
                {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
88 89 90 91
                    Clip*   clip = it.value();
                    clip->setUuid( QUuid( uuid ) );
                    m_clips.erase( it );
                    m_clips[clip->uuid()] = clip;
92 93 94 95 96 97
                    break ;
                }
            }
        }
        else
        {
98 99 100 101 102
            if ( addMedia( path, uuid ) == false )
            {
                elem = elem.nextSibling().toElement();
                continue ;
            }
103
        }
104 105 106 107 108 109 110 111 112 113 114 115
        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 != "" )
                    {
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
116 117 118 119 120
                        if ( m_clips.contains( uuid ) == false )
                            continue ;
                        Clip* parentClip = m_clips[QUuid( uuid )];
                        Clip* clip = new Clip( parentClip, beg.toInt(), end.toInt(), QUuid( clipUuid ) );
                        parentClip->addSubclip( clip );
121 122 123 124
                    }
                }
            }
        }
125 126
        elem = elem.nextSibling().toElement();
    }
127
    emit projectLoaded();
128 129
}

130 131 132
//void
//Library::saveProject( QDomDocument& doc, QDomElement& rootNode )
//{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
//    QHash<QUuid, Media*>::iterator          it = m_clips.begin();
//    QHash<QUuid, Media*>::iterator          end = m_clips.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;
//        text = doc.createTextNode( it.value()->fileInfo()->absoluteFilePath() );
//
//        QDomElement uuid = doc.createElement( "uuid" );
//        QDomCharacterData text2 = doc.createTextNode( it.value()->baseClip()->uuid().toString() );
//
//        mrl.appendChild( text );
//        uuid.appendChild( text2 );
//
//        media.appendChild( mrl );
//        media.appendChild( uuid );
//        //Creating the clip branch
//        if ( it.value()->clipsCount() != 0 )
//        {
//            QDomElement clips = doc.createElement( "clips" );
//            foreach( Clip* c, it.value()->clips().values() )
//            {
//                QDomElement clip = doc.createElement( "clip" );
//                clip.setAttribute( "begin", c->begin() );
//                clip.setAttribute( "end", c->end() );
//                clip.setAttribute( "uuid", c->uuid() );
//                clip.setAttribute( "parentUuid", c->getMedia()->baseClip()->uuid() );
//                clips.appendChild( clip );
//            }
//            media.appendChild( clips );
//        }
//    }
//    rootNode.appendChild( medias );
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
//    #warning "FIXME: Project saving";
//}

void
Library::saveProject( QXmlStreamWriter& project )
{
    QHash<QUuid, Clip*>::const_iterator     it = m_clips.begin();
    QHash<QUuid, Clip*>::const_iterator     end = m_clips.end();

    project.writeStartElement( "medias" );
    while ( it != end )
    {
        it.value()->getMedia()->save( project );
        ++it;
    }
    project.writeEndElement();
    project.writeStartElement( "clips" );
    save( project );
    project.writeEndElement();
192
}