FileDialogStyle.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.IO.Abstractions;
  7. using System.Linq;
  8. using Terminal.Gui.Resources;
  9. using static System.Environment;
  10. using static Terminal.Gui.ConfigurationManager;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// Stores style settings for <see cref="FileDialog"/>.
  14. /// </summary>
  15. public class FileDialogStyle {
  16. readonly IFileSystem _fileSystem;
  17. /// <summary>
  18. /// Gets or sets the default value to use for <see cref="UseColors"/>.
  19. /// This can be populated from .tui config files via <see cref="ConfigurationManager"/>
  20. /// </summary>
  21. [SerializableConfigurationProperty(Scope = typeof (SettingsScope))]
  22. public static bool DefaultUseColors { get; set; }
  23. /// <summary>
  24. /// Gets or sets the default value to use for <see cref="UseUnicodeCharacters"/>.
  25. /// This can be populated from .tui config files via <see cref="ConfigurationManager"/>
  26. /// </summary>
  27. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  28. public static bool DefaultUseUnicodeCharacters { get; set; }
  29. /// <summary>
  30. /// Gets or Sets a value indicating whether different colors
  31. /// should be used for different file types/directories. Defaults
  32. /// to false.
  33. /// </summary>
  34. public bool UseColors { get; set; } = DefaultUseColors;
  35. /// <summary>
  36. /// Gets or sets the class responsible for determining which symbol
  37. /// to use to represent files and directories.
  38. /// </summary>
  39. public FileSystemIconProvider IconProvider { get; set;} = new FileSystemIconProvider();
  40. /// <summary>
  41. /// Gets or sets the class thatis responsible for determining which color
  42. /// to use to represent files and directories when <see cref="UseColors"/> is
  43. /// <see langword="true"/>.
  44. /// </summary>
  45. public FileSystemColorProvider ColorProvider { get;set;} = new FileSystemColorProvider();
  46. /// <summary>
  47. /// Gets or sets the culture to use (e.g. for number formatting).
  48. /// Defaults to <see cref="CultureInfo.CurrentUICulture"/>.
  49. /// </summary>
  50. public CultureInfo Culture {get;set;} = CultureInfo.CurrentUICulture;
  51. /// <summary>
  52. /// Gets or sets the header text displayed in the Filename column of the files table.
  53. /// </summary>
  54. public string FilenameColumnName { get; set; } = Strings.fdFilename;
  55. /// <summary>
  56. /// Gets or sets the header text displayed in the Size column of the files table.
  57. /// </summary>
  58. public string SizeColumnName { get; set; } = Strings.fdSize;
  59. /// <summary>
  60. /// Gets or sets the header text displayed in the Modified column of the files table.
  61. /// </summary>
  62. public string ModifiedColumnName { get; set; } = Strings.fdModified;
  63. /// <summary>
  64. /// Gets or sets the header text displayed in the Type column of the files table.
  65. /// </summary>
  66. public string TypeColumnName { get; set; } = Strings.fdType;
  67. /// <summary>
  68. /// Gets or sets the text displayed in the 'Search' text box when user has not supplied any input yet.
  69. /// </summary>
  70. public string SearchCaption { get; set; } = Strings.fdSearchCaption;
  71. /// <summary>
  72. /// Gets or sets the text displayed in the 'Path' text box when user has not supplied any input yet.
  73. /// </summary>
  74. public string PathCaption { get; set; } = Strings.fdPathCaption;
  75. /// <summary>
  76. /// Gets or sets the text on the 'Ok' button. Typically you may want to change this to
  77. /// "Open" or "Save" etc.
  78. /// </summary>
  79. public string OkButtonText { get; set; } = "Ok";
  80. /// <summary>
  81. /// Gets or sets the text on the 'Cancel' button.
  82. /// </summary>
  83. public string CancelButtonText { get; set; } = "Cancel";
  84. /// <summary>
  85. /// Gets or sets error message when user attempts to select a file type that is not one of <see cref="FileDialog.AllowedTypes"/>
  86. /// </summary>
  87. public string WrongFileTypeFeedback { get; set; } = Strings.fdWrongFileTypeFeedback;
  88. /// <summary>
  89. /// Gets or sets error message when user selects a directory that does not exist and
  90. /// <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  91. /// </summary>
  92. public string DirectoryMustExistFeedback { get; set; } = Strings.fdDirectoryMustExistFeedback;
  93. /// <summary>
  94. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/>
  95. /// and user enters the name of an existing file (File system cannot have a folder with the same name as a file).
  96. /// </summary>
  97. public string FileAlreadyExistsFeedback { get; set; } = Strings.fdFileAlreadyExistsFeedback;
  98. /// <summary>
  99. /// Gets or sets error message when user selects a file that does not exist and
  100. /// <see cref="OpenMode"/> is <see cref="OpenMode.File"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  101. /// </summary>
  102. public string FileMustExistFeedback { get; set; } = Strings.fdFileMustExistFeedback;
  103. /// <summary>
  104. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.File"/>
  105. /// and user enters the name of an existing directory (File system cannot have a folder with the same name as a file).
  106. /// </summary>
  107. public string DirectoryAlreadyExistsFeedback { get; set; } = Strings.fdDirectoryAlreadyExistsFeedback;
  108. /// <summary>
  109. /// Gets or sets error message when user selects a file/dir that does not exist and
  110. /// <see cref="OpenMode"/> is <see cref="OpenMode.Mixed"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  111. /// </summary>
  112. public string FileOrDirectoryMustExistFeedback { get; set; } = Strings.fdFileOrDirectoryMustExistFeedback;
  113. /// <summary>
  114. /// Gets the style settings for the table of files (in currently selected directory).
  115. /// </summary>
  116. public TableStyle TableStyle { get; internal set; }
  117. /// <summary>
  118. /// Gets the style settings for the collapse-able directory/places tree
  119. /// </summary>
  120. public TreeStyle TreeStyle { get; internal set; }
  121. /// <summary>
  122. /// Gets or Sets the method for getting the root tree objects that are displayed in
  123. /// the collapse-able tree in the <see cref="FileDialog"/>. Defaults to all accessible
  124. /// <see cref="System.Environment.GetLogicalDrives"/> and unique
  125. /// <see cref="Environment.SpecialFolder"/>.
  126. /// </summary>
  127. /// <remarks>Must be configured before showing the dialog.</remarks>
  128. public Func<Dictionary<IDirectoryInfo, string>> TreeRootGetter { get; set; }
  129. /// <summary>
  130. /// Gets or sets whether to use advanced unicode characters which might not be installed
  131. /// on all users computers.
  132. /// </summary>
  133. public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
  134. /// <summary>
  135. /// Gets or sets the format to use for date/times in the Modified column.
  136. /// Defaults to <see cref="DateTimeFormatInfo.SortableDateTimePattern"/>
  137. /// of the <see cref="CultureInfo.CurrentCulture"/>
  138. /// </summary>
  139. public string DateFormat { get; set; }
  140. /// <summary>
  141. /// Creates a new instance of the <see cref="FileDialogStyle"/> class.
  142. /// </summary>
  143. public FileDialogStyle (IFileSystem fileSystem)
  144. {
  145. _fileSystem = fileSystem;
  146. TreeRootGetter = DefaultTreeRootGetter;
  147. DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
  148. }
  149. private Dictionary<IDirectoryInfo,string> DefaultTreeRootGetter ()
  150. {
  151. var roots = new Dictionary<IDirectoryInfo, string> ();
  152. try {
  153. foreach (var d in Environment.GetLogicalDrives ()) {
  154. var dir = _fileSystem.DirectoryInfo.New (d);
  155. if (!roots.ContainsKey(dir)) {
  156. roots.Add (dir, d);
  157. }
  158. }
  159. } catch (Exception) {
  160. // Cannot get the system disks thats fine
  161. }
  162. try {
  163. foreach (var special in Enum.GetValues (typeof (Environment.SpecialFolder)).Cast<SpecialFolder> ()) {
  164. try {
  165. var path = Environment.GetFolderPath (special);
  166. if(string.IsNullOrWhiteSpace (path)) {
  167. continue;
  168. }
  169. var dir = _fileSystem.DirectoryInfo.New (path);
  170. if (!roots.ContainsKey (dir) && dir.Exists) {
  171. roots.Add (dir, special.ToString());
  172. }
  173. } catch (Exception) {
  174. // Special file exists but contents are unreadable (permissions?)
  175. // skip it anyway
  176. }
  177. }
  178. } catch (Exception) {
  179. // Cannot get the special files for this OS oh well
  180. }
  181. return roots;
  182. }
  183. }
  184. }