| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "profileview.h" |
| 5 | |
| 6 | #include "iprofilepart.h" |
| 7 | #include "isyscomponentprofilepart.h" |
| 8 | #include "isysmodel.h" |
| 9 | #include "profilepartview.h" |
| 10 | #include <algorithm> |
| 11 | #include <utility> |
| 12 | |
| 13 | std::string const &ProfileView::name() const |
| 14 | { |
| 15 | return name_; |
| 16 | } |
| 17 | |
| 18 | std::vector<std::unique_ptr<IProfilePartView>> const &ProfileView::parts() const |
| 19 | { |
| 20 | return parts_; |
| 21 | } |
| 22 | |
| 23 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 24 | ProfileView::provideImporter(Item const &i) |
| 25 | { |
| 26 | if (i.ID() == ISysModel::ItemID) |
| 27 | return *this; |
| 28 | else { |
| 29 | auto partIter = std::find_if( |
| 30 | parts_.cbegin(), parts_.cend(), |
| 31 | [&](std::unique_ptr<IProfilePartView> const &part) { |
| 32 | auto *sysPart = dynamic_cast<ISysComponentProfilePart const *>( |
| 33 | part->part().get()); |
| 34 | if (sysPart == nullptr) |
| 35 | return false; |
| 36 | |
| 37 | return sysPart->belongsTo(i); |
| 38 | }); |
| 39 | if (partIter != parts_.cend()) |
| 40 | return dynamic_cast<Importable::Importer &>(*partIter->get()->part()); |
| 41 | } |
| 42 | |
| 43 | return {}; |
| 44 | } |
| 45 | |
| 46 | ProfileView::Initializer::Initializer( |
| 47 | ProfileView &profileView, |
| 48 | std::optional<std::reference_wrapper<IProfileView>> base) noexcept |
| 49 | : profileView_(profileView) |
| 50 | , base_(base) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 55 | ProfileView::Initializer::provideExporter(Item const &i) |
| 56 | { |
| 57 | if (i.ID() == IProfile::ItemID) { |
| 58 | auto &pView = dynamic_cast<IProfileView::View const &>(i); |
| 59 | profileView_.name_ = pView.name(); |
| 60 | |
| 61 | auto const &parts = pView.parts(); |
| 62 | for (auto const &part : parts) { |
| 63 | std::shared_ptr<IProfilePart> profilePart; |
| 64 | std::string partProfileName; |
| 65 | |
| 66 | if (!part->active() && base_.has_value()) { // compose from base_ |
| 67 | partProfileName = base_->get().name(); |
| 68 | |
| 69 | auto const &baseParts = base_->get().parts(); |
| 70 | auto const basePartIter = std::find_if( |
| 71 | baseParts.cbegin(), baseParts.cend(), [&](auto const &partView) { |
| 72 | return partView->part()->ID() == part->ID(); |
| 73 | }); |
| 74 | if (basePartIter != baseParts.cend()) |
| 75 | profilePart = basePartIter->get()->part(); |
| 76 | } |
| 77 | else { |
| 78 | partProfileName = dynamic_cast<IProfile const &>(i).info().name; |
| 79 | profilePart = part; |
| 80 | } |
| 81 | |
| 82 | profileView_.parts_.emplace_back(std::make_unique<ProfilePartView>( |
| 83 | std::move(partProfileName), std::move(profilePart))); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return {}; |
| 88 | } |
| 89 | |
| 90 | void ProfileView::Initializer::takeActive(bool) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void ProfileView::Initializer::takeInfo(IProfile::Info const &) |
| 95 | { |
| 96 | } |