Settings.cpp 2.7 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
const uint32_t Settings::DbModelVersion = 14u;
36

37 38
Settings::Settings( MediaLibrary* ml )
    : m_ml( ml )
39
    , m_dbModelVersion( 0 )
40 41 42 43
    , m_changed( false )
{
}

44
bool Settings::load()
45
{
46
    sqlite::Statement s( m_ml->getConn()->handle(), "SELECT * FROM Settings" );
47 48 49 50
    auto row = s.row();
    // First launch: no settings
    if ( row == nullptr )
    {
51
        if ( sqlite::Tools::executeInsert( m_ml->getConn(), "INSERT INTO Settings VALUES(?)", DbModelVersion ) == false )
52
            return false;
53
        m_dbModelVersion = DbModelVersion;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    }
    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;
74
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_dbModelVersion ) == true )
75 76 77 78 79 80 81 82 83 84 85 86 87
    {
        m_changed = false;
        return true;
    }
    return false;
}

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

88
void Settings::createTable( sqlite::Connection* dbConn )
89 90
{
    const std::string req = "CREATE TABLE IF NOT EXISTS Settings("
91
                "db_model_version UNSIGNED INTEGER NOT NULL"
92
            ")";
93
    sqlite::Tools::executeRequest( dbConn, req );
94 95
}

96
}