| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "corefactory.h" |
| 5 | |
| 6 | #include "common/cryptolayer.h" |
| 7 | #include "config.h" |
| 8 | #include "core/ccpro/ccproparser.h" |
| 9 | #include "core/components/controls/cpucontrolprovider.h" |
| 10 | #include "core/components/controls/gpucontrolprovider.h" |
| 11 | #include "core/components/sensors/cpusensorprovider.h" |
| 12 | #include "core/components/sensors/gpusensorprovider.h" |
| 13 | #include "core/filecache.h" |
| 14 | #include "core/info/hwiddatasource.h" |
| 15 | #include "core/info/hwidtranslator.h" |
| 16 | #include "core/info/infoproviderregistry.h" |
| 17 | #include "core/info/swinfo.h" |
| 18 | #include "core/iprofileparser.h" |
| 19 | #include "core/profile.h" |
| 20 | #include "core/profilefactory.h" |
| 21 | #include "core/profileiconcache.h" |
| 22 | #include "core/profilemanager.h" |
| 23 | #include "core/profilepartprovider.h" |
| 24 | #include "core/profilepartxmlparserprovider.h" |
| 25 | #include "core/profilestorage.h" |
| 26 | #include "core/profileviewfactory.h" |
| 27 | #include "core/profilexmlparserfactory.h" |
| 28 | #include "core/qmlcomponentfactory.h" |
| 29 | #include "core/qmlcomponentregistry.h" |
| 30 | #include "core/session.h" |
| 31 | #include "core/sysexplorer.h" |
| 32 | #include "core/sysmodelfactory.h" |
| 33 | #include "core/sysmodelsyncer.h" |
| 34 | #include "core/uifactory.h" |
| 35 | #include "helper/helpercontrol.h" |
| 36 | #include "helper/helpermonitor.h" |
| 37 | #include "helper/helpersysctl.h" |
| 38 | #include <QStandardPaths> |
| 39 | #include <algorithm> |
| 40 | #include <cctype> |
| 41 | #include <exception> |
| 42 | #include <format> |
| 43 | #include <spdlog/spdlog.h> |
| 44 | #include <system_error> |
| 45 | #include <utility> |
| 46 | |
| 47 | namespace fs = std::filesystem; |
| 48 | |
| 49 | CoreFactory::CoreFactory() noexcept |
| 50 | : gpuVendors_{Vendor::AMD} |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | std::optional<CoreFactory::Components> |
| 55 | CoreFactory::build(std::string &&appName, bool logCommands, |
| 56 | bool logProfileStack) const |
| 57 | { |
| 58 | try { |
| 59 | std::transform(appName.cbegin(), appName.cend(), appName.begin(), ::tolower); |
| 60 | |
| 61 | auto [config, cache] = standardDirectories(); |
| 62 | createAppDirectories(appName, config, cache); |
| 63 | |
| 64 | auto cryptoLayer = std::make_shared<CryptoLayer>(); |
| 65 | auto helperControl = std::make_unique<HelperControl>(cryptoLayer); |
| 66 | |
| 67 | InfoProviderRegistry infoProviderRegistry; |
| 68 | auto swInfo = std::make_unique<SWInfo>(); |
| 69 | swInfo->initialize(infoProviderRegistry.swInfoProviders()); |
| 70 | |
| 71 | SysModelFactory sysModelFactory( |
| 72 | std::move(swInfo), std::make_unique<SysExplorer>(gpuVendors_), |
| 73 | std::make_unique<HWIDTranslator>( |
| 74 | gpuVendors_, std::make_unique<HWIDDataSource>(PCI_IDS_PATH)), |
| 75 | std::make_unique<CPUControlProvider>(), |
| 76 | std::make_unique<CPUSensorProvider>(), |
| 77 | std::make_unique<GPUControlProvider>(), |
| 78 | std::make_unique<GPUSensorProvider>(), infoProviderRegistry, |
| 79 | infoProviderRegistry); |
| 80 | auto sysModel = sysModelFactory.build(); |
| 81 | |
| 82 | ProfileFactory profileFactory(std::make_unique<ProfilePartProvider>()); |
| 83 | auto defaultProfile = profileFactory.build(*sysModel); |
| 84 | ProfileXMLParserFactory parserFactory( |
| 85 | std::make_unique<ProfilePartXMLParserProvider>()); |
| 86 | auto profileParser = parserFactory.build(*defaultProfile); |
| 87 | auto profileFileParser = std::make_unique<CCPROParser>(); |
| 88 | auto iconCache = std::make_unique<ProfileIconCache>( |
| 89 | std::make_unique<FileCache>(cache / "icons")); |
| 90 | auto profileManager = std::make_unique<ProfileManager>( |
| 91 | std::move(defaultProfile), |
| 92 | std::make_unique<ProfileStorage>( |
| 93 | config / appName / "profiles", std::move(profileParser), |
| 94 | std::move(profileFileParser), std::move(iconCache))); |
| 95 | |
| 96 | auto sysModelSyncer = std::make_shared<SysModelSyncer>( |
| 97 | std::move(sysModel), |
| 98 | std::make_unique<HelperSysCtl>(cryptoLayer, logCommands)); |
| 99 | |
| 100 | auto session = std::make_unique<Session>( |
| 101 | sysModelSyncer, std::move(profileManager), |
| 102 | std::make_unique<ProfileViewFactory>(), |
| 103 | std::make_unique<HelperMonitor>(cryptoLayer), logProfileStack); |
| 104 | auto uiFactory = |
| 105 | std::make_unique<UIFactory>(std::make_unique<QMLComponentFactory>( |
| 106 | std::make_unique<QMLComponentRegistry>())); |
| 107 | |
| 108 | return Components{std::move(helperControl), std::move(sysModelSyncer), |
| 109 | std::move(session), std::move(uiFactory)}; |
| 110 | } |
| 111 | catch (std::exception const &e) { |
| 112 | SPDLOG_WARN("Cannot create core components"); |
| 113 | SPDLOG_WARN(e.what()); |
| 114 | } |
| 115 | |
| 116 | return {}; |
| 117 | } |
| 118 | |
| 119 | std::tuple<std::filesystem::path, std::filesystem::path> |
| 120 | CoreFactory::standardDirectories() const |
| 121 | { |
| 122 | return {QStandardPaths::standardLocations(QStandardPaths::ConfigLocation) |
| 123 | .first() |
| 124 | .toStdString(), |
| 125 | QStandardPaths::standardLocations(QStandardPaths::CacheLocation) |
| 126 | .first() |
| 127 | .toStdString()}; |
| 128 | } |
| 129 | |
| 130 | void CoreFactory::createAppDirectories(std::string const &appDirectory, |
| 131 | std::filesystem::path const &config, |
| 132 | std::filesystem::path const &cache) const |
| 133 | { |
| 134 | std::error_code ec; |
| 135 | fs::perms dirPerms = fs::perms::owner_all | fs::perms::group_read | |
| 136 | fs::perms::group_exec | fs::perms::others_read | |
| 137 | fs::perms::others_exec; |
| 138 | |
| 139 | if (!fs::exists(config)) { |
| 140 | fs::create_directory(config); |
| 141 | fs::permissions(config, dirPerms, ec); |
| 142 | if (ec.value() != 0) |
| 143 | SPDLOG_DEBUG("Cannot set permissions for {}", config.c_str()); |
| 144 | } |
| 145 | |
| 146 | if (!fs::is_directory(config)) |
| 147 | throw std::runtime_error( |
| 148 | std::format("{} is not a directory", config.c_str())); |
| 149 | |
| 150 | fs::path appConfigDir = config / appDirectory; |
| 151 | if (!fs::exists(appConfigDir)) { |
| 152 | fs::create_directory(appConfigDir); |
| 153 | fs::permissions(appConfigDir, dirPerms, ec); |
| 154 | if (ec.value() != 0) |
| 155 | SPDLOG_DEBUG("Cannot set permissions for {}", appConfigDir.c_str()); |
| 156 | } |
| 157 | |
| 158 | if (!fs::is_directory(appConfigDir)) |
| 159 | throw std::runtime_error( |
| 160 | std::format("{} is not a directory", appConfigDir.c_str())); |
| 161 | |
| 162 | if (!fs::exists(cache)) { |
| 163 | fs::create_directory(cache); |
| 164 | fs::permissions(cache, dirPerms, ec); |
| 165 | if (ec.value() != 0) |
| 166 | SPDLOG_DEBUG("Cannot set permissions for {}", cache.c_str()); |
| 167 | } |
| 168 | |
| 169 | if (!fs::is_directory(cache)) |
| 170 | throw std::runtime_error(std::format("{} is not a directory", cache.c_str())); |
| 171 | } |