SourceList.h (4630B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include "ISource.h" 5 #include <winget/Settings.h> 6 7 8 namespace AppInstaller::Repository 9 { 10 // Built-in values for default sources 11 std::string_view GetWellKnownSourceName(WellKnownSource source); 12 std::string_view GetWellKnownSourceArg(WellKnownSource source); 13 std::string_view GetWellKnownSourceIdentifier(WellKnownSource source); 14 std::optional<WellKnownSource> CheckForWellKnownSourceMatch(std::string_view name, std::string_view arg, std::string_view type); 15 16 // SourceDetails with additional data used internally. 17 struct SourceDetailsInternal : public SourceDetails 18 { 19 SourceDetailsInternal() = default; 20 SourceDetailsInternal(const SourceDetails& details) : SourceDetails(details) {} 21 22 // Copies the metadata fields to this target. 23 void CopyMetadataFieldsTo(SourceDetailsInternal& target); 24 25 // Copies the metadata fields from this source. This only include partial metadata. 26 void CopyMetadataFieldsFrom(const SourceDetails& source); 27 28 // If true, this is a tombstone, marking the deletion of a source at a lower priority origin. 29 bool IsTombstone = false; 30 31 // If false, this is not visible in GetCurrentSource or GetAllSources, it's only available when explicitly requested. 32 bool IsVisible = true; 33 34 // Accepted agreements info. 35 std::string AcceptedAgreementsIdentifier; 36 int AcceptedAgreementFields = 0; 37 }; 38 39 // Gets the internal details for a well known source. 40 SourceDetailsInternal GetWellKnownSourceDetailsInternal(WellKnownSource source); 41 42 // Struct containing internal implementation of the source list. 43 // This contains all source data; including tombstoned sources. 44 struct SourceList 45 { 46 SourceList(); 47 48 // Get a list of current sources references which can be used to update the contents in place. 49 // e.g. update the LastTimeUpdated value of sources. 50 std::vector<std::reference_wrapper<SourceDetailsInternal>> GetCurrentSourceRefs(); 51 52 // Current source means source that's not in tombstone 53 SourceDetailsInternal* GetCurrentSource(std::string_view name); 54 55 // Source includes ones in tombstone 56 SourceDetailsInternal* GetSource(std::string_view name); 57 58 // Add/remove a current source 59 void AddSource(const SourceDetailsInternal& details); 60 void RemoveSource(const SourceDetailsInternal& details); 61 62 // Save source metadata; the particular source with the metadata update is given. 63 // The given source must already be in the internal source list. 64 void SaveMetadata(const SourceDetailsInternal& details); 65 66 // Checks the source agreements and returns if agreements are satisfied. 67 bool CheckSourceAgreements(std::string_view sourceName, std::string_view agreementsIdentifier, ImplicitAgreementFieldEnum agreementFields); 68 69 // Save agreements information. 70 void SaveAcceptedSourceAgreements(std::string_view sourceName, std::string_view agreementsIdentifier, ImplicitAgreementFieldEnum agreementFields); 71 72 // Removes all settings streams associated with the source list. 73 // Implements `winget source reset --force`. 74 static void RemoveSettingsStreams(); 75 76 private: 77 // Overwrites the source list with all sources. 78 void OverwriteSourceList(); 79 80 // Overwrites the source list with the current metadata. 81 void OverwriteMetadata(); 82 83 // calls std::find_if and return the iterator. 84 auto FindSource(std::string_view name, bool includeHidden = false); 85 86 std::vector<SourceDetailsInternal> GetSourcesByOrigin(SourceOrigin origin); 87 // Does *NOT* set metadata; call SaveMetadataInternal afterward. 88 [[nodiscard]] bool SetSourcesByOrigin(SourceOrigin origin, const std::vector<SourceDetailsInternal>& sources); 89 90 std::vector<SourceDetailsInternal> GetMetadata(); 91 [[nodiscard]] bool SetMetadata(const std::vector<SourceDetailsInternal>& sources); 92 93 // Save source metadata; the particular source with the metadata update is given. 94 // If remove is true, the given source is being removed. 95 void SaveMetadataInternal(const SourceDetailsInternal& details, bool remove = false); 96 97 std::vector<SourceDetailsInternal> m_sourceList; 98 Settings::Stream m_userSourcesStream; 99 Settings::Stream m_metadataStream; 100 }; 101 }