FileDialogStyle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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; internal 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; internal 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 error message when user attempts to select a file type that is not one of <see cref="FileDialog.AllowedTypes"/>
  82. /// </summary>
  83. public string WrongFileTypeFeedback { get; internal set; } = Strings.fdWrongFileTypeFeedback;
  84. /// <summary>
  85. /// Gets or sets error message when user selects a directory that does not exist and
  86. /// <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  87. /// </summary>
  88. public string DirectoryMustExistFeedback { get; internal set; } = Strings.fdDirectoryMustExistFeedback;
  89. /// <summary>
  90. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.Directory"/>
  91. /// and user enters the name of an existing file (File system cannot have a folder with the same name as a file).
  92. /// </summary>
  93. public string FileAlreadyExistsFeedback { get; internal set; } = Strings.fdFileAlreadyExistsFeedback;
  94. /// <summary>
  95. /// Gets or sets error message when user selects a file that does not exist and
  96. /// <see cref="OpenMode"/> is <see cref="OpenMode.File"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  97. /// </summary>
  98. public string FileMustExistFeedback { get; internal set; } = Strings.fdFileMustExistFeedback;
  99. /// <summary>
  100. /// Gets or sets error message when user <see cref="OpenMode"/> is <see cref="OpenMode.File"/>
  101. /// and user enters the name of an existing directory (File system cannot have a folder with the same name as a file).
  102. /// </summary>
  103. public string DirectoryAlreadyExistsFeedback { get; internal set; } = Strings.fdDirectoryAlreadyExistsFeedback;
  104. /// <summary>
  105. /// Gets or sets error message when user selects a file/dir that does not exist and
  106. /// <see cref="OpenMode"/> is <see cref="OpenMode.Mixed"/> and <see cref="FileDialog.MustExist"/> is <see langword="true"/>.
  107. /// </summary>
  108. public string FileOrDirectoryMustExistFeedback { get; internal set; } = Strings.fdFileOrDirectoryMustExistFeedback;
  109. /// <summary>
  110. /// Gets the style settings for the table of files (in currently selected directory).
  111. /// </summary>
  112. public TableStyle TableStyle { get; internal set; }
  113. /// <summary>
  114. /// Gets the style settings for the collapse-able directory/places tree
  115. /// </summary>
  116. public TreeStyle TreeStyle { get; internal set; }
  117. /// <summary>
  118. /// Gets or Sets the method for getting the root tree objects that are displayed in
  119. /// the collapse-able tree in the <see cref="FileDialog"/>. Defaults to all accessible
  120. /// <see cref="System.Environment.GetLogicalDrives"/> and unique
  121. /// <see cref="Environment.SpecialFolder"/>.
  122. /// </summary>
  123. /// <remarks>Must be configured before showing the dialog.</remarks>
  124. public Func<Dictionary<IDirectoryInfo, string>> TreeRootGetter { get; set; }
  125. /// <summary>
  126. /// Gets or sets whether to use advanced unicode characters which might not be installed
  127. /// on all users computers.
  128. /// </summary>
  129. public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
  130. /// <summary>
  131. /// Gets or sets the format to use for date/times in the Modified column.
  132. /// Defaults to <see cref="DateTimeFormatInfo.SortableDateTimePattern"/>
  133. /// of the <see cref="CultureInfo.CurrentCulture"/>
  134. /// </summary>
  135. public string DateFormat { get; set; }
  136. /// <summary>
  137. /// Creates a new instance of the <see cref="FileDialogStyle"/> class.
  138. /// </summary>
  139. public FileDialogStyle (IFileSystem fileSystem)
  140. {
  141. _fileSystem = fileSystem;
  142. TreeRootGetter = DefaultTreeRootGetter;
  143. DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
  144. }
  145. private Dictionary<IDirectoryInfo,string> DefaultTreeRootGetter ()
  146. {
  147. var roots = new Dictionary<IDirectoryInfo, string> ();
  148. try {
  149. foreach (var d in Environment.GetLogicalDrives ()) {
  150. var dir = _fileSystem.DirectoryInfo.New (d);
  151. if (!roots.ContainsKey(dir)) {
  152. roots.Add (dir, d);
  153. }
  154. }
  155. } catch (Exception) {
  156. // Cannot get the system disks thats fine
  157. }
  158. try {
  159. foreach (var special in Enum.GetValues (typeof (Environment.SpecialFolder)).Cast<SpecialFolder> ()) {
  160. try {
  161. var path = Environment.GetFolderPath (special);
  162. if(string.IsNullOrWhiteSpace (path)) {
  163. continue;
  164. }
  165. var dir = _fileSystem.DirectoryInfo.New (path);
  166. if (!roots.ContainsKey (dir) && dir.Exists) {
  167. roots.Add (dir, special.ToString());
  168. }
  169. } catch (Exception) {
  170. // Special file exists but contents are unreadable (permissions?)
  171. // skip it anyway
  172. }
  173. }
  174. } catch (Exception) {
  175. // Cannot get the special files for this OS oh well
  176. }
  177. return roots;
  178. }
  179. }
  180. }