Skip to content

Commit 5ba00c5

Browse files
committed
Added the function to get a list of all admin settings without getting the full winget info data; Updated the AminSettingBuilder
1 parent 86fa2ab commit 5ba00c5

6 files changed

Lines changed: 208 additions & 11 deletions

File tree

src/WGet.NET/Builder/AdminSettingBuilder.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ internal class AdminSettingBuilder : WinGetObjectBuilder<WinGetAdminSetting?>
1414
private string _entryName = string.Empty;
1515
private string _rawContent = string.Empty;
1616
private bool _hasShortenedContent = false;
17-
private bool _isEnabled = false;
18-
19-
private bool _parsed = false;
17+
private bool? _isEnabled = false;
2018

2119
/// <summary>
2220
/// Initializes a new instance of the <see cref="WGetNET.Builder.AdminSettingBuilder"/> class.
@@ -56,11 +54,31 @@ public void AddRawContent(string rawContent)
5654
_rawContent = rawContent;
5755
}
5856

59-
bool? isEnabled = ParseToBool(_rawContent, _hasShortenedContent);
60-
if (isEnabled != null)
57+
_isEnabled = ParseToBool(_rawContent, _hasShortenedContent);
58+
}
59+
60+
/// <summary>
61+
/// Adds the status of admin settings.
62+
/// </summary>
63+
/// <remarks>
64+
/// There is no need to add raw content after using this method.
65+
/// Using <see cref="AdminSettingBuilder.AddRawContent(string)"/> will override this value again.
66+
/// </remarks>
67+
/// <param name="status">
68+
/// The status of the admin setting.
69+
/// </param>
70+
public void AddStatus(bool status)
71+
{
72+
_isEnabled = status;
73+
74+
// Set the raw content to a value that could be parsed.
75+
if (_isEnabled.Value)
76+
{
77+
_rawContent = "Enabled";
78+
}
79+
else
6180
{
62-
_isEnabled = isEnabled.Value;
63-
_parsed = true;
81+
_rawContent = "Disabled";
6482
}
6583
}
6684

@@ -72,12 +90,12 @@ public void AddRawContent(string rawContent)
7290
/// </returns>
7391
public override WinGetAdminSetting? GetInstance()
7492
{
75-
if (!_parsed)
93+
if (!_isEnabled.HasValue)
7694
{
7795
return null;
7896
}
7997

80-
return new WinGetAdminSetting(_entryName, _rawContent, _hasShortenedContent, _isEnabled);
98+
return new WinGetAdminSetting(_entryName, _rawContent, _hasShortenedContent, _isEnabled.Value);
8199
}
82100

83101
/// <inheritdoc/>

src/WGet.NET/Components/WinGet.cs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
using System.Security;
88
using System.Threading.Tasks;
99
using System.Security.Principal;
10+
using System.Collections.Generic;
1011
using WGetNET.Models;
1112
using WGetNET.Helper;
1213
using WGetNET.Parser;
14+
using WGetNET.Builder;
1315
using WGetNET.Exceptions;
1416
using WGetNET.Components.Internal;
1517

@@ -263,7 +265,90 @@ public async Task ExportSettingsToFileAsync(string file)
263265
}
264266
//---------------------------------------------------------------------------------------------
265267

266-
//---Manage Settings---------------------------------------------------------------------------
268+
//---Admin Settings----------------------------------------------------------------------------
269+
/// <summary>
270+
/// Gets all winget admin settings.
271+
/// </summary>
272+
/// <returns>
273+
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetAdminSetting"/> object.
274+
/// </returns>
275+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
276+
/// WinGet is not installed or not found on the system.
277+
/// </exception>
278+
public List<WinGetAdminSetting> GetAdminSettings()
279+
{
280+
List<WinGetAdminSetting> adminSettings = new List<WinGetAdminSetting>();
281+
282+
string settingsJson = ExportSettings();
283+
284+
SettingsModel settings = JsonHelper.StringToObject<SettingsModel>(settingsJson);
285+
if (settings == null)
286+
{
287+
return adminSettings;
288+
}
289+
290+
AdminSettingBuilder builder = new AdminSettingBuilder();
291+
foreach (KeyValuePair<string, bool> entry in settings.AdminSettings)
292+
{
293+
builder.Clear();
294+
295+
builder.AddEntryName(entry.Key);
296+
builder.AddStatus(entry.Value);
297+
298+
WinGetAdminSetting? adminSetting = builder.GetInstance();
299+
if (adminSetting != null)
300+
{
301+
adminSettings.Add(adminSetting);
302+
}
303+
}
304+
305+
return adminSettings;
306+
}
307+
308+
/// <summary>
309+
/// Asynchronously gets all winget admin settings.
310+
/// </summary>
311+
/// <returns>
312+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
313+
/// The result is a <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetAdminSetting"/> object.
314+
/// </returns>
315+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
316+
/// WinGet is not installed or not found on the system.
317+
/// </exception>
318+
public async Task<List<WinGetAdminSetting>> GetAdminSettingsAsync()
319+
{
320+
List<WinGetAdminSetting> adminSettings = new List<WinGetAdminSetting>();
321+
322+
string settingsJson = await ExportSettingsAsync();
323+
324+
#if NETCOREAPP3_1_OR_GREATER
325+
SettingsModel settings = await JsonHelper.StringToObjectAsync<SettingsModel>(settingsJson);
326+
#elif NETSTANDARD2_0
327+
SettingsModel settings = JsonHelper.StringToObject<SettingsModel>(settingsJson);
328+
#endif
329+
if (settings == null)
330+
{
331+
return adminSettings;
332+
}
333+
334+
AdminSettingBuilder builder = new AdminSettingBuilder();
335+
foreach (KeyValuePair<string, bool> entry in settings.AdminSettings)
336+
{
337+
builder.Clear();
338+
339+
builder.AddEntryName(entry.Key);
340+
builder.AddStatus(entry.Value);
341+
342+
WinGetAdminSetting? adminSetting = builder.GetInstance();
343+
if (adminSetting != null)
344+
{
345+
adminSettings.Add(adminSetting);
346+
}
347+
}
348+
349+
return adminSettings;
350+
}
351+
267352
/// <summary>
268353
/// Enables the provided admin setting (Needs administrator rights).
269354
/// </summary>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
using System.Collections.Generic;
6+
#if NETCOREAPP3_1_OR_GREATER
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
#elif NETSTANDARD2_0
10+
using Newtonsoft.Json;
11+
#endif
12+
13+
namespace WGetNET.Models
14+
{
15+
/// <summary>
16+
/// Represents the global winget settings for parsing.
17+
/// </summary>
18+
internal class SettingsModel
19+
{
20+
/// <summary>
21+
/// Gets or sets a <see cref="System.Collections.Generic.Dictionary{TKey, TValue}"/> of admin settings.
22+
/// </summary>
23+
#if NETCOREAPP3_1_OR_GREATER
24+
[JsonPropertyName("adminSettings")]
25+
#elif NETSTANDARD2_0
26+
[JsonProperty("adminSettings")]
27+
#endif
28+
public Dictionary<string, bool> AdminSettings { get; set; } = new();
29+
30+
/// <summary>
31+
/// Gets or sets the user settings file path.
32+
/// </summary>
33+
#if NETCOREAPP3_1_OR_GREATER
34+
[JsonPropertyName("userSettingsFile")]
35+
#elif NETSTANDARD2_0
36+
[JsonProperty("userSettingsFile")]
37+
#endif
38+
public string UserSettingsFile { get; set; } = string.Empty;
39+
}
40+
}

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WGetTest/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Threading.Tasks;
3+
using System.Diagnostics;
34
using System.Collections.Generic;
45
using WGetNET;
5-
using System.Diagnostics;
66

77
namespace WGetTest
88
{
@@ -35,6 +35,8 @@ private void Run()
3535
Console.WriteLine(info.Version);
3636

3737
//---Tests-----------------------------------------------------------------------------
38+
List<WinGetAdminSetting> adminSettings = connector.GetAdminSettings();
39+
3840
//bool enableResult = winget.EnableAdminSetting("LocalManifestFiles");
3941
//bool disableResult = winget.DisableAdminSetting("LocalManifestFiles");
4042

src/WGetTestLegacySupport/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private void Run()
3535
Console.WriteLine(info.Version);
3636

3737
//---Tests-----------------------------------------------------------------------------
38+
List<WinGetAdminSetting> adminSettings = connector.GetAdminSettings();
39+
3840
//bool enableResult = winget.EnableAdminSetting("LocalManifestFiles");
3941
//bool disableResult = winget.DisableAdminSetting("LocalManifestFiles");
4042

0 commit comments

Comments
 (0)