Library.cpp 8.72 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
51
52
53
    QHash<QUuid, Media*>::const_iterator   it = m_medias.find( uuid );
    if ( it == m_medias.end() )
        return NULL;
    return *it;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
54
55
}

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

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

77
78
79
Clip*
Library::clip( const QUuid& mediaUuid, const QUuid& clipUuid )
{
80
81
    if ( m_medias.contains( mediaUuid ) )
    {
82
        if ( m_medias.value( mediaUuid )->clips().contains( clipUuid ) )
83
84
            return m_medias.value( mediaUuid )->clip( clipUuid );
        else
85
            m_medias.value( mediaUuid )->baseClip();
86
87
    }
    return NULL;
88
89
}

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

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

104
bool
105
Library::addMedia( const QFileInfo& fileInfo, const QString& uuid )
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
106
{
107
108
    if ( QFile::exists( fileInfo.absoluteFilePath() ) == false )
        return false;
109
    Media* media = new Media( fileInfo.filePath(), uuid );
110

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


void
Library::addMedia( Media *media )
{
128
    m_medias[media->uuid()] = media;
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
129
    emit newMediaLoaded( media );
130
131
}

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

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

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

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

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

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

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

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

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

294
295
296
297
298
299
300
301
302
void
Library::removeClip( const QUuid& mediaId, const QUuid& clipId )
{
    Media*  med = 0;
    if ( m_medias.contains( mediaId ) )
        med = m_medias[mediaId];
    else
        return;

303
    if ( med->clips().contains( clipId ) )
304
305
        med->removeClip( clipId );
}