AllowedType.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.CodeDom;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui.Resources;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Interface for <see cref="FileDialog"/> restrictions on which file type(s) the
  10. /// user is allowed to select/enter.
  11. /// </summary>
  12. public interface IAllowedType
  13. {
  14. /// <summary>
  15. /// Returns true if the file at <paramref name="path"/> is compatible with this
  16. /// allow option. Note that the file may not exist (e.g. in the case of saving).
  17. /// </summary>
  18. /// <param name="path"></param>
  19. /// <returns></returns>
  20. bool IsAllowed (string path);
  21. }
  22. /// <summary>
  23. /// <see cref="IAllowedType"/> that allows selection of any types (*.*).
  24. /// </summary>
  25. public class AllowedTypeAny : IAllowedType {
  26. /// <inheritdoc/>
  27. public bool IsAllowed (string path)
  28. {
  29. return true;
  30. }
  31. /// <summary>
  32. /// Returns a string representation of this <see cref="AllowedTypeAny"/>.
  33. /// </summary>
  34. /// <returns></returns>
  35. public override string ToString ()
  36. {
  37. return Strings.fdAnyFiles + "(*.*)";
  38. }
  39. }
  40. /// <summary>
  41. /// Describes a requirement on what <see cref="FileInfo"/> can be selected.
  42. /// This can be combined with other <see cref="IAllowedType"/> in a <see cref="FileDialog"/>
  43. /// to for example show only .csv files but let user change to open any if they want.
  44. /// </summary>
  45. public class AllowedType : IAllowedType {
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="AllowedType"/> class.
  48. /// </summary>
  49. /// <param name="description">The human readable text to display.</param>
  50. /// <param name="extensions">Extension(s) to match e.g. .csv.</param>
  51. public AllowedType (string description, params string [] extensions)
  52. {
  53. if (extensions.Length == 0) {
  54. throw new ArgumentException ("You must supply at least one extension");
  55. }
  56. this.Description = description;
  57. this.Extensions = extensions;
  58. }
  59. /// <summary>
  60. /// Gets or Sets the human readable description for the file type
  61. /// e.g. "Comma Separated Values".
  62. /// </summary>
  63. public string Description { get; set; }
  64. /// <summary>
  65. /// Gets or Sets the permitted file extension(s) (e.g. ".csv").
  66. /// </summary>
  67. public string [] Extensions { get; set; }
  68. /// <summary>
  69. /// Returns <see cref="Description"/> plus all <see cref="Extensions"/> separated by semicolons.
  70. /// </summary>
  71. public override string ToString ()
  72. {
  73. const int maxLength = 30;
  74. var desc = $"{this.Description} ({string.Join (";", this.Extensions.Select (e => '*' + e).ToArray ())})";
  75. if(desc.Length > maxLength) {
  76. return desc.Substring (0, maxLength-2) + "…";
  77. }
  78. return desc;
  79. }
  80. /// <inheritdoc/>
  81. public bool IsAllowed(string path)
  82. {
  83. if(string.IsNullOrWhiteSpace(path)) {
  84. return false;
  85. }
  86. var extension = Path.GetExtension (path);
  87. if(this.Extensions.Any(e=>path.EndsWith(e, StringComparison.InvariantCultureIgnoreCase))) {
  88. return true;
  89. }
  90. // There is a requirement to have a particular extension and we have none
  91. if (string.IsNullOrEmpty (extension)) {
  92. return false;
  93. }
  94. return this.Extensions.Any (e => e.Equals (extension, StringComparison.InvariantCultureIgnoreCase));
  95. }
  96. }
  97. }