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

Revision 2366, 9.8 KB checked in by lowjoel, 3 years ago (diff)

Since the types have been moved to the Eraser.Plugins.ExtensionPoints? namespace, reference that.

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