// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2019 Juan Palacios #include "corefactory.h" #include "common/cryptolayer.h" #include "config.h" #include "core/ccpro/ccproparser.h" #include "core/components/controls/cpucontrolprovider.h" #include "core/components/controls/gpucontrolprovider.h" #include "core/components/sensors/cpusensorprovider.h" #include "core/components/sensors/gpusensorprovider.h" #include "core/filecache.h" #include "core/info/hwiddatasource.h" #include "core/info/hwidtranslator.h" #include "core/info/infoproviderregistry.h" #include "core/info/swinfo.h" #include "core/iprofileparser.h" #include "core/profile.h" #include "core/profilefactory.h" #include "core/profileiconcache.h" #include "core/profilemanager.h" #include "core/profilepartprovider.h" #include "core/profilepartxmlparserprovider.h" #include "core/profilestorage.h" #include "core/profileviewfactory.h" #include "core/profilexmlparserfactory.h" #include "core/qmlcomponentfactory.h" #include "core/qmlcomponentregistry.h" #include "core/session.h" #include "core/sysexplorer.h" #include "core/sysmodelfactory.h" #include "core/sysmodelsyncer.h" #include "core/uifactory.h" #include "helper/helpercontrol.h" #include "helper/helpermonitor.h" #include "helper/helpersysctl.h" #include #include #include #include #include #include #include #include namespace fs = std::filesystem; CoreFactory::CoreFactory() noexcept : gpuVendors_{Vendor::AMD} { } std::optional CoreFactory::build(std::string &&appName, bool logCommands, bool logProfileStack) const { try { std::transform(appName.cbegin(), appName.cend(), appName.begin(), ::tolower); auto [config, cache] = standardDirectories(); createAppDirectories(appName, config, cache); auto cryptoLayer = std::make_shared(); auto helperControl = std::make_unique(cryptoLayer); InfoProviderRegistry infoProviderRegistry; auto swInfo = std::make_unique(); swInfo->initialize(infoProviderRegistry.swInfoProviders()); SysModelFactory sysModelFactory( std::move(swInfo), std::make_unique(gpuVendors_), std::make_unique( gpuVendors_, std::make_unique(PCI_IDS_PATH)), std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique(), infoProviderRegistry, infoProviderRegistry); auto sysModel = sysModelFactory.build(); ProfileFactory profileFactory(std::make_unique()); auto defaultProfile = profileFactory.build(*sysModel); ProfileXMLParserFactory parserFactory( std::make_unique()); auto profileParser = parserFactory.build(*defaultProfile); auto profileFileParser = std::make_unique(); auto iconCache = std::make_unique( std::make_unique(cache / "icons")); auto profileManager = std::make_unique( std::move(defaultProfile), std::make_unique( config / appName / "profiles", std::move(profileParser), std::move(profileFileParser), std::move(iconCache))); auto sysModelSyncer = std::make_shared( std::move(sysModel), std::make_unique(cryptoLayer, logCommands)); auto session = std::make_unique( sysModelSyncer, std::move(profileManager), std::make_unique(), std::make_unique(cryptoLayer), logProfileStack); auto uiFactory = std::make_unique(std::make_unique( std::make_unique())); return Components{std::move(helperControl), std::move(sysModelSyncer), std::move(session), std::move(uiFactory)}; } catch (std::exception const &e) { SPDLOG_WARN("Cannot create core components"); SPDLOG_WARN(e.what()); } return {}; } std::tuple CoreFactory::standardDirectories() const { return {QStandardPaths::standardLocations(QStandardPaths::ConfigLocation) .first() .toStdString(), QStandardPaths::standardLocations(QStandardPaths::CacheLocation) .first() .toStdString()}; } void CoreFactory::createAppDirectories(std::string const &appDirectory, std::filesystem::path const &config, std::filesystem::path const &cache) const { std::error_code ec; fs::perms dirPerms = fs::perms::owner_all | fs::perms::group_read | fs::perms::group_exec | fs::perms::others_read | fs::perms::others_exec; if (!fs::exists(config)) { fs::create_directory(config); fs::permissions(config, dirPerms, ec); if (ec.value() != 0) SPDLOG_DEBUG("Cannot set permissions for {}", config.c_str()); } if (!fs::is_directory(config)) throw std::runtime_error( std::format("{} is not a directory", config.c_str())); fs::path appConfigDir = config / appDirectory; if (!fs::exists(appConfigDir)) { fs::create_directory(appConfigDir); fs::permissions(appConfigDir, dirPerms, ec); if (ec.value() != 0) SPDLOG_DEBUG("Cannot set permissions for {}", appConfigDir.c_str()); } if (!fs::is_directory(appConfigDir)) throw std::runtime_error( std::format("{} is not a directory", appConfigDir.c_str())); if (!fs::exists(cache)) { fs::create_directory(cache); fs::permissions(cache, dirPerms, ec); if (ec.value() != 0) SPDLOG_DEBUG("Cannot set permissions for {}", cache.c_str()); } if (!fs::is_directory(cache)) throw std::runtime_error(std::format("{} is not a directory", cache.c_str())); }