using Kbg.NppPluginNET.PluginInfrastructure; using PdfSharp; using System; using System.IO; using System.Linq; using System.Reflection; using System.Text; /// /// /// namespace com.insanitydesign.MarkdownViewerPlusPlus { /// /// /// public class MarkdownViewerConfiguration { /// /// /// public struct Options { /// /// /// public bool synchronizeScrolling; /// /// /// public string fileExtensions; /// /// /// public bool inclNewFiles; /// /// /// private string htmlCssStyle; /// /// /// public string HtmlCssStyle { get { if (htmlCssStyle == null) return ""; return htmlCssStyle.Replace(@" \n ", Environment.NewLine); } set { htmlCssStyle = value.Replace(Environment.NewLine, @" \n "); } } /// /// /// public PageOrientation pdfOrientation; /// /// /// public PageSize pdfPageSize; /// /// /// public string margins; /// /// /// /// public int[] GetMargins() { try { return this.margins.Split(',').Select(x => int.Parse(x)).ToArray(); } catch { } //Default to 5mm return new int[] { 5, 5, 5, 5 }; } /// /// /// public bool pdfOpenExport; /// /// /// public bool htmlOpenExport; } /// /// /// protected string iniFilePath = null; /// /// /// protected string assemblyName = ""; /// /// /// public Options options; /// /// /// public MarkdownViewerConfiguration() { this.assemblyName = Assembly.GetExecutingAssembly().GetName().Name; Init(); } /// /// Used for initing and re-initing /// public void Init() { // StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH); Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath); this.iniFilePath = sbIniFilePath.ToString(); // if config path doesn't exist, we create it if (!Directory.Exists(iniFilePath)) { Directory.CreateDirectory(iniFilePath); } // this.iniFilePath = Path.Combine(iniFilePath, this.assemblyName + ".ini"); Load(); } /// /// Load all configuration settings /// public void Load() { //Grab ini file settings based on struct members this.options = GetDefaultOptions(); //Unbox/Box magic to set structs object options = this.options; foreach (FieldInfo field in this.options.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (field.FieldType == typeof(bool)) { field.SetValue(options, (Win32.GetPrivateProfileInt(this.assemblyName, field.Name, 0, iniFilePath) != 0)); } else if (field.FieldType == typeof(string)) { StringBuilder sbFieldValue = new StringBuilder(32767); Win32.GetPrivateProfileString(this.assemblyName, field.Name, "", sbFieldValue, 32767, iniFilePath); field.SetValue(options, sbFieldValue.ToString()); } else if (field.FieldType.IsEnum) { StringBuilder sbFieldValue = new StringBuilder(Win32.MAX_PATH); Win32.GetPrivateProfileString(this.assemblyName, field.Name, "", sbFieldValue, Win32.MAX_PATH, iniFilePath); try { field.SetValue(options, Enum.Parse(field.FieldType, sbFieldValue.ToString())); } catch { } } } //Unbox/Box magic to set structs this.options = (Options)options; } /// /// Save all made changes to the configuration /// public void Save() { //Save ini file settings based on struct members foreach (FieldInfo field in this.options.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { var value = field.GetValue(this.options); value = value != null ? value : ""; if (field.FieldType == typeof(bool)) { Win32.WritePrivateProfileString(this.assemblyName, field.Name, ((bool)value) ? "1" : "0", iniFilePath); } else if (field.FieldType == typeof(string) || field.FieldType.IsEnum) { Win32.WritePrivateProfileString(this.assemblyName, field.Name, value.ToString(), iniFilePath); } } } /// /// /// /// /// /// public bool ValidateFileExtension(string fileExtension, string fileName = "") { //Nothing set -> Render all if (this.options.fileExtensions == null || this.options.fileExtensions == "") return true; //Something set but nothing given, check for "new " files (dirty dirty ^^) if (fileExtension == null || fileExtension == "") return this.options.inclNewFiles && fileName.StartsWith("new "); //Otherwise check return this.options.fileExtensions.ToLower().Contains(fileExtension.ToLower()); } /// /// /// /// An Options object containing some pre-set default options protected Options GetDefaultOptions() { Options options = new Options() { inclNewFiles = true, pdfOrientation = PageOrientation.Portrait, pdfPageSize = PageSize.A4, synchronizeScrolling = false, margins = "5,5,5,5", pdfOpenExport = false, htmlOpenExport = false }; return options; } } }