using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Microsoft.Win32.TaskScheduler
{
///
/// A wizard that walks the user through the creation of a simple task.
///
[ToolboxItem(true), ToolboxItemFilter("System.Windows.Forms.Control.TopLevel"), Description("Wizard that walks the user through the creation of a simple task.")]
[Designer("System.ComponentModel.Design.ComponentDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[DesignTimeVisible(true), DefaultProperty("RegisterTaskOnFinish")]
[System.Drawing.ToolboxBitmap(typeof(TaskEditDialog), "TaskDialog")]
public sealed partial class TaskSchedulerWizard :
#if DEBUG
Form
#else
DialogBase
#endif
{
private Action action;
private AvailableWizardActions availActions = (AvailableWizardActions)0xC1;
private AvailableWizardPages availPages = (AvailableWizardPages)0xFF;
private AvailableWizardTriggers availTriggers = (AvailableWizardTriggers)0x7FF;
private bool flagExecutorIsGroup, flagExecutorIsServiceAccount;
private bool IsV2 = true;
private bool onAssignment = false;
private bool registerTaskOnFinish;
private Task task;
private TaskDefinition td;
private Trigger trigger;
private bool flagRunOnlyWhenUserIsLoggedOn;
///
/// Initializes a new instance of the class.
///
public TaskSchedulerWizard()
{
InitializeComponent();
wizardControl1.TitleIcon = this.Icon;
AllowEditorOnFinish = true;
RegisterTaskOnFinish = false;
repeatSpan.Items.AddRange(new TimeSpan2[] { TimeSpan2.FromMinutes(5), TimeSpan2.FromMinutes(10), TimeSpan2.FromMinutes(15), TimeSpan2.FromMinutes(30), TimeSpan2.FromHours(1) });
durationSpan.Items.AddRange(new TimeSpan2[] { TimeSpan2.Zero, TimeSpan2.FromMinutes(15), TimeSpan2.FromMinutes(30), TimeSpan2.FromHours(1), TimeSpan2.FromHours(12), TimeSpan2.FromDays(1) });
durationSpan.FormattedZero = EditorProperties.Resources.TimeSpanIndefinitely;
}
///
/// Initializes a new instance of the class.
///
/// A instance.
/// An optional . Leaving null creates a new task.
/// If set to true the task will be registered when Finish is pressed.
public TaskSchedulerWizard(TaskService service, TaskDefinition definition = null, bool registerOnFinish = false)
: this()
{
Initialize(service, definition);
RegisterTaskOnFinish = registerOnFinish;
}
///
/// Initializes a new instance of the class.
///
/// A instance.
/// If set to true the task will be registered when Finish is pressed.
public TaskSchedulerWizard(Task task, bool registerOnFinish = false)
: this()
{
Initialize(task);
RegisterTaskOnFinish = registerOnFinish;
}
///
/// Flags to indicate which actions are available in the .
///
[Flags]
public enum AvailableWizardActions
{
/// This action performs a command-line operation. For example, the action can run a script, launch an executable, or, if the name of a document is provided, find its associated application and launch the application with the document.
Execute = 0x1,
/*/// This action fires a handler.
ComHandler = 0x20,*/
/// This action sends and e-mail.
SendEmail = 0x40,
/// This action shows a message box.
ShowMessage = 0x80
}
///
/// Flags to indicate which pages are visible in the .
///
[Flags]
public enum AvailableWizardPages
{
/// Displays the introduction page with name and description.
IntroPage = 0x1,
/// Displays the security options page with user and password options.
SecurityPage = 0x10,
/// Displays trigger selection page.
TriggerSelectPage = 0x42,
/// Displays generic trigger properties page.
TriggerPropertiesPage = 0x20,
/// Displays action selection page.
ActionSelectPage = 0x84,
/// Displays the summary page.
SummaryPage = 0x8,
/// Displays a page to edit the selected trigger.
TriggerEditPage = 0x40,
/// Displays a page to edit the selected action.
ActionEditPage = 0x80
}
///
/// Flags to indicate which triggers are available in the .
///
[Flags]
public enum AvailableWizardTriggers
{
/// Triggers the task when a specific event occurs. Version 1.2 only.
Event = 0x1,
/// Triggers the task at a specific time of day.
Time = 0x2,
/// Triggers the task on a daily schedule.
Daily = 0x4,
/// Triggers the task on a weekly schedule.
Weekly = 0x8,
/// Triggers the task on a monthly schedule.
Monthly = 0x10,
/// Triggers the task on a monthly day-of-week schedule.
MonthlyDOW = 0x20,
/// Triggers the task when the computer goes into an idle state.
Idle = 0x40,
/// Triggers the task when the task is registered. Version 1.2 only.
Registration = 0x80,
/// Triggers the task when the computer boots.
Boot = 0x100,
/// Triggers the task when a specific user logs on.
Logon = 0x200,
/// Triggers the task when a specific user session state changes. Version 1.2 only.
SessionStateChange = 0x400,
}
///
/// Gets or sets a value indicating whether show a check box on the summary page that will open the full editor when the user presses Finish.
///
///
/// true if shown; otherwise, false.
///
[DefaultValue(true), Category("Behavior"), Description("Show a check box on the summary page that will open the full editor.")]
public bool AllowEditorOnFinish { get; set; }
///
/// Gets or sets the available actions.
///
///
/// The available actions.
///
[DefaultValue((AvailableWizardActions)0xC1)]
public AvailableWizardActions AvailableActions
{
get { return availActions; }
set
{
if (value != availActions && ((availPages & AvailableWizardPages.ActionSelectPage) == AvailableWizardPages.ActionSelectPage))
{
availActions = value;
SetupActionList();
}
}
}
///
/// Gets or sets the available pages.
///
///
/// The available pages.
///
[DefaultValue((AvailableWizardPages)0xFF)]
public AvailableWizardPages AvailablePages
{
get { return availPages; }
set
{
if (availPages != value)
{
availPages = value;
SetupPages();
if ((availPages & AvailableWizardPages.TriggerSelectPage) != AvailableWizardPages.TriggerSelectPage)
{
if (td != null && (availPages & AvailableWizardPages.TriggerEditPage) == AvailableWizardPages.TriggerEditPage)
{
if (td.Triggers.Count > 0)
availTriggers = (AvailableWizardTriggers)(1 << (int)td.Triggers[0].TriggerType);
}
else
availTriggers = 0;
SetupTriggerList();
}
if ((availPages & AvailableWizardPages.ActionSelectPage) != AvailableWizardPages.ActionSelectPage)
{
if (td != null && (availPages & AvailableWizardPages.ActionEditPage) == AvailableWizardPages.ActionEditPage)
{
if (td.Actions.Count > 0)
availActions = (AvailableWizardActions)(1 << (int)td.Actions[0].ActionType);
}
else
availActions = 0;
SetupActionList();
}
}
}
}
///
/// Gets or sets the available triggers.
///
///
/// The available triggers.
///
[DefaultValue((AvailableWizardPages)0x7FF)]
public AvailableWizardTriggers AvailableTriggers
{
get { return availTriggers; }
set
{
if (value != availTriggers && ((availPages & AvailableWizardPages.TriggerSelectPage) == AvailableWizardPages.TriggerSelectPage))
{
availTriggers = value;
SetupTriggerList();
}
}
}
///
/// Gets or sets the text shown on the summary page prompting for editor on finish click.
///
///
/// The editor on finish text.
///
[DefaultValue((string)null), Category("Appearance")]
public string EditorOnFinishText { get; set; }
///
/// Gets or sets the icon for the form.
///
///
/// An that represents the icon for the form.
///
///
///
///
///
///
///
public new System.Drawing.Icon Icon
{
get { return wizardControl1.TitleIcon; }
set { wizardControl1.TitleIcon = value; }
}
///
/// Gets the password.
///
[Browsable(false), DefaultValue((string)null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Password { get; private set; }
///
/// Gets or sets a value indicating whether to register the task on Finish.
///
/// true if task registered on Finish; otherwise, false.
[DefaultValue(false), Category("Behavior"), Description("Indicates whether to register the task on Finish")]
public bool RegisterTaskOnFinish
{
get { return registerTaskOnFinish; }
set
{
if (registerTaskOnFinish != value)
{
registerTaskOnFinish = value;
summaryPrompt.Visible = registerTaskOnFinish;
}
}
}
///
/// Gets or sets a value indicating whether an icon is displayed in the caption bar of the form.
///
///
/// true if the form displays an icon in the caption bar; otherwise, false. The default is true.
///
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new bool ShowIcon
{
get { return base.ShowIcon; }
set { base.ShowIcon = value; }
}
///
/// Gets or sets the summary format string.
///
///
/// The summary format string.
///
[DefaultValue((string)null), Category("Appearance")]
public string SummaryFormatString { get; set; }
///
/// Gets or sets the summary registration notice.
///
///
/// The summary registration notice.
///
[DefaultValue((string)null), Category("Appearance")]
public string SummaryRegistrationNotice { get; set; }
///
/// Gets the current . This is only the task used to initialize this control. The updates made to the control are not registered.
///
/// The task.
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Task Task
{
get
{
return task;
}
private set
{
task = value;
if (task != null)
{
TaskFolder = System.IO.Path.GetDirectoryName(task.Path);
TaskService = task.TaskService;
TaskDefinition = task.Definition;
}
}
}
///
/// Gets or sets the .
///
/// The task definition.
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public TaskDefinition TaskDefinition
{
get
{
return td;
}
set
{
if (TaskService == null)
throw new ArgumentNullException("TaskDefinition cannot be set until TaskService has been set with a valid object.");
if (value == null)
throw new ArgumentNullException("TaskDefinition cannot be set to null.");
td = value;
onAssignment = true;
IsV2 = TaskService.HighestSupportedVersion >= (new Version(1, 2)) && td.Settings.Compatibility >= TaskCompatibility.V2;
// Set General tab
nameText.Text = task != null ? task.Name : string.Empty;
descText.Text = td.RegistrationInfo.Description;
SetUserControls(td.Principal.LogonType);
taskLoggedOnRadio.Checked = flagRunOnlyWhenUserIsLoggedOn;
taskLoggedOptionalRadio.Checked = !flagRunOnlyWhenUserIsLoggedOn;
taskLocalOnlyCheck.Checked = !flagRunOnlyWhenUserIsLoggedOn && td.Principal.LogonType == TaskLogonType.S4U;
// Setup trigger values
if (td.Triggers.Count > 0)
trigger = (Trigger)td.Triggers[0].Clone();
else
trigger = new TimeTrigger();
if (availTriggers > 0)
{
switch (trigger.TriggerType)
{
case TaskTriggerType.Time:
oneTimeStartTimePicker.Value = trigger.StartBoundary;
SetTriggerListItem(AvailableWizardTriggers.Time);
break;
case TaskTriggerType.Daily:
dailyTriggerUI1.Trigger = trigger;
SetTriggerListItem(AvailableWizardTriggers.Daily);
break;
case TaskTriggerType.Weekly:
weeklyTriggerUI1.Trigger = trigger;
SetTriggerListItem(AvailableWizardTriggers.Weekly);
break;
case TaskTriggerType.Monthly:
case TaskTriggerType.MonthlyDOW:
monthlyTriggerUI1.Trigger = trigger;
SetTriggerListItem(AvailableWizardTriggers.Monthly);
break;
case TaskTriggerType.Event:
eventTriggerUI1.TargetServer = this.TaskService.TargetServer;
eventTriggerUI1.Trigger = trigger;
SetTriggerListItem(AvailableWizardTriggers.Event);
break;
default:
break;
}
bool hasRep = trigger.Repetition.Interval != TimeSpan.Zero;
if (!hasRep)
{
durationSpan.Value = repeatSpan.Value = TimeSpan.Zero;
}
else
{
durationSpan.Value = trigger.Repetition.Duration;
repeatSpan.Value = trigger.Repetition.Interval;
}
repeatCheckBox.Checked = repeatSpan.Enabled = durationLabel.Enabled = durationSpan.Enabled = hasRep;
enabledCheckBox.Checked = trigger.Enabled;
}
// Setup action values
if (td.Actions.Count > 0)
action = (Action)td.Actions[0].Clone();
else
action = new ExecAction();
if (availActions > 0)
{
if (action is ExecAction)
{
execActionUI1.Action = action;
SetActionListItem(AvailableWizardActions.Execute);
}
else if (action is EmailAction)
{
emailActionUI1.Action = action;
SetActionListItem(AvailableWizardActions.SendEmail);
}
else if (action is ShowMessageAction)
{
showMessageActionUI1.Action = action;
SetActionListItem(AvailableWizardActions.ShowMessage);
}
}
SetupPages();
SetupTriggerList();
SetupActionList();
onAssignment = false;
}
}
///
/// Gets or sets the folder for the task. Used only if property has not been set and property is true.
///
/// The task folder name.
[DefaultValue(null), Category("Behavior"), Description("Folder for registering the task.")]
public string TaskFolder { get; set; }
///
/// Gets or sets the name of the task.
///
/// The name of the task.
[DefaultValue(""), Category("Appearance"), Description("Name of the task to display")]
public string TaskName
{
get { return nameText.Text; }
set { nameText.Text = value; }
}
///
/// Gets or sets the .
///
/// The task service.
[DefaultValue(null), Category("Data"), Description("The TaskService for this wizard")]
public TaskService TaskService { get; set; }
///
/// Gets or sets the text associated with this control.
///
///
/// The text associated with this control.
///
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new string Text
{
get { return base.Text; }
set { base.Text = value; }
}
///
/// Gets or sets the title.
///
/// The title.
[Category("Appearance"), Description("A string to display in the title bar of the dialog box."), Localizable(true)]
public string Title
{
get { return wizardControl1.Title; }
set { wizardControl1.Title = value; }
}
///
/// Gets or sets the trigger page prompt.
///
///
/// The trigger page prompt.
///
[DefaultValue((string)null), Category("Appearance"), Description("Trigger page title prompt.")]
public string TriggerPagePrompt { get; set; }
///
/// Gets or sets the trigger properties page instructions text.
///
///
/// The trigger properties instruction text.
///
[DefaultValue((string)null), Category("Appearance"), Description("Trigger properties page instruction text.")]
public string TriggerPropertiesInstructions { get; set; }
///
/// Initializes the control for the editing of a new .
///
/// A instance.
/// An optional . Leaving null creates a new task.
public void Initialize(TaskService service, TaskDefinition td = null)
{
this.TaskService = service;
this.task = null;
if (td == null)
this.TaskDefinition = service.NewTask();
else
{
if (td.Triggers.Count > 1)
throw new ArgumentException("Only tasks with a single trigger can be used to initialize the wizard.");
this.TaskDefinition = td;
}
this.wizardControl1.RestartPages();
}
///
/// Initializes the control for the editing of an existing .
///
/// A instance.
public void Initialize(Task task)
{
this.Task = task;
this.wizardControl1.RestartPages();
}
private void actionSelectionList_SelectedIndexChanged(object sender, System.EventArgs e)
{
actionSelectPage.AllowNext = (actionSelectionList.SelectedIndex >= 0);
}
private void actionSelectPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
bool hasValue = (this.action != null);
AvailableWizardActions selAct = (AvailableWizardActions)actionSelectionList.SelectedItem.Tag;
switch (selAct)
{
case AvailableWizardActions.Execute:
e.Page.NextPage = runActionPage;
if (!hasValue || this.action.ActionType != TaskActionType.Execute)
action = new ExecAction();
break;
case AvailableWizardActions.SendEmail:
e.Page.NextPage = emailActionPage;
if (!hasValue || this.action.ActionType != TaskActionType.SendEmail)
action = new EmailAction();
break;
case AvailableWizardActions.ShowMessage:
e.Page.NextPage = msgActionPage;
if (!hasValue || this.action.ActionType != TaskActionType.ShowMessage)
action = new ShowMessageAction();
break;
default:
e.Cancel = true;
break;
}
if (e.Page.NextPage != null)
e.Page.NextPage.NextPage = secOptPage;
}
private void AddActionToSelectionList(AvailableWizardActions action)
{
this.actionSelectionList.Items.Add(new GroupControls.RadioButtonListItem()
{
Text = TaskPropertiesControl.BuildEnumString("WizActionText", action),
Subtext = TaskPropertiesControl.BuildEnumString("WizActionSubtext", action),
Tag = (int)action
});
}
private void AddTriggerToSelectionList(AvailableWizardTriggers trig)
{
this.triggerSelectionList.Items.Add(new GroupControls.RadioButtonListItem()
{
Text = TaskPropertiesControl.BuildEnumString("WizTriggerText", trig),
Subtext = TaskPropertiesControl.BuildEnumString("WizTriggerSubtext", trig),
Tag = (int)trig
});
}
private void changePrincipalButton_Click(object sender, EventArgs e)
{
string acct = String.Empty, sid;
if (!HelperMethods.SelectAccount(this, TaskService.TargetServer, ref acct, out flagExecutorIsGroup, out flagExecutorIsServiceAccount, out sid))
return;
if (flagExecutorIsServiceAccount)
{
if (!IsV2 && acct != "SYSTEM")
{
MessageBox.Show(this, EditorProperties.Resources.TaskSchedulerName, EditorProperties.Resources.Error_NoGroupsUnderV1, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
flagExecutorIsGroup = false;
if (IsV2)
td.Principal.GroupId = null;
td.Principal.UserId = acct;
td.Principal.LogonType = TaskLogonType.ServiceAccount;
//this.flagExecutorIsCurrentUser = false;
}
else if (flagExecutorIsGroup)
{
if (!IsV2)
{
MessageBox.Show(this, EditorProperties.Resources.TaskSchedulerName, EditorProperties.Resources.Error_NoGroupsUnderV1, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
td.Principal.GroupId = acct;
td.Principal.UserId = null;
td.Principal.LogonType = TaskLogonType.Group;
//this.flagExecutorIsCurrentUser = false;
}
else
{
if (IsV2)
td.Principal.GroupId = null;
td.Principal.UserId = acct;
//this.flagExecutorIsCurrentUser = this.UserIsExecutor(objArray[0].ObjectName);
if (td.Principal.LogonType == TaskLogonType.Group)
{
td.Principal.LogonType = TaskLogonType.InteractiveToken;
}
else if (td.Principal.LogonType == TaskLogonType.ServiceAccount)
{
td.Principal.LogonType = TaskLogonType.InteractiveTokenOrPassword;
}
}
SetUserControls(td.Principal.LogonType);
}
private void durationSpan_ValueChanged(object sender, EventArgs e)
{
if (!onAssignment)
{
trigger.Repetition.Duration = durationSpan.Value;
if (trigger.Repetition.Duration < trigger.Repetition.Interval && trigger.Repetition.Duration != TimeSpan.Zero)
{
onAssignment = true;
repeatSpan.Value = trigger.Repetition.Duration - TimeSpan.FromMinutes(1);
trigger.Repetition.Interval = repeatSpan.Value;
onAssignment = false;
}
}
}
private void emailActionPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
action = emailActionUI1.Action;
}
private void emailActionUI1_KeyValueChanged(object sender, EventArgs e)
{
emailActionPage.AllowNext = emailActionUI1.IsActionValid();
}
private void enabledCheckBox_CheckedChanged(object sender, EventArgs e)
{
trigger.Enabled = enabledCheckBox.Checked;
}
private void execActionUI1_KeyValueChanged(object sender, EventArgs e)
{
runActionPage.AllowNext = execActionUI1.IsActionValid();
}
private string InvokeCredentialDialog(string userName)
{
CredentialsDialog dlg = new CredentialsDialog(EditorProperties.Resources.TaskSchedulerName,
EditorProperties.Resources.CredentialPromptMessage, userName);
dlg.Options |= CredentialsDialogOptions.Persist;
dlg.ValidatePassword = true;
if (dlg.ShowDialog(this.ParentForm) == DialogResult.OK)
return dlg.Password;
return null;
}
private void monthlyTriggerUI1_TriggerTypeChanged(object sender, EventArgs e)
{
if (!onAssignment)
{
Trigger newTrigger = null;
if (monthlyTriggerUI1.TriggerType == TaskTriggerType.Monthly)
newTrigger = new MonthlyTrigger();
else
newTrigger = new MonthlyDOWTrigger();
if (trigger != null)
newTrigger.CopyProperties(trigger);
monthlyTriggerUI1.Trigger = (trigger = newTrigger);
}
}
private void msgActionPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
action = showMessageActionUI1.Action;
}
private void nameText_TextChanged(object sender, System.EventArgs e)
{
introPage.AllowNext = nameText.TextLength > 0;
}
private void oneTimeTriggerPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
trigger.StartBoundary = oneTimeStartTimePicker.Value;
}
private void onEventTriggerPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
trigger = eventTriggerUI1.Trigger;
}
private void onEventTriggerPage_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
if (eventTriggerUI1.Trigger == null)
eventTriggerUI1.Trigger = new EventTrigger();
if (System.Environment.Version.Major < 4)
onEventTriggerPage.AllowNext = true;
}
void eventTriggerUI1_TriggerChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (eventTriggerUI1.IsTriggerValid())
onEventTriggerPage.AllowNext = true;
}
private void repeatCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!onAssignment)
{
if (repeatCheckBox.Checked)
{
repeatSpan.Value = repeatSpan.Items[repeatSpan.Items.Count - 1];
durationSpan.Value = TimeSpan.Zero;
}
else
{
trigger.Repetition.Duration = trigger.Repetition.Interval = TimeSpan.Zero;
}
repeatSpan.Enabled = durationSpan.Enabled = durationLabel.Enabled = repeatCheckBox.Checked;
}
}
private void repeatSpan_ValueChanged(object sender, EventArgs e)
{
if (!onAssignment)
{
trigger.Repetition.Interval = repeatSpan.Value;
if (trigger.Repetition.Duration < trigger.Repetition.Interval && trigger.Repetition.Duration != TimeSpan.Zero)
{
onAssignment = true;
durationSpan.Value = trigger.Repetition.Interval + TimeSpan.FromMinutes(1);
trigger.Repetition.Duration = durationSpan.Value;
onAssignment = false;
}
}
}
private void runActionPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
action = execActionUI1.Action;
}
private void secOptPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
string user = this.TaskDefinition.Principal.UserId;
Password = null;
if (this.TaskDefinition.Principal.LogonType == TaskLogonType.InteractiveTokenOrPassword || this.TaskDefinition.Principal.LogonType == TaskLogonType.Password)
{
Password = InvokeCredentialDialog(user);
if (Password == null)
{
MessageBox.Show(this, EditorProperties.Resources.UserAuthenticationError, null);
e.Cancel = true;
}
}
}
private void SetActionListItem(AvailableWizardActions availableWizardActions)
{
foreach (var item in actionSelectionList.Items)
{
if (item.Tag.Equals((int)availableWizardActions))
{
actionSelectionList.SelectedItem = item;
return;
}
}
}
private bool SetPage(AeroWizard.WizardPage page, int flag, int flagSet)
{
bool set = (flagSet & flag) == flag;
page.Suppress = !set;
return set;
}
private void SetTriggerListItem(AvailableWizardTriggers availableWizardTriggers)
{
foreach (var item in triggerSelectionList.Items)
{
if (item.Tag.Equals((int)availableWizardTriggers))
{
triggerSelectionList.SelectedItem = item;
return;
}
}
}
private void SetupActionList()
{
actionSelectionList.Items.Clear();
actionSelectionList.SelectedIndex = -1;
if (SetPage(runActionPage, (int)AvailableWizardActions.Execute, (int)AvailableActions))
AddActionToSelectionList(AvailableWizardActions.Execute);
if (IsV2 && SetPage(msgActionPage, (int)AvailableWizardActions.ShowMessage, (int)AvailableActions))
AddActionToSelectionList(AvailableWizardActions.ShowMessage);
if (IsV2 && SetPage(emailActionPage, (int)AvailableWizardActions.SendEmail, (int)AvailableActions))
AddActionToSelectionList(AvailableWizardActions.SendEmail);
}
private void SetupPages()
{
SetPage(introPage, (int)AvailableWizardPages.IntroPage, (int)availPages);
SetPage(triggerSelectPage, (int)AvailableWizardPages.TriggerSelectPage, (int)availPages);
SetPage(triggerPropPage, (int)AvailableWizardPages.TriggerPropertiesPage, (int)availPages);
SetPage(actionSelectPage, (int)AvailableWizardPages.ActionSelectPage, (int)availPages);
SetPage(secOptPage, (int)AvailableWizardPages.SecurityPage, (int)availPages);
SetPage(summaryPage, (int)AvailableWizardPages.SummaryPage, (int)availPages);
}
private void SetupTriggerList()
{
triggerSelectionList.Items.Clear();
triggerSelectionList.SelectedIndex = -1;
if (SetPage(dailyTriggerPage, (int)AvailableWizardTriggers.Daily, (int)AvailableTriggers))
AddTriggerToSelectionList(AvailableWizardTriggers.Daily);
if (IsV2 && SetPage(onEventTriggerPage, (int)AvailableWizardTriggers.Event, (int)AvailableTriggers))
AddTriggerToSelectionList(AvailableWizardTriggers.Event);
if (SetPage(monthlyTriggerPage, (int)AvailableWizardTriggers.Monthly, (int)AvailableTriggers))
AddTriggerToSelectionList(AvailableWizardTriggers.Monthly);
if (SetPage(oneTimeTriggerPage, (int)AvailableWizardTriggers.Time, (int)AvailableTriggers))
AddTriggerToSelectionList(AvailableWizardTriggers.Time);
if (SetPage(weeklyTriggerPage, (int)AvailableWizardTriggers.Weekly, (int)AvailableTriggers))
AddTriggerToSelectionList(AvailableWizardTriggers.Weekly);
}
private void SetUserControls(TaskLogonType logonType)
{
switch (logonType)
{
case TaskLogonType.InteractiveToken:
this.flagRunOnlyWhenUserIsLoggedOn = true;
this.flagExecutorIsServiceAccount = false;
this.flagExecutorIsGroup = false;
break;
case TaskLogonType.Group:
this.flagRunOnlyWhenUserIsLoggedOn = true;
this.flagExecutorIsServiceAccount = false;
this.flagExecutorIsGroup = true;
break;
case TaskLogonType.ServiceAccount:
this.flagRunOnlyWhenUserIsLoggedOn = false;
this.flagExecutorIsServiceAccount = true;
this.flagExecutorIsGroup = false;
break;
default:
this.flagRunOnlyWhenUserIsLoggedOn = false;
this.flagExecutorIsServiceAccount = false;
this.flagExecutorIsGroup = false;
break;
}
if (this.flagExecutorIsServiceAccount)
{
taskLoggedOnRadio.Enabled = false;
taskLoggedOptionalRadio.Enabled = false;
taskLocalOnlyCheck.Enabled = false;
}
else if (this.flagExecutorIsGroup)
{
taskLoggedOnRadio.Enabled = true;
taskLoggedOptionalRadio.Enabled = false;
taskLocalOnlyCheck.Enabled = false;
}
else if (this.flagRunOnlyWhenUserIsLoggedOn)
{
taskLoggedOnRadio.Enabled = true;
taskLoggedOptionalRadio.Enabled = true;
taskLocalOnlyCheck.Enabled = false;
}
else
{
taskLoggedOnRadio.Enabled = true;
taskLoggedOptionalRadio.Enabled = true;
taskLocalOnlyCheck.Enabled = true && (task == null || IsV2);
}
taskLoggedOnRadio.Checked = flagRunOnlyWhenUserIsLoggedOn;
taskLoggedOptionalRadio.Checked = !flagRunOnlyWhenUserIsLoggedOn;
taskLocalOnlyCheck.Checked = !flagRunOnlyWhenUserIsLoggedOn && logonType == TaskLogonType.S4U;
string user = td == null ? null : td.Principal.ToString();
if (string.IsNullOrEmpty(user))
user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
taskPrincipalText.Text = user;
}
private void showMessageActionUI1_KeyValueChanged(object sender, EventArgs e)
{
msgActionPage.AllowNext = showMessageActionUI1.IsActionValid();
}
private void summaryPage_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
summaryPrompt.Visible = RegisterTaskOnFinish;
if (SummaryRegistrationNotice != null)
summaryPrompt.Text = SummaryRegistrationNotice;
openDlgAfterCheck.Visible = AllowEditorOnFinish;
if (EditorOnFinishText != null)
openDlgAfterCheck.Text = EditorOnFinishText;
string fmt = string.IsNullOrEmpty(SummaryFormatString) ? EditorProperties.Resources.WizardSummaryFormatString : SummaryFormatString;
sumText.Text = string.Format(fmt,
nameText.Text,
descText.Text,
trigger.ToString(),
TaskEnumGlobalizer.GetString(action.ActionType) + ": " + action.ToString());
sumText.Select(0, 0);
}
private void taskLocalOnlyCheck_CheckedChanged(object sender, EventArgs e)
{
if (!onAssignment)
td.Principal.LogonType = IsV2 ? ((taskLocalOnlyCheck.Checked) ? TaskLogonType.S4U : TaskLogonType.Password) : TaskLogonType.InteractiveTokenOrPassword;
}
private void taskLoggedOnRadio_CheckedChanged(object sender, EventArgs e)
{
if (!onAssignment)
td.Principal.LogonType = TaskLogonType.InteractiveToken;
}
private void taskLoggedOptionalRadio_CheckedChanged(object sender, EventArgs e)
{
taskLocalOnlyCheck.Enabled = (task == null || IsV2) && taskLoggedOptionalRadio.Checked;
taskLocalOnlyCheck_CheckedChanged(sender, e);
}
private void triggerPropPage_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
if (this.TriggerPropertiesInstructions != null)
triggerPropText.Text = this.TriggerPropertiesInstructions;
}
private void triggerSelectionList_SelectedIndexChanged(object sender, System.EventArgs e)
{
triggerSelectPage.AllowNext = (triggerSelectionList.SelectedIndex >= 0);
}
private void triggerSelectPage_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
bool hasValue = (this.trigger != null);
AvailableWizardTriggers selTrig = (AvailableWizardTriggers)triggerSelectionList.SelectedItem.Tag;
switch (selTrig)
{
case AvailableWizardTriggers.Event:
e.Page.NextPage = onEventTriggerPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Event)
trigger = new EventTrigger();
break;
case AvailableWizardTriggers.Time:
e.Page.NextPage = oneTimeTriggerPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Time)
trigger = new TimeTrigger();
oneTimeStartTimePicker.Value = trigger.StartBoundary;
break;
case AvailableWizardTriggers.Daily:
e.Page.NextPage = dailyTriggerPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Daily)
dailyTriggerUI1.Trigger = (trigger = new DailyTrigger());
break;
case AvailableWizardTriggers.Weekly:
e.Page.NextPage = weeklyTriggerPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Weekly)
weeklyTriggerUI1.Trigger = (trigger = new WeeklyTrigger());
break;
case AvailableWizardTriggers.Monthly:
e.Page.NextPage = monthlyTriggerPage;
if (!hasValue || (this.trigger.TriggerType != TaskTriggerType.Monthly && this.trigger.TriggerType != TaskTriggerType.MonthlyDOW))
monthlyTriggerUI1.Trigger = (trigger = new MonthlyTrigger());
break;
case AvailableWizardTriggers.Idle:
e.Page.NextPage = actionSelectPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Idle)
trigger = new IdleTrigger();
break;
case AvailableWizardTriggers.Boot:
e.Page.NextPage = actionSelectPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Boot)
trigger = new BootTrigger();
break;
case AvailableWizardTriggers.Logon:
e.Page.NextPage = actionSelectPage;
if (!hasValue || this.trigger.TriggerType != TaskTriggerType.Logon)
trigger = new LogonTrigger();
break;
default:
e.Cancel = true;
break;
}
if (e.Page.NextPage != actionSelectPage && e.Page.NextPage != null)
e.Page.NextPage.NextPage = actionSelectPage;
}
private void triggerSelectPage_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
if (this.TriggerPagePrompt != null)
this.triggerSelectPage.Text = this.TriggerPagePrompt;
}
private void weeklyCheck_CheckedChanged(object sender, System.EventArgs e)
{
var weeklyTrigger = (WeeklyTrigger)trigger;
var cb = (CheckBox)sender;
DaysOfTheWeek dow = (DaysOfTheWeek)cb.Tag;
if (cb.Checked)
weeklyTrigger.DaysOfWeek |= dow;
else
{
// Ensure that ONE day is always checked.
if (weeklyTrigger.DaysOfWeek == dow)
cb.Checked = true;
else
weeklyTrigger.DaysOfWeek &= ~dow;
}
}
private void wizardControl1_Finished(object sender, System.EventArgs e)
{
bool myTS = false;
if (this.TaskService == null)
{
this.TaskService = new TaskService();
myTS = true;
}
td.Data = TaskName;
td.RegistrationInfo.Description = descText.Text;
td.Triggers.Clear();
td.Triggers.Add(trigger);
td.Actions.Clear();
td.Actions.Add(action);
if (openDlgAfterCheck.Checked)
{
TaskEditDialog dlg = new TaskEditDialog();
dlg.Editable = true;
dlg.Initialize(this.TaskService, td);
dlg.RegisterTaskOnAccept = false;
dlg.TaskName = TaskName;
if (dlg.ShowDialog(this.ParentForm) == System.Windows.Forms.DialogResult.OK)
this.td = dlg.TaskDefinition;
}
if (RegisterTaskOnFinish)
{
TaskFolder fld = this.TaskService.RootFolder;
if (!string.IsNullOrEmpty(this.TaskFolder) && TaskService.HighestSupportedVersion.CompareTo(new Version(1, 1)) != 0)
fld = this.TaskService.GetFolder(this.TaskFolder);
task = fld.RegisterTaskDefinition(TaskName, td, TaskCreation.CreateOrUpdate, td.Principal.ToString(), Password, td.Principal.LogonType);
}
if (myTS)
this.TaskService = null;
}
}
}