chore: code cleanup

This commit is contained in:
Marc Durdin 2020-10-28 10:12:07 +11:00
parent c9f9bfb2c2
commit b66915bcd0
2 changed files with 27 additions and 18 deletions

View file

@ -376,7 +376,7 @@ begin
{ Run app reconfiguration tasks }
TKeymanStartTask.Recreate;
TKeymanStartTask.RecreateTask;
UILanguages.CreateUILanguages;

View file

@ -3,10 +3,12 @@ unit Keyman.System.KeymanStartTask;
interface
type
/// <summary>Create or recreate the automatic task to start Keyman if a TIP
/// is selected based on an event in the Event Log</summary>
TKeymanStartTask = class
private
public
class procedure Recreate;
class procedure RecreateTask;
class procedure DeleteTask;
class procedure CreateTask;
end;
@ -28,12 +30,18 @@ uses
const
CTaskName = 'Start On Demand';
CTaskDescription = 'Starts Keyman on demand when a Keyman input method is selected';
CTaskAuthor = 'SIL International';
CTaskFolderName = 'Keyman';
CTaskTrigger =
'<QueryList>'+
'<Query Id="0" Path="Application">'+
'<Select Path="Application">*[System[Provider[@Name=''Keyman''] and EventID=256]]</Select>'+
'</Query>'+
'</QueryList>';
CTaskEventTriggerId = 'Keyman_StartOnDemand';
CKeymanExeStartArguments = '-kmc start';
class procedure TKeymanStartTask.CreateTask;
var
@ -47,35 +55,37 @@ var
begin
pService := CoTaskScheduler_.Create;
pService.Connect(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
// Create or open the Keyman task folder
try
pTaskFolder := pService.GetFolder('\Keyman');
pTaskFolder := pService.GetFolder('\' + CTaskFolderName);
except
pTaskFolder := pService.GetFolder('\').CreateFolder('Keyman', EmptyParam);
pTaskFolder := pService.GetFolder('\').CreateFolder(CTaskFolderName, EmptyParam);
end;
// Create a new task
pTask := pService.NewTask(0);
pService := nil;
// TODO: create a Keyman folder
pRegInfo := pTask.RegistrationInfo;
pRegInfo.Author := 'SIL International';
pRegInfo.Description := 'Starts Keyman on demand when a Keyman input method is selected';
pRegInfo.Author := CTaskAuthor;
pRegInfo.Description := CTaskDescription;
pRegInfo.Version := CKeymanVersionInfo.VersionWithTag;
pRegInfo := nil;
pSettings := pTask.Settings;
pSettings.DisallowStartIfOnBatteries := False;
pSettings.StopIfGoingOnBatteries := False;
pSettings.ExecutionTimeLimit := 'PT0S';
pSettings.ExecutionTimeLimit := 'PT0S'; // no time limit
pSettings.Priority := 4; // https://github.com/microsoft/WinDev/issues/55
// Start the task when Keyman event 256 is logged
pEventTrigger := pTask.Triggers.Create(TASK_TRIGGER_EVENT) as IEventTrigger;
pEventTrigger.Id := 'Keyman_StartOnDemand';
pEventTrigger.Id := CTaskEventTriggerId;
pEventTrigger.Subscription := CTaskTrigger;
// Action is to run keyman.exe as login user
pAction := pTask.Actions.Create(TASK_ACTION_EXEC) as IExecAction;
pAction.Path := TKeymanPaths.KeymanEngineInstallPath(TKeymanPaths.S_KeymanExe);
pAction.Arguments := '-kmc start';
pAction.Arguments := CKeymanExeStartArguments;
pTaskFolder.RegisterTaskDefinition(CTaskName, pTask, TASK_CREATE_OR_UPDATE or TASK_IGNORE_REGISTRATION_TRIGGERS,
EmptyParam, EmptyParam, TASK_LOGON_NONE, EmptyParam);
@ -89,9 +99,9 @@ begin
pService := CoTaskScheduler_.Create;
pService.Connect(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
try
pTaskFolder := pService.GetFolder('\Keyman');
pTaskFolder := pService.GetFolder('\'+CTaskFolderName);
except
// Assume that the folder doesn't exist
// Assume that the folder doesn't exist, nothing to do
Exit;
end;
@ -100,17 +110,16 @@ begin
except
// We'll assume that the task doesn't exist, and continue
end;
if pTaskFolder.GetTasks(0).Count = 0 then
begin
pTaskFolder := nil;
pService.GetFolder('\').DeleteFolder('Keyman', 0);
pService.GetFolder('\').DeleteFolder(CTaskFolderName, 0);
end;
end;
class procedure TKeymanStartTask.Recreate;
class procedure TKeymanStartTask.RecreateTask;
begin
// Create or recreate the automatic task to start Keyman if a TIP is selected
// based on an event in the Event Log
if not Reg_GetDebugFlag(SRegValue_Flag_UseAutoStartTask, True) then
begin
DeleteTask;