/
IntegrationState.cs
62 lines (53 loc) · 3.06 KB
/
IntegrationState.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
namespace ZeroInstall.DesktopIntegration.ViewModel;
/// <summary>
/// A View-Model for modifying desktop integration. Provides data-binding lists and applies modifications in bulk.
/// </summary>
public partial class IntegrationState
{
/// <summary>
/// The integration manager used to apply selected integration options.
/// </summary>
private readonly IIntegrationManager _integrationManager;
/// <summary>
/// The application being integrated.
/// </summary>
public AppEntry AppEntry { get; }
/// <summary>
/// The feed providing additional metadata, icons, etc. for the application.
/// </summary>
public Feed Feed { get; }
/// <summary>
/// Creates a new integration state View-Model.
/// </summary>
/// <param name="integrationManager">The integration manager used to apply selected integration options.</param>
/// <param name="appEntry">The application being integrated.</param>
/// <param name="feed">The feed providing additional metadata, icons, etc. for the application.</param>
public IntegrationState(IIntegrationManager integrationManager, AppEntry appEntry, Feed feed)
{
_integrationManager = integrationManager ?? throw new ArgumentNullException(nameof(integrationManager));
AppEntry = appEntry ?? throw new ArgumentNullException(nameof(appEntry));
Feed = feed ?? throw new ArgumentNullException(nameof(feed));
CapabilityRegistration = (AppEntry.AccessPoints == null) || AppEntry.AccessPoints.Entries.OfType<AccessPoints.CapabilityRegistration>().Any();
LoadCommandAccessPoints();
LoadDefaultAccessPoints();
}
/// <summary>
/// Applies any changes made to the View-Model to the underlying system.
/// </summary>
/// <exception cref="OperationCanceledException">The user canceled the task.</exception>
/// <exception cref="ConflictException">One or more of the new <see cref="AccessPoints.AccessPoint"/>s would cause a conflict with the existing <see cref="AccessPoints.AccessPoint"/>s in <see cref="IIntegrationManager.AppList"/>.</exception>
/// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
/// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception>
/// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception>
public void ApplyChanges()
{
List<AccessPoints.AccessPoint> toAdd = [], toRemove = [];
(CapabilityRegistration ? toAdd : toRemove).Add(new AccessPoints.CapabilityRegistration());
CollectCommandAccessPointChanges(toAdd, toRemove);
CollectDefaultAccessPointChanges(toAdd, toRemove);
if (toRemove.Any()) _integrationManager.RemoveAccessPoints(AppEntry, toRemove);
if (toAdd.Any()) _integrationManager.AddAccessPoints(AppEntry, Feed, toAdd);
}
}