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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| unit WGetFireClass;
interface
uses
Windows,
SysUtils,
Classes,
Controls,
Dialogs,
WGetFire2;
type
TWGFThread = (
wtD6 = WGFThreadD6,
wtD8 = WGFThreadD8
);
TWGFDlProgressEvent = procedure(Sender: TObject; aDlTotal, aDlNow, aDlSpeed: Double;
aHandle: integer; var aReturn: Integer) of object;
TWGFDlSplitEvent = procedure(Sender: TObject; aTotal: double;
aChunck: integer; var aReturn: integer) of object;
TWGFUrlGrabber = class(TComponent)
private
ffLock : boolean;
ffFcStp : boolean;
ffURL : string;
ffUser : string;
ffPass : string;
ffLocal : string;
ffThread : TWGFThread;
fHttpFile : TWGFHttpFile;
fOnDlProgress: TWGFDlProgressEvent;
fOnDlSplit : TWGFDlSplitEvent;
fOnLock : TNotifyEvent;
fOnUnlock : TNotifyEvent;
function GetChunk: LongInt;
function GetPort: Word;
procedure SetChunk(const Value: LongInt);
procedure SetLocal(const Value: string);
procedure SetPass(const Value: string);
procedure SetPort(const Value: Word);
procedure SetUrl(const Value: string);
procedure SetUser(const Value: string);
function GetThreads: TWGFThread;
procedure SetThreads(const Value: TWGFThread);
protected
procedure DoLock; // component is locked on Execute starting
procedure DoUnlock; // component is unlocked on Execute ending
published
property URL : string read ffUrl write SetUrl;
property Port : Word read GetPort write SetPort default WGFPortDefault;
property User : string read ffUser write SetUser;
property Pass : string read ffPass write SetPass;
property Local : string read ffLocal write SetLocal;
property Threads : TWGFThread read GetThreads write SetThreads default wtD6;
property Chunk : LongInt read GetChunk write SetChunk default WGFChunkDefault;
property Locked : boolean read ffLock;
property OnDownloadProgress : TWGFDlProgressEvent read fOnDlProgress write fOnDlProgress;
property OnDownloadSplit : TWGFDlSplitEvent read fOnDlSplit write fOnDlSplit;
property OnLock : TNotifyEvent read fOnLock write fOnLock;
property OnUnlock : TNotifyEvent read fOnUnlock write fOnUnlock;
public
function Execute: Integer;
function ForceStop: Integer;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
implementation
{ TWGFUrlGrabber }
function CallBackDlProgress(aPtr: Pointer; aDlTotal, aDlNow, aDlSpeed: Double; aHandle: Integer): Integer; cdecl;
var UG : TWGFUrlGrabber;
begin
UG := TWGFUrlGrabber(aPtr);
if UG.ffFcStp then
result := WGFCallBackStopNeeded
else
result := WGFCallBackContinue;
if Assigned(UG.fOnDlProgress) then
UG.fOnDlProgress(UG, aDlTotal, aDlNow, aDlSpeed, aHandle, result);
if UG.ffFcStp then
result := result or WGFCallBackStopNeeded;
end;
function CallBackSplitAll(aPtr: Pointer; aTotal: Double; aChunck: integer): Integer; cdecl;
var UG : TWGFUrlGrabber;
begin
UG := TWGFUrlGrabber(aPtr);
if UG.ffFcStp then
result := WGFCallBackStopNeeded
else
result := WGFCallBackContinue;
if Assigned(UG.fOnDlSplit) then
UG.fOnDlSplit(UG, aTotal, aChunck, result);
if UG.ffFcStp then
result := result or WGFCallBackStopNeeded;
end;
constructor TWGFUrlGrabber.Create(AOwner: TComponent);
begin
inherited;
ffLock := false;
ffFcStp := false;
ffURL := '';
ffUser := '';
ffPass := '';
ffLocal := '';
ffThread := wtD6;
fHttpFile.URL := PChar(ffURL);
fHttpFile.User := PChar(ffUser);
fHttpFile.Pass := PChar(ffPass);
fHttpFile.Local := PChar(ffLocal);
fHttpFile.Port := WGFPortDefault;
fHttpFile.Threads := WGFThreadD6;
fHttpFile.Chunk := WGFChunkDefault;
fHttpFile.Spt := CallBackSplitAll;
fHttpFile.WPrg := CallBackDlProgress;
end;
destructor TWGFUrlGrabber.Destroy;
begin
if not ffLock then
begin
MessageDlg('Url grabber is running, cannot be destroyed at this time.', mtWarning, [mbOk], 0);
exit;
end;
inherited;
end;
procedure TWGFUrlGrabber.DoLock;
begin
if not ffLock then
begin
if assigned(fOnLock) then
fOnLock(Self);
ffLock := true;
end;
end;
procedure TWGFUrlGrabber.DoUnlock;
begin
if ffLock then
begin
ffLock := false;
if assigned(fOnUnlock) then
fOnUnlock(Self);
end;
end;
function TWGFUrlGrabber.Execute: integer;
begin
result := -1;
if ffLock then
exit;
DoLock;
try
{ others checks here }
case fHttpFile.Threads of
WGFThreadD6 : result := WGFGetFileFromHttp6(@fHttpFile);
WGFThreadD8 : result := WGFGetFileFromHttp8(@fHttpFile);
end;
finally
DoUnlock;
ffFcStp := false;
end;
end;
function TWGFUrlGrabber.ForceStop: Integer;
begin
ffFcStp := true;
end;
function TWGFUrlGrabber.GetChunk: LongInt;
begin
result := fHttpFile.Chunk;
end;
function TWGFUrlGrabber.GetPort: Word;
begin
result := fHttpFile.Port;
end;
function TWGFUrlGrabber.GetThreads: TWGFThread;
begin
case fHttpFile.Threads of
WGFThreadD6 : result := wtD6;
WGFThreadD8 : result := wtD8;
end;
end;
procedure TWGFUrlGrabber.SetChunk(const Value: LongInt);
begin
if (fHttpFile.Chunk <> Value) and not ffLock then
begin
fHttpFile.Chunk := Value;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetLocal(const Value: string);
begin
if (ffLocal <> Value) and not ffLock then
begin
ffLocal := Value;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetPass(const Value: string);
begin
if (ffPass <> Value) and not ffLock then
begin
ffPass := Value;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetPort(const Value: Word);
begin
if (fHttpFile.Port <> Value) and not ffLock then
begin
fHttpFile.Port := Value;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetThreads(const Value: TWGFThread);
begin
if (ffThread <> Value) and not ffLock then
begin
ffThread := value;
case ffThread of
wtD6 : fHttpFile.Threads := WGFThreadD6;
wtD8 : fHttpFile.Threads := WGFThreadD8;
end;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetUrl(const Value: string);
begin
if (ffUrl <> Value) and not ffLock then
begin
ffUrl := Value;
{ others traitments here }
end;
end;
procedure TWGFUrlGrabber.SetUser(const Value: string);
begin
if (ffUser <> Value) and not ffLock then
begin
ffUser := Value;
{ others traitments here }
end;
end;
end. |
Partager