PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms 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 */
 26using System;
 27using System.Collections.Generic;
 28using System.Linq;
 29using System.Text;
 30using System.Windows.Forms;
 31
 32namespace steganography
 33{
 34    interface IError
 35    {
 36        void OnThrowError(Exception e);
 37    }
 38
 39    /// <summary>
 40    /// Handles the throwing of errors
 41    /// </summary>
 42    static class ErrorHandler
 43    {
 44        /// <summary>
 45        /// Throws an error specified by the given error object
 46        /// </summary>
 47        /// <param name="errorObj">The error object</param>
 48        /// <param name="e">The exception object</param>
 49        /// <returns>True/false if the error was handled propperly</returns>
 50        public static void ThrowError(Error errorObj, Exception e = null)
 51        {
 52            if (e == null)
 53                e = new Exception();
 54
 55           errorObj.OnThrowError(e);
 56        }
 57    }
 58
 59    /// <summary>
 60    /// Abstract class with a few default methods to handle errors.
 61    /// </summary>
 62    abstract class Error : EventArgs, IError
 63    {
 64        public string InfoText = "";
 65
 66        /// <summary>
 67        /// Callback when an event occurs
 68        /// </summary>
 69        /// <param name="e">Exception object</param>
 70        /// <returns>True/false if the error was handled propperly</returns>
 71        public virtual void OnThrowError(Exception em)
 72        {
 73            this.onError(em);
 74
 75            GUIform MainForm = this.getMainForm();
 76            if (MainForm != null)
 77            {
 78                MainForm.BeginInvoke((MethodInvoker)delegate
 79                {
 80                    MainForm.Error = this;
 81                });
 82            }
 83
 84        }
 85        
 86        /// <summary>
 87        /// Shows a message box to the user with the given arguments.
 88        /// </summary>
 89        /// <param name="Title">The title</param>
 90        /// <param name="Message">The message</param>
 91        /// <param name="Buttons">The used buttons</param>
 92        /// <param name="Icon">The icon used</param>
 93        /// <returns>The clicked button (DialogResult)</returns>
 94        public DialogResult ShowMessage(
 95            String Title, 
 96            String Message, 
 97            MessageBoxButtons Buttons = MessageBoxButtons.OK,
 98            MessageBoxIcon Icon = MessageBoxIcon.None
 99            )
100        {
101            return MessageBox.Show(Message, Title, Buttons, Icon);
102        }
103
104        /// <summary>
105        /// Retrieve the main form from the open form list
106        /// </summary>
107        /// <returns>The main form</returns>
108        public GUIform getMainForm()
109        {
110            GUIform main = null;
111            foreach (Form f in Application.OpenForms)
112            {
113                if (f is GUIform)
114                {
115                    main = (GUIform)f;
116                }
117            }
118            return main;
119        }
120
121        /// <summary>
122        /// Check if an object has a method
123        /// </summary>
124        /// <param name="obj">The object to check on</param>
125        /// <param name="methodName">The method name</param>
126        /// <returns>True/false if the object has the method</returns>
127        private bool HasMethod(Object obj, string methodName)
128        {
129            Type type = obj.GetType();
130            return type.GetMethod(methodName) != null;
131        }
132
133        protected virtual void onError(Exception e) { }
134    }
135
136    /// <summary>
137    /// Allows other error objects to inherit common messagebox properties
138    /// </summary>
139    class ErrorWithMessageBox : Error
140    {
141        protected String Message, Title;
142        protected MessageBoxButtons Buttons;
143        protected MessageBoxIcon Icon;
144
145        /// <summary>
146        /// Set common button and icon for the messagebox
147        /// </summary>
148        public ErrorWithMessageBox()
149        {
150            this.Buttons = MessageBoxButtons.OK;
151            this.Icon = MessageBoxIcon.Warning;
152        }
153
154        /// <summary>
155        /// Display a messagebox containg error-relevant information
156        /// </summary>
157        /// <param name="e">Exception object</param>
158        protected override void onError(Exception e)
159        {
160            this.ShowMessage(this.Title, this.Message + "\n\n Error code:\t" + e.Message, this.Buttons, this.Icon);
161        }
162
163    }
164
165    class ImportImageError : Error
166    {
167        public ImportImageError()
168            : base()
169        {
170            this.InfoText = "Import Error" + Environment.NewLine + "There was an error importing your image, please check your file path.";
171        }
172    }
173
174    class InputError : Error
175    {
176        public InputError()
177            : base()
178        {
179            this.InfoText = "Input Error" + Environment.NewLine + "The file you want hide could not be opened correctly. Please try a different file.";
180        }
181    }
182
183    class OutputError : Error
184    {
185        public OutputError()
186            : base()
187        {
188            this.InfoText = "Output Error" + Environment.NewLine + "The output could not be written to the specified path. Please choose a different path.";
189        }
190    }
191
192    class SaveKeyError : Error
193    {
194        public SaveKeyError()
195            : base()
196        {
197            this.InfoText = "Save Key Error" + Environment.NewLine + "The savepath for the key is invalid.";
198        }
199    }
200
201    class ReadKeyError : Error
202    {
203        public ReadKeyError()
204            : base()
205        {
206            this.InfoText = "Read Key Error" + Environment.NewLine + "The keyfile could not be read correctly from the specified loadpath.";
207        }
208    }
209
210    class RSADecryptError : Error
211    {
212        public RSADecryptError()
213            : base()
214        {
215            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?";
216        }
217    }
218
219    class BitmapError : Error
220    {
221        public BitmapError()
222            : base()
223        {
224            this.InfoText = "Bitmap Error" + Environment.NewLine + "The specified image is not a losless bitmap.";
225        }
226    }
227
228    class CapacityError : Error
229    {
230        public CapacityError()
231            : base()
232        {
233            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.";
234        }
235    }
236
237    class ImageFormatError : Error
238    {
239        public ImageFormatError()
240            : base()
241        {
242            this.InfoText = "Image format Error" + Environment.NewLine + "The format of the image is not supported.";
243        }
244    }
245
246    class CoverError : Error
247    {
248        public CoverError()
249            : base()
250        {
251            this.InfoText = "Cover Error" + Environment.NewLine + "The carrier file is invalid.";
252        }
253    }
254
255    class WAVEError : Error
256    {
257        public WAVEError()
258            : base()
259        {
260            this.InfoText = "Wav Error" + Environment.NewLine + "The wav file is invalid.";
261        }
262    }
263
264    class OpenAVIError : Error
265    {
266        public OpenAVIError()
267            : base()
268        {
269            this.InfoText = "AVI Error" + Environment.NewLine + "Failed to open the AVI file. Have you installed the necessary codec pack?";
270        }
271    }
272}