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

Revision 2485, 7.7 KB checked in by lowjoel, 2 years ago (diff)

Replace the UIText property with ToString?(). Fix the resulting compile errors, too.

  • 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 : ITask, 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
103        {
104            get
105            {
106                return executor;
107            }
108            internal set
109            {
110                if (value == null)
111                    throw new ArgumentNullException();
112                if (executor != null)
113                    throw new InvalidOperationException();
114
115                executor = value;
116            }
117        }
118
119        /// <summary>
120        /// The name for this task. This is just an opaque value for the user to
121        /// recognize the task.
122        /// </summary>
123        public string Name { get; set; }
124
125        /// <summary>
126        /// The name of the task, used for display in UI elements.
127        /// </summary>
128        public override string ToString()
129        {
130            //Simple case, the task name was given by the user.
131            if (!string.IsNullOrEmpty(Name))
132                return Name;
133
134            string result = string.Empty;
135            if (Targets.Count == 0)
136                return result;
137            else if (Targets.Count < 5)
138            {
139                //Simpler case, small set of data.
140                foreach (IErasureTarget tgt in Targets)
141                    result += S._("{0}, ", tgt);
142
143                return result.Remove(result.Length - 2);
144            }
145            else
146            {
147                //Ok, we've quite a few entries, get the first, the mid and the end.
148                result = S._("{0}, ", Targets[0]);
149                result += S._("{0}, ", Targets[Targets.Count / 2]);
150                result += Targets[Targets.Count - 1];
151
152                return S._("{0} and {1} other targets", result, Targets.Count - 3);
153            }
154        }
155
156        /// <summary>
157        /// Gets the status of the task - whether it is being executed.
158        /// </summary>
159        public bool Executing { get; private set; }
160
161        /// <summary>
162        /// Gets whether this task is currently queued to run. This is true only
163        /// if the queue it is in is an explicit request, i.e will run when the
164        /// executor is idle.
165        /// </summary>
166        public bool Queued
167        {
168            get
169            {
170                return Executor.IsTaskQueued(this);
171            }
172        }
173
174        /// <summary>
175        /// Gets whether the task has been cancelled from execution.
176        /// </summary>
177        public bool Canceled
178        {
179            get;
180            internal set;
181        }
182
183        /// <summary>
184        /// The set of data to erase when this task is executed.
185        /// </summary>
186        public ErasureTargetCollection Targets { get; private set; }
187
188        /// <summary>
189        /// <see cref="Targets"/>
190        /// </summary>
191        ICollection<IErasureTarget> ITask.Targets
192        {
193            get { return Targets; }
194        }
195
196        /// <summary>
197        /// The schedule for running the task.
198        /// </summary>
199        public Schedule Schedule
200        {
201            get
202            {
203                return schedule;
204            }
205            set
206            {
207                if (value.Owner != null)
208                    throw new ArgumentException("The schedule provided can only " +
209                        "belong to one task at a time");
210
211                if (schedule is RecurringSchedule)
212                    ((RecurringSchedule)schedule).Owner = null;
213                schedule = value;
214                if (schedule is RecurringSchedule)
215                    ((RecurringSchedule)schedule).Owner = this;
216                OnTaskEdited();
217            }
218        }
219
220        /// <summary>
221        /// The log entries which this task has accumulated.
222        /// </summary>
223        public List<LogSink> Log { get; private set; }
224
225        /// <summary>
226        /// The progress manager object which manages the progress of this task.
227        /// </summary>
228        public SteppedProgressManager Progress
229        {
230            get
231            {
232                if (!Executing)
233                    throw new InvalidOperationException("The progress of an erasure can only " +
234                        "be queried when the task is being executed.");
235
236                return progress;
237            }
238            private set
239            {
240                progress = value;
241            }
242        }
243
244        private Executor executor;
245        private Schedule schedule;
246        private SteppedProgressManager progress;
247
248        #region Events
249        /// <summary>
250        /// The task has been edited.
251        /// </summary>
252        public EventHandler TaskEdited { get; set; }
253
254        /// <summary>
255        /// The start of the execution of a task.
256        /// </summary>
257        public EventHandler TaskStarted { get; set; }
258
259        /// <summary>
260        /// The completion of the execution of a task.
261        /// </summary>
262        public EventHandler TaskFinished { get; set; }
263
264        /// <summary>
265        /// Broadcasts the task edited event.
266        /// </summary>
267        internal void OnTaskEdited()
268        {
269            if (TaskEdited != null)
270                TaskEdited(this, EventArgs.Empty);
271        }
272
273        /// <summary>
274        /// Broadcasts the task execution start event.
275        /// </summary>
276        internal void OnTaskStarted()
277        {
278            if (TaskStarted != null)
279                TaskStarted(this, EventArgs.Empty);
280            Executing = true;
281            Progress = new SteppedProgressManager();
282        }
283
284        /// <summary>
285        /// Broadcasts the task execution completion event.
286        /// </summary>
287        internal void OnTaskFinished()
288        {
289            Progress = null;
290            Executing = false;
291            if (TaskFinished != null)
292                TaskFinished(this, EventArgs.Empty);
293        }
294        #endregion
295    }
296
297    /// <summary>
298    /// A base event class for all event arguments involving a task.
299    /// </summary>
300    public class TaskEventArgs : EventArgs
301    {
302        /// <summary>
303        /// Constructor.
304        /// </summary>
305        /// <param name="task">The task being referred to by this event.</param>
306        public TaskEventArgs(Task task)
307        {
308            Task = task;
309        }
310
311        /// <summary>
312        /// The executing task.
313        /// </summary>
314        public Task Task { get; private set; }
315    }
316}
Note: See TracBrowser for help on using the repository browser.