source: branches/eraser6/Eraser/TaskPropertiesForm.cs @ 905

Revision 905, 12.0 KB checked in by lowjoel, 6 years ago (diff)

Ran static code analysis on Manager.

-Do not initialise class members to default values (runtime will handle that)
-Deserialisation constructors should be marked protected for unsealed classes
-Classes implementing ICollection should end with Collection (Mainly fixed here is ErasureTargetsCollection?)
-ISerializable.GetObjectData? should be marked virtual
-Use proper camel casing (Prng, Guid, FileName? etc)
-Enumeration values should be Camel casing
-Bitfields should be marked with the [Flags] attribute
-Implement the proper IDisposable pattern - one Dispose() and one Dispose(bool) - the latter doing the clean ups for both Dispose and finalisers
-public variables should instead be properties
-Abstract classes should have protected constructors
-Renamed Schedule.Type to Schedule.ScheduleType? to prevent ambiguity with GetType?()
-There shouldn't be a need to pass reference variables by reference

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29
30using System.Globalization;
31using Eraser.Manager;
32using Eraser.Util;
33
34namespace Eraser
35{
36    public partial class TaskPropertiesForm : Form
37    {
38        public TaskPropertiesForm()
39        {
40            InitializeComponent();
41            scheduleTime.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
42
43            //Set a default task type
44            typeImmediate.Checked = true;
45            scheduleDaily.Checked = true;
46        }
47
48        /// <summary>
49        /// Sets or retrieves the task object to be edited or being edited.
50        /// </summary>
51        public Task Task
52        {
53            get { UpdateTaskFromUI(); return task; }
54            set { task = value; UpdateUIFromTask(); }
55        }
56
57        /// <summary>
58        /// Updates the local task object from the UI elements.
59        /// </summary>
60        private void UpdateTaskFromUI()
61        {
62            //Set the name of the task
63            task.Name = name.Text;
64
65            //And the schedule, if selected.
66            if (typeImmediate.Checked)
67            {
68                task.Schedule = Schedule.RunNow;
69            }
70            else if (typeRestart.Checked)
71            {
72                task.Schedule = Schedule.RunOnRestart;
73            }
74            else if (typeRecurring.Checked)
75            {
76                RecurringSchedule schedule = new RecurringSchedule();
77                task.Schedule = schedule;
78                schedule.ExecutionTime = new DateTime(1, 1, 1, scheduleTime.Value.Hour,
79                    scheduleTime.Value.Minute, scheduleTime.Value.Second);
80
81                if (scheduleDaily.Checked)
82                {
83                    if (scheduleDailyByDay.Checked)
84                    {
85                        schedule.ScheduleType = RecurringSchedule.ScheduleUnit.Daily;
86                        schedule.Frequency = (int)scheduleDailyByDayFreq.Value;
87                    }
88                    else
89                    {
90                        schedule.ScheduleType = RecurringSchedule.ScheduleUnit.Weekdays;
91                    }
92                }
93                else if (scheduleWeekly.Checked)
94                {
95                    schedule.ScheduleType = RecurringSchedule.ScheduleUnit.Weekly;
96                    schedule.Frequency = (int)scheduleWeeklyFreq.Value;
97                    RecurringSchedule.DaysOfWeek weeklySchedule = 0;
98                    if (scheduleWeeklyMonday.Checked)
99                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Monday;
100                    if (scheduleWeeklyTuesday.Checked)
101                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Tuesday;
102                    if (scheduleWeeklyWednesday.Checked)
103                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Wednesday;
104                    if (scheduleWeeklyThursday.Checked)
105                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Thursday;
106                    if (scheduleWeeklyFriday.Checked)
107                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Friday;
108                    if (scheduleWeeklySaturday.Checked)
109                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Saturday;
110                    if (scheduleWeeklySunday.Checked)
111                        weeklySchedule |= RecurringSchedule.DaysOfWeek.Sunday;
112                    schedule.WeeklySchedule = weeklySchedule;
113                }
114                else if (scheduleMonthly.Checked)
115                {
116                    schedule.ScheduleType = RecurringSchedule.ScheduleUnit.Monthly;
117                    schedule.Frequency = (int)scheduleMonthlyFreq.Value;
118                    schedule.MonthlySchedule = (int)scheduleMonthlyDayNumber.Value;
119                }
120                else
121                    throw new ArgumentException("No such scheduling method.");
122            }
123        }
124
125        /// <summary>
126        /// Updates the UI elements to reflect the data in the Task object.
127        /// </summary>
128        private void UpdateUIFromTask()
129        {
130            //Set the name of the task
131            name.Text = task.Name;
132
133            //The data
134            foreach (Task.ErasureTarget target in task.Targets)
135            {
136                ListViewItem item = data.Items.Add(target.UIText);
137                item.SubItems.Add(target.MethodDefined ? target.Method.Name : S._("(default)"));
138                item.Tag = target;
139            }
140
141            //And the schedule, if selected.
142            if (task.Schedule == Schedule.RunNow)
143            {
144                typeImmediate.Checked = true;
145            }
146            else if (task.Schedule == Schedule.RunOnRestart)
147            {
148                typeRestart.Checked = true;
149            }
150            else
151            {
152                typeRecurring.Checked = true;
153                RecurringSchedule schedule = (RecurringSchedule)task.Schedule;
154                scheduleTime.Value = scheduleTime.MinDate.Add(schedule.ExecutionTime.TimeOfDay);
155
156                switch (schedule.ScheduleType)
157                {
158                    case RecurringSchedule.ScheduleUnit.Daily:
159                        scheduleDailyByDay.Checked = true;
160                        scheduleDailyByDayFreq.Value = schedule.Frequency;
161                        break;
162                    case RecurringSchedule.ScheduleUnit.Weekdays:
163                        scheduleDailyByWeekday.Checked = true;
164                        break;
165                    case RecurringSchedule.ScheduleUnit.Weekly:
166                        scheduleWeeklyFreq.Value = schedule.Frequency;
167                        scheduleWeekly.Checked = true;
168                        scheduleWeeklyMonday.Checked =
169                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Monday) != 0;
170                        scheduleWeeklyTuesday.Checked =
171                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Tuesday) != 0;
172                        scheduleWeeklyWednesday.Checked =
173                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Wednesday) != 0;
174                        scheduleWeeklyThursday.Checked =
175                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Thursday) != 0;
176                        scheduleWeeklyFriday.Checked =
177                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Friday) != 0;
178                        scheduleWeeklySaturday.Checked =
179                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Saturday) != 0;
180                        scheduleWeeklySunday.Checked =
181                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.Sunday) != 0;
182                        break;
183                    case RecurringSchedule.ScheduleUnit.Monthly:
184                        scheduleMonthly.Checked = true;
185                        scheduleMonthlyFreq.Value = schedule.Frequency;
186                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
187                        break;
188                    default:
189                        throw new ArgumentException("Unknown schedule type.");
190                }
191            }
192        }
193
194        /// <summary>
195        /// Triggered when the user clicks on the Add Data button.
196        /// </summary>
197        /// <param name="sender">The button.</param>
198        /// <param name="e">Event argument.</param>
199        private void dataAdd_Click(object sender, EventArgs e)
200        {
201            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
202            {
203                if (form.ShowDialog() == DialogResult.OK)
204                {
205                    Task.ErasureTarget target = form.Target;
206                    ListViewItem item = data.Items.Add(target.UIText);
207                    item.SubItems.Add(target.MethodDefined ? target.Method.Name : S._("(default)"));
208                    item.Tag = target;
209
210                    task.Targets.Add(target);
211                    errorProvider.Clear();
212                }
213            }
214        }
215
216        /// <summary>
217        /// Generated when the user double-clicks an item in the list-view.
218        /// </summary>
219        /// <param name="sender">The list-view which generated this event.</param>
220        /// <param name="e">Event argument.</param>
221        private void data_ItemActivate(object sender, EventArgs e)
222        {
223            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
224            {
225                ListViewItem item = data.SelectedItems[0];
226                form.Target = task.Targets[item.Index];
227
228                if (form.ShowDialog() == DialogResult.OK)
229                {
230                    Task.ErasureTarget target = form.Target;
231                    task.Targets[item.Index] = target;
232                    item.Text = target.UIText;
233                    item.SubItems[1].Text = target.MethodDefined ? target.Method.Name : S._("(default)");
234                }
235            }
236        }
237
238        /// <summary>
239        /// Generated when the user right-clicks on the data selection list-view.
240        /// </summary>
241        /// <param name="sender">The menu being opened.</param>
242        /// <param name="e">Event argument.</param>
243        private void dataContextMenuStrip_Opening(object sender, CancelEventArgs e)
244        {
245            if (data.SelectedIndices.Count == 0)
246            {
247                e.Cancel = true;
248                return;
249            }
250        }
251
252        /// <summary>
253        /// Generated when the user selects the menu itm to remove the selected
254        /// data from the list of data to erase.
255        /// </summary>
256        /// <param name="sender">The object triggering the event.</param>
257        /// <param name="e">Event argument.</param>
258        private void deleteDataToolStripMenuItem_Click(object sender, EventArgs e)
259        {
260            if (data.SelectedIndices.Count == 0)
261                return;
262
263            foreach (ListViewItem obj in data.SelectedItems)
264            {
265                task.Targets.Remove((Task.ErasureTarget)obj.Tag);
266                data.Items.Remove(obj);
267            }
268        }
269
270        /// <summary>
271        /// Generated when the task schedule type changes.
272        /// </summary>
273        /// <param name="sender">The object triggering the event.</param>
274        /// <param name="e">Event argument.</param>
275        private void taskType_CheckedChanged(object sender, EventArgs e)
276        {
277            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
278                scheduleDaily.Enabled = scheduleWeekly.Enabled =
279                scheduleMonthly.Enabled = typeRecurring.Checked;
280            nonRecurringPanel.Visible = !typeRecurring.Checked;
281           
282            scheduleSpan_CheckedChanged(sender, e);
283        }
284
285        /// <summary>
286        /// Generated when the scheduling frequency is changed.
287        /// </summary>
288        /// <param name="sender">The object triggering the event.</param>
289        /// <param name="e">Event argument.</param>
290        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
291        {
292            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
293                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
294                typeRecurring.Checked;
295            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
296                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
297                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
298                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
299                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
300                scheduleWeekly.Checked && typeRecurring.Checked;
301            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
302                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
303                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
304                typeRecurring.Checked;
305
306            scheduleDailySpan_CheckedChanged(sender, e);
307        }
308
309        /// <summary>
310        /// Generated when the daily frequency argument is changed.
311        /// </summary>
312        /// <param name="sender">The object triggering the event.</param>
313        /// <param name="e">Event argument.</param>
314        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
315        {
316            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
317                scheduleDaily.Checked && typeRecurring.Checked;
318        }
319
320        /// <summary>
321        /// Generated when the dialog is closed.
322        /// </summary>
323        /// <param name="sender">The object triggering the event.</param>
324        /// <param name="e">Event argument.</param>
325        private void ok_Click(object sender, EventArgs e)
326        {
327            if (data.Items.Count == 0)
328            {
329                errorProvider.SetIconPadding(data, -16);
330                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
331                errorProvider.SetError(data, S._("The task has no data to erase."));
332                container.SelectedIndex = 0;
333                return;
334            }
335            else if (typeRecurring.Checked && scheduleWeekly.Checked)
336            {
337                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
338                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
339                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
340                    !scheduleWeeklySunday.Checked)
341                {
342                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
343                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
344                        "on at least one day a week"));
345                    container.SelectedIndex = 1;
346                    return;
347                }
348            }
349
350            errorProvider.Clear();
351
352            //Close the dialog
353            DialogResult = DialogResult.OK;
354            Close();
355        }
356
357        /// <summary>
358        /// The task being edited.
359        /// </summary>
360        private Task task = new Task();
361    }
362}
Note: See TracBrowser for help on using the repository browser.