From 32514af57c0944bb3d16953f57213a2fafd7ce5e Mon Sep 17 00:00:00 2001 From: Tichau Date: Mon, 11 Jan 2016 16:12:31 +0100 Subject: [PATCH] Add registry to contains implicit user info Registry is save each time the application quit. --- .../FileConverter/FileConverter.csproj | 1 + Application/FileConverter/Registry.cs | 203 ++++++++++++++++++ Application/FileConverter/Settings.cs | 6 +- .../Windows/SettingsWindow.xaml.cs | 2 +- 4 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 Application/FileConverter/Registry.cs diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index b3499ad..ddcd7a8 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -104,6 +104,7 @@ True True + diff --git a/Application/FileConverter/Registry.cs b/Application/FileConverter/Registry.cs new file mode 100644 index 0000000..b2fbd3b --- /dev/null +++ b/Application/FileConverter/Registry.cs @@ -0,0 +1,203 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Xml.Serialization; + + public class Registry : IDisposable + { + private static Registry instance; + + private Dictionary registryEntries = new Dictionary(); + + ~Registry() + { + this.Dispose(); + Registry.instance = null; + } + + [XmlElement("Entry")] + public Entry[] SerializableEntries + { + get + { + Entry[] entries = new Entry[this.registryEntries.Count]; + int index = 0; + foreach (KeyValuePair kvp in this.registryEntries) + { + if (kvp.Value == null) + { + continue; + } + + entries[index] = new Entry(kvp.Key, kvp.Value); + index++; + } + + return entries; + } + + set + { + if (value == null) + { + return; + } + + this.registryEntries.Clear(); + for (int index = 0; index < value.Length; index++) + { + Entry entry = value[index]; + if (entry.Key == null) + { + continue; + } + + if (this.registryEntries.ContainsKey(entry.Key)) + { + Diagnostics.Debug.Log("Ignore registry entry {0}.", entry.Key); + continue; + } + + this.registryEntries.Add(entry.Key, entry.Value); + } + } + } + + private static Registry Instance + { + get + { + if (Registry.instance == null) + { + Registry.Load(); + } + + return Registry.instance; + } + } + + public static T GetValue(string key) + { + Registry registry = Registry.Instance; + + string stringValue; + if (!registry.registryEntries.TryGetValue(key, out stringValue)) + { + return default(T); + } + + try + { + object value = System.Convert.ChangeType(stringValue, typeof(T)); + return (T)value; + } + catch (Exception exception) + { + Diagnostics.Debug.LogError("Can't convert registry value: {0}.", exception.Message); + } + + return default(T); + } + + public static void SetValue(string key, T value) + { + Registry registry = Registry.Instance; + + if (!registry.registryEntries.ContainsKey(key)) + { + registry.registryEntries.Add(key, null); + } + + try + { + string stringValue = (string)System.Convert.ChangeType(value, typeof(string)); + registry.registryEntries[key] = stringValue; + } + catch (Exception exception) + { + Diagnostics.Debug.LogError("Can't convert registry value: {0}.", exception.Message); + } + } + + public void Dispose() + { + // SAVE + string registryFilePath = Registry.GetUserRegistryFilePath(); + + try + { + XmlHelpers.SaveToFile("Registry", registryFilePath, this); + } + catch (Exception exception) + { + Diagnostics.Debug.LogError("Fail to save registry. {0}", exception.Message); + } + } + + private static void Load() + { + string registryFilePath = Registry.GetUserRegistryFilePath(); + if (!File.Exists(registryFilePath)) + { + Registry.instance = new Registry(); + return; + } + + try + { + Registry.instance = null; + XmlHelpers.LoadFromFile("Registry", registryFilePath, out Registry.instance); + } + catch (Exception exception) + { + Registry.instance = new Registry(); + Diagnostics.Debug.LogError("Fail to load registry. {0}", exception.Message); + while (exception.InnerException != null) + { + exception = exception.InnerException; + Diagnostics.Debug.LogError("Inner exception: {0}", exception.Message); + } + } + } + + private static string GetUserRegistryFilePath() + { + string path = PathHelpers.GetUserDataFolderPath(); + path = Path.Combine(path, "Registry.xml"); + return path; + } + + [XmlRoot("Entry")] + public struct Entry + { + public Entry(string key, string value) + { + this.Key = key; + this.Value = value; + } + + [XmlAttribute] + public string Key + { + get; + set; + } + + [XmlText] + public string Value + { + get; + set; + } + } + + public static class Keys + { + public static string LastUpdateCheckDate = "LastUpdateCheckDate"; + } + } +} diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index 35a10d4..f39c338 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -72,7 +72,7 @@ namespace FileConverter Settings settings = null; XmlHelpers.LoadFromFile("Settings", temporaryFilePath, out settings); - RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); + RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) { Diagnostics.Debug.LogError("Can't apply settings in registry. (code 0x03)"); @@ -211,7 +211,7 @@ namespace FileConverter return false; } - RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter", RegistryKeyPermissionCheck.ReadWriteSubTree); + RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\FileConverter", RegistryKeyPermissionCheck.ReadWriteSubTree); if (registryKey == null) { MessageBox.Show("Can't apply settings in registry. (code 0x05)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); @@ -322,7 +322,7 @@ namespace FileConverter private static bool IsRegistryNeedModifications(Dictionary> registryEntries) { // Compare to registry entries. - RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); + RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) { return true; diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml.cs b/Application/FileConverter/Windows/SettingsWindow.xaml.cs index dcd1ab1..667891b 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml.cs +++ b/Application/FileConverter/Windows/SettingsWindow.xaml.cs @@ -319,7 +319,7 @@ namespace FileConverter private void InitializeCompatibleInputExtensions() { - RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); + RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) { MessageBox.Show("Can't retrieve the list of compatible input extensions. (code 0x09)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);