IconRequest.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. unit IconRequest;
  2. interface
  3. uses
  4. Classes, SysUtils, IdHTTP, IdComponent, Windows;
  5. type
  6. TIconResponseEvent = procedure(AppKey: string; AStream: TStream) of object;
  7. TIconRequest = class(TThread)
  8. private
  9. FApps: IInterfaceList;
  10. FMemoryStream: TMemoryStream;
  11. FOnCompleted: TIconResponseEvent;
  12. protected
  13. procedure Execute; override;
  14. public
  15. constructor Create(AApps: IInterfaceList);
  16. destructor Destroy; override;
  17. class function GetMD5Name(AUrl: string): string;
  18. property OnCompleted: TIconResponseEvent read FOnCompleted write FOnCompleted;
  19. end;
  20. const
  21. ICON_URL: string = 'http://%s:%d%s';
  22. implementation
  23. uses
  24. MD5_32, InterfaceAppCentre, LoggerImport, AppCentreImport, Dialogs, IdURI;
  25. const
  26. TEMP_DIR: string = 'Users\';
  27. { TIconRequest }
  28. constructor TIconRequest.Create(AApps: IInterfaceList);
  29. begin
  30. FApps := AApps;
  31. FMemoryStream := TMemoryStream.Create;
  32. FreeOnTerminate := False;
  33. inherited Create(False);
  34. end;
  35. //constructor TIconRequest.Create(AppKey, AUrl: String);
  36. //begin
  37. // FAppKey := AppKey;
  38. // FURL := AUrl;
  39. // FMD5Name := GetMD5Name(AUrl);
  40. // FMemoryStream := TMemoryStream.Create;
  41. // inherited Create(True);
  42. //end;
  43. destructor TIconRequest.Destroy;
  44. begin
  45. FOnCompleted := nil;
  46. FApps := nil;
  47. FreeAndNil(FMemoryStream);
  48. inherited;
  49. end;
  50. procedure TIconRequest.Execute;
  51. var
  52. AIdHttp: TIdHTTP;
  53. iLoop: Integer;
  54. ADownloadThread: TIconRequest;
  55. AHotApp: IHotApp;
  56. AApp: IApp;
  57. AConfig: IAppCentreConfig;
  58. AURL: string;
  59. begin
  60. AIdHttp := TIdHTTP.Create(nil);
  61. AIdHttp.Request.CustomHeaders.Values['Content-Type'] := 'application/octet-stream';
  62. try
  63. iLoop := 0;
  64. while (not Terminated) and (FApps <> nil) and (iLoop <= FApps.Count - 1) do
  65. begin
  66. if FApps[iLoop].QueryInterface(IHotApp, AHotApp) = S_OK then
  67. AApp := AHotApp.GetUserApp
  68. else if FApps[iLoop].QueryInterface(IApp, AApp) = S_OK then
  69. else
  70. begin
  71. inc(iLoop);
  72. Continue;
  73. end;
  74. Inc(iLoop);
  75. if Length(AApp.GetIcon) = 0 then
  76. Continue;
  77. FMemoryStream.Clear;
  78. AConfig := GetAppCentreConfig;
  79. // AURL := AApp.GetIcon
  80. AIdHTTP.Get(AApp.GetIcon, FMemoryStream);
  81. if Terminated then
  82. Break;
  83. if Assigned(FOnCompleted) then
  84. FOnCompleted(AApp.GetAppKey, FMemoryStream);
  85. end;
  86. Debug(IntToStr(ThreadID)+ '-Ö´ÐÐÍê±Ï', 'TIconRequest.Execute');
  87. except
  88. on E: Exception do
  89. begin
  90. Debug(E.Message, 'TIconRequest.Execute');
  91. FApps := nil;
  92. FreeAndNil(AIdHTTP);
  93. end;
  94. end;
  95. FApps := nil;
  96. FreeAndNil(AIdHTTP);
  97. end;
  98. class function TIconRequest.GetMD5Name(AUrl: string): string;
  99. begin
  100. Result := '';
  101. if Length(AUrl) > 0 then
  102. Result := MD5Print(MD5String(AUrl));
  103. end;
  104. end.