source: trunk/eraser/Eraser/TaskPropertiesForm.cs @ 2516

Revision 2516, 15.2 KB checked in by lowjoel, 2 years ago (diff)

Update copyrights to this year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2012 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;
29using System.Linq;
30using System.IO;
31
32using System.Globalization;
33using Eraser.Manager;
34using Eraser.Util;
35using Eraser.Util.ExtensionMethods;
36using Eraser.Plugins;
37using Eraser.Plugins.ExtensionPoints;
38using Eraser.Plugins.Registrars;
39
40namespace Eraser
41{
42    public partial class TaskPropertiesForm : Form
43    {
44        public TaskPropertiesForm()
45        {
46            InitializeComponent();
47            Theming.ApplyTheme(this);
48            scheduleTime.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
49
50            //Set a default task type
51            typeManual.Checked = true;
52            scheduleDaily.Checked = true;
53        }
54
55        /// <summary>
56        /// Sets or retrieves the task object to be edited or being edited.
57        /// </summary>
58        public Task Task
59        {
60            get { UpdateTaskFromUI(); return task; }
61            set { task = value; UpdateUIFromTask(); }
62        }
63
64        /// <summary>
65        /// Updates the local task object from the UI elements.
66        /// </summary>
67        private void UpdateTaskFromUI()
68        {
69            //Set the name of the task
70            task.Name = name.Text;
71
72            //And the schedule, if selected.
73            if (typeManual.Checked)
74            {
75                task.Schedule = Schedule.RunManually;
76            }
77            else if (typeImmediate.Checked)
78            {
79                task.Schedule = Schedule.RunNow;
80            }
81            else if (typeRestart.Checked)
82            {
83                task.Schedule = Schedule.RunOnRestart;
84            }
85            else if (typeRecurring.Checked)
86            {
87                RecurringSchedule schedule = new RecurringSchedule();
88                schedule.ExecutionTime = new DateTime(1, 1, 1, scheduleTime.Value.Hour,
89                    scheduleTime.Value.Minute, scheduleTime.Value.Second);
90
91                if (scheduleDaily.Checked)
92                {
93                    if (scheduleDailyByDay.Checked)
94                    {
95                        schedule.ScheduleType = RecurringScheduleUnit.Daily;
96                        schedule.Frequency = (int)scheduleDailyByDayFreq.Value;
97                    }
98                    else
99                    {
100                        schedule.ScheduleType = RecurringScheduleUnit.Weekdays;
101                    }
102                }
103                else if (scheduleWeekly.Checked)
104                {
105                    schedule.ScheduleType = RecurringScheduleUnit.Weekly;
106                    schedule.Frequency = (int)scheduleWeeklyFreq.Value;
107                    DaysOfWeek weeklySchedule = 0;
108                    if (scheduleWeeklyMonday.Checked)
109                        weeklySchedule |= DaysOfWeek.Monday;
110                    if (scheduleWeeklyTuesday.Checked)
111                        weeklySchedule |= DaysOfWeek.Tuesday;
112                    if (scheduleWeeklyWednesday.Checked)
113                        weeklySchedule |= DaysOfWeek.Wednesday;
114                    if (scheduleWeeklyThursday.Checked)
115                        weeklySchedule |= DaysOfWeek.Thursday;
116                    if (scheduleWeeklyFriday.Checked)
117                        weeklySchedule |= DaysOfWeek.Friday;
118                    if (scheduleWeeklySaturday.Checked)
119                        weeklySchedule |= DaysOfWeek.Saturday;
120                    if (scheduleWeeklySunday.Checked)
121                        weeklySchedule |= DaysOfWeek.Sunday;
122                    schedule.WeeklySchedule = weeklySchedule;
123                }
124                else if (scheduleMonthly.Checked)
125                {
126                    schedule.ScheduleType = RecurringScheduleUnit.Monthly;
127                    schedule.Frequency = (int)scheduleMonthlyFreq.Value;
128                    schedule.MonthlySchedule = (int)scheduleMonthlyDayNumber.Value;
129                }
130                else
131                    throw new ArgumentException("No such scheduling method.");
132
133                task.Schedule = schedule;
134            }
135        }
136
137        /// <summary>
138        /// Updates the UI elements to reflect the data in the Task object.
139        /// </summary>
140        private void UpdateUIFromTask()
141        {
142            //Set the name of the task
143            name.Text = task.Name;
144
145            //And the schedule, if selected.
146            if (task.Schedule == Schedule.RunManually)
147            {
148                typeManual.Checked = true;
149            }
150            else if (task.Schedule == Schedule.RunNow)
151            {
152                typeImmediate.Checked = true;
153            }
154            else if (task.Schedule == Schedule.RunOnRestart)
155            {
156                typeRestart.Checked = true;
157            }
158            else
159            {
160                typeRecurring.Checked = true;
161                RecurringSchedule schedule = (RecurringSchedule)task.Schedule;
162                scheduleTime.Value = scheduleTime.MinDate.Add(schedule.ExecutionTime.TimeOfDay);
163
164                switch (schedule.ScheduleType)
165                {
166                    case RecurringScheduleUnit.Daily:
167                        scheduleDaily.Checked = true;
168                        scheduleDailyByDay.Checked = true;
169                        scheduleDailyByDayFreq.Value = schedule.Frequency;
170                        break;
171                    case RecurringScheduleUnit.Weekdays:
172                        scheduleDaily.Checked = true;
173                        scheduleDailyByWeekday.Checked = true;
174                        break;
175                    case RecurringScheduleUnit.Weekly:
176                        scheduleWeeklyFreq.Value = schedule.Frequency;
177                        scheduleWeekly.Checked = true;
178                        scheduleWeeklyMonday.Checked =
179                            (schedule.WeeklySchedule & DaysOfWeek.Monday) != 0;
180                        scheduleWeeklyTuesday.Checked =
181                            (schedule.WeeklySchedule & DaysOfWeek.Tuesday) != 0;
182                        scheduleWeeklyWednesday.Checked =
183                            (schedule.WeeklySchedule & DaysOfWeek.Wednesday) != 0;
184                        scheduleWeeklyThursday.Checked =
185                            (schedule.WeeklySchedule & DaysOfWeek.Thursday) != 0;
186                        scheduleWeeklyFriday.Checked =
187                            (schedule.WeeklySchedule & DaysOfWeek.Friday) != 0;
188                        scheduleWeeklySaturday.Checked =
189                            (schedule.WeeklySchedule & DaysOfWeek.Saturday) != 0;
190                        scheduleWeeklySunday.Checked =
191                            (schedule.WeeklySchedule & DaysOfWeek.Sunday) != 0;
192                        break;
193                    case RecurringScheduleUnit.Monthly:
194                        scheduleMonthly.Checked = true;
195                        scheduleMonthlyFreq.Value = schedule.Frequency;
196                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
197                        break;
198                    default:
199                        throw new ArgumentException("Unknown schedule type.");
200                }
201            }
202        }
203
204        /// <summary>
205        /// Triggered when the list view needs to display an item.
206        /// </summary>
207        /// <param name="sender">The list view.</param>
208        /// <param name="e">Event argument.</param>
209        private void data_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
210        {
211            IErasureTarget target = task.Targets[e.ItemIndex];
212            e.Item = new ListViewItem(PathUtil.GetCompactPath(target.ToString(),
213                data.Columns[0].Width, data.Font));
214            e.Item.ToolTipText = target.ToString();
215
216            e.Item.SubItems.Add(target.Method == ErasureMethodRegistrar.Default ?
217                S._("(default)") : target.Method.Name);
218        }
219
220        /// <summary>
221        /// Triggered when the user clicks on the Add Data button.
222        /// </summary>
223        /// <param name="sender">The button.</param>
224        /// <param name="e">Event argument.</param>
225        private void dataAdd_Click(object sender, EventArgs e)
226        {
227            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
228            {
229                if (form.ShowDialog() == DialogResult.OK)
230                {
231                    IErasureTarget target = form.Target;
232                    task.Targets.Add(target);
233                    errorProvider.Clear();
234
235                    ++data.VirtualListSize;
236                }
237            }
238        }
239
240        /// <summary>
241        /// Generated when the user double-clicks an item in the list-view.
242        /// </summary>
243        /// <param name="sender">The list-view which generated this event.</param>
244        /// <param name="e">Event argument.</param>
245        private void data_ItemActivate(object sender, EventArgs e)
246        {
247            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
248            {
249                form.Target = task.Targets[data.SelectedIndices[0]];
250
251                if (form.ShowDialog() == DialogResult.OK)
252                {
253                    IErasureTarget target = form.Target;
254                    task.Targets.RemoveAt(data.SelectedIndices[0]);
255                    task.Targets.Insert(data.SelectedIndices[0], target);
256                }
257            }
258        }
259
260        private void data_DragEnter(object sender, DragEventArgs e)
261        {
262            //Get the list of files.
263            bool recycleBin = false;
264            List<string> paths = new List<string>(TaskDragDropHelper.GetFiles(e, out recycleBin));
265
266            for (int i = 0; i < paths.Count; ++i)
267            {
268                //Just use the file name/directory name.
269                paths[i] = Path.GetFileName(paths[i]);
270            }
271
272            //Add the recycle bin if it was dropped.
273            if (recycleBin)
274                paths.Add(S._("Recycle Bin"));
275
276            string description = null;
277            if (paths.Count == 0)
278            {
279                e.Effect = DragDropEffects.None;
280                description = S._("Cannot add the selected items");
281            }
282            else
283            {
284                e.Effect = DragDropEffects.Copy;
285                description = S._("Add {0}");
286            }
287
288            TaskDragDropHelper.OnDragEnter(this, e, description, paths);
289        }
290
291        private void data_DragLeave(object sender, EventArgs e)
292        {
293            DropTargetHelper.DragLeave((Control)sender);
294        }
295
296        private void data_DragOver(object sender, DragEventArgs e)
297        {
298            DropTargetHelper.DragOver(new Point(e.X, e.Y), e.Effect);
299        }
300
301        private void data_DragDrop(object sender, DragEventArgs e)
302        {
303            TaskDragDropHelper.OnDrop(e);
304            if (e.Effect == DragDropEffects.None)
305                return;
306
307            //Add the targets
308            foreach (IErasureTarget target in TaskDragDropHelper.GetTargets(e))
309            {
310                task.Targets.Add(target);
311                ++data.VirtualListSize;
312
313                errorProvider.Clear();
314            }
315        }
316
317        /// <summary>
318        /// Generated when the user right-clicks on the data selection list-view.
319        /// </summary>
320        /// <param name="sender">The menu being opened.</param>
321        /// <param name="e">Event argument.</param>
322        private void dataContextMenuStrip_Opening(object sender, CancelEventArgs e)
323        {
324            if (data.SelectedIndices.Count == 0)
325            {
326                e.Cancel = true;
327                return;
328            }
329        }
330
331        /// <summary>
332        /// Generated when the user selects the menu itm to remove the selected
333        /// data from the list of data to erase.
334        /// </summary>
335        /// <param name="sender">The object triggering the event.</param>
336        /// <param name="e">Event argument.</param>
337        private void deleteDataToolStripMenuItem_Click(object sender, EventArgs e)
338        {
339            if (data.SelectedIndices.Count == 0)
340                return;
341
342            //Get the list of selected indices; sort them in decreasing order so that we
343            //can iterate and remove items without changing indices
344            SortedSet<int> indices = new SortedSet<int>();
345            foreach (int index in data.SelectedIndices)
346                indices.Add(index);
347
348            //Remove the items from the list view
349            data.SelectedIndices.Clear();
350            data.VirtualListSize -= indices.Count;
351
352            //Then finally remove the items from the task list
353            foreach (int index in indices.Reverse())
354                task.Targets.RemoveAt(index);
355        }
356
357        /// <summary>
358        /// Generated when the task schedule type changes.
359        /// </summary>
360        /// <param name="sender">The object triggering the event.</param>
361        /// <param name="e">Event argument.</param>
362        private void taskType_CheckedChanged(object sender, EventArgs e)
363        {
364            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
365                scheduleDaily.Enabled = scheduleWeekly.Enabled =
366                scheduleMonthly.Enabled = typeRecurring.Checked;
367            nonRecurringPanel.Visible = !typeRecurring.Checked;
368           
369            scheduleSpan_CheckedChanged(sender, e);
370        }
371
372        /// <summary>
373        /// Generated when any of the schedule spans have been clicked.
374        /// </summary>
375        /// <param name="sender">The radio button triggering the event.</param>
376        /// <param name="e">Event argument.</param>
377        private void scheduleSpan_Clicked(object sender, EventArgs e)
378        {
379            //Check the selected radio button
380            scheduleDaily.Checked = sender == scheduleDaily;
381            scheduleWeekly.Checked = sender == scheduleWeekly;
382            scheduleMonthly.Checked = sender == scheduleMonthly;
383
384            //Then trigger the checked changed event.
385            scheduleSpan_CheckedChanged(sender, e);
386        }
387
388        /// <summary>
389        /// Generated when the scheduling frequency is changed.
390        /// </summary>
391        /// <param name="sender">The object triggering the event.</param>
392        /// <param name="e">Event argument.</param>
393        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
394        {
395            RadioButton[] group = new RadioButton[] {
396                scheduleDaily, scheduleWeekly, scheduleMonthly
397            };
398            if (group.Contains(sender) && ((RadioButton)sender).Checked)
399            {
400                foreach (RadioButton button in group)
401                    if (button != sender)
402                        button.Checked = false;
403            }
404
405            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
406                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
407                typeRecurring.Checked;
408            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
409                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
410                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
411                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
412                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
413                scheduleWeekly.Checked && typeRecurring.Checked;
414            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
415                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
416                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
417                typeRecurring.Checked;
418
419            scheduleDailySpan_CheckedChanged(sender, e);
420        }
421
422        /// <summary>
423        /// Generated when any of the daily frequency radio buttons are clicked.
424        /// </summary>
425        /// <param name="sender">The radio button which triggers the event.</param>
426        /// <param name="e">Event argument.</param>
427        private void scheduleDailySpan_Clicked(object sender, EventArgs e)
428        {
429            scheduleDailyByDay.CheckedChanged -= scheduleDailySpan_CheckedChanged;
430            scheduleDailyByWeekday.CheckedChanged -= scheduleDailySpan_CheckedChanged;
431
432            scheduleDailyByDay.Checked = sender == scheduleDailyByDay;
433            scheduleDailyByWeekday.Checked = sender == scheduleDailyByWeekday;
434
435            scheduleDailyByDay.CheckedChanged += scheduleDailySpan_CheckedChanged;
436            scheduleDailyByWeekday.CheckedChanged += scheduleDailySpan_CheckedChanged;
437           
438            scheduleDailySpan_CheckedChanged(sender, e);
439        }
440
441        /// <summary>
442        /// Generated when the daily frequency argument is changed.
443        /// </summary>
444        /// <param name="sender">The object triggering the event.</param>
445        /// <param name="e">Event argument.</param>
446        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
447        {
448            scheduleDailyByDayLbl.Enabled = scheduleDailyByDayFreq.Enabled =
449                scheduleDailyByDay.Checked && scheduleDaily.Checked && typeRecurring.Checked;
450        }
451
452        /// <summary>
453        /// Generated when the dialog is closed.
454        /// </summary>
455        /// <param name="sender">The object triggering the event.</param>
456        /// <param name="e">Event argument.</param>
457        private void ok_Click(object sender, EventArgs e)
458        {
459            if (task.Targets.Count == 0)
460            {
461                errorProvider.SetIconPadding(data, -16);
462                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
463                errorProvider.SetError(data, S._("The task has no data to erase."));
464                container.SelectedIndex = 0;
465                return;
466            }
467            else if (typeRecurring.Checked && scheduleWeekly.Checked)
468            {
469                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
470                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
471                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
472                    !scheduleWeeklySunday.Checked)
473                {
474                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
475                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
476                        "on at least one day a week"));
477                    container.SelectedIndex = 1;
478                    return;
479                }
480            }
481
482            errorProvider.Clear();
483
484            //Close the dialog
485            DialogResult = DialogResult.OK;
486            Close();
487        }
488
489        /// <summary>
490        /// The task being edited.
491        /// </summary>
492        private Task task = new Task();
493    }
494}
Note: See TracBrowser for help on using the repository browser.