Settings.cpp 2.71 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

27 28 29 30 31
#include "Settings.h"

#include "database/SqliteTools.h"
#include "MediaLibrary.h"

32 33 34
namespace medialibrary
{

35
Settings::Settings()
36 37
    : m_dbConn( nullptr )
    , m_dbModelVersion( 0 )
38 39 40 41 42 43 44
    , m_changed( false )
{
}

bool Settings::load( DBConnection dbConn )
{
    m_dbConn = dbConn;
45
    sqlite::Statement s( m_dbConn->getConn(), "SELECT * FROM Settings" );
46 47 48 49
    auto row = s.row();
    // First launch: no settings
    if ( row == nullptr )
    {
50
        if ( sqlite::Tools::executeInsert( m_dbConn, "INSERT INTO Settings VALUES(?)", MediaLibrary::DbModelVersion ) == false )
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            return false;
        m_dbModelVersion = MediaLibrary::DbModelVersion;
    }
    else
    {
        row >> m_dbModelVersion;
        // safety check: there sould only be one row
        assert( s.row() == nullptr );
    }
    return true;
}

uint32_t Settings::dbModelVersion() const
{
    return m_dbModelVersion;
}

bool Settings::save()
{
    static const std::string req = "UPDATE Settings SET db_model_version = ?";
    if ( m_changed == false )
        return true;
    if ( sqlite::Tools::executeUpdate( m_dbConn, req, m_dbModelVersion ) == true )
    {
        m_changed = false;
        return true;
    }
    return false;
}

void Settings::setDbModelVersion(uint32_t dbModelVersion)
{
    m_dbModelVersion = dbModelVersion;
    m_changed = true;
}

bool Settings::createTable( DBConnection dbConn )
{
    const std::string req = "CREATE TABLE IF NOT EXISTS Settings("
                "db_model_version UNSIGNED INTEGER NOT NULL DEFAULT 1"
            ")";
    return sqlite::Tools::executeRequest( dbConn, req );
}

95
}