-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertthreadunit.pas
More file actions
145 lines (132 loc) · 4.21 KB
/
convertthreadunit.pas
File metadata and controls
145 lines (132 loc) · 4.21 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
unit convertthreadunit;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, amitubelocale, filedownloadunit, downloadlistunit;
type
{ TStartConvertThread }
{Conversation and download thread.
Originally seperate tasks to convert and download, but now only one call needed, also also usable for
other downloads like the update}
TStartConvertThread = class(TThread)
private
LastTime: Cardinal;
procedure DoProgress(APercent: integer; AText: string);
procedure ProgressUpdate(Sender: TObject; Percent, Speed: Integer; ASize, Fullsize: Int64);
protected
procedure Execute; override;
procedure DoOnEnd;
public
property Terminated;
public
DL: TDownloadEntry;
//
OnEnd: TNotifyEvent; // event when threadf ended, must be connected!
OnProgress: TProgressEvent; // progress reports to main gui
end;
implementation
function FormatTime(t: LongWord): string;
var
m: LongWord;
begin
t := t div 1000;
m := t div 60;
t := t mod 60;
Result := Format('%2.2d:%2.2d',[m,t]);
end;
{Progress update when downloading a big file}
procedure TStartConvertThread.ProgressUpdate(Sender: TObject; Percent: integer; Speed: Integer; ASize, Fullsize: Int64);
var
t1, sec: Cardinal;
s: string;
Left: Int64;
begin
// do not fire too often, GUI will not update that often ;)
t1 := GetTickCount;
if t1 - LastTime > 500 then
begin
// form status text with downloaded size and overall speed
s := GetLocString(MSG_STATUS_DOWNLOADING) + '...' + FloatToStrF(ASize/1000/1000, ffFixed, 8, 3) + ' MB';
if Speed > 0 then
begin
Left := FullSize - ASize;
sec := Round((Left/Speed) * 1000);
//writeln('asize ', asize, ' full size, ', fullsize, ' left ', left, ' speed ', speed, ' sec ', sec);
s := s + ' @' + FloatToStrF(Speed/1000, ffFixed, 8,2) + ' kb/s ' + FormatTime(sec);
end;
//
if Percent = 0 then
Percent := -1; // -1 means no change
// do the actual status bar settting
DoProgress(Percent, s);
//
LastTime := GetTickCount; // update again in 500 ms
end;
end;
procedure TStartConvertThread.DoProgress(APercent: integer; AText: string);
begin
if Assigned(OnProgress) then
OnProgress(Self, APercent, AText);
end;
procedure TStartConvertThread.Execute;
var
Url: string;
begin
try
// conversation thread main routine, only runs once, then the thread ends
// if Format ID is given -> its a direct download job via ytdownload.php
// if FormatID is empty, normal conversation
if DL.FormatID = '' then
begin
DoProgress(0, GetLocString(MSG_STATUS_CONVERT) + '...');
Url := ConvertURL + DL.ID + '&format=' + IntToStr(DL.Format);
try
// the actual download in FileDownloadUnit, with progressbar, see there
DownloadFile(@ProgressUpdate, URL, DL.Filename);
//
if FileExists(DL.Filename) then // successfull if file exists (it will delete it when error)
begin
if Terminated then // we got terminated, then delete the file, it's not complete and exit
begin
DeleteFile(DL.Filename);
Exit;
end;
// write the description next to the filename
with TStringList.Create do
begin
Text := DL.Desc;
SaveToFile(ChangeFileExt(DL.Filename, '.txt'));
Free;
end;
end;
// finished!
except
on e:Exception do
begin
writeln('Convert Thread Exception ' + E.Message); // something went horribly wrong, should never happen
end;
end;
end
else
begin
// starts with http -> download directly (like the Updated AmiTube :-D)
// if not then it's a FormatID
if Pos('http', DL.FormatID) = 1 then
Url := DL.FormatID
else
Url := DownloadURL + DL.ID + '&format=' + DL.FormatID;
// the actual download with progress see in FileDownloadUnit
DownloadFile(@ProgressUpdate, Url, DL.Filename);
end;
finally
Terminate;
Synchronize(@DoOnEnd); // tell main gui that we are finished the work
end;
end;
{ call the main Gui event, this should ALWAYS called via Synchronize}
procedure TStartConvertThread.DoOnEnd;
begin
if Assigned(OnEnd) then
OnEnd(Self)
end;
end.