MediaContainer.cpp 6.84 KB
Newer Older
1 2 3
/*****************************************************************************
 * MediaContainer.cpp: Implements the library basics
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *****************************************************************************/

#include <QHash>
24
#include <QDomElement>
25 26
#include <QUuid>

27
#include "Library.h"
28
#include "MediaContainer.h"
29 30 31
#include "Media/Clip.h"
#include "Media/Media.h"
#include "Metadata/MetaDataManager.h"
32
#include "Settings/Settings.h"
33 34
#include "Tools/VlmcDebug.h"
#include "Project/Workspace.h"
35

36
MediaContainer::MediaContainer( Clip* parent /*= NULL*/ ) : m_parent( parent )
37 38 39
{
}

40 41 42 43 44 45 46
MediaContainer::~MediaContainer()
{
    foreach ( Clip* c, m_clips.values() )
        delete c;
    m_clips.clear();
}

47 48 49
Clip*
MediaContainer::clip( const QUuid& uuid )
{
50 51 52
    QHash<QUuid, Clip*>::iterator   it = m_clips.find( uuid );
    if ( it != m_clips.end() )
        return it.value();
53 54 55
    return NULL;
}

56 57 58 59
Clip*
MediaContainer::clip( const QString &uuid )
{
    MediaContainer      *mc = this;
60
    Clip                *clip = NULL;
61 62 63 64 65 66 67 68 69 70 71 72
    QStringList         ids = uuid.split( '/' );

    foreach ( QString id, ids )
    {
        clip = mc->clip( QUuid( id ) );
        if ( clip == NULL )
            return NULL;
        mc = clip->getChilds();
    }
    return clip;
}

73
void
74
MediaContainer::addMedia( Media *media )
75
{
76 77
    m_clips[media->baseClip()->uuid()] = media->baseClip();
    emit newClipLoaded( media->baseClip() );
78 79
}

80
Media*
81
MediaContainer::addMedia( const QFileInfo& fileInfo )
82 83
{
    if ( QFile::exists( fileInfo.absoluteFilePath() ) == false )
84 85
    {
        vlmcCritical() << "Can't add" << fileInfo.absoluteFilePath() << ": File not found";
86
        return NULL;
87
    }
88
    foreach( Clip* it, m_clips.values() )
89
    {
90 91 92
        if ( (*it->getMedia()->fileInfo()) == fileInfo )
        {
            vlmcWarning() << "Ignoring aleady imported media" << fileInfo.absolutePath();
93
            return NULL;
94
        }
95
    }
96
    Media* media = new Media( fileInfo.filePath() );
97
    return media;
98 99 100 101 102
}

bool
MediaContainer::mediaAlreadyLoaded( const QFileInfo& fileInfo )
{
103
    foreach( Clip* clip, m_clips.values() )
104
    {
105
        if ( clip->getMedia()->fileInfo()->filePath() == fileInfo.filePath() )
106 107 108 109 110
            return true;
    }
    return false;
}

111
bool
112 113
MediaContainer::addClip( Clip* clip )
{
114 115 116
    foreach ( Clip* c, m_clips.values() )
    {
        if ( clip->uuid() == c->uuid() ||
117
             ( clip->getMedia()->fileInfo() == c->getMedia()->fileInfo() &&
118
                    ( clip->begin() == c->begin() && clip->end() == c->end() ) ) )
119
        {
120
            vlmcWarning() << "Clip already loaded.";
121
            return false;
122
        }
123
    }
124 125
    m_clips[clip->uuid()] = clip;
    emit newClipLoaded( clip );
126
    return true;
127 128
}

129 130 131
void
MediaContainer::clear()
{
132 133
    QHash<QUuid, Clip*>::iterator  it = m_clips.begin();
    QHash<QUuid, Clip*>::iterator  end = m_clips.end();
134 135 136

    while ( it != end )
    {
137
        emit clipRemoved( it.key() );
138 139
        it.value()->clear();
        it.value()->deleteLater();
140 141
        ++it;
    }
142
    m_clips.clear();
143 144 145 146 147
}

void
MediaContainer::removeAll()
{
148 149
    QHash<QUuid, Clip*>::iterator  it = m_clips.begin();
    QHash<QUuid, Clip*>::iterator  end = m_clips.end();
150 151 152

    while ( it != end )
    {
153
        emit clipRemoved( it.key() );
154 155
        ++it;
    }
156
    m_clips.clear();
157 158
}

159 160
void
MediaContainer::deleteClip( const QUuid &uuid )
161
{
162
    QHash<QUuid, Clip*>::iterator  it = m_clips.find( uuid );
163
    if ( it != m_clips.end() )
164
    {
165 166
        Clip* clip = it.value();
        m_clips.remove( uuid );
167
        emit clipRemoved( uuid );
168
        // don't use delete as the clip may be used in the slot that'll handle clipRemoved signal.
169
        clip->deleteLater();
170 171 172
    }
}

173 174 175 176 177 178 179 180
const QHash<QUuid, Clip*>&
MediaContainer::clips() const
{
    return m_clips;
}

Clip*
MediaContainer::getParent()
181
{
182
    return m_parent;
183 184
}

185 186
quint32
MediaContainer::count() const
187
{
188
    return m_clips.size();
189
}
190 191

void
192
MediaContainer::saveContainer( QXmlStreamWriter &project ) const
193 194 195 196
{
    foreach ( Clip* c, m_clips.values() )
        c->save( project );
}
197

198 199
bool
MediaContainer::loadContainer( const QDomElement& clips, MediaContainer *parentMC )
200
{
201
    QDomElement clip = clips.firstChildElement();
202 203 204 205 206 207

    while ( clip.isNull() == false )
    {
        QString     uuid = clip.attribute( "uuid" );
        QString     metatags = clip.attribute( "metatags" );
        QString     notes = clip.attribute( "notes" );
208
        Clip        *c = NULL;
209 210 211 212

        if ( clip.hasAttribute( "media" ) == true )
        {
            QString media = clip.attribute( "media" );
213 214 215

            //Handle workspace stuff.
            if ( media.startsWith( Workspace::workspacePrefix ) == true )
216
                media = media.replace( Workspace::workspacePrefix, VLMC_PROJECT_GET_STRING( "vlmc/Workspace" ) );
217
            if ( m_medias.contains( media ) == true )
218
            {
219
                Media*  m = m_medias[media];
220
                c = new Clip( m, 0, -1, uuid );
221
                m->setBaseClip( c );
222 223
                addClip( c );
            }
224 225
            else
            {
226
                vlmcWarning() << "Can't find parent media:" << media;
227
                clip = clip.nextSiblingElement();
228 229
                continue ;
            }
230 231 232 233 234 235 236 237 238 239 240 241 242 243
        }
        else
        {
            QString     parent = clip.attribute( "parent" );
            QString     begin = clip.attribute( "begin" );
            QString     end = clip.attribute( "end" );

            if ( parent.isEmpty() == false )
            {
                Clip*   p = parentMC->clip( QUuid( parent ) );
                c = new Clip( p, begin.toLongLong(), end.toLongLong(), uuid );
                addClip( c );
            }
        }
244 245 246 247 248 249 250
        if ( c != NULL )
        {
            if ( metatags.isEmpty() == false )
                c->setMetaTags( metatags.split( ',' ) );
            c->setNotes( notes );
            QDomElement subClips = clip.firstChildElement( "subClips" );
            if ( subClips.isNull() == false )
251
                c->getChilds()->loadContainer( subClips, this );
252
        }
253
        clip = clip.nextSiblingElement();
254
    }
255
    return true;
256
}