Parser.cpp 3.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
27 28
#include "Parser.h"

29 30
#include <algorithm>

31
#include "medialibrary/IMediaLibrary.h"
32 33
#include "Media.h"
#include "File.h"
34
#include "utils/ModificationsNotifier.h"
35

36 37 38
namespace medialibrary
{

39
Parser::Parser( MediaLibrary* ml )
40 41
    : m_ml( ml )
    , m_callback( ml->getCb() )
42
    , m_notifier( ml->getNotifier() )
43 44 45
    , m_opToDo( 0 )
    , m_opDone( 0 )
    , m_percent( 0 )
46 47 48
{
}

49 50
Parser::~Parser()
{
51
    stop();
52 53
}

54
void Parser::addService( ServicePtr service )
55
{
56 57
    service->initialize( m_ml, this );
    m_services.push_back( std::move( service ) );
58 59
}

60
void Parser::parse( std::shared_ptr<Media> media, std::shared_ptr<File> file )
61
{
62 63
    if ( m_services.size() == 0 )
        return;
64
    m_services[0]->parse( std::unique_ptr<parser::Task>( new parser::Task( media, file ) ) );
65
    m_opToDo += m_services.size();
66
    updateStats();
67 68
}

69 70
void Parser::start()
{
71 72 73
    restore();
    for ( auto& s : m_services )
        s->start();
74 75
}

76 77
void Parser::pause()
{
78 79
    for ( auto& s : m_services )
        s->pause();
80 81 82 83
}

void Parser::resume()
{
84 85
    for ( auto& s : m_services )
        s->resume();
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99
void Parser::stop()
{
    for ( auto& s : m_services )
    {
        s->signalStop();
    }
    for ( auto& s : m_services )
    {
        s->stop();
    }
}

100 101
void Parser::restore()
{
102 103 104
    if ( m_services.empty() == true )
        return;

105
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
106
            + " WHERE parsed = 0 AND is_present = 1";
107
    auto files = File::fetchAll<File>( m_ml, req );
108

109
    for ( auto& f : files )
110
    {
111
        auto m = f->media();
112
        parse( m, f );
113 114 115
    }
}

116
void Parser::updateStats()
117
{
118 119
    auto percent = m_opToDo > 0 ? ( m_opDone * 100 / m_opToDo ) : 0;
    if ( percent != m_percent )
120
    {
121 122
        m_percent = percent;
        m_callback->onParsingStatsUpdated( m_percent );
123 124 125
    }
}

126
void Parser::done( std::unique_ptr<parser::Task> t, parser::Task::Status status )
127
{
128
    ++m_opDone;
129 130

    auto serviceIdx = ++t->currentService;
131

132 133
    if ( status == parser::Task::Status::TemporaryUnavailable ||
         status == parser::Task::Status::Fatal )
134
    {
135 136 137 138 139 140
        if ( serviceIdx < m_services.size() )
        {
            // We won't process the next tasks, so we need to keep the number of "todo" operations coherent:
            m_opToDo -= m_services.size() - serviceIdx;
            updateStats();
        }
141
        return;
142
    }
143 144
    updateStats();

145
    if ( status == parser::Task::Status::Success )
146
    {
147
        m_notifier->notifyMediaModification( t->media );
148 149
    }

150
    if ( serviceIdx == m_services.size() )
151
    {
152
        t->file->markParsed();
153 154
        return;
    }
155
    m_services[serviceIdx]->parse( std::move( t ) );
156
}
157 158

}