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

Revision 349, 10.0 KB checked in by lowjoel, 6 years ago (diff)

Added the copyright headers to all the source files.

  • 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            S.TranslateControl(this);
42            scheduleTime.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
43
44            //Set a default task type
45            typeImmediate.Checked = true;
46            scheduleDaily.Checked = true;
47        }
48
49        /// <summary>
50        /// Sets or retrieves the task object to be edited or being edited.
51        /// </summary>
52        public Task Task
53        {
54            get { UpdateTaskFromUI(); return task; }
55            set { task = value; UpdateUIFromTask(); }
56        }
57
58        /// <summary>
59        /// Updates the local task object from the UI elements.
60        /// </summary>
61        private void UpdateTaskFromUI()
62        {
63            //Set the name of the task
64            task.Name = name.Text;
65
66            //And the schedule, if selected.
67            if (typeImmediate.Checked)
68            {
69                task.Schedule = Schedule.RunNow;
70            }
71            else if (typeRestart.Checked)
72            {
73                task.Schedule = Schedule.RunOnRestart;
74            }
75            else if (typeRecurring.Checked)
76            {
77                RecurringSchedule schedule = new RecurringSchedule();
78                task.Schedule = schedule;
79                schedule.ExecutionTime = new DateTime(1, 1, 1, scheduleTime.Value.Hour,
80                    scheduleTime.Value.Minute, scheduleTime.Value.Second);
81
82                if (scheduleDaily.Checked)
83                {
84                    if (scheduleDailyByDay.Checked)
85                    {
86                        schedule.Type = RecurringSchedule.ScheduleUnit.DAILY;
87                        schedule.Frequency = (int)scheduleDailyByDayFreq.Value;
88                    }
89                    else
90                    {
91                        schedule.Type = RecurringSchedule.ScheduleUnit.WEEKDAYS;
92                    }
93                }
94                else if (scheduleWeekly.Checked)
95                {
96                    schedule.Type = RecurringSchedule.ScheduleUnit.WEEKLY;
97                    schedule.Frequency = (int)scheduleWeeklyFreq.Value;
98                    RecurringSchedule.DaysOfWeek weeklySchedule = 0;
99                    if (scheduleWeeklyMonday.Checked)
100                        weeklySchedule |= RecurringSchedule.DaysOfWeek.MONDAY;
101                    if (scheduleWeeklyTuesday.Checked)
102                        weeklySchedule |= RecurringSchedule.DaysOfWeek.TUESDAY;
103                    if (scheduleWeeklyWednesday.Checked)
104                        weeklySchedule |= RecurringSchedule.DaysOfWeek.WEDNESDAY;
105                    if (scheduleWeeklyThursday.Checked)
106                        weeklySchedule |= RecurringSchedule.DaysOfWeek.THURSDAY;
107                    if (scheduleWeeklyFriday.Checked)
108                        weeklySchedule |= RecurringSchedule.DaysOfWeek.FRIDAY;
109                    if (scheduleWeeklySaturday.Checked)
110                        weeklySchedule |= RecurringSchedule.DaysOfWeek.SATURDAY;
111                    if (scheduleWeeklySunday.Checked)
112                        weeklySchedule |= RecurringSchedule.DaysOfWeek.SUNDAY;
113                    schedule.WeeklySchedule = weeklySchedule;
114                }
115                else if (scheduleMonthly.Checked)
116                {
117                    schedule.Type = RecurringSchedule.ScheduleUnit.MONTHLY;
118                    schedule.Frequency = (int)scheduleMonthlyFreq.Value;
119                    schedule.MonthlySchedule = (int)scheduleMonthlyDayNumber.Value;
120                }
121                else
122                    throw new NotImplementedException("No such scheduling method.");
123            }
124        }
125
126        /// <summary>
127        /// Updates the UI elements to reflect the data in the Task object.
128        /// </summary>
129        private void UpdateUIFromTask()
130        {
131            //Set the name of the task
132            name.Text = task.Name;
133
134            //The data
135            foreach (Task.ErasureTarget target in task.Targets)
136            {
137                ListViewItem item = data.Items.Add(target.UIText);
138                item.SubItems.Add(target.Method.Name);
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.Type)
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                        scheduleWeeklyMonday.Checked =
168                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.MONDAY) != 0;
169                        scheduleWeeklyTuesday.Checked =
170                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.TUESDAY) != 0;
171                        scheduleWeeklyWednesday.Checked =
172                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.WEDNESDAY) != 0;
173                        scheduleWeeklyThursday.Checked =
174                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.THURSDAY) != 0;
175                        scheduleWeeklyFriday.Checked =
176                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.FRIDAY) != 0;
177                        scheduleWeeklySaturday.Checked =
178                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SATURDAY) != 0;
179                        scheduleWeeklySunday.Checked =
180                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SUNDAY) != 0;
181                        break;
182                    case RecurringSchedule.ScheduleUnit.MONTHLY:
183                        scheduleMonthlyFreq.Value = schedule.Frequency;
184                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
185                        break;
186                    default:
187                        throw new NotImplementedException("Unknown schedule type.");
188                }
189            }
190        }
191
192        /// <summary>
193        /// Triggered when the user clicks on the Add Data button.
194        /// </summary>
195        /// <param name="sender">The button.</param>
196        /// <param name="e">Event argument.</param>
197        private void dataAdd_Click(object sender, EventArgs e)
198        {
199            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
200            {
201                if (form.ShowDialog() == DialogResult.OK)
202                {
203                    Task.ErasureTarget target = form.Target;
204                    ListViewItem item = data.Items.Add(target.UIText);
205                   
206                    item.SubItems.Add(target.Method.Name);
207                    task.Targets.Add(target);
208                    errorProvider.Clear();
209                }
210            }
211        }
212
213        /// <summary>
214        /// Generated when the user double-clicks an item in the list-view.
215        /// </summary>
216        /// <param name="sender">The list-view which generated this event.</param>
217        /// <param name="e">Event argument.</param>
218        private void data_ItemActivate(object sender, EventArgs e)
219        {
220            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
221            {
222                ListViewItem item = data.SelectedItems[0];
223                form.Target = task.Targets[item.Index];
224
225                if (form.ShowDialog() == DialogResult.OK)
226                {
227                    Task.ErasureTarget target = form.Target;
228                    task.Targets[item.Index] = target;
229                    item.Text = target.UIText;
230                    item.SubItems[1].Text = target.Method.Name;
231                }
232            }
233        }
234
235        private void taskType_CheckedChanged(object sender, EventArgs e)
236        {
237            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
238                scheduleDaily.Enabled = scheduleWeekly.Enabled =
239                scheduleMonthly.Enabled = typeRecurring.Checked;
240            nonRecurringPanel.Visible = !typeRecurring.Checked;
241           
242            scheduleSpan_CheckedChanged(sender, e);
243        }
244
245        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
246        {
247            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
248                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
249                typeRecurring.Checked;
250            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
251                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
252                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
253                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
254                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
255                scheduleWeekly.Checked && typeRecurring.Checked;
256            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
257                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
258                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
259                typeRecurring.Checked;
260
261            scheduleDailySpan_CheckedChanged(sender, e);
262        }
263
264        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
265        {
266            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
267                scheduleDaily.Checked && typeRecurring.Checked;
268        }
269
270        private void ok_Click(object sender, EventArgs e)
271        {
272            if (data.Items.Count == 0)
273            {
274                errorProvider.SetIconPadding(data, -16);
275                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
276                errorProvider.SetError(data, S._("The task has no data to erase."));
277                container.SelectedIndex = 0;
278                return;
279            }
280            else if (typeRecurring.Checked && scheduleWeekly.Checked)
281            {
282                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
283                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
284                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
285                    !scheduleWeeklySunday.Checked)
286                {
287                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
288                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
289                        "on at least one day a week"));
290                    container.SelectedIndex = 1;
291                    return;
292                }
293            }
294
295            errorProvider.Clear();
296
297            //Close the dialog
298            DialogResult = DialogResult.OK;
299            Close();
300        }
301
302        private Task task = new Task();
303    }
304}
Note: See TracBrowser for help on using the repository browser.