Commit d9664e4d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add a settings table & store the database model version in it

parent 3b292ab3
......@@ -63,6 +63,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
AudioTrack.cpp
Parser.cpp
Artist.cpp
Settings.cpp
factory/FileSystem.cpp
filesystem/${ARCH_FOLDER}/Directory.cpp
......
......@@ -71,6 +71,8 @@ const std::vector<std::string> MediaLibrary::supportedAudioExtensions {
"wma", "wv", "xa", "xm"
};
const uint32_t MediaLibrary::DbModelVersion = 1;
MediaLibrary::MediaLibrary()
: m_discoverer( new DiscovererWorker )
, m_verbosity( LogLevel::Error )
......@@ -143,7 +145,7 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
}
auto t = m_dbConnection->newTransaction();
if ( ! ( Media::createTable( m_dbConnection.get() ) &&
if ( ( Media::createTable( m_dbConnection.get() ) &&
Folder::createTable( m_dbConnection.get() ) &&
Label::createTable( m_dbConnection.get() ) &&
Album::createTable( m_dbConnection.get() ) &&
......@@ -154,11 +156,17 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
VideoTrack::createTable( m_dbConnection.get() ) &&
AudioTrack::createTable( m_dbConnection.get() ) &&
Artist::createTable( m_dbConnection.get() ) &&
Artist::createDefaultArtists( m_dbConnection.get() ) ) )
Artist::createDefaultArtists( m_dbConnection.get() ) &&
Settings::createTable( m_dbConnection.get() ) ) == false )
{
LOG_ERROR( "Failed to create database structure" );
return false;
}
if ( m_settings.load( m_dbConnection.get() ) == false )
return false;
// We only have one version so far, so a mismatching version is an error
if ( m_settings.dbModelVersion() != DbModelVersion )
return false;
t->commit();
m_discoverer->setCallback( m_callback );
m_discoverer->addDiscoverer( std::unique_ptr<IDiscoverer>( new FsDiscoverer( m_fsFactory, this, m_dbConnection.get() ) ) );
......
......@@ -31,6 +31,7 @@ class SqliteConnection;
#include "IDiscoverer.h"
#include "logging/Logger.h"
#include "vlcpp/vlc.hpp"
#include "Settings.h"
class Album;
class Artist;
......@@ -86,6 +87,9 @@ class MediaLibrary : public IMediaLibrary
virtual void pauseBackgroundOperations() override;
virtual void resumeBackgroundOperations() override;
public:
static const uint32_t DbModelVersion;
private:
static const std::vector<std::string> supportedVideoExtensions;
static const std::vector<std::string> supportedAudioExtensions;
......@@ -113,5 +117,6 @@ class MediaLibrary : public IMediaLibrary
//FIXME: Having to maintain a specific ordering sucks, let's use shared_ptr or something
std::unique_ptr<DiscovererWorker> m_discoverer;
LogLevel m_verbosity;
Settings m_settings;
};
#endif // MEDIALIBRARY_H
/*****************************************************************************
* 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.
*****************************************************************************/
#include "Settings.h"
#include "database/SqliteTools.h"
#include "MediaLibrary.h"
Settings::Settings()
: m_dbModelVersion( 0 )
, m_changed( false )
{
}
bool Settings::load( DBConnection dbConn )
{
m_dbConn = dbConn;
sqlite::Statement s( m_dbConn, "SELECT * FROM Settings" );
auto row = s.row();
// First launch: no settings
if ( row == nullptr )
{
if ( sqlite::Tools::insert( m_dbConn, "INSERT INTO Settings VALUES(?)", MediaLibrary::DbModelVersion ) == false )
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 );
}
/*****************************************************************************
* 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.
*****************************************************************************/
#ifndef SETTINGS_H
#define SETTINGS_H
#include "Types.h"
class Settings
{
public:
Settings();
bool load(DBConnection dbConn);
uint32_t dbModelVersion() const;
bool save();
void setDbModelVersion( uint32_t dbModelVersion );
static bool createTable(DBConnection dbConn);
private:
DBConnection m_dbConn;
uint32_t m_dbModelVersion;
bool m_changed;
};
#endif // SETTINGS_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment