MarkdownViewerConfiguration.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using Kbg.NppPluginNET.PluginInfrastructure;
  2. using PdfSharp;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. namespace com.insanitydesign.MarkdownViewerPlusPlus
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public class MarkdownViewerConfiguration
  17. {
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public struct Options
  22. {
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public bool synchronizeScrolling;
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public string fileExtensions;
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public bool inclNewFiles;
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. private string htmlCssStyle;
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. public string HtmlCssStyle {
  43. get {
  44. if (htmlCssStyle == null) return "";
  45. return htmlCssStyle.Replace(@" \n ", Environment.NewLine);
  46. }
  47. set {
  48. htmlCssStyle = value.Replace(Environment.NewLine, @" \n ");
  49. }
  50. }
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. public PageOrientation pdfOrientation;
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. public PageSize pdfPageSize;
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. public string margins;
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <returns></returns>
  67. public int[] GetMargins()
  68. {
  69. try
  70. {
  71. return this.margins.Split(',').Select(x => int.Parse(x)).ToArray();
  72. }
  73. catch { }
  74. //Default to 5mm
  75. return new int[] { 5, 5, 5, 5 };
  76. }
  77. /// <summary>
  78. ///
  79. /// </summary>
  80. public bool pdfOpenExport;
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. public bool htmlOpenExport;
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. protected string iniFilePath = null;
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. protected string assemblyName = "";
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public Options options;
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. public MarkdownViewerConfiguration()
  102. {
  103. this.assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
  104. Init();
  105. }
  106. /// <summary>
  107. /// Used for initing and re-initing
  108. /// </summary>
  109. public void Init()
  110. {
  111. //
  112. StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
  113. Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
  114. this.iniFilePath = sbIniFilePath.ToString();
  115. // if config path doesn't exist, we create it
  116. if (!Directory.Exists(iniFilePath))
  117. {
  118. Directory.CreateDirectory(iniFilePath);
  119. }
  120. //
  121. this.iniFilePath = Path.Combine(iniFilePath, this.assemblyName + ".ini");
  122. Load();
  123. }
  124. /// <summary>
  125. /// Load all configuration settings
  126. /// </summary>
  127. public void Load()
  128. {
  129. //Grab ini file settings based on struct members
  130. this.options = GetDefaultOptions();
  131. //Unbox/Box magic to set structs
  132. object options = this.options;
  133. foreach (FieldInfo field in this.options.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
  134. {
  135. if (field.FieldType == typeof(bool))
  136. {
  137. field.SetValue(options, (Win32.GetPrivateProfileInt(this.assemblyName, field.Name, 0, iniFilePath) != 0));
  138. }
  139. else if (field.FieldType == typeof(string))
  140. {
  141. StringBuilder sbFieldValue = new StringBuilder(32767);
  142. Win32.GetPrivateProfileString(this.assemblyName, field.Name, "", sbFieldValue, 32767, iniFilePath);
  143. field.SetValue(options, sbFieldValue.ToString());
  144. }
  145. else if (field.FieldType.IsEnum)
  146. {
  147. StringBuilder sbFieldValue = new StringBuilder(Win32.MAX_PATH);
  148. Win32.GetPrivateProfileString(this.assemblyName, field.Name, "", sbFieldValue, Win32.MAX_PATH, iniFilePath);
  149. try
  150. {
  151. field.SetValue(options, Enum.Parse(field.FieldType, sbFieldValue.ToString()));
  152. }
  153. catch { }
  154. }
  155. }
  156. //Unbox/Box magic to set structs
  157. this.options = (Options)options;
  158. }
  159. /// <summary>
  160. /// Save all made changes to the configuration
  161. /// </summary>
  162. public void Save()
  163. {
  164. //Save ini file settings based on struct members
  165. foreach (FieldInfo field in this.options.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
  166. {
  167. var value = field.GetValue(this.options);
  168. value = value != null ? value : "";
  169. if (field.FieldType == typeof(bool))
  170. {
  171. Win32.WritePrivateProfileString(this.assemblyName, field.Name, ((bool)value) ? "1" : "0", iniFilePath);
  172. }
  173. else if (field.FieldType == typeof(string) || field.FieldType.IsEnum)
  174. {
  175. Win32.WritePrivateProfileString(this.assemblyName, field.Name, value.ToString(), iniFilePath);
  176. }
  177. }
  178. }
  179. /// <summary>
  180. ///
  181. /// </summary>
  182. /// <param name="fileExtension"></param>
  183. /// <param name="fileName"></param>
  184. /// <returns></returns>
  185. public bool ValidateFileExtension(string fileExtension, string fileName = "")
  186. {
  187. //Nothing set -> Render all
  188. if (this.options.fileExtensions == null || this.options.fileExtensions == "") return true;
  189. //Something set but nothing given, check for "new " files (dirty dirty ^^)
  190. if (fileExtension == null || fileExtension == "") return this.options.inclNewFiles && fileName.StartsWith("new ");
  191. //Otherwise check
  192. return this.options.fileExtensions.ToLower().Contains(fileExtension.ToLower());
  193. }
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. /// <returns>An Options object containing some pre-set default options</returns>
  198. protected Options GetDefaultOptions()
  199. {
  200. Options options = new Options()
  201. {
  202. inclNewFiles = true,
  203. pdfOrientation = PageOrientation.Portrait,
  204. pdfPageSize = PageSize.A4,
  205. synchronizeScrolling = false,
  206. margins = "5,5,5,5",
  207. pdfOpenExport = false,
  208. htmlOpenExport = false
  209. };
  210. return options;
  211. }
  212. }
  213. }