TransferResumeSupport.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Runtime.InteropServices;
  2. using System;
  3. using System.Globalization;
  4. namespace WinSCP
  5. {
  6. [Guid("0ADAAEBC-4A15-4A9C-8ED4-D85F5630035C")]
  7. [ComVisible(true)]
  8. public enum TransferResumeSupportState
  9. {
  10. Default,
  11. On,
  12. Off,
  13. Smart
  14. }
  15. [Guid("6CED4579-0DF2-4E46-93E9-18780546B421")]
  16. [ClassInterface(Constants.ClassInterface)]
  17. [ComVisible(true)]
  18. public sealed class TransferResumeSupport
  19. {
  20. public TransferResumeSupportState State { get; set; }
  21. public int Threshold { get { return GetThreshold(); } set { SetThreshold(value); } }
  22. public TransferResumeSupport()
  23. {
  24. State = TransferResumeSupportState.Default;
  25. _threshold = 100; // (100 KB)
  26. }
  27. public override string ToString()
  28. {
  29. string result;
  30. switch (State)
  31. {
  32. case TransferResumeSupportState.Default:
  33. result = "default";
  34. break;
  35. case TransferResumeSupportState.Off:
  36. result = "off";
  37. break;
  38. case TransferResumeSupportState.On:
  39. result = "on";
  40. break;
  41. case TransferResumeSupportState.Smart:
  42. result = Threshold.ToString(CultureInfo.InvariantCulture);
  43. break;
  44. default:
  45. result = "unknown";
  46. break;
  47. }
  48. return result;
  49. }
  50. private int GetThreshold()
  51. {
  52. if (State != TransferResumeSupportState.Smart)
  53. {
  54. throw new InvalidOperationException("Threshold is undefined when state is not Smart");
  55. }
  56. return _threshold;
  57. }
  58. private void SetThreshold(int threshold)
  59. {
  60. if (_threshold != threshold)
  61. {
  62. if (threshold <= 0)
  63. {
  64. throw new ArgumentOutOfRangeException(nameof(threshold), "Threshold must be positive");
  65. }
  66. State = TransferResumeSupportState.Smart;
  67. _threshold = threshold;
  68. }
  69. }
  70. private int _threshold;
  71. }
  72. }