PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/steganography/Error.cs

https://bitbucket.org/PaulVisscher/introductie-project-informatica
C# | 272 lines | 175 code | 27 blank | 70 comment | 6 complexity | 1e5c9c632c9bbbb86939d58acb60e319 MD5 | raw file
  1. /**
  2. * @file Error.cs
  3. * @brief Holds the programs error handling
  4. *
  5. * @details This file discribes the way errors should be managed, and how
  6. * they are called from the error reporters.
  7. *
  8. * @author Paul Visscher
  9. *
  10. * @license © Ikiwisi 2012
  11. * This file is part of Steganographer.
  12. *
  13. * Steganographer is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * Steganographer is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with Steganographer. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Windows.Forms;
  31. namespace steganography
  32. {
  33. interface IError
  34. {
  35. void OnThrowError(Exception e);
  36. }
  37. /// <summary>
  38. /// Handles the throwing of errors
  39. /// </summary>
  40. static class ErrorHandler
  41. {
  42. /// <summary>
  43. /// Throws an error specified by the given error object
  44. /// </summary>
  45. /// <param name="errorObj">The error object</param>
  46. /// <param name="e">The exception object</param>
  47. /// <returns>True/false if the error was handled propperly</returns>
  48. public static void ThrowError(Error errorObj, Exception e = null)
  49. {
  50. if (e == null)
  51. e = new Exception();
  52. errorObj.OnThrowError(e);
  53. }
  54. }
  55. /// <summary>
  56. /// Abstract class with a few default methods to handle errors.
  57. /// </summary>
  58. abstract class Error : EventArgs, IError
  59. {
  60. public string InfoText = "";
  61. /// <summary>
  62. /// Callback when an event occurs
  63. /// </summary>
  64. /// <param name="e">Exception object</param>
  65. /// <returns>True/false if the error was handled propperly</returns>
  66. public virtual void OnThrowError(Exception em)
  67. {
  68. this.onError(em);
  69. GUIform MainForm = this.getMainForm();
  70. if (MainForm != null)
  71. {
  72. MainForm.BeginInvoke((MethodInvoker)delegate
  73. {
  74. MainForm.Error = this;
  75. });
  76. }
  77. }
  78. /// <summary>
  79. /// Shows a message box to the user with the given arguments.
  80. /// </summary>
  81. /// <param name="Title">The title</param>
  82. /// <param name="Message">The message</param>
  83. /// <param name="Buttons">The used buttons</param>
  84. /// <param name="Icon">The icon used</param>
  85. /// <returns>The clicked button (DialogResult)</returns>
  86. public DialogResult ShowMessage(
  87. String Title,
  88. String Message,
  89. MessageBoxButtons Buttons = MessageBoxButtons.OK,
  90. MessageBoxIcon Icon = MessageBoxIcon.None
  91. )
  92. {
  93. return MessageBox.Show(Message, Title, Buttons, Icon);
  94. }
  95. /// <summary>
  96. /// Retrieve the main form from the open form list
  97. /// </summary>
  98. /// <returns>The main form</returns>
  99. public GUIform getMainForm()
  100. {
  101. GUIform main = null;
  102. foreach (Form f in Application.OpenForms)
  103. {
  104. if (f is GUIform)
  105. {
  106. main = (GUIform)f;
  107. }
  108. }
  109. return main;
  110. }
  111. /// <summary>
  112. /// Check if an object has a method
  113. /// </summary>
  114. /// <param name="obj">The object to check on</param>
  115. /// <param name="methodName">The method name</param>
  116. /// <returns>True/false if the object has the method</returns>
  117. private bool HasMethod(Object obj, string methodName)
  118. {
  119. Type type = obj.GetType();
  120. return type.GetMethod(methodName) != null;
  121. }
  122. protected virtual void onError(Exception e) { }
  123. }
  124. /// <summary>
  125. /// Allows other error objects to inherit common messagebox properties
  126. /// </summary>
  127. class ErrorWithMessageBox : Error
  128. {
  129. protected String Message, Title;
  130. protected MessageBoxButtons Buttons;
  131. protected MessageBoxIcon Icon;
  132. /// <summary>
  133. /// Set common button and icon for the messagebox
  134. /// </summary>
  135. public ErrorWithMessageBox()
  136. {
  137. this.Buttons = MessageBoxButtons.OK;
  138. this.Icon = MessageBoxIcon.Warning;
  139. }
  140. /// <summary>
  141. /// Display a messagebox containg error-relevant information
  142. /// </summary>
  143. /// <param name="e">Exception object</param>
  144. protected override void onError(Exception e)
  145. {
  146. this.ShowMessage(this.Title, this.Message + "\n\n Error code:\t" + e.Message, this.Buttons, this.Icon);
  147. }
  148. }
  149. class ImportImageError : Error
  150. {
  151. public ImportImageError()
  152. : base()
  153. {
  154. this.InfoText = "Import Error" + Environment.NewLine + "There was an error importing your image, please check your file path.";
  155. }
  156. }
  157. class InputError : Error
  158. {
  159. public InputError()
  160. : base()
  161. {
  162. this.InfoText = "Input Error" + Environment.NewLine + "The file you want hide could not be opened correctly. Please try a different file.";
  163. }
  164. }
  165. class OutputError : Error
  166. {
  167. public OutputError()
  168. : base()
  169. {
  170. this.InfoText = "Output Error" + Environment.NewLine + "The output could not be written to the specified path. Please choose a different path.";
  171. }
  172. }
  173. class SaveKeyError : Error
  174. {
  175. public SaveKeyError()
  176. : base()
  177. {
  178. this.InfoText = "Save Key Error" + Environment.NewLine + "The savepath for the key is invalid.";
  179. }
  180. }
  181. class ReadKeyError : Error
  182. {
  183. public ReadKeyError()
  184. : base()
  185. {
  186. this.InfoText = "Read Key Error" + Environment.NewLine + "The keyfile could not be read correctly from the specified loadpath.";
  187. }
  188. }
  189. class RSADecryptError : Error
  190. {
  191. public RSADecryptError()
  192. : base()
  193. {
  194. this.InfoText = "RSA Decrypt Error Error" + Environment.NewLine + "Your data could not be decrypted. Are you sure the RSA key in the key file is valid?";
  195. }
  196. }
  197. class BitmapError : Error
  198. {
  199. public BitmapError()
  200. : base()
  201. {
  202. this.InfoText = "Bitmap Error" + Environment.NewLine + "The specified image is not a losless bitmap.";
  203. }
  204. }
  205. class CapacityError : Error
  206. {
  207. public CapacityError()
  208. : base()
  209. {
  210. this.InfoText = "Capacity Error" + Environment.NewLine + "The amount of data to be hidden is too large. Please decrease the size or try a different file to hide in.";
  211. }
  212. }
  213. class ImageFormatError : Error
  214. {
  215. public ImageFormatError()
  216. : base()
  217. {
  218. this.InfoText = "Image format Error" + Environment.NewLine + "The format of the image is not supported.";
  219. }
  220. }
  221. class CoverError : Error
  222. {
  223. public CoverError()
  224. : base()
  225. {
  226. this.InfoText = "Cover Error" + Environment.NewLine + "The carrier file is invalid.";
  227. }
  228. }
  229. class WAVEError : Error
  230. {
  231. public WAVEError()
  232. : base()
  233. {
  234. this.InfoText = "Wav Error" + Environment.NewLine + "The wav file is invalid.";
  235. }
  236. }
  237. class OpenAVIError : Error
  238. {
  239. public OpenAVIError()
  240. : base()
  241. {
  242. this.InfoText = "AVI Error" + Environment.NewLine + "Failed to open the AVI file. Have you installed the necessary codec pack?";
  243. }
  244. }
  245. }