Skip to content

Commit

Permalink
Shared base class. Closes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
alxnull committed Feb 8, 2019
1 parent 9c4c716 commit eba8df6
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 138 deletions.
71 changes: 3 additions & 68 deletions PortableJsonSettingsProvider/PortableJsonSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -13,66 +10,30 @@ namespace Bluegrams.Application
/// <summary>
/// Provides portable, persistent application settings in JSON format.
/// </summary>
public class PortableJsonSettingsProvider : SettingsProvider, IApplicationSettingsProvider
public class PortableJsonSettingsProvider : PortableSettingsProviderBase, IApplicationSettingsProvider
{
/// <summary>
/// Specifies the name of the settings file to be used.
/// </summary>
public static string SettingsFileName { get; set; } = "settings.json";

/// <summary>
/// Specifies the directory of the settings file.
/// </summary>
public static string SettingsDirectory { get; set; } = AppDomain.CurrentDomain.BaseDirectory;

/// <summary>
/// Specifies if all settings should be roaming.
/// </summary>
public static bool AllRoaming { get; set; } = false;

public override string Name => "PortableJsonSettingsProvider";

/// <summary>
/// Applies this settings provider to each property of the given settings.
/// </summary>
/// <param name="settingsList">An array of settings.</param>
public static void ApplyProvider(params ApplicationSettingsBase[] settingsList)
{
foreach (var settings in settingsList)
{
var provider = new PortableJsonSettingsProvider();
settings.Providers.Clear();
settings.Providers.Add(provider);
foreach (SettingsProperty prop in settings.Properties)
prop.Provider = provider;
settings.Reload();
}
}
=> ApplyProvider(new PortableJsonSettingsProvider(), settingsList);

private string ApplicationSettingsFile => Path.Combine(SettingsDirectory, SettingsFileName);

public override string ApplicationName { get { return Assembly.GetExecutingAssembly().GetName().Name; } set { } }

public override void Initialize(string name, NameValueCollection config)
{
if (String.IsNullOrEmpty(name)) name = Name;
base.Initialize(name, config);
}

public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property)
{
throw new NotImplementedException();
}

public void Reset(SettingsContext context)
public override void Reset(SettingsContext context)
{
if (File.Exists(ApplicationSettingsFile))
File.Delete(ApplicationSettingsFile);
}

public void Upgrade(SettingsContext context, SettingsPropertyCollection properties)
{ /* don't do anything here*/ }

private JObject GetJObject()
{
// to deal with multiple settings providers accessing the same file, reload on every set or get request.
Expand Down Expand Up @@ -208,31 +169,5 @@ private void setSettingsValue(JObject jObject, string scope, SettingsPropertyVal
}
}
}

// Iterates through the properties' attributes to determine whether it's user-scoped or application-scoped.
private bool IsUserScoped(SettingsProperty prop)
{
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(UserScopedSettingAttribute))
return true;
}
return false;
}

// Iterates through the properties' attributes to determine whether it's set to roam.
private bool IsRoaming(SettingsProperty prop)
{
if (AllRoaming)
return true;
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(SettingsManageabilityAttribute))
return true;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<PackageTags>winforms wpf application settings portable json</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared\PortableSettingsProviderBase.cs" Link="PortableSettingsProviderBase.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
Expand Down
74 changes: 4 additions & 70 deletions PortableSettingsProvider/PortableSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,38 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;

namespace Bluegrams.Application
{
/// <summary>
/// Provides portable, persistent application settings.
/// </summary>
public class PortableSettingsProvider : SettingsProvider, IApplicationSettingsProvider
public class PortableSettingsProvider : PortableSettingsProviderBase
{
/// <summary>
/// Specifies the name of the settings file to be used.
/// </summary>
public static string SettingsFileName { get; set; } = "portable.config";

/// <summary>
/// Specifies the directory of the settings file.
/// </summary>
public static string SettingsDirectory { get; set; } = AppDomain.CurrentDomain.BaseDirectory;

/// <summary>
/// Specifies if all settings should be roaming.
/// </summary>
public static bool AllRoaming { get; set; } = false;

public override string Name => "PortableSettingsProvider";

/// <summary>
/// Applies this settings provider to each property of the given settings.
/// </summary>
/// <param name="settingsList">An array of settings.</param>
public static void ApplyProvider(params ApplicationSettingsBase[] settingsList)
{
foreach (var settings in settingsList)
{
var provider = new PortableSettingsProvider();
settings.Providers.Clear();
settings.Providers.Add(provider);
foreach (SettingsProperty prop in settings.Properties)
prop.Provider = provider;
settings.Reload();
}
}
=> ApplyProvider(new PortableSettingsProvider(), settingsList);

private string ApplicationSettingsFile => Path.Combine(SettingsDirectory, SettingsFileName);


public override string ApplicationName { get { return Assembly.GetExecutingAssembly().GetName().Name; } set { } }

public override void Initialize(string name, NameValueCollection config)
{
if (String.IsNullOrEmpty(name)) name = Name;
base.Initialize(name, config);
}

public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property)
{
throw new NotImplementedException();
}

public void Reset(SettingsContext context)
public override void Reset(SettingsContext context)
{
if (File.Exists(ApplicationSettingsFile))
File.Delete(ApplicationSettingsFile);
}

public void Upgrade(SettingsContext context, SettingsPropertyCollection properties)
{ /* don't do anything here*/ }

private XDocument GetXmlDoc()
{
// to deal with multiple settings providers accessing the same file, reload on every set or get request.
Expand Down Expand Up @@ -207,31 +167,5 @@ private void setXmlValue(XDocument xmlDoc, string scope, SettingsPropertyValue v
}
}
}

// Iterates through the properties' attributes to determine whether it's user-scoped or application-scoped.
private bool IsUserScoped(SettingsProperty prop)
{
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(UserScopedSettingAttribute))
return true;
}
return false;
}

// Iterates through the properties' attributes to determine whether it's set to roam.
private bool IsRoaming(SettingsProperty prop)
{
if (AllRoaming)
return true;
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(SettingsManageabilityAttribute))
return true;
}
return false;
}
}
}
3 changes: 3 additions & 0 deletions PortableSettingsProvider/PortableSettingsProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Read more at https://www.codeproject.com/Articles/1238550/Making-Application-Set
(Note: The package license has changed to BSD-3-Clause.)</PackageReleaseNotes>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Shared\PortableSettingsProviderBase.cs" Link="PortableSettingsProviderBase.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions SettingsProviders.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{A1671D86-B204-4FE3-80F0-CB5A1A5E80D1}"
ProjectSection(SolutionItems) = preProject
Shared\PortableSettingsProviderBase.cs = Shared\PortableSettingsProviderBase.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -79,6 +84,9 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A1671D86-B204-4FE3-80F0-CB5A1A5E80D1} = {6B71E16E-C9E5-49F6-9072-F072481C3561}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {288B84D7-34C9-40D6-8B02-969FDC8235B8}
EndGlobalSection
Expand Down
84 changes: 84 additions & 0 deletions Shared/PortableSettingsProviderBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;

namespace Bluegrams.Application
{
/// <summary>
/// A shared base class for portable settings providers.
/// </summary>
public abstract class PortableSettingsProviderBase : SettingsProvider, IApplicationSettingsProvider
{
/// <summary>
/// Specifies if all settings should be roaming.
/// </summary>
public static bool AllRoaming { get; set; } = false;

/// <summary>
/// Specifies the directory of the settings file.
/// </summary>
public static string SettingsDirectory { get; set; } = AppDomain.CurrentDomain.BaseDirectory;

public override string ApplicationName { get { return Assembly.GetExecutingAssembly().GetName().Name; } set { } }

protected static void ApplyProvider(PortableSettingsProviderBase provider, params ApplicationSettingsBase[] settingsList)
{
foreach (var settings in settingsList)
{
settings.Providers.Clear();
settings.Providers.Add(provider);
foreach (SettingsProperty prop in settings.Properties)
prop.Provider = provider;
settings.Reload();
}
}

public override void Initialize(string name, NameValueCollection config)
{
if (String.IsNullOrEmpty(name)) name = Name;
base.Initialize(name, config);
}

public virtual SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property)
{
throw new NotImplementedException();
}

public abstract void Reset(SettingsContext context);

public virtual void Upgrade(SettingsContext context, SettingsPropertyCollection properties)
{ /* don't do anything here*/ }

/// <summary>
/// Iterates through a property's attributes to determine whether it is user-scoped or application-scoped.
/// </summary>
protected bool IsUserScoped(SettingsProperty prop)
{
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(UserScopedSettingAttribute))
return true;
}
return false;
}

/// <summary>
/// Iterates through a property's attributes to determine whether it is set to roam.
/// </summary>
protected bool IsRoaming(SettingsProperty prop)
{
if (AllRoaming)
return true;
foreach (DictionaryEntry d in prop.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a.GetType() == typeof(SettingsManageabilityAttribute))
return true;
}
return false;
}
}
}

0 comments on commit eba8df6

Please sign in to comment.