-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSwitch.pas
More file actions
181 lines (160 loc) · 4.5 KB
/
TaskSwitch.pas
File metadata and controls
181 lines (160 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
unit TaskSwitch;
interface
uses Classes, TimeMap, Tasks;
type
TTaskSwitchData = class
private
FitTimeStart : TIntTime;
FitTimeEnd : TIntTime;
FTaskID : Integer;
function GetTimeAsString : String;
public
property itTimeStart : TIntTime read FitTimeStart write FitTimeStart;
property itTimeEnd : TIntTime read FitTimeEnd write FitTimeEnd;
property TaskID : Integer read FTaskID write FTaskID;
property TimeAsString : String read GetTimeAsString;
function ToString : String;
constructor FromString(const s : String);
end;
TTaskSwitches = class;
TOnAddSwitchEvent = procedure(sender : TTaskSwitches; switch : TTaskSwitchData) of object;
TTaskSwitches = class
private
FSwitchData : TList;
FLastSwitchData : TTaskSwitchData;
FLastSwitchFilePos : LongInt;
FFileName : String;
FOnChange : TOnAddSwitchEvent;
function GetTaskSwitchData(index : Integer) : TTaskSwitchData;
function GetCount : Integer;
procedure UpdateToFile(FLastSwitchData : TTaskSwitchData);
procedure LoadAll;
public
property Switches[index : Integer] : TTaskSwitchData read GetTaskSwitchData; default;
property Count : Integer read GetCount;
property PersistentFileName : String read FFileName;
property OnChange : TOnAddSwitchEvent read FOnChange write FOnChange;
procedure UpdateTaskSwitch(task : TTask);
constructor Create(const FileName : String);
destructor Destroy; override;
end;
implementation
uses SysUtils, Math, StrUtils;
{ TTaskSwitchData }
constructor TTaskSwitchData.FromString(const s : String);
var i1, i2 : Integer;
begin
i1 := Pos(#9, s);
TaskID := StrToInt(Copy(s, 1, i1 - 1));
i2 := PosEx(#9, s, i1 + 1);
itTimeStart := ITFromDT(StrToDateTime(Copy(s, i1 + 1, i2 - i1 - 1)));
itTimeEnd := itTimeStart + StrToInt(Copy(s, i2 + 1, Length(s)));
end;
function TTaskSwitchData.ToString : String;
begin
Result := IntToStr(TaskID) + #9 + DateTimeToStr(DTFromIT(itTimeStart)) + #9 + IntToStr(itTimeEnd - itTimeStart);
end;
function TTaskSwitchData.GetTimeAsString : String;
var
t : TIntTime;
h, m : Word;
s : String;
begin
t := FitTimeEnd - FitTimeStart;
DivMod(t, 60, h, m);
Result := IntToStr(h);
if Length(Result) < 2 then
Result := '0' + Result;
s := IntToStr(m);
if Length(s) < 2 then
s := '0' + s;
Result := Result + ':' + s;
end;
{ TTaskSwitches }
constructor TTaskSwitches.Create(const FileName : String);
begin
FSwitchData := TList.Create;
FFileName := FileName;
LoadAll;
end;
destructor TTaskSwitches.Destroy;
var i : Integer;
begin
for i := 0 to FSwitchData.Count - 1 do
TObject(FSwitchData[i]).Free;
FSwitchData.Free;
inherited;
end;
procedure TTaskSwitches.UpdateTaskSwitch(task : TTask);
var sw : TTaskSwitchData;
begin
if FLastSwitchData <> nil then begin
FLastSwitchData.itTimeEnd := ITFromDT(Now);
if FFileName <> '' then
UpdateToFile(FLastSwitchData);
end;
if task = nil then
FLastSwitchData := nil
else
if (FLastSwitchData = nil) or (task.TaskID <> FLastSwitchData.TaskID) then begin
sw := TTaskSwitchData.Create;
sw.itTimeStart := ITFromDT(Now);
sw.itTimeEnd := sw.itTimeStart;
sw.TaskID := task.TaskID;
FSwitchData.Add(sw);
FLastSwitchData := sw;
FLastSwitchFilePos := -1;
if Assigned(FOnChange) then
FOnChange(self, sw);
end;
end;
procedure TTaskSwitches.UpdateToFile;
var
f : File;
s : String;
begin
AssignFile(f, FFileName);
if FileExists(FFileName) then
Reset(f, 1)
else begin
Rewrite(f, 1);
FLastSwitchFilePos := 0;
end;
try
if FLastSwitchFilePos = -1 then
FLastSwitchFilePos := FileSize(f);
Seek(f, FLastSwitchFilePos);
s := FLastSwitchData.ToString + #13#10;
BlockWrite(f, s[1], Length(s));
Truncate(f);
finally
CloseFile(f);
end;
end;
procedure TTaskSwitches.LoadAll;
var
f : TextFile;
s : String;
begin
if not FileExists(FFileName) then
Exit;
AssignFile(f, FFileName);
Reset(f);
try
while not EOF(f) do begin
ReadLn(f, s);
FSwitchData.Add(TTaskSwitchData.FromString(s));
end;
finally
CloseFile(f);
end;
end;
function TTaskSwitches.GetTaskSwitchData(index : Integer) : TTaskSwitchData;
begin
Result := TTaskSwitchData(FSwitchData[index]);
end;
function TTaskSwitches.GetCount : Integer;
begin
Result := FSwitchData.Count;
end;
end.