From cc9e7cfcd18014a2119b1d7a3d60b861f9aa5fd3 Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Wed, 10 Mar 2021 09:13:21 +0100 Subject: [PATCH] Rework extension so it uses directly the settings file on disk instead of registry settings. --- .../ConversionPresetReference.cs | 59 +++++ .../FileConverterExtension.cs | 216 ++++++++---------- .../FileConverterExtension.csproj | 6 +- .../PresetDefinition.cs | 41 ---- .../Properties/AssemblyInfo.cs | 4 +- 5 files changed, 164 insertions(+), 162 deletions(-) create mode 100644 Application/FileConverterExtension/ConversionPresetReference.cs delete mode 100644 Application/FileConverterExtension/PresetDefinition.cs diff --git a/Application/FileConverterExtension/ConversionPresetReference.cs b/Application/FileConverterExtension/ConversionPresetReference.cs new file mode 100644 index 0000000..91e1a08 --- /dev/null +++ b/Application/FileConverterExtension/ConversionPresetReference.cs @@ -0,0 +1,59 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverterExtension +{ + using System.Xml.Serialization; + + [XmlRoot("ConversionPreset")] + [XmlType("ConversionPreset")] + public class PresetReference + { + private string fullName; + + private PresetReference() + { + } + + [XmlAttribute("Name")] + public string FullName + { + get => this.fullName; + set + { + this.fullName = value; + + if (!string.IsNullOrEmpty(this.fullName)) + { + string[] folders = this.fullName.Split('/'); + if (folders.Length > 0) + { + this.Name = folders[folders.Length - 1]; + System.Array.Resize(ref folders, folders.Length - 1); + this.Folders = folders; + } + } + } + } + + [XmlElement] + public string[] InputTypes + { + get; + set; + } + + [XmlIgnore] + public string Name + { + get; + private set; + } + + [XmlIgnore] + public string[] Folders + { + get; + private set; + } + } +} diff --git a/Application/FileConverterExtension/FileConverterExtension.cs b/Application/FileConverterExtension/FileConverterExtension.cs index 2ef9b24..a91b242 100644 --- a/Application/FileConverterExtension/FileConverterExtension.cs +++ b/Application/FileConverterExtension/FileConverterExtension.cs @@ -2,7 +2,6 @@ namespace FileConverterExtension { - using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; @@ -12,8 +11,6 @@ namespace FileConverterExtension using System.Text; using System.Windows.Forms; - using Microsoft.Win32; - using SharpShell.Attributes; using SharpShell.SharpContextMenu; @@ -24,46 +21,30 @@ namespace FileConverterExtension [COMServerAssociation(AssociationType.AllFiles)] public class FileConverterExtension : SharpContextMenu { - private string fileConverterPath; - private RegistryKey fileConverterRegistryKey; - private List presetList = new List(); - private List compatibleInputExtensions = new List(); + private PresetReference[] presetReferences = null; + private List menuEntries = new List(); - private RegistryKey FileConverterRegistryKey + private HashSet extensionCache = new HashSet(); + + private class MenuEntry { - get - { - if (this.fileConverterRegistryKey == null) - { - this.fileConverterRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); - if (this.fileConverterRegistryKey == null) - { - throw new Exception("Can't retrieve file converter registry entry."); - } - } + public PresetReference PresetReference; + public bool Enabled; + public int ExtensionRefCount; - return this.fileConverterRegistryKey; + public MenuEntry(PresetReference presetReference) + { + this.PresetReference = presetReference; + this.Enabled = false; + this.ExtensionRefCount = 0; } } - - private string FileConverterPath - { - get - { - if (string.IsNullOrEmpty(this.fileConverterPath)) - { - this.fileConverterPath = this.FileConverterRegistryKey.GetValue("Path") as string; - } - - return this.fileConverterPath; - } - } - + private bool DisplayPresetIcons { get { - string displayPresetIcons = this.FileConverterRegistryKey.GetValue("DisplayPresetIcons") as string; + string displayPresetIcons = PathHelpers.FileConverterRegistryKey.GetValue("DisplayPresetIcons") as string; if (displayPresetIcons == null) { return false; @@ -78,41 +59,29 @@ namespace FileConverterExtension } } - private IEnumerable CompatibleInputExtensions + private PresetReference[] PresetReferences { get { - if (this.compatibleInputExtensions != null && this.compatibleInputExtensions.Count > 0) - { - return this.compatibleInputExtensions; - } + this.LoadExtensionSettingsIfNecessary(); - this.compatibleInputExtensions.Clear(); - string registryValue = this.FileConverterRegistryKey.GetValue("CompatibleInputExtensions") as string; - string[] extensions = registryValue.Split(';'); - for (int index = 0; index < extensions.Length; index++) - { - this.compatibleInputExtensions.Add(extensions[index]); - } - - return this.compatibleInputExtensions; + return this.presetReferences; } } protected override bool CanShowMenu() { - foreach (string filePath in this.SelectedItemPaths) - { - string extension = Path.GetExtension(filePath); - if (string.IsNullOrEmpty(extension)) - { - continue; - } + this.RefreshExtensionCacheFromSelectedItems(); - extension = extension.Substring(1).ToLowerInvariant(); - if (this.CompatibleInputExtensions.Contains(extension)) + PresetReference[] presets = this.PresetReferences; + foreach (string extension in this.extensionCache) + { + foreach (PresetReference presetReference in presets) { - return true; + if (presetReference.InputTypes.Contains(extension)) + { + return true; + } } } @@ -133,12 +102,12 @@ namespace FileConverterExtension Image = new Icon(Properties.Resources.ApplicationIcon, SystemInformation.SmallIconSize).ToBitmap(), }; - foreach (PresetDefinition preset in this.presetList) + foreach (MenuEntry menuEntry in this.menuEntries) { ToolStripMenuItem root = fileConverterItem; - if (preset.Folders != null) + if (menuEntry.PresetReference.Folders != null) { - foreach (string folder in preset.Folders) + foreach (string folder in menuEntry.PresetReference.Folders) { ToolStripItem[] folderItems = root.DropDownItems.Find(folder, false); if (folderItems.Length == 0) @@ -173,8 +142,8 @@ namespace FileConverterExtension ToolStripMenuItem subItem = new ToolStripMenuItem { - Text = preset.Name, - Enabled = preset.Enabled + Text = menuEntry.PresetReference.Name, + Enabled = menuEntry.Enabled }; if (displayPresetIcons) @@ -183,10 +152,10 @@ namespace FileConverterExtension } root.DropDownItems.Add(subItem); - subItem.Click += (sender, args) => this.ConvertFiles(preset.FullName); + subItem.Click += (sender, args) => this.ConvertFiles(menuEntry.PresetReference.FullName); } - if (this.presetList.Count > 0) + if (this.menuEntries.Count > 0) { fileConverterItem.DropDownItems.Add(new ToolStripSeparator()); } @@ -207,10 +176,10 @@ namespace FileConverterExtension return menu; } - private void RefreshPresetList() + private void RefreshExtensionCacheFromSelectedItems() { // Retrieve selected files extensions. - List extensions = new List(); + this.extensionCache.Clear(); foreach (string filePath in this.SelectedItemPaths) { string extension = Path.GetExtension(filePath); @@ -220,79 +189,90 @@ namespace FileConverterExtension } extension = extension.Substring(1).ToLowerInvariant(); - if (extensions.Contains(extension)) - { - continue; - } - extensions.Add(extension); + this.extensionCache.Add(extension); } + } - // Compute preset list. - this.presetList.Clear(); - RegistryKey fileConverterKey = this.FileConverterRegistryKey; - for (int extensionIndex = 0; extensionIndex < extensions.Count; extensionIndex++) + private void RefreshPresetList() + { + this.RefreshExtensionCacheFromSelectedItems(); + + // Activate compatible menu entries. + PresetReference[] presets = this.presetReferences; + this.menuEntries.Clear(); + foreach (string extension in this.extensionCache) { - string extension = extensions[extensionIndex]; - RegistryKey extensionKey = fileConverterKey.OpenSubKey(extension); - if (extensionKey == null) + foreach (PresetReference presetReference in presets) { - continue; - } - - string presetsString = extensionKey.GetValue("Presets") as string; - if (presetsString == null) - { - continue; - } - - string[] presets = presetsString.Split(';'); - - for (int presetIndex = 0; presetIndex < presets.Length; presetIndex++) - { - string presetName = presets[presetIndex]; - PresetDefinition presetDefinition = this.presetList.FirstOrDefault(match => match.FullName == presetName); - if (presetDefinition == null) + if (!presetReference.InputTypes.Contains(extension)) { - string[] folders = presetName.Split('/'); - if (folders.Length == 0) - { - continue; - } - - string name = folders[folders.Length - 1]; - Array.Resize(ref folders, folders.Length - 1); - - presetDefinition = new PresetDefinition(presetName, name, folders); - this.presetList.Add(presetDefinition); + continue; } - presetDefinition.ExtensionRefCount++; + MenuEntry menuEntry = this.menuEntries.Find(entry => entry.PresetReference.FullName == presetReference.FullName); + if (menuEntry == null) + { + menuEntry = new MenuEntry(presetReference); + this.menuEntries.Add(menuEntry); + } + + menuEntry.ExtensionRefCount++; } } - // Update enable states. - for (int index = 0; index < this.presetList.Count; index++) + // Enable presets compatible with all input files. + foreach (MenuEntry menuEntry in this.menuEntries) { - this.presetList[index].Enabled = this.presetList[index].ExtensionRefCount == extensions.Count; + menuEntry.Enabled = menuEntry.ExtensionRefCount == this.extensionCache.Count; + } + } + + private void LoadExtensionSettingsIfNecessary() + { + if (this.presetReferences != null) + { + return; + } + + if (File.Exists(PathHelpers.UserSettingsFilePath)) + { + try + { + XmlHelpers.LoadFromFile("Settings", PathHelpers.UserSettingsFilePath, out this.presetReferences); + return; + } + catch + { + // Can't handle this error in the explorer extension. + } + } + + try + { + XmlHelpers.LoadFromFile("Settings", PathHelpers.DefaultSettingsFilePath, out this.presetReferences); + } + catch + { + // Can't handle this error in the explorer extension. } } private void OpenSettings() { - if (string.IsNullOrEmpty(this.FileConverterPath)) + if (string.IsNullOrEmpty(PathHelpers.FileConverterPath)) { MessageBox.Show("Can't retrieve the file converter executable path. You should try to reinstall the application."); return; } - if (!File.Exists(this.FileConverterPath)) + if (!File.Exists(PathHelpers.FileConverterPath)) { - MessageBox.Show($"Can't find the file converter executable ({this.FileConverterPath}). You should try to reinstall the application."); + MessageBox.Show($"Can't find the file converter executable ({PathHelpers.FileConverterPath}). You should try to reinstall the application."); return; } - ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath) + ProcessStartInfo processStartInfo = new ProcessStartInfo(PathHelpers.FileConverterPath) { CreateNoWindow = false, UseShellExecute = false, @@ -309,19 +289,19 @@ namespace FileConverterExtension private void ConvertFiles(string presetName) { - if (string.IsNullOrEmpty(this.FileConverterPath)) + if (string.IsNullOrEmpty(PathHelpers.FileConverterPath)) { MessageBox.Show("Can't retrieve the file converter executable path. You should try to reinstall the application."); return; } - if (!File.Exists(this.FileConverterPath)) + if (!File.Exists(PathHelpers.FileConverterPath)) { - MessageBox.Show($"Can't find the file converter executable ({this.FileConverterPath}). You should try to reinstall the application."); + MessageBox.Show($"Can't find the file converter executable ({PathHelpers.FileConverterPath}). You should try to reinstall the application."); return; } - ProcessStartInfo processStartInfo = new ProcessStartInfo(this.FileConverterPath) + ProcessStartInfo processStartInfo = new ProcessStartInfo(PathHelpers.FileConverterPath) { CreateNoWindow = false, UseShellExecute = false, diff --git a/Application/FileConverterExtension/FileConverterExtension.csproj b/Application/FileConverterExtension/FileConverterExtension.csproj index 6199dd3..49034f6 100644 --- a/Application/FileConverterExtension/FileConverterExtension.csproj +++ b/Application/FileConverterExtension/FileConverterExtension.csproj @@ -57,10 +57,13 @@ + + - + + true @@ -69,6 +72,7 @@ True Resources.resx + diff --git a/Application/FileConverterExtension/PresetDefinition.cs b/Application/FileConverterExtension/PresetDefinition.cs deleted file mode 100644 index a4751e9..0000000 --- a/Application/FileConverterExtension/PresetDefinition.cs +++ /dev/null @@ -1,41 +0,0 @@ -// License: http://www.gnu.org/licenses/gpl.html GPL version 3. - -namespace FileConverterExtension -{ - public class PresetDefinition - { - public PresetDefinition(string fullName, string name, string[] folders) - { - this.FullName = fullName; - this.Name = name; - this.Folders = folders; - } - - public string FullName - { - get; - } - - public string Name - { - get; - } - - public string[] Folders - { - get; - } - - public bool Enabled - { - get; - set; - } - - public int ExtensionRefCount - { - get; - set; - } - } -} diff --git a/Application/FileConverterExtension/Properties/AssemblyInfo.cs b/Application/FileConverterExtension/Properties/AssemblyInfo.cs index 7db2355..233d90f 100644 --- a/Application/FileConverterExtension/Properties/AssemblyInfo.cs +++ b/Application/FileConverterExtension/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.1.0.0")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")]