Centralized scan settings into a dedicated ScanSettings table
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "Artist.hpp"
|
||||
#include "Release.hpp"
|
||||
#include "ScanSettings.hpp"
|
||||
#include "SqlQuery.hpp"
|
||||
#include "Track.hpp"
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace Database {
|
||||
|
||||
class Track;
|
||||
class ClusterType;
|
||||
class ScanSettings;
|
||||
|
||||
class Cluster : public Wt::Dbo::Dbo<Cluster>
|
||||
{
|
||||
@@ -101,6 +102,7 @@ class ClusterType : public Wt::Dbo::Dbo<ClusterType>
|
||||
{
|
||||
Wt::Dbo::field(a, _name, "name");
|
||||
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
|
||||
Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -109,6 +111,7 @@ class ClusterType : public Wt::Dbo::Dbo<ClusterType>
|
||||
|
||||
std::string _name;
|
||||
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
|
||||
Wt::Dbo::ptr<ScanSettings> _scanSettings;
|
||||
};
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -37,9 +37,9 @@
|
||||
|
||||
#include "Artist.hpp"
|
||||
#include "Cluster.hpp"
|
||||
#include "MediaDirectory.hpp"
|
||||
#include "Playlist.hpp"
|
||||
#include "Release.hpp"
|
||||
#include "ScanSettings.hpp"
|
||||
#include "Track.hpp"
|
||||
|
||||
namespace Database {
|
||||
@@ -98,20 +98,21 @@ Handler::Handler(Wt::Dbo::SqlConnectionPool& connectionPool)
|
||||
{
|
||||
_session.setConnectionPool(connectionPool);
|
||||
|
||||
_session.mapClass<Database::Artist>("artist");
|
||||
_session.mapClass<Database::Cluster>("cluster");
|
||||
_session.mapClass<Database::ClusterType>("cluster_type");
|
||||
_session.mapClass<Database::MediaDirectory>("media_directory");
|
||||
_session.mapClass<Database::Playlist>("playlist");
|
||||
_session.mapClass<Database::PlaylistEntry>("playlist_entry");
|
||||
_session.mapClass<Database::Release>("release");
|
||||
_session.mapClass<Database::Setting>("setting");
|
||||
_session.mapClass<Database::Track>("track");
|
||||
_session.mapClass<Artist>("artist");
|
||||
_session.mapClass<Cluster>("cluster");
|
||||
_session.mapClass<ClusterType>("cluster_type");
|
||||
_session.mapClass<Playlist>("playlist");
|
||||
_session.mapClass<PlaylistEntry>("playlist_entry");
|
||||
_session.mapClass<Release>("release");
|
||||
_session.mapClass<Setting>("setting");
|
||||
_session.mapClass<Track>("track");
|
||||
|
||||
_session.mapClass<Database::AuthInfo>("auth_info");
|
||||
_session.mapClass<Database::AuthInfo::AuthIdentityType>("auth_identity");
|
||||
_session.mapClass<Database::AuthInfo::AuthTokenType>("auth_token");
|
||||
_session.mapClass<Database::User>("user");
|
||||
_session.mapClass<ScanSettings>("scan_settings");
|
||||
|
||||
_session.mapClass<AuthInfo>("auth_info");
|
||||
_session.mapClass<AuthInfo::AuthIdentityType>("auth_identity");
|
||||
_session.mapClass<AuthInfo::AuthTokenType>("auth_token");
|
||||
_session.mapClass<User>("user");
|
||||
|
||||
try {
|
||||
Wt::Dbo::Transaction transaction(_session);
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "MediaDirectory.hpp"
|
||||
|
||||
#include "utils/Utils.hpp"
|
||||
|
||||
namespace Database {
|
||||
|
||||
MediaDirectory::MediaDirectory(boost::filesystem::path p)
|
||||
: _path(stringTrimEnd(p.string(), "/\\"))
|
||||
{
|
||||
}
|
||||
|
||||
MediaDirectory::pointer
|
||||
MediaDirectory::create(Wt::Dbo::Session& session, boost::filesystem::path p)
|
||||
{
|
||||
return session.add( std::make_unique<MediaDirectory>(p) );
|
||||
}
|
||||
|
||||
void
|
||||
MediaDirectory::eraseAll(Wt::Dbo::Session& session)
|
||||
{
|
||||
for (auto dir : getAll(session))
|
||||
dir.remove();
|
||||
}
|
||||
|
||||
std::vector<MediaDirectory::pointer>
|
||||
MediaDirectory::getAll(Wt::Dbo::Session& session)
|
||||
{
|
||||
Wt::Dbo::collection< MediaDirectory::pointer > res = session.find<MediaDirectory>();
|
||||
|
||||
return std::vector<MediaDirectory::pointer>(res.begin(), res.end());
|
||||
}
|
||||
|
||||
boost::filesystem::path
|
||||
MediaDirectory::getPath(void) const
|
||||
{
|
||||
return boost::filesystem::path(stringTrimEnd(_path, "/\\"));
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <Wt/Dbo/Dbo.h>
|
||||
|
||||
namespace Database {
|
||||
|
||||
class MediaDirectory
|
||||
{
|
||||
public:
|
||||
typedef Wt::Dbo::ptr<MediaDirectory> pointer;
|
||||
|
||||
MediaDirectory() {}
|
||||
MediaDirectory(boost::filesystem::path p);
|
||||
|
||||
// Accessors
|
||||
static pointer create(Wt::Dbo::Session& session, boost::filesystem::path p);
|
||||
static std::vector<MediaDirectory::pointer> getAll(Wt::Dbo::Session& session);
|
||||
|
||||
static void eraseAll(Wt::Dbo::Session& session);
|
||||
static void eraseByPath(Wt::Dbo::Session& session, boost::filesystem::path p);
|
||||
|
||||
boost::filesystem::path getPath(void) const;
|
||||
|
||||
template<class Action>
|
||||
void persist(Action& a)
|
||||
{
|
||||
Wt::Dbo::field(a, _path, "path");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string _path;
|
||||
};
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ScanSettings.hpp"
|
||||
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "utils/Logger.hpp"
|
||||
#include "utils/Utils.hpp"
|
||||
|
||||
#include "Cluster.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
std::set<std::string> defaultClusterTypeNames =
|
||||
{
|
||||
"GENRE",
|
||||
"ALBUMGROUPING",
|
||||
"MOOD",
|
||||
"ALBUMMOOD",
|
||||
"COMMENT:SONGS-DB_OCCASION",
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace Database {
|
||||
|
||||
|
||||
ScanSettings::pointer
|
||||
ScanSettings::get(Wt::Dbo::Session& session)
|
||||
{
|
||||
ScanSettings::pointer settings = session.find<ScanSettings>();
|
||||
if (!settings)
|
||||
{
|
||||
settings = session.add(std::make_unique<ScanSettings>());
|
||||
settings.modify()->setClusterTypes(defaultClusterTypeNames);
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
std::set<boost::filesystem::path>
|
||||
ScanSettings::getAudioFileExtensions() const
|
||||
{
|
||||
auto extensions = splitString(_audioFileExtensions, " ");
|
||||
return std::set<boost::filesystem::path>(extensions.begin(), extensions.end());
|
||||
}
|
||||
|
||||
std::vector<ClusterType::pointer>
|
||||
ScanSettings::getClusterTypes() const
|
||||
{
|
||||
return std::vector<ClusterType::pointer>(_clusterTypes.begin(), _clusterTypes.end());
|
||||
}
|
||||
|
||||
void
|
||||
ScanSettings::setMediaDirectory(boost::filesystem::path p)
|
||||
{
|
||||
_mediaDirectory = stringTrimEnd(p.string(), "/\\");
|
||||
}
|
||||
|
||||
void
|
||||
ScanSettings::setClusterTypes(const std::set<std::string>& clusterTypeNames)
|
||||
{
|
||||
assert(session());
|
||||
|
||||
// Backup the old list
|
||||
std::vector<ClusterType::pointer> oldClusterTypes(_clusterTypes.begin(), _clusterTypes.end());
|
||||
|
||||
_clusterTypes.clear();
|
||||
// Create any missing cluster type
|
||||
for (auto clusterTypeName : clusterTypeNames)
|
||||
{
|
||||
auto clusterType = ClusterType::getByName(*session(), clusterTypeName);
|
||||
if (!clusterType)
|
||||
{
|
||||
LMS_LOG(DB, INFO) << "Creating cluster type " << clusterTypeName;
|
||||
clusterType = ClusterType::create(*session(), clusterTypeName);
|
||||
}
|
||||
_clusterTypes.insert(clusterType);
|
||||
}
|
||||
|
||||
// Delete no longer existing cluster types
|
||||
for (auto oldClusterType : oldClusterTypes)
|
||||
{
|
||||
if (std::none_of(clusterTypeNames.begin(), clusterTypeNames.end(),
|
||||
[oldClusterType](const std::string& name) { return name == oldClusterType->getName(); }))
|
||||
{
|
||||
LMS_LOG(DB, INFO) << "Deleting cluster type " << oldClusterType->getName();
|
||||
oldClusterType.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <Wt/Dbo/Dbo.h>
|
||||
#include <Wt/WTime.h>
|
||||
|
||||
namespace Database {
|
||||
|
||||
class ClusterType;
|
||||
// class meant to store general settings
|
||||
class ScanSettings : public Wt::Dbo::Dbo<ScanSettings>
|
||||
{
|
||||
public:
|
||||
using pointer = Wt::Dbo::ptr<ScanSettings>;
|
||||
|
||||
enum class UpdatePeriod {
|
||||
Never = 0,
|
||||
Daily,
|
||||
Weekly,
|
||||
Monthly
|
||||
};
|
||||
|
||||
ScanSettings() {}
|
||||
|
||||
static pointer get(Wt::Dbo::Session& session);
|
||||
|
||||
// Getters
|
||||
boost::filesystem::path getMediaDirectory() const { return _mediaDirectory; }
|
||||
Wt::WTime getUpdateStartTime() const { return _startTime; }
|
||||
UpdatePeriod getUpdatePeriod() const { return _updatePeriod; }
|
||||
std::vector<Wt::Dbo::ptr<ClusterType>> getClusterTypes() const;
|
||||
std::set<boost::filesystem::path> getAudioFileExtensions() const;
|
||||
|
||||
// Setters
|
||||
void setMediaDirectory(boost::filesystem::path p);
|
||||
void setUpdateStartTime(Wt::WTime t) { _startTime = t; }
|
||||
void setUpdatePeriod(UpdatePeriod p) { _updatePeriod = p; }
|
||||
void setClusterTypes(const std::set<std::string>& clusterTypeNames);
|
||||
void setAudioFileExtensions(std::set<boost::filesystem::path> fileExtensions);
|
||||
|
||||
template<class Action>
|
||||
void persist(Action& a)
|
||||
{
|
||||
Wt::Dbo::field(a, _mediaDirectory, "media_directory");
|
||||
Wt::Dbo::field(a, _startTime, "start_time");
|
||||
Wt::Dbo::field(a, _updatePeriod, "update_period");
|
||||
Wt::Dbo::field(a, _audioFileExtensions, "audio_file_extensions");
|
||||
Wt::Dbo::hasMany(a, _clusterTypes, Wt::Dbo::ManyToOne, "scan_settings");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string _mediaDirectory = "";
|
||||
Wt::WTime _startTime = Wt::WTime(0,0,0);
|
||||
UpdatePeriod _updatePeriod = UpdatePeriod::Never;
|
||||
std::string _audioFileExtensions = ".mp3 .ogg .oga .aac .m4a .flac .wav .wma .aif .aiff .ape .mpc .shn";
|
||||
Wt::Dbo::collection<Wt::Dbo::ptr<ClusterType>> _clusterTypes;
|
||||
};
|
||||
|
||||
|
||||
} // namespace Database
|
||||
|
||||
Reference in New Issue
Block a user