/* * 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 . */ #include "database/ScanSettings.hpp" #include #include "utils/Logger.hpp" #include "utils/String.hpp" #include "database/Cluster.hpp" #include "database/Session.hpp" namespace { const std::set defaultClusterTypeNames = { "GENRE", "ALBUMGROUPING", "MOOD", "ALBUMMOOD", }; } namespace Database { void ScanSettings::init(Session& session) { session.checkUniqueLocked(); pointer settings {get(session)}; if (settings) return; settings = session.getDboSession().add(std::make_unique()); settings.modify()->setClusterTypes(session, defaultClusterTypeNames ); } ScanSettings::pointer ScanSettings::get(Session& session) { session.checkSharedLocked(); return session.getDboSession().find(); } std::set ScanSettings::getAudioFileExtensions() const { auto extensions = StringUtils::splitString(_audioFileExtensions, " "); return std::set(std::cbegin(extensions), std::cend(extensions)); } void ScanSettings::addAudioFileExtension(const std::filesystem::path& ext) { _audioFileExtensions += " " + ext.string(); } std::vector ScanSettings::getClusterTypes() const { return std::vector(std::cbegin(_clusterTypes), std::cend(_clusterTypes)); } void ScanSettings::setMediaDirectory(const std::filesystem::path& p) { _mediaDirectory = StringUtils::stringTrimEnd(p.string(), "/\\"); } template std::set getNames(It begin, It end) { std::set names; std::transform(begin, end, std::inserter(names, std::cbegin(names)), [](const ClusterType::pointer& clusterType) { return clusterType->getName(); }); return names; } void ScanSettings::setClusterTypes(Session& session, const std::set& clusterTypeNames) { session.checkUniqueLocked(); bool needRescan {}; // Create any missing cluster type for (const std::string& 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); needRescan = true; } } // Delete no longer existing cluster types for (ClusterType::pointer& clusterType : _clusterTypes) { if (std::none_of(clusterTypeNames.begin(), clusterTypeNames.end(), [clusterType](const std::string& name) { return name == clusterType->getName(); })) { LMS_LOG(DB, INFO) << "Deleting cluster type " << clusterType->getName(); clusterType.remove(); } } if (needRescan) _scanVersion += 1; } void ScanSettings::incScanVersion() { _scanVersion += 1; } } // namespace Database