CallbackBase.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #if UNMANAGED
  2. namespace SevenZip
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. internal class CallbackBase : MarshalByRefObject
  8. {
  9. /// <summary>
  10. /// User exceptions thrown during the requested operations, for example, in events.
  11. /// </summary>
  12. private readonly List<Exception> _exceptions = new List<Exception>();
  13. /// <summary>
  14. /// Initializes a new instance of the CallbackBase class.
  15. /// </summary>
  16. protected CallbackBase()
  17. {
  18. Password = "";
  19. ReportErrors = true;
  20. }
  21. /// <summary>
  22. /// Initializes a new instance of the CallbackBase class.
  23. /// </summary>
  24. /// <param name="password">The archive password.</param>
  25. protected CallbackBase(string password)
  26. {
  27. if (string.IsNullOrEmpty(password))
  28. {
  29. throw new SevenZipException("Empty password was specified.");
  30. }
  31. Password = password;
  32. ReportErrors = true;
  33. }
  34. /// <summary>
  35. /// Gets or sets the archive password
  36. /// </summary>
  37. public string Password { get; }
  38. /// <summary>
  39. /// Gets or sets the value indicating whether the current procedure was cancelled.
  40. /// </summary>
  41. public bool Canceled { get; set; }
  42. /// <summary>
  43. /// Gets or sets throw exceptions on archive errors flag
  44. /// </summary>
  45. public bool ReportErrors { get; }
  46. /// <summary>
  47. /// Gets the user exceptions thrown during the requested operations, for example, in events.
  48. /// </summary>
  49. public ReadOnlyCollection<Exception> Exceptions => new ReadOnlyCollection<Exception>(_exceptions);
  50. public void AddException(Exception e)
  51. {
  52. _exceptions.Add(e);
  53. }
  54. public void ClearExceptions()
  55. {
  56. _exceptions.Clear();
  57. }
  58. public bool HasExceptions => _exceptions.Count > 0;
  59. /// <summary>
  60. /// Throws the specified exception when is able to.
  61. /// </summary>
  62. /// <param name="e">The exception to throw.</param>
  63. /// <param name="handler">The handler responsible for the exception.</param>
  64. public bool ThrowException(CallbackBase handler, params Exception[] e)
  65. {
  66. if (ReportErrors && (handler == null || !handler.Canceled))
  67. {
  68. throw e[0];
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// Throws the first exception in the list if any exists.
  74. /// </summary>
  75. /// <returns>True means no exceptions.</returns>
  76. public bool ThrowException()
  77. {
  78. if (HasExceptions && ReportErrors)
  79. {
  80. throw _exceptions[0];
  81. }
  82. return true;
  83. }
  84. public void ThrowUserException()
  85. {
  86. if (HasExceptions)
  87. {
  88. throw new SevenZipException(SevenZipException.USER_EXCEPTION_MESSAGE);
  89. }
  90. }
  91. }
  92. }
  93. #endif