source: branches/eraser6/pluginsRewrite/Eraser.Manager/Task.cs @ 2440

Revision 2440, 9.9 KB checked in by lowjoel, 2 years ago (diff)

Use the new identifier names defined in Eraser.Plugins.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.IO;
26using System.Runtime.Serialization;
27using System.Security.Permissions;
28using System.Threading;
29
30using Eraser.Util;
31using Eraser.Util.ExtensionMethods;
32using Eraser.Plugins;
33using Eraser.Plugins.ExtensionPoints;
34
35namespace Eraser.Manager
36{
37    /// <summary>
38    /// Deals with an erase task.
39    /// </summary>
40    [Serializable]
41    public class Task : ISerializable
42    {
43        #region Serialization code
44        protected Task(SerializationInfo info, StreamingContext context)
45        {
46            Name = (string)info.GetValue("Name", typeof(string));
47            Executor = context.Context as Executor;
48            Targets = (ErasureTargetCollection)info.GetValue("Targets", typeof(ErasureTargetCollection));
49            Targets.Owner = this;
50            Log = (List<LogSink>)info.GetValue("Log", typeof(List<LogSink>));
51            Canceled = false;
52
53            Schedule schedule = (Schedule)info.GetValue("Schedule", typeof(Schedule));
54            if (schedule.GetType() == Schedule.RunManually.GetType())
55                Schedule = Schedule.RunManually;
56            else if (schedule.GetType() == Schedule.RunNow.GetType())
57                Schedule = Schedule.RunNow;
58            else if (schedule.GetType() == Schedule.RunOnRestart.GetType())
59                Schedule = Schedule.RunOnRestart;
60            else if (schedule is RecurringSchedule)
61                Schedule = schedule;
62            else
63                throw new InvalidDataException(S._("An invalid type was found when loading " +
64                    "the task schedule"));
65        }
66
67        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
68        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
69        {
70            info.AddValue("Name", Name);
71            info.AddValue("Schedule", Schedule);
72            info.AddValue("Targets", Targets);
73            info.AddValue("Log", Log);
74        }
75        #endregion
76
77        /// <summary>
78        /// Constructor.
79        /// </summary>
80        public Task()
81        {
82            Name = string.Empty;
83            Targets = new ErasureTargetCollection(this);
84            Schedule = Schedule.RunNow;
85            Canceled = false;
86            Log = new List<LogSink>();
87        }
88
89        /// <summary>
90        /// Cancels the task from running, or, if the task is queued for running,
91        /// removes the task from the queue.
92        /// </summary>
93        public void Cancel()
94        {
95            Executor.UnqueueTask(this);
96            Canceled = true;
97        }
98
99        /// <summary>
100        /// The Executor object which is managing this task.
101        /// </summary>
102        public Executor Executor { get; internal set; }
103
104        /// <summary>
105        /// The name for this task. This is just an opaque value for the user to
106        /// recognize the task.
107        /// </summary>
108        public string Name { get; set; }
109
110        /// <summary>
111        /// The name of the task, used for display in UI elements.
112        /// </summary>
113        public string UIText
114        {
115            get
116            {
117                //Simple case, the task name was given by the user.
118                if (!string.IsNullOrEmpty(Name))
119                    return Name;
120
121                string result = string.Empty;
122                if (Targets.Count == 0)
123                    return result;
124                else if (Targets.Count < 5)
125                {
126                    //Simpler case, small set of data.
127                    foreach (IErasureTarget tgt in Targets)
128                        result += S._("{0}, ", tgt.UIText);
129
130                    return result.Remove(result.Length - 2);
131                }
132                else
133                {
134                    //Ok, we've quite a few entries, get the first, the mid and the end.
135                    result = S._("{0}, ", Targets[0].UIText);
136                    result += S._("{0}, ", Targets[Targets.Count / 2].UIText);
137                    result += Targets[Targets.Count - 1].UIText;
138
139                    return S._("{0} and {1} other targets", result, Targets.Count - 3);
140                }
141            }
142        }
143
144        /// <summary>
145        /// Gets the status of the task - whether it is being executed.
146        /// </summary>
147        public bool Executing { get; private set; }
148
149        /// <summary>
150        /// Gets whether this task is currently queued to run. This is true only
151        /// if the queue it is in is an explicit request, i.e will run when the
152        /// executor is idle.
153        /// </summary>
154        public bool Queued
155        {
156            get
157            {
158                return Executor.IsTaskQueued(this);
159            }
160        }
161
162        /// <summary>
163        /// Gets whether the task has been cancelled from execution.
164        /// </summary>
165        public bool Canceled
166        {
167            get;
168            internal set;
169        }
170
171        /// <summary>
172        /// The set of data to erase when this task is executed.
173        /// </summary>
174        public ErasureTargetCollection Targets { get; private set; }
175
176        /// <summary>
177        /// The schedule for running the task.
178        /// </summary>
179        public Schedule Schedule
180        {
181            get
182            {
183                return schedule;
184            }
185            set
186            {
187                if (value.Owner != null)
188                    throw new ArgumentException("The schedule provided can only " +
189                        "belong to one task at a time");
190
191                if (schedule is RecurringSchedule)
192                    ((RecurringSchedule)schedule).Owner = null;
193                schedule = value;
194                if (schedule is RecurringSchedule)
195                    ((RecurringSchedule)schedule).Owner = this;
196                OnTaskEdited();
197            }
198        }
199
200        /// <summary>
201        /// The log entries which this task has accumulated.
202        /// </summary>
203        public List<LogSink> Log { get; private set; }
204
205        /// <summary>
206        /// The progress manager object which manages the progress of this task.
207        /// </summary>
208        public SteppedProgressManager Progress
209        {
210            get
211            {
212                if (!Executing)
213                    throw new InvalidOperationException("The progress of an erasure can only " +
214                        "be queried when the task is being executed.");
215
216                return progress;
217            }
218            private set
219            {
220                progress = value;
221            }
222        }
223
224        private Schedule schedule;
225        private SteppedProgressManager progress;
226
227        #region Events
228        /// <summary>
229        /// The task has been edited.
230        /// </summary>
231        public EventHandler TaskEdited { get; set; }
232
233        /// <summary>
234        /// The start of the execution of a task.
235        /// </summary>
236        public EventHandler TaskStarted { get; set; }
237
238        /// <summary>
239        /// The event object holding all event handlers.
240        /// </summary>
241        public EventHandler<ProgressChangedEventArgs> ProgressChanged { get; set; }
242
243        /// <summary>
244        /// The completion of the execution of a task.
245        /// </summary>
246        public EventHandler TaskFinished { get; set; }
247
248        /// <summary>
249        /// Broadcasts the task edited event.
250        /// </summary>
251        internal void OnTaskEdited()
252        {
253            if (TaskEdited != null)
254                TaskEdited(this, EventArgs.Empty);
255        }
256
257        /// <summary>
258        /// Broadcasts the task execution start event.
259        /// </summary>
260        internal void OnTaskStarted()
261        {
262            if (TaskStarted != null)
263                TaskStarted(this, EventArgs.Empty);
264            Executing = true;
265            Progress = new SteppedProgressManager();
266        }
267
268        /// <summary>
269        /// Broadcasts a ProgressChanged event. The sender will be the erasure target
270        /// which broadcast this event; e.UserState will contain extra information
271        /// about the progress which is stored as a TaskProgressChangedEventArgs
272        /// object.
273        /// </summary>
274        /// <param name="sender">The <see cref="IErasureTarget"/> which is reporting
275        /// progress.</param>
276        /// <param name="e">The new progress value.</param>
277        /// <exception cref="ArgumentException">e.UserState must be of the type
278        /// <see cref="TaskProgressEventargs"/></exception>
279        /// <exception cref="ArgumentNullException">Both sender and e cannot be null.</exception>
280        internal void OnProgressChanged(IErasureTarget sender, ProgressChangedEventArgs e)
281        {
282            if (sender == null)
283                throw new ArgumentNullException("sender");
284            if (e == null)
285                throw new ArgumentNullException("sender");
286            if (e.UserState.GetType() != typeof(TaskProgressChangedEventArgs))
287                throw new ArgumentException("The Task.OnProgressChanged event expects a " +
288                    "TaskProgressEventArgs argument for the ProgressChangedEventArgs' UserState " +
289                    "object.", "e");
290
291            if (ProgressChanged != null)
292                ProgressChanged(sender, e);
293        }
294
295        /// <summary>
296        /// Broadcasts the task execution completion event.
297        /// </summary>
298        internal void OnTaskFinished()
299        {
300            Progress = null;
301            Executing = false;
302            if (TaskFinished != null)
303                TaskFinished(this, EventArgs.Empty);
304        }
305        #endregion
306    }
307
308    /// <summary>
309    /// A base event class for all event arguments involving a task.
310    /// </summary>
311    public class TaskEventArgs : EventArgs
312    {
313        /// <summary>
314        /// Constructor.
315        /// </summary>
316        /// <param name="task">The task being referred to by this event.</param>
317        public TaskEventArgs(Task task)
318        {
319            Task = task;
320        }
321
322        /// <summary>
323        /// The executing task.
324        /// </summary>
325        public Task Task { get; private set; }
326    }
327
328    /// <summary>
329    /// Stores extra information in the <see cref="ProgressChangedEventArgs"/>
330    /// structure that is not conveyed in the ProgressManagerBase classes.
331    /// </summary>
332    public class TaskProgressChangedEventArgs
333    {
334        /// <summary>
335        /// Constructor.
336        /// </summary>
337        /// <param name="itemName">The item whose erasure progress is being erased.</param>
338        /// <param name="itemPass">The current pass number for this item.</param>
339        /// <param name="itemTotalPasses">The total number of passes to complete erasure
340        /// of this item.</param>
341        public TaskProgressChangedEventArgs(string itemName, int itemPass,
342            int itemTotalPasses)
343        {
344            ItemName = itemName;
345            ItemPass = itemPass;
346            ItemTotalPasses = itemTotalPasses;
347        }
348
349        /// <summary>
350        /// The file name of the item being erased.
351        /// </summary>
352        public string ItemName { get; private set; }
353
354        /// <summary>
355        /// The pass number of a multi-pass erasure method.
356        /// </summary>
357        public int ItemPass { get; private set; }
358
359        /// <summary>
360        /// The total number of passes to complete before this erasure method is
361        /// completed.
362        /// </summary>
363        public int ItemTotalPasses { get; private set; }
364    }
365}
Note: See TracBrowser for help on using the repository browser.