AllowedType.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /// <inheritdoc/>
  32. public override string ToString ()
  33. {
  34. return Strings.fdAnyFiles + "(*.*)";
  35. }
  36. }
  37. /// <summary>
  38. /// Describes a requirement on what <see cref="FileInfo"/> can be selected.
  39. /// This can be combined with other <see cref="IAllowedType"/> in a <see cref="FileDialog"/>
  40. /// to for example show only .csv files but let user change to open any if they want.
  41. /// </summary>
  42. public class AllowedType : IAllowedType {
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="AllowedType"/> class.
  45. /// </summary>
  46. /// <param name="description">The human readable text to display.</param>
  47. /// <param name="extensions">Extension(s) to match e.g. .csv.</param>
  48. public AllowedType (string description, params string [] extensions)
  49. {
  50. if (extensions.Length == 0) {
  51. throw new ArgumentException ("You must supply at least one extension");
  52. }
  53. this.Description = description;
  54. this.Extensions = extensions;
  55. }
  56. /// <summary>
  57. /// Gets or Sets the human readable description for the file type
  58. /// e.g. "Comma Separated Values".
  59. /// </summary>
  60. public string Description { get; set; }
  61. /// <summary>
  62. /// Gets or Sets the permitted file extension(s) (e.g. ".csv").
  63. /// </summary>
  64. public string [] Extensions { get; set; }
  65. /// <summary>
  66. /// Returns <see cref="Description"/> plus all <see cref="Extensions"/> separated by semicolons.
  67. /// </summary>
  68. public override string ToString ()
  69. {
  70. const int maxLength = 30;
  71. var desc = $"{this.Description} ({string.Join (";", this.Extensions.Select (e => '*' + e).ToArray ())})";
  72. if(desc.Length > maxLength) {
  73. return desc.Substring (0, maxLength-2) + "…";
  74. }
  75. return desc;
  76. }
  77. /// <inheritdoc/>
  78. public bool IsAllowed(string path)
  79. {
  80. var extension = Path.GetExtension (path);
  81. // There is a requirement to have a particular extension and we have none
  82. if (string.IsNullOrEmpty (extension)) {
  83. return false;
  84. }
  85. return this.Extensions.Any (e => e.Equals (extension));
  86. }
  87. }
  88. }